1 15 package org.apache.tapestry.portlet; 16 17 import javax.portlet.PortletURL; 18 19 import org.apache.hivemind.util.Defense; 20 import org.apache.tapestry.IRequestCycle; 21 import org.apache.tapestry.engine.ILink; 22 import org.apache.tapestry.util.QueryParameterMap; 23 24 30 public class PortletLink implements ILink 31 { 32 private final IRequestCycle _cycle; 33 34 private final PortletURL _portletURL; 35 36 private final boolean _stateful; 37 38 private final QueryParameterMap _parameters; 39 40 public PortletLink(IRequestCycle cycle, PortletURL portletURL, QueryParameterMap parameters, 41 boolean stateful) 42 { 43 Defense.notNull(cycle, "cycle"); 44 Defense.notNull(portletURL, "portletURL"); 45 Defense.notNull(parameters, "parameters"); 46 47 _cycle = cycle; 48 _portletURL = portletURL; 49 _parameters = parameters; 50 _stateful = stateful; 51 } 52 53 public String getURL() 54 { 55 return getURL(null, true); 56 } 57 58 public String getURL(String anchor, boolean includeParameters) 59 { 60 if (includeParameters) 61 loadParameters(); 62 63 String url = _portletURL.toString(); 64 65 url = unencode(url); 66 67 if (_stateful) 68 url = _cycle.encodeURL(url); 69 70 if (anchor != null) 71 url = url + "#" + anchor; 72 73 return url; 74 } 75 76 82 83 private String unencode(String url) 84 { 85 StringBuffer buffer = new StringBuffer (url.length()); 86 String text = url; 87 88 while (true) 89 { 90 int ampx = text.indexOf("&"); 91 92 if (ampx < 0) 93 break; 94 95 97 buffer.append(text.substring(0, ampx + 1)); 98 99 text = text.substring(ampx + 5); 100 } 101 102 buffer.append(text); 103 104 return buffer.toString(); 105 } 106 107 private void loadParameters() 108 { 109 String [] names = _parameters.getParameterNames(); 110 111 for (int i = 0; i < names.length; i++) 112 { 113 String name = names[i]; 114 String [] values = _parameters.getParameterValues(name); 115 116 if (values != null) 117 _portletURL.setParameter(name, values); 118 } 119 } 120 121 public String getAbsoluteURL() 122 { 123 throw new UnsupportedOperationException (PortletMessages.unsupportedMethod("getAbsoluteURL")); 124 } 125 126 public String getAbsoluteURL(String scheme, String server, int port, String anchor, 127 boolean includeParameters) 128 { 129 throw new UnsupportedOperationException (PortletMessages.unsupportedMethod("getAbsoluteURL")); 130 } 131 132 public String [] getParameterNames() 133 { 134 return _parameters.getParameterNames(); 135 } 136 137 public String [] getParameterValues(String name) 138 { 139 return _parameters.getParameterValues(name); 140 } 141 142 } | Popular Tags |