001    /*
002     * Copyright © 2008, 2009 Pedro Agulló Soliveres.
003     * 
004     * This file is part of DirectJNgine.
005     *
006     * DirectJNgine is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation, either version 3 of the License.
009     *
010     * Commercial use is permitted to the extent that the code/component(s)
011     * do NOT become part of another Open Source or Commercially developed
012     * licensed development library or toolkit without explicit permission.
013     *
014     * DirectJNgine is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
021     * 
022     * This software uses the ExtJs library (http://extjs.com), which is 
023     * distributed under the GPL v3 license (see http://extjs.com/license).
024     */
025    
026    package com.softwarementors.extjs.djn.router.processor.standard.form.upload;
027    
028    import java.io.IOException;
029    import java.io.Writer;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    import org.apache.commons.fileupload.FileItem;
035    import org.apache.commons.fileupload.FileUploadException;
036    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
037    import org.apache.commons.fileupload.servlet.ServletFileUpload;
038    import org.apache.log4j.Logger;
039    
040    import com.softwarementors.extjs.djn.api.Registry;
041    import com.softwarementors.extjs.djn.config.GlobalConfiguration;
042    import com.softwarementors.extjs.djn.router.dispatcher.Dispatcher;
043    import com.softwarementors.extjs.djn.router.processor.standard.form.FormPostRequestProcessorBase;
044    
045    public class UploadFormPostRequestProcessor extends FormPostRequestProcessorBase {
046      
047      private static final Logger logger = Logger.getLogger(UploadFormPostRequestProcessor.class);
048      
049      public UploadFormPostRequestProcessor(Registry registry, Dispatcher dispatcher, GlobalConfiguration globalConfiguration) {
050        super( registry, dispatcher, globalConfiguration );
051      }
052    
053      public void process(List<FileItem> fileItems, Writer writer) throws IOException {
054        assert fileItems != null;
055        assert writer != null;
056        
057        Map<String, String> formParameters = new HashMap<String,String>();
058        Map<String, FileItem> fileFields = new HashMap<String,FileItem>();
059        for( FileItem item : fileItems ) {
060          if (item.isFormField()) {
061            formParameters.put( item.getFieldName(), item.getString());
062          } 
063          else {
064            fileFields.put( item.getFieldName(), item );
065          }
066        }
067        
068        if( logger.isDebugEnabled()) { // Avoid expensive function calls if logging is not needed
069          logger.debug( "Request data (UPLOAD FORM)=>" + getFormParametersLogString(formParameters) + " FILES: " + getFileParametersLogString(fileFields));
070        }
071        String result = process(formParameters, fileFields);
072        
073        String resultString = "<html><body><textarea>";
074        resultString += result.toString(); // .replace("\"", "\\\""); 
075        resultString += ("</textarea></body></html>");
076        writer.write( resultString );
077        if( logger.isDebugEnabled() ) {
078          logger.debug( "Response data (UPLOAD FORM)=>" + resultString );
079        }
080      }
081    
082      private String getFormParametersLogString(Map<String, String> formParameters) {
083        StringBuilder result = new StringBuilder();
084        for( String fieldName : formParameters.keySet() ) {
085          result.append( fieldName );
086          result.append( "=");
087          result.append( formParameters.get(fieldName));
088          result.append( ";");
089        }
090        return result.toString();
091      }
092    
093      private String getFileParametersLogString(Map<String, FileItem> fileFields) {
094        StringBuilder result = new StringBuilder();
095        for( String fieldName : fileFields.keySet() ) {
096          FileItem fileItem = fileFields.get(fieldName);
097          result.append( fieldName );
098          result.append( "=");
099          String fileName = fileItem.getName();
100          result.append( fileName );
101          result.append( ";");
102        }
103        return result.toString();
104      }
105    
106      public static ServletFileUpload createFileUploader() {
107        // Create a factory for disk-based file items
108        DiskFileItemFactory factory = new DiskFileItemFactory();
109        // Set factory constraints
110        /*
111          int maxMemorySizeThreshold = 1024 * 10; 
112          factory.setSizeThreshold(maxMemorySizeThreshold); // If request size bigger than this, store files to disk
113          factory.setRepository(new File("c:\temp")); // Where to store temporary files created because of size bigger than that specified in setSizeThreshold
114         */
115    
116        // Create a new file upload handler
117        ServletFileUpload upload = new ServletFileUpload(factory);
118    
119        // Set upload handler limits
120        /*
121          int maxRequestSize = 1024 * 1000; // What happens if
122          int maxFileSize = 1024 * 1000;
123          upload.setSizeMax(maxRequestSize);
124          upload.setFileSizeMax(maxFileSize);
125         */
126        return upload;
127      }
128    
129      public void handleFileUploadException(FileUploadException e) {
130        assert e != null;
131        
132        com.softwarementors.extjs.djn.router.processor.standard.form.upload.FileUploadException ex = com.softwarementors.extjs.djn.router.processor.standard.form.upload.FileUploadException.forFileUploadException(e);
133        logger.error( ex.getMessage(), ex);
134        throw ex;
135      }
136      
137    }