KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > PortletURLConverter


1 /*
2  * Copyright 2003,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
17 package org.apache.cocoon.portal.pluto;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import javax.portlet.PortletMode;
25 import javax.portlet.WindowState;
26
27 import org.apache.pluto.util.StringUtils;
28 import org.apache.cocoon.portal.coplet.CopletInstanceData;
29
30 /**
31  * Create the URL for a portlet
32  *
33  * @author <a HREF="mailto:rgoers@apache.org">Ralph Goers</a>
34  * @version SVN $Id: $
35  */

36
37 public class PortletURLConverter {
38
39     public static final String JavaDoc ACTION = "ac";
40     public static final String JavaDoc MODE = "md";
41     public static final String JavaDoc PORTLET_ID = "pid";
42     public static final String JavaDoc PREFIX = "_";
43     public static final String JavaDoc PARAM = "pm";
44     public static final String JavaDoc STATE = "st";
45
46     private final Map JavaDoc urlData = new HashMap JavaDoc();
47     private final Map JavaDoc parameters = new HashMap JavaDoc();
48     private String JavaDoc portletId;
49
50     /**
51      * Constructor used when the URL will be marshalled.
52      * @param cid The coplet id.
53      */

54     public PortletURLConverter(CopletInstanceData cid) {
55         this.portletId = cid.getId();
56     }
57
58     /**
59      * Constructor used when the URL will be unmarshalled.
60      * @param eventData The url data.
61      */

62     public PortletURLConverter(String JavaDoc eventData) {
63         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(eventData, "/");
64         while (tokenizer.hasMoreTokens()) {
65             String JavaDoc token = tokenizer.nextToken();
66             if (isParameter(token)) {
67                 String JavaDoc name = decodeParameterName(token);
68                 String JavaDoc key = encodeParameterName(name);
69                 this.parameters.put(key, token.substring(key.length()));
70             } else {
71                 StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(token, PREFIX);
72
73                 if (tokens.countTokens() > 1) {
74                     String JavaDoc key = tokens.nextToken();
75                     if (key.equals(PORTLET_ID)) {
76                         this.portletId = tokens.nextToken();
77                     } else {
78                         String JavaDoc value = tokens.nextToken();
79                         urlData.put(key, value);
80                     }
81                 }
82             }
83         }
84     }
85
86     /**
87      * Return the PortletMode
88      * @return The PortletMode
89      */

90     public PortletMode getMode() {
91         String JavaDoc mode = (String JavaDoc)urlData.get(getModeKey());
92         if (mode!=null) {
93             return new PortletMode(mode);
94         }
95         return PortletMode.VIEW;
96     }
97
98     /**
99      * Return the WindowState
100      * @return The WindowState
101      */

102     public WindowState getState() {
103         String JavaDoc state = (String JavaDoc) urlData.get(getStateKey());
104         if (state != null) {
105             return new WindowState(state);
106         }
107         return WindowState.NORMAL;
108     }
109
110     /**
111      * Return the indicator if this is an action.
112      * @return true if this is an action URL, false if a render URL.
113      */

114     public boolean isAction() {
115         return (urlData.get(getActionKey()) != null);
116     }
117
118     /**
119      * Indicates that the URL is an action.
120      */

121     public void setAction() {
122         urlData.put(getActionKey(),ACTION.toUpperCase());
123     }
124
125     /**
126      * Sets the PortletMode.
127      * @param mode The PortletMode
128      */

129     public void setMode(PortletMode mode) {
130         urlData.put(getModeKey(), mode.toString());
131     }
132
133     /**
134      * Sets the WindowState
135      * @param state The WindowState
136      */

137     public void setState(WindowState state) {
138         urlData.put(getStateKey(), state.toString());
139     }
140
141     /**
142      * Returns the portlet id.
143      * @return The portlet id.
144      */

145     public String JavaDoc getPortletId() {
146         return this.portletId;
147     }
148
149     /**
150      * Returns the request parameters for this portlet URL.
151      * @return A Map containing the parameters for this URL.
152      */

153     public Map JavaDoc getParameters() {
154         Map JavaDoc map = new HashMap JavaDoc();
155         Iterator JavaDoc iter = this.parameters.entrySet().iterator();
156         while (iter.hasNext()) {
157             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
158             String JavaDoc key = decodeParameterName((String JavaDoc) entry.getKey());
159             String JavaDoc[] values = decodeParameterValues((String JavaDoc)entry.getValue());
160             map.put(key, values);
161         }
162         return map;
163     }
164
165     /**
166      * Adds the parameter, replacing a parameter with the same name.
167      * @param name The parameter name
168      * @param values An array of Strings.
169      */

170     public void setParam(String JavaDoc name, String JavaDoc[] values) {
171         this.parameters.put(encodeParameterName(name), encodeParameterValues(values));
172     }
173
174     /**
175      * Returns the marshalled URL.
176      * @return A String containing the marshalled URL.
177      */

178     public String JavaDoc toString() {
179         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("");
180         buffer.append(PORTLET_ID).append(PREFIX).append(portletId);
181         Iterator JavaDoc iter = urlData.entrySet().iterator();
182         while (iter.hasNext()) {
183             buffer.append("/");
184             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
185             buffer.append(entry.getKey()).append(PREFIX).append(entry.getValue());
186         }
187         iter = this.parameters.entrySet().iterator();
188         while (iter.hasNext()) {
189             buffer.append("/");
190             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
191             buffer.append(entry.getKey()).append(PREFIX).append(entry.getValue());
192         }
193         return buffer.toString();
194     }
195
196     private String JavaDoc getActionKey() {
197         return ACTION;
198     }
199
200     private String JavaDoc getModeKey() {
201         return MODE;
202     }
203
204     private String JavaDoc getStateKey() {
205         return STATE;
206     }
207
208     private String JavaDoc getParamKey() {
209         return PARAM + PREFIX;
210     }
211
212     private boolean isParameter(String JavaDoc key) {
213         return key.startsWith(getParamKey());
214     }
215
216     private String JavaDoc decodeParameterName(String JavaDoc encodedParamName) {
217         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(encodedParamName, PREFIX);
218         if (!tokenizer.hasMoreTokens()) {
219             return null;
220         }
221         // Skip the key
222
tokenizer.nextToken();
223         if (!tokenizer.hasMoreTokens()) {
224             return null;
225         }
226         String JavaDoc name = tokenizer.nextToken();
227         return decodeValue(name);
228     }
229
230     private String JavaDoc[] decodeParameterValues(String JavaDoc encodedParamValues) {
231         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(encodedParamValues, PREFIX);
232         if (!tokenizer.hasMoreTokens()) {
233             return null;
234         }
235         String JavaDoc _count = tokenizer.nextToken();
236         int count = Integer.valueOf(_count).intValue();
237         String JavaDoc[] values = new String JavaDoc[count];
238         for (int i = 0; i < count; i++) {
239             if (!tokenizer.hasMoreTokens()) {
240                 return null;
241             }
242             values[i] = decodeValue(tokenizer.nextToken());
243         }
244         return values;
245     }
246
247     private String JavaDoc decodeValue(String JavaDoc value) {
248         value = StringUtils.replace(value, "0x1", "_");
249         value = StringUtils.replace(value, "0x2", ".");
250         value = StringUtils.replace(value, "0x3", "/");
251         value = StringUtils.replace(value, "0x4", "\r");
252         value = StringUtils.replace(value, "0x5", "\n");
253         value = StringUtils.replace(value, "0x6", "<");
254         value = StringUtils.replace(value, "0x7", ">");
255         value = StringUtils.replace(value, "0x8", " ");
256         value = StringUtils.replace(value, "0x0", "0x");
257         return value;
258     }
259
260     private String JavaDoc encodeParameterName(String JavaDoc paramName) {
261         StringBuffer JavaDoc returnvalue = new StringBuffer JavaDoc(50);
262         returnvalue.append(getParamKey()).append(encodeValue(paramName));
263         return returnvalue.toString();
264     }
265
266     private String JavaDoc encodeParameterValues(String JavaDoc[] paramValues) {
267         StringBuffer JavaDoc returnvalue = new StringBuffer JavaDoc(100);
268         returnvalue.append(paramValues.length);
269         for (int i = 0; i < paramValues.length; i++) {
270             returnvalue.append("_");
271             returnvalue.append(encodeValue(paramValues[i]));
272         }
273         return returnvalue.toString();
274     }
275
276     private String JavaDoc encodeValue(String JavaDoc value) {
277         value = StringUtils.replace(value, "0x", "0x0");
278         value = StringUtils.replace(value, "_", "0x1");
279         value = StringUtils.replace(value, ".", "0x2");
280         value = StringUtils.replace(value, "/", "0x3");
281         value = StringUtils.replace(value, "\r", "0x4");
282         value = StringUtils.replace(value, "\n", "0x5");
283         value = StringUtils.replace(value, "<", "0x6");
284         value = StringUtils.replace(value, ">", "0x7");
285         value = StringUtils.replace(value, " ", "0x8");
286         return value;
287     }
288 }
289
Popular Tags