1 16 package org.apache.cocoon.portal.impl; 17 18 import java.io.UnsupportedEncodingException ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.apache.avalon.framework.activity.Disposable; 23 import org.apache.avalon.framework.context.Context; 24 import org.apache.avalon.framework.context.ContextException; 25 import org.apache.avalon.framework.context.Contextualizable; 26 import org.apache.avalon.framework.logger.AbstractLogEnabled; 27 import org.apache.avalon.framework.parameters.ParameterException; 28 import org.apache.avalon.framework.parameters.Parameterizable; 29 import org.apache.avalon.framework.parameters.Parameters; 30 import org.apache.avalon.framework.service.ServiceException; 31 import org.apache.avalon.framework.service.ServiceManager; 32 import org.apache.avalon.framework.service.Serviceable; 33 import org.apache.avalon.framework.thread.ThreadSafe; 34 import org.apache.cocoon.components.ContextHelper; 35 import org.apache.cocoon.environment.Request; 36 import org.apache.cocoon.portal.LinkService; 37 import org.apache.cocoon.portal.event.ComparableEvent; 38 import org.apache.cocoon.portal.event.ConvertableEvent; 39 import org.apache.cocoon.portal.event.Event; 40 import org.apache.cocoon.portal.event.EventConverter; 41 import org.apache.cocoon.portal.event.RequestEvent; 42 import org.apache.cocoon.util.NetUtils; 43 44 51 public class DefaultLinkService 52 extends AbstractLogEnabled 53 implements LinkService, 54 ThreadSafe, 55 Serviceable, 56 Disposable, 57 Contextualizable, 58 Parameterizable { 59 60 61 protected EventConverter converter; 62 63 protected ServiceManager manager; 64 65 protected Context context; 66 67 protected Boolean eventsMarshalled; 68 69 70 protected int defaultPort = 80; 71 72 protected int defaultSecurePort = 443; 73 74 77 public void service(ServiceManager manager) throws ServiceException { 78 this.manager = manager; 79 this.converter = (EventConverter)this.manager.lookup(EventConverter.ROLE); 80 } 81 82 85 public void parameterize(Parameters params) throws ParameterException { 86 this.defaultPort = params.getParameterAsInteger("defaultPort", this.defaultPort); 87 this.defaultSecurePort = params.getParameterAsInteger("defaultSecurePort", this.defaultSecurePort); 88 } 89 90 94 protected LinkInfo getInfo() { 95 final Request request = ContextHelper.getRequest( this.context ); 96 LinkInfo info = (LinkInfo)request.getAttribute(DefaultLinkService.class.getName()); 97 if ( info == null ) { 98 synchronized ( this ) { 99 info = (LinkInfo)request.getAttribute(DefaultLinkService.class.getName()); 100 if ( info == null ) { 101 info = new LinkInfo(request, this.defaultPort, this.defaultSecurePort); 102 request.setAttribute(DefaultLinkService.class.getName(), info); 103 } 104 } 105 } 106 return info; 107 } 108 109 112 public boolean isSecure() { 113 return ContextHelper.getRequest(this.context).isSecure(); 114 } 115 116 119 public String encodeURL(String url) { 120 return ContextHelper.getResponse(this.context).encodeURL(url); 121 } 122 123 126 public String getLinkURI(Event event) { 127 return this.getLinkURI(event, null); 128 } 129 130 133 public String getLinkURI(Event event, Boolean secure) { 134 if (event == null) { 135 return this.getRefreshLinkURI(secure); 136 } 137 final LinkInfo info = this.getInfo(); 138 final StringBuffer buffer = new StringBuffer (info.getBase(secure)); 139 boolean hasParams = info.hasParameters(); 140 141 final boolean comparableEvent = event instanceof ComparableEvent; 143 Iterator iter = info.comparableEvents.iterator(); 144 while (iter.hasNext()) { 145 Object [] objects = (Object []) iter.next(); 146 ComparableEvent current = (ComparableEvent) objects[0]; 147 if (!comparableEvent || !current.equalsEvent((ComparableEvent) event)) { 148 if (hasParams) { 149 buffer.append('&'); 150 } else { 151 buffer.append('?'); 152 } 153 try { 154 buffer.append((String ) objects[1]).append('=').append(NetUtils.encode((String ) objects[2], "utf-8")); 155 } catch (UnsupportedEncodingException uee) { 156 } 158 hasParams = true; 159 } 160 } 161 162 hasParams = this.addEvent(buffer, event, hasParams); 164 165 return buffer.toString(); 166 } 167 168 172 protected boolean addEvent(StringBuffer buffer, Event event, boolean hasParams) { 173 if ( hasParams ) { 174 buffer.append('&'); 175 } else { 176 buffer.append('?'); 177 } 178 StringBuffer value = new StringBuffer (""); 179 String parameterName = processEvent(event, value); 180 try { 181 buffer.append(parameterName).append('=').append(NetUtils.encode(value.toString(), "utf-8")); 182 } catch (UnsupportedEncodingException uee) { 183 } 185 return true; 186 } 187 188 191 public String getLinkURI(List events) { 192 return this.getLinkURI(events, null); 193 } 194 195 198 public String getLinkURI(List events, Boolean secure) { 199 if (events == null || events.size() == 0) { 200 return this.getRefreshLinkURI(secure); 201 } 202 final LinkInfo info = this.getInfo(); 203 boolean hasParams = info.hasParameters(); 204 final StringBuffer buffer = new StringBuffer (info.getBase(secure)); 205 206 Iterator iter = info.comparableEvents.iterator(); 208 while (iter.hasNext()) { 209 Object [] objects = (Object []) iter.next(); 210 ComparableEvent current = (ComparableEvent) objects[0]; 211 212 Iterator eventIterator = events.iterator(); 213 boolean found = false; 214 while (!found && eventIterator.hasNext()) { 215 final Object inEvent = eventIterator.next(); 216 if (inEvent instanceof ComparableEvent 217 && current.equalsEvent((ComparableEvent) inEvent)) { 218 found = true; 219 } 220 } 221 if (!found) { 222 if (hasParams) { 223 buffer.append('&'); 224 } else { 225 buffer.append('?'); 226 } 227 try { 228 buffer.append((String ) objects[1]).append('=').append(NetUtils.encode((String ) objects[2], "utf-8")); 229 } catch (UnsupportedEncodingException uee) { 230 } 232 hasParams = true; 233 } 234 } 235 236 iter = events.iterator(); 238 while (iter.hasNext()) { 239 final Object current = iter.next(); 240 if (current instanceof Event) { 241 hasParams = this.addEvent(buffer, (Event) current, hasParams); 242 } else if ( current instanceof ParameterDescription ) { 243 if (hasParams) { 244 buffer.append('&'); 245 } else { 246 buffer.append('?'); 247 hasParams = true; 248 } 249 buffer.append(((ParameterDescription) current).parameters); 250 } 251 } 252 return buffer.toString(); 253 } 254 255 258 public void addEventToLink(Event event) { 259 if (event == null) { 260 return; 261 } 262 StringBuffer value = new StringBuffer (""); 263 String parameterName = processEvent(event, value); 264 265 final LinkInfo info = this.getInfo(); 266 if (event instanceof ComparableEvent) { 267 final Iterator iter = info.comparableEvents.iterator(); 269 boolean found = false; 270 while (!found && iter.hasNext()) { 271 Object [] objects = (Object [])iter.next(); 272 if (((ComparableEvent) objects[0]).equalsEvent((ComparableEvent) event)) { 273 found = true; 274 info.comparableEvents.remove(objects); 275 } 276 } 277 info.comparableEvents.add(new Object []{event, parameterName, value.toString()}); 278 } else { 279 this.addParameterToLink(parameterName, value.toString()); 280 } 281 } 282 283 286 public void addParameterToLink(String name, String value) { 287 final LinkInfo info = this.getInfo(); 288 info.addParameterToBase(name, value); 289 } 290 291 294 public void addUniqueParameterToLink(String name, String value) { 295 final LinkInfo info = this.getInfo(); 296 info.deleteParameterFromBase(name); 297 this.addParameterToLink(name, value); 298 } 299 300 303 public String getRefreshLinkURI() { 304 return this.getRefreshLinkURI(null); 305 } 306 307 310 public String getRefreshLinkURI(Boolean secure) { 311 final LinkInfo info = this.getInfo(); 312 313 final StringBuffer buffer = new StringBuffer (info.getBase(secure)); 314 315 Iterator iter = info.comparableEvents.iterator(); 317 boolean hasParams = info.hasParameters(); 318 while (iter.hasNext()) { 319 Object [] objects = (Object [])iter.next(); 320 if ( hasParams ) { 321 buffer.append('&'); 322 } else { 323 buffer.append('?'); 324 } 325 try { 326 buffer.append((String )objects[1]).append('=').append(NetUtils.encode((String )objects[2], "utf-8")); 327 } catch (UnsupportedEncodingException uee) { 328 } 330 hasParams = true; 331 } 332 return buffer.toString(); 333 } 334 335 338 public void dispose() { 339 if (this.manager != null) { 340 this.manager.release( this.converter ); 341 this.converter = null; 342 this.manager = null; 343 } 344 } 345 346 349 public void contextualize(Context context) throws ContextException { 350 this.context = context; 351 } 352 353 private boolean getEventsMarshalled() { 354 if (this.eventsMarshalled == null) { 355 this.eventsMarshalled = new Boolean (this.converter.isMarshallEvents()); 356 } 357 return this.eventsMarshalled.booleanValue(); 358 } 359 360 private String processEvent(Event event, StringBuffer value) { 361 String parameterName = DEFAULT_REQUEST_EVENT_PARAMETER_NAME; 362 if (event instanceof ConvertableEvent && getEventsMarshalled()) { 363 final String eventParName = ((ConvertableEvent) event).getRequestParameterName(); 364 String eventStr = ((ConvertableEvent) event).asString(); 365 if (eventStr == null) { 366 value.append(this.converter.encode(event)); 368 } else { 369 parameterName = DEFAULT_CONVERTABLE_EVENT_PARAMETER_NAME; 370 try { 371 String eventValue = NetUtils.encode(eventStr, "utf-8"); 372 value.append(eventParName).append('(').append(eventValue).append(')'); 373 } catch (UnsupportedEncodingException uee) { 374 } 376 } 377 } else { 378 if (event instanceof RequestEvent) { 379 final String eventParName = ((RequestEvent) event).getRequestParameterName(); 380 if (eventParName != null) { 381 parameterName = eventParName; 382 } 383 } 384 value.append(this.converter.encode(event)); 385 } 386 return parameterName; 387 } 388 } 389 | Popular Tags |