001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.modules.input;
018:
019: import org.apache.cocoon.environment.ObjectModelHelper;
020: import org.apache.cocoon.environment.Request;
021: import org.apache.cocoon.environment.portlet.PortletEnvironment;
022: import org.apache.cocoon.environment.portlet.PortletObjectModelHelper;
023: import org.apache.cocoon.util.NetUtils;
024:
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.thread.ThreadSafe;
028:
029: import javax.portlet.PortletURL;
030: import javax.portlet.RenderResponse;
031:
032: import java.util.ArrayList;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Map;
037:
038: /**
039: * Input module to be used in together with
040: * {@link org.apache.cocoon.transformation.LinkRewriterTransformer}
041: * in JSR-168 (Portlet) environment. Allows creation of render, action, and
042: * resource URLs using syntax:
043: * <ul>
044: * <li><code>portlet:action:<path></code> for action URL;
045: * <li><code>portlet:render:<path></code> for render URL;
046: * <li><code>portlet:resource:<path></code> for resource URL;
047: * </ul>
048: *
049: * Outside of portlet environment, prefixes (<code>portlet:action:</code>,
050: * <code>portlet:render:</code>, <code>portlet:resource:</code>) are omitted.
051: *
052: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
053: * @version CVS $Id: PortletURLModule.java 433543 2006-08-22 06:22:54Z crossley $
054: */
055: public class PortletURLModule extends AbstractInputModule implements
056: ThreadSafe {
057:
058: public static final String NAME_RENDER = "render";
059: public static final String NAME_RESOURCE = "resource";
060: public static final String NAME_ACTION = "action";
061:
062: private static final String PREFIX_RENDER = NAME_RENDER + ":";
063: private static final String PREFIX_RESOURCE = NAME_RESOURCE + ":";
064: private static final String PREFIX_ACTION = NAME_ACTION + ":";
065:
066: private static final List returnNames;
067:
068: static {
069: List tmp = new ArrayList();
070: tmp.add(NAME_RENDER);
071: tmp.add(NAME_RESOURCE);
072: tmp.add(NAME_ACTION);
073: returnNames = tmp;
074: }
075:
076: public Iterator getAttributeNames(Configuration modeConf,
077: Map objectModel) throws ConfigurationException {
078: return PortletURLModule.returnNames.iterator();
079: }
080:
081: public Object getAttribute(String name, Configuration modeConf,
082: Map objectModel) throws ConfigurationException {
083: if (name == null) {
084: throw new NullPointerException("Attribute name is null");
085: }
086:
087: Request request = ObjectModelHelper.getRequest(objectModel);
088:
089: RenderResponse renderResponse = PortletObjectModelHelper
090: .getRenderResponse(objectModel);
091: if (renderResponse != null) {
092: PortletURL url = null;
093: if (name.startsWith(PREFIX_RENDER)) {
094: url = renderResponse.createRenderURL();
095: name = name.substring(PREFIX_RENDER.length());
096: if (name.length() > 0 && name.charAt(0) == '/') {
097: name = name.substring(1);
098: }
099: } else if (name.startsWith(PREFIX_RESOURCE)) {
100: name = name.substring(PREFIX_RESOURCE.length());
101: if (name.length() == 0 || name.charAt(0) != '/') {
102: String uri = request.getContextPath() + "/"
103: + request.getServletPath();
104: name = NetUtils.absolutize(uri, name);
105: }
106: return renderResponse.encodeURL(name);
107: } else if (name.startsWith(PREFIX_ACTION)) {
108: url = renderResponse.createActionURL();
109: name = name.substring(PREFIX_ACTION.length());
110: if (name.length() > 0 && name.charAt(0) == '/') {
111: name = name.substring(1);
112: }
113: } else {
114: throw new IllegalArgumentException(
115: "Invalid attribute name '" + name + "' for '"
116: + getClass().getName() + "'");
117: }
118:
119: Map parameters = new HashMap(7);
120: name = NetUtils.deparameterize(name, parameters);
121: if (name.length() > 0) {
122: parameters.put(PortletEnvironment.PARAMETER_PATH_INFO,
123: name);
124: }
125: for (Iterator i = parameters.entrySet().iterator(); i
126: .hasNext();) {
127: Map.Entry me = (Map.Entry) i.next();
128: String param = (String) me.getKey();
129: Object values = me.getValue();
130: if (values instanceof String) {
131: url.setParameter(param, (String) values);
132: } else {
133: url.setParameter(param, (String[]) values);
134: }
135: }
136: return url.toString();
137: }
138: if (name.startsWith(PREFIX_RENDER)) {
139: return name.substring(PREFIX_RENDER.length());
140: } else if (name.startsWith(PREFIX_RESOURCE)) {
141: return name.substring(PREFIX_RESOURCE.length());
142: } else if (name.startsWith(PREFIX_ACTION)) {
143: return name.substring(PREFIX_ACTION.length());
144: } else {
145: throw new IllegalArgumentException(
146: "Invalid attribute name '" + name + "' for '"
147: + getClass().getName() + "'");
148: }
149: }
150:
151: public Object[] getAttributeValues(String name,
152: Configuration modeConf, Map objectModel)
153: throws ConfigurationException {
154: Object result = getAttribute(name, modeConf, objectModel);
155: if (result != null) {
156: return new Object[] { result };
157: }
158: return null;
159: }
160: }
|