Author: haint
Date: 2011-09-21 23:16:42 -0400 (Wed, 21 Sep 2011)
New Revision: 7478
Added:
portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/MimeTypeUploadPlugin.java
portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/conf/mimetype/
portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/conf/mimetype/mimetypes.properties
Modified:
portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadService.java
portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/conf/sample-portal/web/web-inf-extension-configuration.xml
portal/trunk/testsuite/webuibasedsamples/src/main/webapp/WEB-INF/web.xml
Log:
GTNPORTAL-1966: Mime Type definition for file uploading
Added:
portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/MimeTypeUploadPlugin.java
===================================================================
---
portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/MimeTypeUploadPlugin.java
(rev 0)
+++
portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/MimeTypeUploadPlugin.java 2011-09-22
03:16:42 UTC (rev 7478)
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2003-2011 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.upload;
+
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Properties;
+
+import org.exoplatform.container.component.BaseComponentPlugin;
+import org.exoplatform.container.configuration.ConfigurationManager;
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.container.xml.ValueParam;
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
+
+/**
+ * Created by The eXo Platform SAS
+ * Author : Nguyen Thanh Hai
+ * haint(a)exoplatform.com
+ * Jun 29, 2011
+ */
+public class MimeTypeUploadPlugin extends BaseComponentPlugin
+{
+ final private static Logger log =
LoggerFactory.getLogger(MimeTypeUploadPlugin.class);
+ final private static String MIMETYPE_PATH = "mimetype-path" ;
+ final private static String DEFAULT_MIMETYPE = "mimetype-default";
+
+ private Properties mimeTypes = new Properties();
+ private String mimetypeDefault ;
+
+ public MimeTypeUploadPlugin(InitParams initParams, ConfigurationManager
configurationService) throws Exception
+ {
+ ValueParam param = initParams.getValueParam(MIMETYPE_PATH) ;
+ URL filePath = configurationService.getURL(param.getValue());
+ URLConnection connection = filePath.openConnection() ;
+ mimeTypes.load(connection.getInputStream()) ;
+
+ param = initParams.getValueParam(DEFAULT_MIMETYPE) ;
+ if(param != null) mimetypeDefault = param.getValue() ;
+ }
+
+ public String getMimeType(String fileName)
+ {
+ if(fileName.indexOf('.') == -1) return mimetypeDefault ;
+ String ext = fileName.substring(fileName.lastIndexOf('.') + 1);
+ String mimeType = mimeTypes.getProperty(ext.toLowerCase(), mimetypeDefault);
+ if (mimeType == null || mimeType.length() == 0) return null ;
+ return mimeType;
+ }
+}
Modified:
portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadService.java
===================================================================
---
portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadService.java 2011-09-21
17:34:23 UTC (rev 7477)
+++
portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadService.java 2011-09-22
03:16:42 UTC (rev 7478)
@@ -20,6 +20,9 @@
package org.exoplatform.upload;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
@@ -44,6 +47,8 @@
{
/** . */
private static final Logger log = LoggerFactory.getLogger(UploadService.class);
+
+ private List<MimeTypeUploadPlugin> plugins ;
private Map<String, UploadResource> uploadResources = new
LinkedHashMap<String, UploadResource>();
@@ -52,7 +57,7 @@
private int defaultUploadLimitMB_;
private Map<String, Integer> uploadLimitsMB_ = new LinkedHashMap<String,
Integer>();
-
+
public static String UPLOAD_RESOURCES_STACK = "uploadResourcesStack";
public UploadService(PortalContainerInfo pinfo, InitParams params) throws Exception
@@ -67,7 +72,12 @@
if (!uploadDir.exists())
uploadDir.mkdirs();
}
-
+
+ public void register(MimeTypeUploadPlugin plugin) {
+ if(plugins == null) plugins = new ArrayList<MimeTypeUploadPlugin>() ;
+ plugins.add(plugin) ;
+ }
+
/**
* Create UploadResource for HttpServletRequest
*
@@ -120,6 +130,12 @@
upResource.setFileName(fileName);
upResource.setMimeType(fileItem.getContentType());
+ if(plugins != null)
+ for(MimeTypeUploadPlugin plugin : plugins)
+ {
+ String mimeType = plugin.getMimeType(fileName) ;
+ if(mimeType != null) upResource.setMimeType(mimeType) ;
+ }
upResource.setStoreLocation(storeLocation);
upResource.setStatus(UploadResource.UPLOADED_STATUS);
}
@@ -276,4 +292,6 @@
}
return false;
}
+
+
}
Added:
portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/conf/mimetype/mimetypes.properties
===================================================================
---
portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/conf/mimetype/mimetypes.properties
(rev 0)
+++
portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/conf/mimetype/mimetypes.properties 2011-09-22
03:16:42 UTC (rev 7478)
@@ -0,0 +1,156 @@
+ai=application/postscript
+aif=audio/x-aiff
+aifc=audio/x-aiff
+aiff=audio/x-aiff
+any=text/any
+asc=text/plain
+au=audio/basic
+avi=video/x-msvideo
+bcpio=application/x-bcpio
+bin=application/octet-stream
+bz2=application/x-bzip2
+cdf=application/x-netcdf
+class=application/octet-stream
+cpio=application/x-cpio
+cpt=application/mac-compactpro
+cq=application/cq-durboser
+csh=application/x-csh
+css=text/css
+dcr=application/x-director
+dir=application/x-director
+dms=application/octet-stream
+doc=application/msword
+dvi=application/x-dvi
+dxr=application/x-director
+ecma=text/qhtml
+eps=application/postscript
+esp=text/qhtml
+etx=text/x-setext
+exe=application/octet-stream
+ez=application/andrew-inset
+gif=image/gif
+gtar=application/x-gtar
+gz=application/x-gzip
+hdf=application/x-hdf
+hqx=application/mac-binhex40
+htm=text/html
+html=text/html
+ice=x-conference/x-cooltalk
+ief=image/ief
+iges=model/iges
+igs=model/iges
+jpeg=image/jpeg
+jpe=image/jpeg
+jpg=image/jpeg
+bmp=image/bmp
+js=application/x-javascript
+kar=audio/midi
+latex=application/x-latex
+lha=application/octet-stream
+lzh=application/octet-stream
+man=application/x-troff-man
+me=application/x-troff-me
+mesh=model/mesh
+mid=audio/midi
+midi=audio/midi
+mif=application/vnd=mif
+mov=video/quicktime
+m4v=video/x-m4v
+m4a=audio/x-m4a
+movie=video/x-sgi-movie
+mp2=audio/mp2
+mp3=audio/mp3
+mpe=video/mpe
+mp4=video/mp4
+mpeg=video/mpeg
+mpg=video/mpeg
+mpg=video/mpg
+mpga=audio/mpga
+ms=application/x-troff-ms
+msh=model/mesh
+nc=application/x-netcdf
+oda=application/oda
+pbm=image/x-portable-bitmap
+pdb=chemical/x-pdb
+pdf=application/pdf
+pgm=image/x-portable-graymap
+pgn=application/x-chess-pgn
+png=image/png
+pnm=image/x-portable-anymap
+ppm=image/x-portable-pixmap
+ppt=application/ppt
+properties=text/plain
+ps=application/postscript
+qhtml=text/qhtml
+qt=video/quicktime
+ra=audio/x-realaudio
+ram=audio/x-pn-realaudio
+rm=audio/x-pn-realaudio
+ras=image/x-cmu-raster
+rgb=image/x-rgb
+roff=application/x-troff
+rpm=application/x-rpm
+rtf=application/rtf
+sgm=text/sgml
+sgml=text/sgml
+sh=application/x-sh
+shar=application/x-shar
+silo=model/mesh
+sit=application/x-stuffit
+skd=application/x-koan
+skm=application/x-koan
+skp=application/x-koan
+skt=application/x-koan
+smi=application/smil
+smil=application/smil
+snd=audio/basic
+spl=application/x-futuresplash
+src=application/x-wais-source
+sv4cpio=application/x-sv4cpio
+sv4crc=application/x-sv4crc
+swf=application/x-shockwave-flash
+t=application/x-troff
+tar=application/x-tar
+tcl=application/x-tcl
+tex=application/x-tex
+texi=application/x-texinfo
+texinfo=application/x-texinfo
+tgz=application/x-gzip
+tif=image/tiff
+tiff=image/tiff
+tr=application/x-troff
+tsv=text/tab-separated-values
+txt=text/plain
+odt=application/vnd.oasis.opendocument.text
+ods=application/vnd.oasis.opendocument.spreadsheet
+odp=application/vnd.oasis.opendocument.presentation
+odb=application/vnd.oasis.opendocument.database
+ustar=application/x-ustar
+vcd=application/x-cdlink
+vm=text/plain
+vrml=model/vrml
+wav=audio/x-wav
+wrl=model/vrml
+xbm=image/x-xbitmap
+xls=application/xls
+xml=text/xml
+xpdl=text/xml
+xpm=image/x-xpixmap
+xwd=image/x-xwindowdump
+xyz=chemical/x-pdb
+zip=application/zip
+rar=application/rar
+msg=application/vnd.ms-outlook
+flv=video/x-flv
+docx=application/vnd.openxmlformats-officedocument.wordprocessingml.document
+wpd=application/wordperfect
+wiki=text/x-wiki
+sxc=application/vnd.sun.xml.calc
+xlsx=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
+csv=text/csv
+tsv=text/tab-separated-values
+sxi=application/vnd.sun.xml.impress
+pptx=application/vnd.openxmlformats-officedocument.presentationml.presentation
+odg=application/vnd.oasis.opendocument.graphics
+svg=image/svg+xml
+sxw=application/vnd.sun.xml.writer
\ No newline at end of file
Modified:
portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/conf/sample-portal/web/web-inf-extension-configuration.xml
===================================================================
---
portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/conf/sample-portal/web/web-inf-extension-configuration.xml 2011-09-21
17:34:23 UTC (rev 7477)
+++
portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/conf/sample-portal/web/web-inf-extension-configuration.xml 2011-09-22
03:16:42 UTC (rev 7478)
@@ -88,4 +88,22 @@
</init-params>
</component-plugin>
</external-component-plugins>
+ <external-component-plugins>
+ <target-component>org.exoplatform.upload.UploadService</target-component>
+ <component-plugin>
+ <name>MIME type Plugins for UploadService</name>
+ <set-method>register</set-method>
+ <type>org.exoplatform.upload.MimeTypeUploadPlugin</type>
+ <init-params>
+ <value-param>
+ <name>mimetype-path</name>
+ <value>war:/conf/mimetype/mimetypes.properties</value>
+ </value-param>
+ <value-param>
+ <name>mimetype-default</name>
+ <value>application/octet-stream</value>
+ </value-param>
+ </init-params>
+ </component-plugin>
+ </external-component-plugins>
</configuration>
\ No newline at end of file
Modified: portal/trunk/testsuite/webuibasedsamples/src/main/webapp/WEB-INF/web.xml
===================================================================
--- portal/trunk/testsuite/webuibasedsamples/src/main/webapp/WEB-INF/web.xml 2011-09-21
17:34:23 UTC (rev 7477)
+++ portal/trunk/testsuite/webuibasedsamples/src/main/webapp/WEB-INF/web.xml 2011-09-22
03:16:42 UTC (rev 7478)
@@ -28,11 +28,12 @@
<servlet>
<servlet-name>GateInServlet</servlet-name>
<servlet-class>org.gatein.wci.api.GateInServlet</servlet-class>
+ <load-on-startup>0</load-on-startup>
</servlet>
+
<servlet-mapping>
<servlet-name>GateInServlet</servlet-name>
<url-pattern>/gateinservlet</url-pattern>
- <load-on-startup>0</load-on-startup>
</servlet-mapping>
<security-role>