Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/acs/damsel/client/ingestionClient/IngestionClientUploadZipAction.java


1   package org.acs.damsel.client.ingestionClient;
2   
3   import org.apache.struts.action.*;
4   import javax.servlet.http.*;
5   import java.util.zip.*;
6   import java.io.*;
7   import java.util.*;
8   import org.acs.damsel.srvr.Config;
9   import org.apache.struts.upload.*;
10  import org.acs.damsel.client.ClientApp;
11  import org.acs.damsel.srvr.asset.*;
12  import java.sql.*;
13  import org.acs.damsel.srvr.db.*;
14  
15  public class IngestionClientUploadZipAction extends Action {
16    private Config config = Config.instance();
17  
18    /**@todo check for directories in the zip
19     */
20    public ActionForward execute(ActionMapping actionMapping,
21                                 ActionForm actionForm,
22                                 HttpServletRequest httpServletRequest,
23                                 HttpServletResponse httpServletResponse) {
24  
25      IngestionClientUploadZipForm ingestionClientUploadZipForm = (
26          IngestionClientUploadZipForm) actionForm;
27  
28      /*Get the list of assets supplied in the tab-delimited file uploaded */
29      Vector assetFileNames = (Vector) httpServletRequest.getSession().getAttribute("assetsFileNames");
30      assetFileNames = DBUtils.decode(assetFileNames);
31      Vector assetsNotAdded = new Vector();
32      Vector assetsAdded = new Vector();
33      FormFile zipFile = ingestionClientUploadZipForm.getZipFile();
34      String zipFileName = zipFile.getFileName();
35      ActionErrors errors;
36      String fileName;
37      File file;
38  
39      /*Check for an empty fileName */
40      if (zipFile == null || zipFileName.trim().equals("")) {
41        errors = new ActionErrors();
42        errors.add("ingestionClient", new ActionError("no.file.name"));
43        this.saveErrors(httpServletRequest, errors);
44        return actionMapping.findForward("failure");
45      }
46  
47      /*Upload File*/
48      try {
49        int lastSlash = zipFileName.lastIndexOf("\\");
50        if (lastSlash == -1)
51          lastSlash = zipFileName.lastIndexOf("/");
52        fileName = zipFileName.substring(lastSlash+1);
53        fileName = fileName.toLowerCase();
54        file = this.doUpload(zipFile, fileName);
55        }
56      catch (Exception ex3) {
57        /*Tell the user the file could not be uploaded */
58        errors = new ActionErrors();
59        errors.add("ingestionClient", new ActionError("upload.failed"));
60        this.saveErrors(httpServletRequest, errors);
61        return actionMapping.findForward("failure");
62      }
63  
64      try {
65        /*read in the file */
66        ZipFile zip = new ZipFile(file);
67        ZipEntry zipEntry;
68        Enumeration fileNames = zip.entries();
69        FileOutputStream out;
70        InputStream in;
71        int remainingBytes;
72        byte[] b = new byte[1024];
73        /*While we have more filenames to cycle through, pull out each file as
74        a ZipEntry and write to a FileOutputStream */
75        while (fileNames.hasMoreElements()) {
76          zipEntry = (ZipEntry) fileNames.nextElement();
77          /*Write the new file to the assets directory */
78          fileName = zipEntry.getName();
79          fileName = fileName.toLowerCase();
80          fileName = fileName.replaceAll("'", "");
81          /*Only write the asset to the assets directory if it was defined in the
82          tab-delimited excel file uploaded previously */
83          if (assetFileNames.contains(fileName)) {
84            assetsAdded.add(fileName);
85            out = new FileOutputStream(config.getUploadPrefix() + fileName);
86            in = zip.getInputStream(zipEntry);
87            /*Write out the file */while ( (remainingBytes = in.read(b)) >= 0)
88              out.write(b, 0, remainingBytes);
89  
90            in.close();
91            out.close();
92          }
93          else {
94            assetsNotAdded.add(fileName);
95          }
96        }
97        zip.close();
98      }
99      catch (IOException ex) {
100       /*Shout at the user if there was a problem*/
101       file.delete();
102       errors = new ActionErrors();
103       errors.add("ingestionClient", new ActionError("zip.file.bad"));
104       this.saveErrors(httpServletRequest, errors);
105       return actionMapping.findForward("failure");
106     }
107 
108     for (int i = 0; i < assetFileNames.size(); i ++ ) {
109       if(!assetsAdded.contains(assetFileNames.elementAt(i)))
110         assetsNotAdded.add(assetFileNames.elementAt(i));
111     }
112 
113     /*Attach the assetNotAdded to the session */
114     httpServletRequest.getSession().setAttribute("assetsNotAdded", assetsNotAdded);
115     httpServletRequest.getSession().setAttribute("assetsAdded", assetsAdded);
116 
117     /*Destroy the file!!!!! */
118     file.delete();
119 
120     /*Remove the assets (in the database) that were not added */
121     Asset asset;
122     String assetName;
123     for (int i = 0; i < assetsNotAdded.size(); i ++ ) {
124       try {
125         assetName = (String) assetsNotAdded.elementAt(i);
126         asset = ClientApp.instance().getRepositoryMgr().getAsset(assetName);
127         if (asset != null)
128           ClientApp.instance().getRepositoryMgr().removeAsset(asset);
129       }
130       catch (SQLException ex1) {
131       }
132     }
133 
134     return actionMapping.findForward("success");
135   }
136 
137 // this method writes the uploaded file to a new file with the given fileName
138   public File doUpload(FormFile theFile, String fileName) throws IOException {
139     File file = new File(Config.instance().getUploadPrefix() + fileName);
140     FileOutputStream out = new FileOutputStream(file);
141     out.write(theFile.getFileData(), 0, theFile.getFileSize());
142     out.close();
143     return file;
144   }
145 
146 }