WinCESynchroFileGenerator.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 package org.openmobileis.synchro.openmsp.server.util;
00030 
00031 import org.openmobileis.common.context.UserTerminal;
00032 import org.openmobileis.common.user.profile.Profile;
00033 import org.openmobileis.common.util.file.FileUtilities;
00034 import org.openmobileis.common.util.exception.SynchroException;
00035 
00036 import java.io.IOException;
00037 import java.io.ByteArrayOutputStream;
00038 import java.util.HashMap;
00039 import java.util.Iterator;
00040 import java.util.zip.*;
00041 
00042 import javax.servlet.http.HttpServletRequest;
00043 import javax.servlet.http.HttpServletResponse;
00044 
00052 public class WinCESynchroFileGenerator implements SynchroFileGenerator {
00053   class ZipArrayEntryFile {
00054     ByteArrayOutputStream array;
00055 
00056     ZipOutputStream zip;
00057 
00058     String fileName;
00059 
00060     public int hashCode() {
00061       return fileName.hashCode();
00062     }
00063 
00064     public boolean equals(Object obj) {
00065       if (obj instanceof ZipArrayEntryFile) {
00066         return ((ZipArrayEntryFile) obj).fileName.equals(this.fileName);
00067       }
00068       return false;
00069     }
00070 
00071   }
00072 
00073   public WinCESynchroFileGenerator() {
00074   }
00075 
00076   public void generateSynchroFile(UserTerminal terminal, Profile profil, HttpServletRequest request, HttpServletResponse res, FileSystem fileSystem,
00077       String newSessionId) throws SynchroException {
00078     ByteArrayOutputStream array = new ByteArrayOutputStream(100000);
00079     try {
00080       ZipOutputStream zip = new ZipOutputStream(array);
00081       try {
00082         //manage added zipEntryMemoryFiles
00083         HashMap zipFileMap = new HashMap(10);
00084         OpenMISFile[] syncfileList = fileSystem.getFileList();
00085         for (int i = 0; i < syncfileList.length; i++) {
00086           if (syncfileList[i] instanceof ZipEntryMemoryFile) {
00087             String zipFileName = ((ZipEntryMemoryFile) syncfileList[i]).getZipFileName();
00088             ZipArrayEntryFile zipEntry = (ZipArrayEntryFile) zipFileMap.get(zipFileName);
00089             if (zipEntry == null) {
00090               zipEntry = new ZipArrayEntryFile();
00091               zipEntry.array = new ByteArrayOutputStream(100000);
00092               zipEntry.zip = new ZipOutputStream(zipEntry.array);
00093               zipEntry.fileName = zipFileName;
00094               zipFileMap.put(zipFileName, zipEntry);
00095             }
00096             ZipEntry entry = new ZipEntry(FileUtilities.convertToUnixFomat(syncfileList[i].getFileName()));
00097             entry.setTime(syncfileList[i].getFileDate());
00098             entry.setMethod(ZipEntry.DEFLATED);
00099             zipEntry.zip.putNextEntry(entry);
00100             zipEntry.zip.write(syncfileList[i].getFileData());
00101             zipEntry.zip.closeEntry();
00102           } else {
00103             ZipEntry entry = new ZipEntry(FileUtilities.convertToUnixFomat(syncfileList[i].getFileCompleteName()));
00104             entry.setTime(syncfileList[i].getFileDate());
00105             entry.setMethod(ZipEntry.DEFLATED);
00106             zip.putNextEntry(entry);
00107             zip.write(syncfileList[i].getFileData());
00108             zip.closeEntry();
00109           }
00110         }
00111         
00112         //add zip entry files to synchro file.
00113         Iterator iter = zipFileMap.keySet().iterator();
00114         while (iter.hasNext())  {
00115           String filename = (String) iter.next();
00116           ZipArrayEntryFile zipEntry = (ZipArrayEntryFile) zipFileMap.get(filename);
00117           zipEntry.zip.flush();
00118           zipEntry.zip.close();
00119           ZipEntry entry = new ZipEntry(FileUtilities.convertToUnixFomat(filename));
00120          // entry.setTime(syncfileList[i].getFileDate());
00121           entry.setMethod(ZipEntry.DEFLATED);
00122           zip.putNextEntry(entry);
00123           zip.write(zipEntry.array.toByteArray());
00124           zip.closeEntry();          
00125         }
00126       } finally {
00127         zip.flush();
00128         zip.close();
00129       }
00130       res.addHeader("SyncNum", newSessionId);
00131       res.setContentType("application/zip");
00132       res.getOutputStream().write(array.toByteArray());
00133     } catch (IOException ex) {
00134       throw new SynchroException("Error during synchro file genration ", ex);
00135     }
00136   }
00137 
00138 }

Generated on Mon Dec 4 11:03:31 2006 for OpenMobileIS by  doxygen 1.5.1-p1