KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > PortletURLModule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.modules.input;
17
18 import org.apache.cocoon.environment.ObjectModelHelper;
19 import org.apache.cocoon.environment.Request;
20 import org.apache.cocoon.environment.portlet.PortletEnvironment;
21 import org.apache.cocoon.environment.portlet.PortletObjectModelHelper;
22 import org.apache.cocoon.util.NetUtils;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.thread.ThreadSafe;
27
28 import javax.portlet.PortletURL;
29 import javax.portlet.RenderResponse;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * Input module to be used in together with
39  * {@link org.apache.cocoon.transformation.LinkRewriterTransformer}
40  * in JSR-168 (Portlet) environment. Allows creation of render, action, and
41  * resource URLs using syntax:
42  * <ul>
43  * <li><code>portlet:action:&lt;path&gt;</code> for action URL;
44  * <li><code>portlet:render:&lt;path&gt;</code> for render URL;
45  * <li><code>portlet:resource:&lt;path&gt;</code> for resource URL;
46  * </ul>
47  *
48  * Outside of portlet environment, prefixes (<code>portlet:action:</code>,
49  * <code>portlet:render:</code>, <code>portlet:resource:</code>) are omitted.
50  *
51  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
52  * @version CVS $Id: PortletURLModule.java 148910 2005-01-28 18:19:44Z cziegeler $
53  */

54 public class PortletURLModule extends AbstractInputModule implements ThreadSafe {
55
56     public static final String JavaDoc NAME_RENDER = "render";
57     public static final String JavaDoc NAME_RESOURCE = "resource";
58     public static final String JavaDoc NAME_ACTION = "action";
59
60     private static final String JavaDoc PREFIX_RENDER = NAME_RENDER + ":";
61     private static final String JavaDoc PREFIX_RESOURCE = NAME_RESOURCE + ":";
62     private static final String JavaDoc PREFIX_ACTION = NAME_ACTION + ":";
63
64     private static final List JavaDoc returnNames;
65
66     static {
67         List JavaDoc tmp = new ArrayList JavaDoc();
68         tmp.add(NAME_RENDER);
69         tmp.add(NAME_RESOURCE);
70         tmp.add(NAME_ACTION);
71         returnNames = tmp;
72     }
73
74     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel) throws ConfigurationException {
75         return PortletURLModule.returnNames.iterator();
76     }
77
78     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel) throws ConfigurationException {
79         if (name == null) {
80             throw new NullPointerException JavaDoc("Attribute name is null");
81         }
82
83         Request request = ObjectModelHelper.getRequest(objectModel);
84
85         RenderResponse renderResponse = PortletObjectModelHelper.getRenderResponse(objectModel);
86         if (renderResponse != null) {
87             PortletURL url = null;
88             if (name.startsWith(PREFIX_RENDER)) {
89                 url = renderResponse.createRenderURL();
90                 name = name.substring(PREFIX_RENDER.length());
91                 if (name.length() > 0 && name.charAt(0) == '/') {
92                     name = name.substring(1);
93                 }
94             } else if (name.startsWith(PREFIX_RESOURCE)) {
95                 name = name.substring(PREFIX_RESOURCE.length());
96                 if (name.length() == 0 || name.charAt(0) != '/') {
97                     String JavaDoc uri = request.getContextPath() + "/" + request.getServletPath();
98                     name = NetUtils.absolutize(uri, name);
99                 }
100                 return renderResponse.encodeURL(name);
101             } else if (name.startsWith(PREFIX_ACTION)) {
102                 url = renderResponse.createActionURL();
103                 name = name.substring(PREFIX_ACTION.length());
104                 if (name.length() > 0 && name.charAt(0) == '/') {
105                     name = name.substring(1);
106                 }
107             } else {
108                 throw new IllegalArgumentException JavaDoc("Invalid attribute name '" + name + "' for '" + getClass().getName() + "'");
109             }
110
111             Map JavaDoc parameters = new HashMap JavaDoc(7);
112             name = NetUtils.deparameterize(name, parameters);
113             if (name.length() > 0) {
114                 parameters.put(PortletEnvironment.PARAMETER_PATH_INFO, name);
115             }
116             for (Iterator JavaDoc i = parameters.entrySet().iterator(); i.hasNext();) {
117                 Map.Entry JavaDoc me = (Map.Entry JavaDoc)i.next();
118                 String JavaDoc param = (String JavaDoc)me.getKey();
119                 Object JavaDoc values = me.getValue();
120                 if (values instanceof String JavaDoc) {
121                     url.setParameter(param, (String JavaDoc) values);
122                 } else {
123                     url.setParameter(param, (String JavaDoc[]) values);
124                 }
125             }
126             return url.toString();
127         }
128         if (name.startsWith(PREFIX_RENDER)) {
129             return name.substring(PREFIX_RENDER.length());
130         } else if (name.startsWith(PREFIX_RESOURCE)) {
131             return name.substring(PREFIX_RESOURCE.length());
132         } else if (name.startsWith(PREFIX_ACTION)) {
133             return name.substring(PREFIX_ACTION.length());
134         } else {
135             throw new IllegalArgumentException JavaDoc("Invalid attribute name '" + name + "' for '" + getClass().getName() + "'");
136         }
137     }
138
139     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel) throws ConfigurationException {
140         Object JavaDoc result = getAttribute(name, modeConf, objectModel);
141         if (result != null) {
142             return new Object JavaDoc[]{result};
143         }
144         return null;
145     }
146 }
147
Popular Tags