Author: rhauch
Date: 2009-01-09 16:37:10 -0500 (Fri, 09 Jan 2009)
New Revision: 700
Added:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetector.java
trunk/dna-graph/src/main/resources/org/jboss/dna/graph/MimeTypes.properties
trunk/dna-graph/src/test/java/org/jboss/dna/graph/mimetype/
trunk/dna-graph/src/test/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetectorTest.java
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/BasicExecutionContext.java
trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContext.java
trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContexts.java
trunk/dna-graph/src/main/java/org/jboss/dna/graph/GraphI18n.java
trunk/dna-graph/src/main/resources/org/jboss/dna/graph/GraphI18n.properties
trunk/dna-graph/src/test/java/org/jboss/dna/graph/sequencers/MockSequencerContext.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryLibrary.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeType.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectors.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencers/SequencerNodeContext.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencers/SequencingService.java
trunk/extensions/dna-connector-filesystem/src/main/java/org/jboss/dna/connector/filesystem/FileSystemRequestProcessor.java
Log:
DNA-264 Connectors may need to determine the MIME type for content
This change adds the ability in ExecutionContext to get a MimeTypeDetector, which can be
used to get the MIME type for some content. MimeTypeDetectors (the library component in
'org.jboss.dna.repository') now implements this interface (the signature already
matched), so that can be used in an ExecutionContext implementation in the complete DNA
system.
Also added a new ExtensionBasedMimeTypeDetector implementation that reads from the
classloader a properties file defining the extensions for each MIME type. A file
containing many common extensions is provided, although a user can either supply their own
or use a different constructor that allows them to pass the mappings in as a parameter.
The BasicExecutionContext uses this ExtensionBasedMimeTypeDetector as the default
implementation, although its possible to use a different one.
Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/BasicExecutionContext.java
===================================================================
---
trunk/dna-graph/src/main/java/org/jboss/dna/graph/BasicExecutionContext.java 2009-01-09
19:14:32 UTC (rev 699)
+++
trunk/dna-graph/src/main/java/org/jboss/dna/graph/BasicExecutionContext.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -28,6 +28,8 @@
import org.jboss.dna.common.component.ClassLoaderFactory;
import org.jboss.dna.common.component.StandardClassLoaderFactory;
import org.jboss.dna.common.util.Logger;
+import org.jboss.dna.graph.mimetype.ExtensionBasedMimeTypeDetector;
+import org.jboss.dna.graph.mimetype.MimeTypeDetector;
import org.jboss.dna.graph.properties.NamespaceRegistry;
import org.jboss.dna.graph.properties.PropertyFactory;
import org.jboss.dna.graph.properties.ValueFactories;
@@ -48,48 +50,65 @@
private final PropertyFactory propertyFactory;
private final ValueFactories valueFactories;
private final NamespaceRegistry namespaceRegistry;
+ private final MimeTypeDetector mimeTypeDetector;
public BasicExecutionContext() {
- this(null, null, null, null, null);
+ this(null, null, null, null, null, null);
}
public BasicExecutionContext( LoginContext loginContext ) {
- this(loginContext, null, null, null, null);
+ this(loginContext, null, null, null, null, null);
}
public BasicExecutionContext( AccessControlContext accessControlContext ) {
- this(null, accessControlContext, null, null, null);
+ this(null, accessControlContext, null, null, null, null);
}
public BasicExecutionContext( NamespaceRegistry namespaceRegistry,
ValueFactories valueFactories,
PropertyFactory propertyFactory ) {
- this(null, null, namespaceRegistry, valueFactories, propertyFactory);
+ this(null, null, namespaceRegistry, valueFactories, propertyFactory, null);
}
public BasicExecutionContext( NamespaceRegistry namespaceRegistry ) {
- this(null, null, namespaceRegistry, null, null);
+ this(null, null, namespaceRegistry, null, null, null);
}
public BasicExecutionContext( LoginContext loginContext,
NamespaceRegistry namespaceRegistry,
ValueFactories valueFactories,
PropertyFactory propertyFactory ) {
- this(loginContext, null, namespaceRegistry, valueFactories, propertyFactory);
+ this(loginContext, null, namespaceRegistry, valueFactories, propertyFactory,
null);
}
public BasicExecutionContext( AccessControlContext accessControlContext,
NamespaceRegistry namespaceRegistry,
ValueFactories valueFactories,
PropertyFactory propertyFactory ) {
- this(null, accessControlContext, namespaceRegistry, valueFactories,
propertyFactory);
+ this(null, accessControlContext, namespaceRegistry, valueFactories,
propertyFactory, null);
}
+ public BasicExecutionContext( AccessControlContext accessControlContext,
+ NamespaceRegistry namespaceRegistry,
+ ValueFactories valueFactories,
+ PropertyFactory propertyFactory,
+ MimeTypeDetector mimeTypeDetector ) {
+ this(null, accessControlContext, namespaceRegistry, valueFactories,
propertyFactory, mimeTypeDetector);
+ }
+
public BasicExecutionContext( ExecutionContext inheritedContext,
NamespaceRegistry namespaceRegistry ) {
- this(inheritedContext.getLoginContext(),
inheritedContext.getAccessControlContext(), namespaceRegistry, null, null);
+ this(inheritedContext.getLoginContext(),
inheritedContext.getAccessControlContext(), namespaceRegistry, null, null,
+ inheritedContext.getMimeTypeDetector());
}
+ public BasicExecutionContext( ExecutionContext inheritedContext,
+ MimeTypeDetector mimeTypeDetector ) {
+ this(inheritedContext.getLoginContext(),
inheritedContext.getAccessControlContext(),
+ inheritedContext.getNamespaceRegistry(),
inheritedContext.getValueFactories(),
+ inheritedContext.getPropertyFactory(), mimeTypeDetector);
+ }
+
/*
* This constructor exists to deal with mutually-exclusive parameters, such as
LoginContext and AccessControlContext.
*/
@@ -97,7 +116,8 @@
AccessControlContext accessControlContext,
NamespaceRegistry namespaceRegistry,
ValueFactories valueFactories,
- PropertyFactory propertyFactory ) {
+ PropertyFactory propertyFactory,
+ MimeTypeDetector mimeTypeDetector ) {
this.loginContext = loginContext;
this.accessControlContext = accessControlContext;
if (loginContext == null) {
@@ -109,6 +129,7 @@
this.valueFactories = valueFactories == null ? new
StandardValueFactories(this.namespaceRegistry) : valueFactories;
this.propertyFactory = propertyFactory == null ? new
BasicPropertyFactory(this.valueFactories) : propertyFactory;
this.classLoaderFactory = new StandardClassLoaderFactory();
+ this.mimeTypeDetector = mimeTypeDetector != null ? mimeTypeDetector :
ExtensionBasedMimeTypeDetector.getInstance();
}
/**
@@ -147,7 +168,16 @@
/**
* {@inheritDoc}
+ *
+ * @see org.jboss.dna.graph.ExecutionContext#getMimeTypeDetector()
*/
+ public MimeTypeDetector getMimeTypeDetector() {
+ return mimeTypeDetector;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public PropertyFactory getPropertyFactory() {
return propertyFactory;
}
Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContext.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContext.java 2009-01-09
19:14:32 UTC (rev 699)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContext.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -26,6 +26,7 @@
import javax.security.auth.login.LoginContext;
import org.jboss.dna.common.component.ClassLoaderFactory;
import org.jboss.dna.common.util.Logger;
+import org.jboss.dna.graph.mimetype.MimeTypeDetector;
import org.jboss.dna.graph.properties.NamespaceRegistry;
import org.jboss.dna.graph.properties.Property;
import org.jboss.dna.graph.properties.PropertyFactory;
@@ -71,6 +72,13 @@
Logger getLogger( String name );
/**
+ * Return an object that can be used to determine the MIME type of some content, such
as the content of a file.
+ *
+ * @return the detector; never null
+ */
+ MimeTypeDetector getMimeTypeDetector();
+
+ /**
* @return the login context; may be <code>null</code>
*/
LoginContext getLoginContext();
Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContexts.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContexts.java 2009-01-09
19:14:32 UTC (rev 699)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContexts.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -26,6 +26,7 @@
import javax.security.auth.login.LoginContext;
import org.jboss.dna.common.util.CheckArg;
import org.jboss.dna.common.util.Logger;
+import org.jboss.dna.graph.mimetype.MimeTypeDetector;
import org.jboss.dna.graph.properties.NameFactory;
import org.jboss.dna.graph.properties.NamespaceRegistry;
import org.jboss.dna.graph.properties.PathFactory;
@@ -177,6 +178,15 @@
/**
* {@inheritDoc}
*
+ * @see org.jboss.dna.graph.ExecutionContext#getMimeTypeDetector()
+ */
+ public MimeTypeDetector getMimeTypeDetector() {
+ return delegate.getMimeTypeDetector();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see org.jboss.dna.graph.ExecutionContext#getNamespaceRegistry()
*/
public NamespaceRegistry getNamespaceRegistry() {
Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/GraphI18n.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/GraphI18n.java 2009-01-09 19:14:32
UTC (rev 699)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/GraphI18n.java 2009-01-09 21:37:10
UTC (rev 700)
@@ -59,6 +59,7 @@
public static I18n pathExpressionHasInvalidSelect;
public static I18n pathExpressionHasInvalidMatch;
public static I18n messageDigestNotFound;
+ public static I18n unableToAccessResourceFileFromClassLoader;
public static I18n executingRequest;
public static I18n executedRequest;
Added:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetector.java
===================================================================
---
trunk/dna-graph/src/main/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetector.java
(rev 0)
+++
trunk/dna-graph/src/main/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetector.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -0,0 +1,165 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.dna.graph.mimetype;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import net.jcip.annotations.Immutable;
+import org.jboss.dna.common.i18n.I18n;
+import org.jboss.dna.common.util.Logger;
+import org.jboss.dna.graph.GraphI18n;
+
+/**
+ * A {@link MimeTypeDetector} that attempts to match the extension of the supplied name
against a set of known file extensions.
+ *
+ * @author Randall Hauch
+ */
+@Immutable
+public class ExtensionBasedMimeTypeDetector implements MimeTypeDetector {
+
+ protected static final ExtensionBasedMimeTypeDetector DEFAULT_INSTANCE = new
ExtensionBasedMimeTypeDetector();
+
+ /**
+ * Get an immutable shared instance of this detector with the {@link
#MIME_TYPE_EXTENSIONS_RESOURCE_PATH default mappings}.
+ *
+ * @return the shared instance
+ */
+ public static ExtensionBasedMimeTypeDetector getInstance() {
+ return DEFAULT_INSTANCE;
+ }
+
+ /**
+ * The default location of the properties file containing the extension patterns to
MIME types.
+ */
+ public static final String MIME_TYPE_EXTENSIONS_RESOURCE_PATH =
"/org/jboss/dna/graph/MimeTypes.properties";
+
+ /**
+ * The mapping of extension (which includes the leading '.') to MIME type.
+ */
+ private final Map<String, String> mimeTypesByExtension;
+
+ /**
+ * Create a default instance of the extension-based MIME type detector. The set of
extension patterns to MIME-types is loaded
+ * from "org.jboss.dna.graph.MimeTypes.properties".
+ */
+ public ExtensionBasedMimeTypeDetector() {
+ this(null, true);
+ }
+
+ /**
+ * Create an instance of the extension-based MIME type detector by using the supplied
mappings. The set of extension patterns
+ * to MIME-types is loaded from "org.jboss.dna.graph.MimeTypes.properties",
but the supplied extension mappings override any
+ * default mappings.
+ *
+ * @param extensionsToMimeTypes the mapping of extension patterns to MIME types,
which will override the default mappings; may
+ * be null if the default mappings are to be used
+ */
+ public ExtensionBasedMimeTypeDetector( Map<String, String>
extensionsToMimeTypes ) {
+ this(extensionsToMimeTypes, true);
+ }
+
+ /**
+ * Create an instance of the extension-based MIME type detector by using the supplied
mappings. If requested, the set of
+ * extension patterns to MIME-types is loaded from
"org.jboss.dna.graph.MimeTypes.properties" and any supplied extension
+ * mappings override any default mappings.
+ *
+ * @param extensionsToMimeTypes the mapping of extension patterns to MIME types,
which will override the default mappings; may
+ * be null if the default mappings are to be used
+ * @param initWithDefaults true if the default mappings are to be loaded first
+ */
+ public ExtensionBasedMimeTypeDetector( Map<String, String>
extensionsToMimeTypes,
+ boolean initWithDefaults ) {
+ Map<String, String> mappings = getDefaultMappings();
+ if (extensionsToMimeTypes != null) {
+ for (Map.Entry<String, String> entry :
extensionsToMimeTypes.entrySet()) {
+ String extensionString = entry.getKey();
+ if (extensionString == null) continue;
+ // Lowercase, trime, and remove all leading '.' characters ...
+ extensionString =
extensionString.toLowerCase().trim().replaceAll("^.+", "");
+ if (extensionString.length() == 0) continue;
+ String mimeType = entry.getValue();
+ if (mimeType == null) continue;
+ mimeType = entry.getValue().trim();
+ if (mimeType.length() == 0) continue;
+ assert extensionString.length() != 0;
+ assert mimeType.length() != 0;
+ mappings.put(extensionString, mimeType);
+ }
+ }
+ // Now put the mappings into the different maps ...
+ Map<String, String> mappingsByAnyCharExtension = new HashMap<String,
String>();
+ for (Map.Entry<String, String> entry : mappings.entrySet()) {
+ String extensionString = entry.getKey();
+ String mimeType = entry.getValue();
+ assert extensionString != null;
+ assert extensionString.length() != 0;
+ assert mimeType != null;
+ assert mimeType.length() != 0;
+ mappingsByAnyCharExtension.put("." + extensionString, mimeType);
+ }
+ mimeTypesByExtension = Collections.unmodifiableMap(mappingsByAnyCharExtension);
+ }
+
+ protected static Map<String, String> getDefaultMappings() {
+ Properties extensionsToMimeTypes = new Properties();
+ try {
+
extensionsToMimeTypes.load(ExtensionBasedMimeTypeDetector.class.getResourceAsStream(MIME_TYPE_EXTENSIONS_RESOURCE_PATH));
+ } catch (IOException e) {
+ I18n msg = GraphI18n.unableToAccessResourceFileFromClassLoader;
+ Logger.getLogger(ExtensionBasedMimeTypeDetector.class).warn(e, msg,
MIME_TYPE_EXTENSIONS_RESOURCE_PATH);
+ }
+ Map<String, String> mimeTypesByExtension = new HashMap<String,
String>();
+ for (Map.Entry<Object, Object> entry : extensionsToMimeTypes.entrySet()) {
+ String mimeType = entry.getKey().toString().trim();
+ String extensionStrings = entry.getValue().toString().toLowerCase().trim();
+ for (String extensionString : extensionStrings.split("\\s+")) {
+ extensionString = extensionString.trim();
+ if (extensionString.length() != 0)
mimeTypesByExtension.put(extensionString, mimeType);
+ }
+ }
+ return mimeTypesByExtension;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.jboss.dna.graph.mimetype.MimeTypeDetector#mimeTypeOf(java.lang.String,
java.io.InputStream)
+ */
+ public String mimeTypeOf( String name,
+ InputStream content ) {
+ if (name == null || name.length() == 0) return null;
+ String trimmedName = name.trim();
+ if (trimmedName.length() == 0) return null;
+
+ // Find the extension ...
+ int indexOfDelimiter = trimmedName.lastIndexOf('.');
+ if (indexOfDelimiter < 1) return null;
+ String extension = trimmedName.substring(indexOfDelimiter).toLowerCase();
+
+ // Look for a match ...
+ return mimeTypesByExtension.get(extension);
+ }
+}
Property changes on:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetector.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: trunk/dna-graph/src/main/resources/org/jboss/dna/graph/GraphI18n.properties
===================================================================
--- trunk/dna-graph/src/main/resources/org/jboss/dna/graph/GraphI18n.properties 2009-01-09
19:14:32 UTC (rev 699)
+++ trunk/dna-graph/src/main/resources/org/jboss/dna/graph/GraphI18n.properties 2009-01-09
21:37:10 UTC (rev 700)
@@ -46,6 +46,7 @@
pathExpressionHasInvalidSelect = Invalid select expression "{0}" in the path
expression "{1}"
pathExpressionHasInvalidMatch = Invalid match expression "{0}" in the path
expression "{1}"
messageDigestNotFound = The "{0}" message digest algorithm could not be found
+unableToAccessResourceFileFromClassLoader = Unable to access "{0}" resource
from the class loader
executingRequest = Executing {0}
executedRequest = Executed {0}
Added: trunk/dna-graph/src/main/resources/org/jboss/dna/graph/MimeTypes.properties
===================================================================
--- trunk/dna-graph/src/main/resources/org/jboss/dna/graph/MimeTypes.properties
(rev 0)
+++ trunk/dna-graph/src/main/resources/org/jboss/dna/graph/MimeTypes.properties 2009-01-09
21:37:10 UTC (rev 700)
@@ -0,0 +1,168 @@
+# This file contains the mapping of filename extensions to MIME types.
+#
+# The format is simply a line for each MIME type. If multiple extensions map to the same
MIME type, simply
+# put them all on the right-hand side of the '=' and separated by spaces. For
example:
+#
+# text/plain = txt
+# text/html = html htm
+#
+# If the same extension applies to multiple MIME types, then the last MIME type wins.
+#
+# MIME type = Extension
+application/andrew-inset = ez
+application/acad = dwg
+application/arj = arj
+application/astound = asd asn
+application/clariscad = ccad
+application/drafting = drw
+application/dxf = dxf
+application/fractals = fif
+application/finale = mus etf fxt ftm
+application/i-deas = unv
+application/iges = iges, igs
+application/java-archive = jar
+application/json = json
+application/mac-binhex40 = hqx
+application/mac-compactpro = cpt
+application/octet-stream = bin dms lha lzh exe class w02 w03 w04 cab jar dzl
+application/oda = oda
+application/pdf = pdf
+application/postscript = ai eps ps
+application/rtf = rtf
+application/smil = smi smil
+application/pro_eng = part prt
+application/set = set
+application/sla = stl
+application/solids = sol
+application/STEP = st step stp
+application/vda = vda
+application/vnd.mif = mif
+application/vnd.mozilla.xul+xml = xul
+application/vnd.ms-excel = xls csv
+application/vnd.ms-powerpoint = ppt ppz pps pot
+application/vnd.ms-powerpoint.template = potm potx
+application/vnd.ms-access = mdb
+application/vnd.oasis.opendocument.text = odt
+application/vnd.oasis.opendocument.formula = odf
+application/vnd.oasis.opendocument.chart = odc
+application/vnd.oasis.opendocument.graphics = odg
+application/vnd.oasis.opendocument.presentation = odp
+application/vnd.oasis.opendocument.spreadsheet = ods
+application/vnd.oasis.opendocument.presentation-template = otp
+application/vnd.oasis.opendocument.formula-template = ott
+application/vnd.openxmlformats = docx docm dotm xslm xslx
+application/vnd.stardivision.impress = sdd
+application/vnd.stardivision.math = smf
+application/vnd.wap.wbxml = wbxml
+application/vnd.wap.wmlc = wmlc
+application/vnd.wap.wmlscriptc = wmlsc
+application/x-bcpio = bcpio
+application/x-cpio = cpio
+application/x-cdlink = vcd
+application/x-chess-pgn = pgn
+application/x-compress = Z
+application/x-cpio = cpio
+application/x-csh = csh
+application/x-director = dcr dir dxr
+application/x-dvi = dvi
+application/x-dwf = dwf
+application/x-futuresplash = spl
+application/x-gtar = gtar
+application/x-gzip = gz
+application/x-hdf = hdf
+application/x-javascript = js mocha
+application/x-koan = skp skd skt skm
+application/x-latex = latex
+application/x-midi = mid
+application/x-mif = mif
+# various Micro$oft extensions
+application/x-ms-wmz = wmz
+application/x-ms-wmd = wmd
+#
+application/x-netcdf = nc cdf
+application/x-sdp = sdp
+application/x-sh = sh
+application/x-shar = shar
+application/x-shockwave-flash = swf
+application/x-stuffit = sit
+application/x-sv4cpio = sv4cpio
+application/x-sv4crc = sv4crc
+application/x-tar = tar
+application/x-tcl = tcl
+application/x-tex = tex
+application/x-texinfo = texinfo texi
+application/x-troff = t tr roff
+application/x-troff-man = man
+application/x-troff-me = me
+application/x-troff-ms = ms
+application/x-ustar = ustar
+application/x-wais-source = src
+application/x-winhelp = hlp
+application/x400-bp
+# XML support
+application/xml = xml dtd xsl ent cat sty
+application/zip = zip
+
+audio/basic = au snd
+audio/midi = mid midi kar
+audio/mpeg = mpga mp2 mp3
+audio/x-aiff = aif aiff aifc
+# various Micro$oft extensions
+audio/x-ms-wma = wma
+audio/x-ms-wax = wax
+audio/x-pn-realaudio = ram rm
+audio/x-pn-realaudio-plugin = rpm
+audio/x-realaudio = ra
+audio/x-voice = voc
+audio/x-wav = wav
+chemical/x-pdb = pdb
+chemical/x-xyz = xyz
+
+image/bmp = bmp
+image/gif = gif
+image/ief = ief
+image/jpeg = jpeg jpg jpe
+image/pict = pict
+image/png = png
+image/tiff = tiff tif
+image/vnd.wap.wbmp = wbmp
+image/x-cmu-raster = ras
+image/x-freehand = fh4 fh7 fh5 fhc fh
+image/x-portable-anymap = pnm
+image/x-portable-bitmap = pbm
+image/x-portable-graymap = pgm
+image/x-portable-pixmap = ppm
+image/x-rgb = rgb
+image/x-xbitmap = xbm
+image/x-xpixmap = xpm
+image/x-xwindowdump = xwd
+model/iges = igs iges
+model/mesh = msh mesh silo
+model/vrml = wrl vrml
+
+text/css = css
+text/html = html htm docmhtml
+text/plain = asc txt
+text/richtext = rtx
+text/rtf = rtf
+text/sgml = sgml sgm
+text/tab-separated-values = tsv
+text/vnd.wap.wml = wml
+text/vnd.wap.wmlscript = wmls
+text/x-setext = etx
+text/xml = xml
+video/mpeg = mpeg mpg mpe
+# various Micro$oft extensions
+video/x-ms-asf = asf asx
+video/x-ms-wm = wm
+video/x-ms-wmv = wmv
+video/x-ms-wmx = wmx
+video/x-ms-wvx = wvx
+video/quicktime = qt mov
+video/x-msvideo = avi
+video/x-sgi-movie = movie
+x-conference/x-cooltalk = ice
+# not sure if these are necessary
+x-world/x-3dmf = 3dmf 3dm qd3d qd3
+x-world/x-vrml = wrl vrml
+
Property changes on:
trunk/dna-graph/src/main/resources/org/jboss/dna/graph/MimeTypes.properties
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/dna-graph/src/test/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetectorTest.java
===================================================================
---
trunk/dna-graph/src/test/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetectorTest.java
(rev 0)
+++
trunk/dna-graph/src/test/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetectorTest.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -0,0 +1,100 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.dna.graph.mimetype;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.assertThat;
+import java.io.InputStream;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Randall Hauch
+ */
+public class ExtensionBasedMimeTypeDetectorTest {
+
+ private ExtensionBasedMimeTypeDetector detector;
+ private InputStream stream;
+
+ @Before
+ public void beforeEach() throws Exception {
+ detector = new ExtensionBasedMimeTypeDetector();
+ stream = null;
+ }
+
+ @Test
+ public void shouldFindMimeTypeForExtenionsInStandardPropertiesFile() {
+ assertThat(detector.mimeTypeOf("something.txt", stream),
is("text/plain"));
+ }
+
+ @Test
+ public void shouldFindMimeTypeForOneCharacterExtension() {
+ assertThat(detector.mimeTypeOf("something.gzip.Z", stream),
is("application/x-compress"));
+ }
+
+ @Test
+ public void shouldFindMimeTypeForTwoCharacterExtension() {
+ assertThat(detector.mimeTypeOf("something.sh", stream),
is("application/x-sh"));
+ }
+
+ @Test
+ public void shouldFindMimeTypeForThreeCharacterExtension() {
+ assertThat(detector.mimeTypeOf("something.png", stream),
is("image/png"));
+ }
+
+ @Test
+ public void shouldNotFindMimeTypeForNameWithoutExtension() {
+ assertThat(detector.mimeTypeOf("something", stream), is(nullValue()));
+ }
+
+ @Test
+ public void shouldNotFindMimeTypeForNameUnknownExtension() {
+ assertThat(detector.mimeTypeOf("something.thisExtensionIsNotKnown",
stream), is(nullValue()));
+ }
+
+ @Test
+ public void shouldNotFindMimeTypeForZeroLengthName() {
+ assertThat(detector.mimeTypeOf(null, stream), is(nullValue()));
+ }
+
+ @Test
+ public void shouldNotFindMimeTypeForNameContainingWhitespace() {
+ assertThat(detector.mimeTypeOf("/t /n ", stream), is(nullValue()));
+ }
+
+ @Test
+ public void shouldNotFindMimeTypeForNullName() {
+ assertThat(detector.mimeTypeOf(null, stream), is(nullValue()));
+ }
+
+ @Test
+ public void shouldFindMimeTypeForUppercaseExtenionsInStandardPropertiesFile() {
+ assertThat(detector.mimeTypeOf("something.TXT", stream),
is("text/plain"));
+ }
+
+ @Test
+ public void shouldFindMimeTypeForNameWithTrailingWhitespace() {
+ assertThat(detector.mimeTypeOf("something.txt \t", stream),
is("text/plain"));
+ }
+
+}
Property changes on:
trunk/dna-graph/src/test/java/org/jboss/dna/graph/mimetype/ExtensionBasedMimeTypeDetectorTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/dna-graph/src/test/java/org/jboss/dna/graph/sequencers/MockSequencerContext.java
===================================================================
---
trunk/dna-graph/src/test/java/org/jboss/dna/graph/sequencers/MockSequencerContext.java 2009-01-09
19:14:32 UTC (rev 699)
+++
trunk/dna-graph/src/test/java/org/jboss/dna/graph/sequencers/MockSequencerContext.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -32,6 +32,7 @@
import org.jboss.dna.graph.BasicExecutionContext;
import org.jboss.dna.graph.DnaLexicon;
import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.mimetype.MimeTypeDetector;
import org.jboss.dna.graph.properties.Name;
import org.jboss.dna.graph.properties.NamespaceRegistry;
import org.jboss.dna.graph.properties.Path;
@@ -70,6 +71,15 @@
/**
* {@inheritDoc}
*
+ * @see org.jboss.dna.graph.ExecutionContext#getMimeTypeDetector()
+ */
+ public MimeTypeDetector getMimeTypeDetector() {
+ return context.getMimeTypeDetector();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see org.jboss.dna.graph.sequencers.SequencerContext#getInputPath()
*/
public Path getInputPath() {
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryLibrary.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryLibrary.java 2009-01-09
19:14:32 UTC (rev 699)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryLibrary.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -41,6 +41,7 @@
import org.jboss.dna.graph.connectors.RepositoryConnectionPool;
import org.jboss.dna.graph.connectors.RepositoryContext;
import org.jboss.dna.graph.connectors.RepositorySource;
+import org.jboss.dna.repository.mimetype.MimeTypeDetectors;
import org.jboss.dna.repository.services.AbstractServiceAdministrator;
import org.jboss.dna.repository.services.ServiceAdministrator;
@@ -100,6 +101,7 @@
}
+ private final MimeTypeDetectors mimeTypeDetectors = new MimeTypeDetectors();
private final ServiceAdministrator administrator = new Administrator();
private final ReadWriteLock sourcesLock = new ReentrantReadWriteLock();
private final CopyOnWriteArrayList<RepositoryConnectionPool> pools = new
CopyOnWriteArrayList<RepositoryConnectionPool>();
@@ -165,6 +167,7 @@
public RepositoryConnectionFactory getRepositoryConnectionFactory() {
return RepositoryLibrary.this;
}
+
};
}
@@ -176,6 +179,13 @@
}
/**
+ * @return mimeTypeDetectors
+ */
+ public MimeTypeDetectors getMimeTypeDetectors() {
+ return mimeTypeDetectors;
+ }
+
+ /**
* Get the delegate connection factory.
*
* @return the connection factory to which this instance should delegate in the event
that a source is not found in this
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeType.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeType.java 2009-01-09
19:14:32 UTC (rev 699)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeType.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -32,7 +32,10 @@
*/
public final class MimeType {
- public static final MimeTypeDetectors DEFAULT_DETECTORS = new MimeTypeDetectors();
+ /**
+ * The set of
+ */
+ public static final MimeTypeDetectors DEFAULT_DETECTORS =
MimeTypeDetectors.DEFAULT_DETECTORS;
/**
* @param config See {@link MimeTypeDetectors#addDetector(MimeTypeDetectorConfig)}.
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectors.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectors.java 2009-01-09
19:14:32 UTC (rev 699)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectors.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -32,11 +32,15 @@
import org.jboss.dna.graph.mimetype.MimeTypeDetector;
/**
+ * Facility for managing {@link MimeTypeDetectorConfig}s.
+ *
* @author jverhaeg
*/
@ThreadSafe
-public final class MimeTypeDetectors {
+public final class MimeTypeDetectors implements MimeTypeDetector {
+ public static final MimeTypeDetectors DEFAULT_DETECTORS = new MimeTypeDetectors();
+
/**
* Class loader factory instance that always returns the {@link
Thread#getContextClassLoader() current thread's context class
* loader}, or if <code>null</code> the class loader for this class.
@@ -44,11 +48,12 @@
protected static final ClassLoaderFactory DEFAULT_CLASSLOADER_FACTORY = new
StandardClassLoaderFactory(
MimeTypeDetectors.class.getClassLoader());
- private final ComponentLibrary<MimeTypeDetector, MimeTypeDetectorConfig>
library = new ComponentLibrary<MimeTypeDetector, MimeTypeDetectorConfig>(
-
true);
- private final AtomicReference<Logger> logger = new
AtomicReference<Logger>(Logger.getLogger(getClass()));
+ private final ComponentLibrary<MimeTypeDetector, MimeTypeDetectorConfig>
library;
+ private final AtomicReference<Logger> logger;
public MimeTypeDetectors() {
+ logger = new AtomicReference<Logger>(Logger.getLogger(getClass()));
+ library = new ComponentLibrary<MimeTypeDetector,
MimeTypeDetectorConfig>(true);
library.setClassLoaderFactory(DEFAULT_CLASSLOADER_FACTORY);
}
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencers/SequencerNodeContext.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencers/SequencerNodeContext.java 2009-01-09
19:14:32 UTC (rev 699)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencers/SequencerNodeContext.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -38,6 +38,7 @@
import org.jboss.dna.common.collection.Problems;
import org.jboss.dna.common.util.CheckArg;
import org.jboss.dna.common.util.Logger;
+import org.jboss.dna.graph.mimetype.MimeTypeDetector;
import org.jboss.dna.graph.properties.Name;
import org.jboss.dna.graph.properties.NamespaceRegistry;
import org.jboss.dna.graph.properties.Path;
@@ -162,7 +163,16 @@
/**
* {@inheritDoc}
+ *
+ * @see org.jboss.dna.graph.ExecutionContext#getMimeTypeDetector()
*/
+ public MimeTypeDetector getMimeTypeDetector() {
+ return context.getMimeTypeDetector();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public ValueFactories getValueFactories() {
return factories;
}
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencers/SequencingService.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencers/SequencingService.java 2009-01-09
19:14:32 UTC (rev 699)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencers/SequencingService.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -50,6 +50,7 @@
import org.jboss.dna.common.util.CheckArg;
import org.jboss.dna.common.util.HashCode;
import org.jboss.dna.common.util.Logger;
+import org.jboss.dna.graph.mimetype.MimeTypeDetector;
import org.jboss.dna.graph.properties.NamespaceRegistry;
import org.jboss.dna.graph.properties.PropertyFactory;
import org.jboss.dna.graph.properties.ValueFactories;
@@ -595,6 +596,15 @@
/**
* {@inheritDoc}
*
+ * @see org.jboss.dna.graph.ExecutionContext#getMimeTypeDetector()
+ */
+ public MimeTypeDetector getMimeTypeDetector() {
+ return delegate.getMimeTypeDetector();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see org.jboss.dna.graph.ExecutionContext#getAccessControlContext()
*/
public AccessControlContext getAccessControlContext() {
Modified:
trunk/extensions/dna-connector-filesystem/src/main/java/org/jboss/dna/connector/filesystem/FileSystemRequestProcessor.java
===================================================================
---
trunk/extensions/dna-connector-filesystem/src/main/java/org/jboss/dna/connector/filesystem/FileSystemRequestProcessor.java 2009-01-09
19:14:32 UTC (rev 699)
+++
trunk/extensions/dna-connector-filesystem/src/main/java/org/jboss/dna/connector/filesystem/FileSystemRequestProcessor.java 2009-01-09
21:37:10 UTC (rev 700)
@@ -21,8 +21,12 @@
*/
package org.jboss.dna.connector.filesystem;
+import java.io.BufferedInputStream;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.Map;
import org.jboss.dna.common.i18n.I18n;
import org.jboss.dna.graph.ExecutionContext;
@@ -30,6 +34,7 @@
import org.jboss.dna.graph.JcrNtLexicon;
import org.jboss.dna.graph.Location;
import org.jboss.dna.graph.connectors.RepositorySourceException;
+import org.jboss.dna.graph.mimetype.MimeTypeDetector;
import org.jboss.dna.graph.properties.BinaryFactory;
import org.jboss.dna.graph.properties.DateTimeFactory;
import org.jboss.dna.graph.properties.Name;
@@ -58,10 +63,13 @@
*/
public class FileSystemRequestProcessor extends RequestProcessor {
+ private static final String DEFAULT_MIME_TYPE = "application/octet";
+
private final Map<String, File> rootsByName;
private final String defaultNamespaceUri;
private final FilenameFilter filenameFilter;
private final boolean updatesAllowed;
+ private final MimeTypeDetector mimeTypeDetector;
/**
* @param sourceName
@@ -83,6 +91,7 @@
this.defaultNamespaceUri =
getExecutionContext().getNamespaceRegistry().getDefaultNamespaceUri();
this.filenameFilter = filenameFilter;
this.updatesAllowed = updatesAllowed;
+ this.mimeTypeDetector = context.getMimeTypeDetector();
}
/**
@@ -162,8 +171,26 @@
// request.addProperty(factory.create(JcrLexicon.ENCODED,
stringFactory.create("UTF-8")));
// Discover the mime type ...
- // String mimeType = ...
- // request.addProperty(factory.create(JcrLexicon.MIMETYPE, mimeType));
+ String mimeType = null;
+ InputStream contents = null;
+ boolean mimeTypeError = false;
+ try {
+ contents = new BufferedInputStream(new FileInputStream(file));
+ mimeType = mimeTypeDetector.mimeTypeOf(file.getName(), contents);
+ if (mimeType == null) mimeType = DEFAULT_MIME_TYPE;
+ request.addProperty(factory.create(JcrLexicon.MIMETYPE, mimeType));
+ } catch (IOException e) {
+ mimeTypeError = true;
+ request.setError(e);
+ } finally {
+ if (contents != null) {
+ try {
+ contents.close();
+ } catch (IOException e) {
+ if (!mimeTypeError) request.setError(e);
+ }
+ }
+ }
// Now put the file's content into the "jcr:data" property
...
BinaryFactory binaryFactory =
getExecutionContext().getValueFactories().getBinaryFactory();