Skip to content

Commit

Permalink
[playframework#358] Allow plugins to provide additional mimetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
erwan committed May 9, 2011
1 parent add46bf commit 0c6f5ec
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 45 deletions.
22 changes: 16 additions & 6 deletions framework/src/play/PlayPlugin.java
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import play.classloading.ApplicationClasses.ApplicationClass;
import play.db.Model;
import play.mvc.Http.Request;
Expand Down Expand Up @@ -74,14 +75,14 @@ public String getMessage(String locale, Object key, Object... args) {
}

/**
* Retun the plugin status
* Return the plugin status
*/
public String getStatus() {
return null;
}

/**
* Retun the plugin status in JSON format
* Return the plugin status in JSON format
*/
public JsonObject getJsonStatus() {
return null;
Expand Down Expand Up @@ -114,7 +115,7 @@ public boolean rawInvocation(Request request, Response response) throws Exceptio
}

/**
* Let a chance to this plugin to manage a static ressource
* Let a chance to this plugin to manage a static resource
* @param request The Play request
* @param response The Play response
* @return true if this plugin has managed this request
Expand Down Expand Up @@ -147,7 +148,7 @@ public boolean detectClassesChange() {

/**
* Called at application start (and at each reloading)
* Time to start statefull things.
* Time to start stateful things.
*/
public void onApplicationStart() {
}
Expand All @@ -160,7 +161,7 @@ public void afterApplicationStart() {

/**
* Called at application stop (and before each reloading)
* Time to shutdown statefull things.
* Time to shutdown stateful things.
*/
public void onApplicationStop() {
}
Expand Down Expand Up @@ -226,7 +227,7 @@ public void afterActionInvocation() {
}

/**
* Called when the application.cond has been read.
* Called when the application.conf has been read.
*/
public void onConfigurationRead() {
}
Expand All @@ -253,6 +254,15 @@ public List<String> addTemplateExtensions() {
return new ArrayList<String>();
}

/**
* Override to provide additional mime types from your plugin. These mimetypes get priority over
* the default framework mimetypes but not over the application's configuration.
* @return a Map from extensions (without dot) to mimetypes
*/
public Map<String, String> addMimeTypes() {
return new HashMap<String, String>();
}

/**
* Let a chance to the plugin to compile it owns classes.
* Must be added to the mutable list.
Expand Down
75 changes: 46 additions & 29 deletions framework/src/play/libs/MimeTypes.java
@@ -1,10 +1,13 @@
package play.libs;

import play.*;
import play.Logger;
import play.Play;
import play.PlayPlugin;
import play.mvc.Http;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -51,17 +54,17 @@ public static String getMimeType(String filename, String defaultMimeType) {
}
return defaultMimeType;
}

/**
* return the content-type from a file name. If none is found returning application/octet-stream<br/>
* For a text-based content-type, also return the encoding suffix eg. <em>"text/plain; charset=utf-8"</em>
* @param filename the file name
* @return the content-type deduced from the file extension.
*/
public static String getContentType(String filename){
return getContentType(filename, "application/octet-stream");
return getContentType(filename, "application/octet-stream");
}

/**
* return the content-type from a file name.<br/>
* For a text-based content-type, also return the encoding suffix eg. <em>"text/plain; charset=utf-8"</em>
Expand All @@ -70,16 +73,16 @@ public static String getContentType(String filename){
* @return the content-type deduced from the file extension.
*/
public static String getContentType(String filename, String defaultContentType){
String contentType = getMimeType(filename, null);
if (contentType == null){
contentType = defaultContentType;
}
if (contentType != null && contentType.startsWith("text/")){
return contentType + "; charset=" + Http.Response.current().encoding;
}
return contentType;
String contentType = getMimeType(filename, null);
if (contentType == null){
contentType = defaultContentType;
}
if (contentType != null && contentType.startsWith("text/")){
return contentType + "; charset=" + Http.Response.current().encoding;
}
return contentType;
}

/**
* check the mimetype is referenced in the mimetypes database
* @param mimeType the mimeType to verify
Expand All @@ -94,25 +97,39 @@ public static boolean isValidMimeType(String mimeType) {
}
}

private static Properties mimetypes() {
if (mimetypes == null) {
try {
InputStream is = MimeTypes.class.getClassLoader().getResourceAsStream("play/libs/mime-types.properties");
mimetypes = new Properties();
mimetypes.load(is);
} catch (Exception ex) {
Logger.warn(ex.getMessage());
private static synchronized void initMimetypes() {
if (mimetypes != null) return;
// Load default mimetypes from the framework
try {
InputStream is = MimeTypes.class.getClassLoader().getResourceAsStream("play/libs/mime-types.properties");
mimetypes = new Properties();
mimetypes.load(is);
} catch (Exception ex) {
Logger.warn(ex.getMessage());
}
// Load mimetypes from plugins
for (PlayPlugin plugin: Play.pluginCollection.getEnabledPlugins()) {
Map<String, String> pluginTypes = plugin.addMimeTypes();
for (String type: pluginTypes.keySet()) {
mimetypes.setProperty(type, pluginTypes.get(type));
}
Enumeration<Object> confenum = Play.configuration.keys();
while (confenum.hasMoreElements()) {
String key = (String)confenum.nextElement();
if (key.startsWith("mimetype.")) {
String type = key.substring(key.indexOf('.')+1).toLowerCase();
String value = (String)Play.configuration.get(key);
mimetypes.setProperty(type, value);
}
}
// Load custom mimetypes from the application configuration
Enumeration<Object> confenum = Play.configuration.keys();
while (confenum.hasMoreElements()) {
String key = (String)confenum.nextElement();
if (key.startsWith("mimetype.")) {
String type = key.substring(key.indexOf('.') + 1).toLowerCase();
String value = (String)Play.configuration.get(key);
mimetypes.setProperty(type, value);
}
}
}

private static Properties mimetypes() {
if (mimetypes == null) {
initMimetypes();
}
return mimetypes;
}

Expand Down
1 change: 0 additions & 1 deletion framework/src/play/libs/mime-types.properties
Expand Up @@ -369,7 +369,6 @@ sh=text/x-scriptsh
shar=application/x-bsh
shtml=text/x-server-parsed-html
sid=audio/x-psid
sit=application/x-sit
skd=application/x-koan
skm=application/x-koan
skp=application/x-koan
Expand Down
6 changes: 3 additions & 3 deletions framework/src/play/mvc/Before.java
Expand Up @@ -10,8 +10,8 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Before {
public @interface Before {

/**
* Does not intercept these actions
*/
Expand All @@ -22,5 +22,5 @@
* Interceptor priority (0 is high priority)
*/
int priority() default 0;

}
4 changes: 2 additions & 2 deletions framework/src/play/mvc/Finally.java
Expand Up @@ -11,7 +11,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Finally {

/**
* Does not intercept these actions
*/
Expand All @@ -22,5 +22,5 @@
* Interceptor priority (0 is high priority)
*/
int priority() default 0;

}
4 changes: 1 addition & 3 deletions framework/src/play/mvc/Util.java
Expand Up @@ -10,6 +10,4 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Util {

}
public @interface Util {}
1 change: 0 additions & 1 deletion framework/src/play/plugins/PluginCollection.java
Expand Up @@ -324,7 +324,6 @@ public void updatePlayPluginsList(){
Play.plugins = Collections.unmodifiableList( getEnabledPlugins() );
}


/**
* Returns new readonly list of all enabled plugins
* @return
Expand Down

0 comments on commit 0c6f5ec

Please sign in to comment.