View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.connector.fckeditor;
18  
19  import java.io.ByteArrayInputStream;
20  import java.text.DateFormat;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.GregorianCalendar;
24  
25  import javax.jcr.Node;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.ws.rs.core.CacheControl;
28  import javax.ws.rs.core.MediaType;
29  import javax.ws.rs.core.Response;
30  import javax.xml.parsers.DocumentBuilder;
31  import javax.xml.parsers.DocumentBuilderFactory;
32  
33  import org.exoplatform.common.http.HTTPStatus;
34  import org.exoplatform.commons.utils.IOUtil;
35  import org.exoplatform.container.ExoContainer;
36  import org.exoplatform.services.cms.mimetype.DMSMimeTypeResolver;
37  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
38  import org.exoplatform.upload.UploadResource;
39  import org.exoplatform.upload.UploadService;
40  import org.w3c.dom.Document;
41  import org.w3c.dom.Element;
42  
43  /*
44   * Created by The eXo Platform SAS
45   * @author : Hoa.Pham
46   *          hoa.pham@exoplatform.com
47   * Jun 23, 2008
48   */
49  /**
50   * The Class FileUploadHandler.
51   */
52  public class FileUploadHandler {
53  
54    /** The Constant UPLOAD_ACTION. */
55    public final static String UPLOAD_ACTION = "upload";
56  
57    /** The Constant PROGRESS_ACTION. */
58    public final static String PROGRESS_ACTION = "progress";
59  
60    /** The Constant ABORT_ACTION. */
61    public final static String ABORT_ACTION = "abort";
62  
63    /** The Constant DELETE_ACTION. */
64    public final static String DELETE_ACTION = "delete";
65  
66    /** The Constant SAVE_ACTION. */
67    public final static String SAVE_ACTION = "save";
68  
69    private UploadService uploadService;
70  
71  
72    private FCKMessage fckMessage;
73  
74    /**
75     * Instantiates a new file upload handler.
76     *
77     * @param container the container
78     */
79    public FileUploadHandler(ExoContainer container) {
80      uploadService = WCMCoreUtils.getService(UploadService.class);
81      fckMessage = new FCKMessage();
82    }
83  
84    public Response upload(HttpServletRequest servletRequest) throws Exception {
85      CacheControl cacheControl = new CacheControl();
86      cacheControl.setNoCache(true);
87      uploadService.createUploadResource(servletRequest);
88      return Response.ok(null, new MediaType("text", "xml")).cacheControl(cacheControl).build();
89    }
90  
91    public Response control(String uploadId, String action) throws Exception {
92      CacheControl cacheControl = new CacheControl();
93      cacheControl.setNoCache(true);
94      DateFormat dateFormat = new SimpleDateFormat(FCKUtils.IF_MODIFIED_SINCE_DATE_FORMAT);
95      if (FileUploadHandler.PROGRESS_ACTION.equals(action)) {
96        Document currentProgress = getProgress(uploadId);
97        return Response.ok(currentProgress, new MediaType("text", "xml"))
98                       .cacheControl(cacheControl)
99                       .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
100                      .build();
101     } else if (FileUploadHandler.ABORT_ACTION.equals(action)) {
102       uploadService.removeUploadResource(uploadId);
103       return Response.ok(null, new MediaType("text", "xml"))
104                      .cacheControl(cacheControl)
105                      .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
106                      .build();
107     } else if (FileUploadHandler.DELETE_ACTION.equals(action)) {
108       uploadService.removeUploadResource(uploadId);
109       return Response.ok(null, new MediaType("text", "xml"))
110                      .cacheControl(cacheControl)
111                      .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
112                      .build();
113     }
114     return Response.status(HTTPStatus.BAD_REQUEST)
115                    .cacheControl(cacheControl)
116                    .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
117                    .build();
118   }
119 
120   public Response saveAsNTFile(Node parent, String uploadId, String fileName, String language) throws Exception {
121     CacheControl cacheControl = new CacheControl();
122     cacheControl.setNoCache(true);
123     UploadResource resource = uploadService.getUploadResource(uploadId);
124     DateFormat dateFormat = new SimpleDateFormat(FCKUtils.IF_MODIFIED_SINCE_DATE_FORMAT);
125     if (parent == null) {
126       Document fileNotUploaded = fckMessage.createMessage(FCKMessage.FILE_NOT_UPLOADED,
127                                                           FCKMessage.ERROR,
128                                                           language,
129                                                           null);
130       return Response.ok(fileNotUploaded, new MediaType("text", "xml"))
131                      .cacheControl(cacheControl)
132                      .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
133                      .build();
134     }
135     if (!FCKUtils.hasAddNodePermission(parent)) {
136       Object[] args = { parent.getPath() };
137       Document message = fckMessage.createMessage(FCKMessage.FILE_UPLOAD_RESTRICTION,
138                                                   FCKMessage.ERROR,
139                                                   language,
140                                                   args);
141       return Response.ok(message, new MediaType("text", "xml"))
142                      .cacheControl(cacheControl)
143                      .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
144                      .build();
145     }
146     if ((fileName == null) || (fileName.length() == 0)) {
147       fileName = resource.getFileName();
148     }
149     if (parent.hasNode(fileName)) {
150       Object args[] = { fileName, parent.getPath() };
151       Document fileExisted = fckMessage.createMessage(FCKMessage.FILE_EXISTED,
152                                                       FCKMessage.ERROR,
153                                                       language,
154                                                       args);
155       return Response.ok(fileExisted, new MediaType("text", "xml"))
156                      .cacheControl(cacheControl)
157                      .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
158                      .build();
159     }
160     String location = resource.getStoreLocation();
161     byte[] uploadData = IOUtil.getFileContentAsBytes(location);
162     Node file = parent.addNode(fileName,FCKUtils.NT_FILE);
163     Node jcrContent = file.addNode("jcr:content","nt:resource");
164     String mimetype = DMSMimeTypeResolver.getInstance().getMimeType(resource.getFileName());
165     jcrContent.setProperty("jcr:data",new ByteArrayInputStream(uploadData));
166     jcrContent.setProperty("jcr:lastModified",new GregorianCalendar());
167     jcrContent.setProperty("jcr:mimeType",mimetype);
168     parent.save();
169     uploadService.removeUploadResource(uploadId);
170     return Response.ok(null, new MediaType("text", "xml"))
171                    .cacheControl(cacheControl)
172                    .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
173                    .build();
174   }
175 
176   private Document getProgress(String uploadId) throws Exception {
177     UploadResource resource = uploadService.getUploadResource(uploadId);
178     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
179     DocumentBuilder builder = factory.newDocumentBuilder();
180     Document doc = builder.newDocument();
181     if(resource == null) {
182       return doc;
183     }
184     Double percent = 0.0;
185     if (resource.getStatus() == UploadResource.UPLOADING_STATUS) {
186       percent = (resource.getUploadedSize() * 100) / resource.getEstimatedSize();
187     } else {
188       percent = 100.0;
189     }
190     Element rootElement = doc.createElement("UploadProgress");
191     rootElement.setAttribute("uploadId", uploadId);
192     rootElement.setAttribute("fileName", resource.getFileName());
193     rootElement.setAttribute("percent", percent.intValue() + "");
194     doc.appendChild(rootElement);
195     return doc;
196   }
197 }