1 16 17 package org.apache.cocoon.portal.pluto; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.StringTokenizer ; 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 36 37 public class PortletURLConverter { 38 39 public static final String ACTION = "ac"; 40 public static final String MODE = "md"; 41 public static final String PORTLET_ID = "pid"; 42 public static final String PREFIX = "_"; 43 public static final String PARAM = "pm"; 44 public static final String STATE = "st"; 45 46 private final Map urlData = new HashMap (); 47 private final Map parameters = new HashMap (); 48 private String portletId; 49 50 54 public PortletURLConverter(CopletInstanceData cid) { 55 this.portletId = cid.getId(); 56 } 57 58 62 public PortletURLConverter(String eventData) { 63 StringTokenizer tokenizer = new StringTokenizer (eventData, "/"); 64 while (tokenizer.hasMoreTokens()) { 65 String token = tokenizer.nextToken(); 66 if (isParameter(token)) { 67 String name = decodeParameterName(token); 68 String key = encodeParameterName(name); 69 this.parameters.put(key, token.substring(key.length())); 70 } else { 71 StringTokenizer tokens = new StringTokenizer (token, PREFIX); 72 73 if (tokens.countTokens() > 1) { 74 String key = tokens.nextToken(); 75 if (key.equals(PORTLET_ID)) { 76 this.portletId = tokens.nextToken(); 77 } else { 78 String value = tokens.nextToken(); 79 urlData.put(key, value); 80 } 81 } 82 } 83 } 84 } 85 86 90 public PortletMode getMode() { 91 String mode = (String )urlData.get(getModeKey()); 92 if (mode!=null) { 93 return new PortletMode(mode); 94 } 95 return PortletMode.VIEW; 96 } 97 98 102 public WindowState getState() { 103 String state = (String ) urlData.get(getStateKey()); 104 if (state != null) { 105 return new WindowState(state); 106 } 107 return WindowState.NORMAL; 108 } 109 110 114 public boolean isAction() { 115 return (urlData.get(getActionKey()) != null); 116 } 117 118 121 public void setAction() { 122 urlData.put(getActionKey(),ACTION.toUpperCase()); 123 } 124 125 129 public void setMode(PortletMode mode) { 130 urlData.put(getModeKey(), mode.toString()); 131 } 132 133 137 public void setState(WindowState state) { 138 urlData.put(getStateKey(), state.toString()); 139 } 140 141 145 public String getPortletId() { 146 return this.portletId; 147 } 148 149 153 public Map getParameters() { 154 Map map = new HashMap (); 155 Iterator iter = this.parameters.entrySet().iterator(); 156 while (iter.hasNext()) { 157 Map.Entry entry = (Map.Entry )iter.next(); 158 String key = decodeParameterName((String ) entry.getKey()); 159 String [] values = decodeParameterValues((String )entry.getValue()); 160 map.put(key, values); 161 } 162 return map; 163 } 164 165 170 public void setParam(String name, String [] values) { 171 this.parameters.put(encodeParameterName(name), encodeParameterValues(values)); 172 } 173 174 178 public String toString() { 179 StringBuffer buffer = new StringBuffer (""); 180 buffer.append(PORTLET_ID).append(PREFIX).append(portletId); 181 Iterator iter = urlData.entrySet().iterator(); 182 while (iter.hasNext()) { 183 buffer.append("/"); 184 Map.Entry entry = (Map.Entry )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 entry = (Map.Entry ) iter.next(); 191 buffer.append(entry.getKey()).append(PREFIX).append(entry.getValue()); 192 } 193 return buffer.toString(); 194 } 195 196 private String getActionKey() { 197 return ACTION; 198 } 199 200 private String getModeKey() { 201 return MODE; 202 } 203 204 private String getStateKey() { 205 return STATE; 206 } 207 208 private String getParamKey() { 209 return PARAM + PREFIX; 210 } 211 212 private boolean isParameter(String key) { 213 return key.startsWith(getParamKey()); 214 } 215 216 private String decodeParameterName(String encodedParamName) { 217 StringTokenizer tokenizer = new StringTokenizer (encodedParamName, PREFIX); 218 if (!tokenizer.hasMoreTokens()) { 219 return null; 220 } 221 tokenizer.nextToken(); 223 if (!tokenizer.hasMoreTokens()) { 224 return null; 225 } 226 String name = tokenizer.nextToken(); 227 return decodeValue(name); 228 } 229 230 private String [] decodeParameterValues(String encodedParamValues) { 231 StringTokenizer tokenizer = new StringTokenizer (encodedParamValues, PREFIX); 232 if (!tokenizer.hasMoreTokens()) { 233 return null; 234 } 235 String _count = tokenizer.nextToken(); 236 int count = Integer.valueOf(_count).intValue(); 237 String [] values = new String [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 decodeValue(String 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 encodeParameterName(String paramName) { 261 StringBuffer returnvalue = new StringBuffer (50); 262 returnvalue.append(getParamKey()).append(encodeValue(paramName)); 263 return returnvalue.toString(); 264 } 265 266 private String encodeParameterValues(String [] paramValues) { 267 StringBuffer returnvalue = new StringBuffer (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 encodeValue(String 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 |