001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.wsrp;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.portal.Mode;
026: import org.jboss.portal.WindowState;
027: import org.jboss.portal.common.net.URLTools;
028: import org.jboss.portal.common.util.MediaType;
029:
030: import javax.activation.MimeTypeParseException;
031: import java.net.MalformedURLException;
032: import java.net.URL;
033: import java.util.ArrayList;
034: import java.util.List;
035: import java.util.Map;
036:
037: /**
038: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
039: * @version $Revision: 8784 $
040: * @since 2.4
041: */
042: public class WSRPResourceURL extends WSRPPortletURL {
043: private final static Logger log = Logger
044: .getLogger(WSRPResourceURL.class);
045:
046: private URL resourceURL;
047: private boolean requiresRewrite = false;
048: private static final List SUPPORTED_RESOURCE_TYPES = new ArrayList(
049: 4);
050:
051: static {
052: SUPPORTED_RESOURCE_TYPES.add(MediaType.CSS);
053: SUPPORTED_RESOURCE_TYPES.add(MediaType.JS);
054: SUPPORTED_RESOURCE_TYPES.add(MediaType.TEXT);
055: try {
056: SUPPORTED_RESOURCE_TYPES.add(MediaType
057: .parseMimeType("image/*"));
058: } catch (MimeTypeParseException e) {
059: // shouldn't happen;
060: }
061: }
062:
063: public WSRPResourceURL(Mode mode, WindowState windowState,
064: boolean secure, URL resourceURL, boolean requiresRewrite) {
065: super (mode, windowState, secure);
066: this .resourceURL = resourceURL;
067: this .requiresRewrite = requiresRewrite;
068: }
069:
070: public WSRPResourceURL(Map params) {
071: super (params);
072: }
073:
074: public WSRPResourceURL() {
075: super ();
076: }
077:
078: protected String getURLType() {
079: return WSRPRewritingConstants.URL_TYPE_RESOURCE;
080: }
081:
082: protected void appendEnd(StringBuffer sb) {
083: if (resourceURL != null) {
084: createURLParameter(sb, WSRPRewritingConstants.RESOURCE_URL,
085: resourceURL.toExternalForm());
086: }
087:
088: if (requiresRewrite) {
089: createURLParameter(sb,
090: WSRPRewritingConstants.RESOURCE_REQUIRES_REWRITE,
091: "true");
092: }
093: }
094:
095: protected void setParams(Map params) {
096: super .setParams(params);
097:
098: String paramValue = getRawParameterValueFor(params,
099: WSRPRewritingConstants.RESOURCE_REQUIRES_REWRITE);
100: if (paramValue != null) {
101: requiresRewrite = Boolean.valueOf(paramValue)
102: .booleanValue();
103: }
104:
105: paramValue = getRawParameterValueFor(params,
106: WSRPRewritingConstants.RESOURCE_URL);
107: if (paramValue != null) {
108: try {
109: paramValue = URLTools.decodeXWWWFormURL(paramValue);
110:
111: if (requiresRewrite) {
112: // fixme: do something...
113: log
114: .debug("Required re-writing but this is not yet implemented...");
115: }
116:
117: resourceURL = new URL(paramValue);
118: String file = resourceURL.getFile();
119:
120: MediaType mediaType = null;
121: try {
122: mediaType = MediaType
123: .parseMimeTypeByExtension(file);
124: } catch (MimeTypeParseException e) {
125: log
126: .debug("Couldn't determine (based on extension) MIME type of file: "
127: + file
128: + "\nRetrieving the associated resource will probably fail.");
129: }
130:
131: isSupported(mediaType);
132: } catch (MalformedURLException e) {
133: IllegalArgumentException iae = new IllegalArgumentException(
134: "Malformed URL: " + paramValue);
135: iae.initCause(e);
136: throw iae;
137: }
138: }
139: }
140:
141: private void isSupported(MediaType mediaType) {
142: if (mediaType != null) {
143: for (int i = 0; i < SUPPORTED_RESOURCE_TYPES.size(); i++) {
144: MediaType type = (MediaType) SUPPORTED_RESOURCE_TYPES
145: .get(i);
146: if (!type.isAllowedSubType(mediaType)) {
147: log
148: .debug("MIME type '"
149: + mediaType
150: + "' is not currently supported. Retrieving the associated resource will probably fail.");
151: return;
152: }
153: }
154: }
155: }
156:
157: public String toString() {
158: return resourceURL.toExternalForm();
159: }
160: }
|