1 15 package org.apache.tapestry.engine; 16 17 import java.io.UnsupportedEncodingException ; 18 import java.util.Map ; 19 20 import org.apache.commons.codec.net.URLCodec; 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.util.Defense; 23 import org.apache.tapestry.IRequestCycle; 24 import org.apache.tapestry.Tapestry; 25 import org.apache.tapestry.util.QueryParameterMap; 26 import org.apache.tapestry.web.WebRequest; 27 28 38 39 public class EngineServiceLink implements ILink 40 { 41 private static final int DEFAULT_HTTP_PORT = 80; 42 43 private final IRequestCycle _cycle; 44 45 private final String _servletPath; 46 47 private final URLCodec _codec; 48 49 private String _encoding; 50 51 private boolean _stateful; 52 53 54 private final QueryParameterMap _parameters; 55 56 57 58 private final WebRequest _request; 59 60 78 79 public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding, 80 URLCodec codec, WebRequest request, Map parameters, boolean stateful) 81 { 82 Defense.notNull(cycle, "cycle"); 83 Defense.notNull(servletPath, "servletPath"); 84 Defense.notNull(encoding, "encoding"); 85 Defense.notNull(codec, "codec"); 86 Defense.notNull(request, "request"); 87 Defense.notNull(parameters, "parameters"); 88 89 _cycle = cycle; 90 _servletPath = servletPath; 91 _encoding = encoding; 92 _codec = codec; 93 _request = request; 94 _parameters = new QueryParameterMap(parameters); 95 _stateful = stateful; 96 } 97 98 public String getURL() 99 { 100 return getURL(null, true); 101 } 102 103 public String getURL(String anchor, boolean includeParameters) 104 { 105 return constructURL(new StringBuffer (), anchor, includeParameters); 106 } 107 108 public String getAbsoluteURL() 109 { 110 return getAbsoluteURL(null, null, 0, null, true); 111 } 112 113 public String getAbsoluteURL(String scheme, String server, int port, String anchor, 114 boolean includeParameters) 115 { 116 StringBuffer buffer = new StringBuffer (); 117 118 if (scheme == null) 119 scheme = _request.getScheme(); 120 121 buffer.append(scheme); 122 buffer.append("://"); 123 124 if (server == null) 125 server = _request.getServerName(); 126 127 buffer.append(server); 128 129 if (port == 0) 130 port = _request.getServerPort(); 131 132 if (!(scheme.equals("http") && port == DEFAULT_HTTP_PORT)) 133 { 134 buffer.append(':'); 135 buffer.append(port); 136 } 137 138 141 return constructURL(buffer, anchor, includeParameters); 142 } 143 144 private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters) 145 { 146 buffer.append(_servletPath); 147 148 if (includeParameters) 149 addParameters(buffer); 150 151 if (anchor != null) 152 { 153 buffer.append('#'); 154 buffer.append(anchor); 155 } 156 157 String result = buffer.toString(); 158 159 if (_stateful) 160 result = _cycle.encodeURL(result); 161 162 return result; 163 } 164 165 private void addParameters(StringBuffer buffer) 166 { 167 String [] names = getParameterNames(); 168 169 String sep = "?"; 170 171 for (int i = 0; i < names.length; i++) 172 { 173 String name = names[i]; 174 String [] values = getParameterValues(name); 175 176 if (values == null) 177 continue; 178 179 for (int j = 0; j < values.length; j++) 180 { 181 buffer.append(sep); 182 buffer.append(name); 183 buffer.append("="); 184 buffer.append(encode(values[j])); 185 186 sep = "&"; 187 } 188 189 } 190 } 191 192 private String encode(String value) 193 { 194 try 195 { 196 return _codec.encode(value, _encoding); 197 } 198 catch (UnsupportedEncodingException ex) 199 { 200 throw new ApplicationRuntimeException(Tapestry.format("illegal-encoding", _encoding), 201 ex); 202 } 203 } 204 205 public String [] getParameterNames() 206 { 207 return _parameters.getParameterNames(); 208 } 209 210 public String [] getParameterValues(String name) 211 { 212 return _parameters.getParameterValues(name); 213 } 214 } | Popular Tags |