View Javadoc
1 package org.apache.turbine.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import javax.servlet.http.HttpServletRequest; 58 59 /*** 60 * This creates a Dynamic URI for use within the Turbine system 61 * 62 * <p>If you use this class to generate all of your href tags as well 63 * as all of your URI's, then you will not need to worry about having 64 * session data setup for you or using HttpServletRequest.encodeUrl() 65 * since this class does everything for you. 66 * This class generates relative URI's which can be used in environments with 67 * firewalls and gateways for outgoing connections. 68 * 69 * <code><pre> 70 * RelativeDynamicURI dui = new RelativeDynamicURI (data, "UserScreen" ); 71 * dui.setName("Click Here").addPathInfo("user","jon"); 72 * dui.getA(); 73 * </pre></code> 74 * 75 * The above call to getA() would return the String: 76 * 77 * <AHREF="/servlets/Turbine/screen=UserScreen&amp;user=jon">ClickHere</A> 78 * 79 * @author <a href="mailto:dfaller@raleigh.ibm.com">David S. Faller</a> 80 */ 81 public class RelativeDynamicURI extends DynamicURI 82 { 83 /*** 84 * Default constructor - one of the init methods must be called before use. 85 */ 86 public RelativeDynamicURI() 87 { 88 } 89 90 /*** 91 * Constructor sets up some variables. 92 * 93 * @param data A Turbine RunData object. 94 */ 95 public RelativeDynamicURI(RunData data) 96 { 97 super(data); 98 } 99 100 /*** 101 * Constructor sets up some variables. 102 * 103 * @param data A Turbine RunData object. 104 * @param screen A String with the name of a screen. 105 */ 106 public RelativeDynamicURI(RunData data, String screen) 107 { 108 super(data, screen); 109 } 110 111 /*** 112 * Constructor sets up some variables. 113 * 114 * @param data A Turbine RunData object. 115 * @param screen A String with the name of a screen. 116 * @param action A String with the name of an action. 117 */ 118 public RelativeDynamicURI(RunData data, String screen, String action) 119 { 120 super(data, screen, action); 121 } 122 123 /*** 124 * Constructor sets up some variables. 125 * 126 * @param data A Turbine RunData object. 127 * @param screen A String with the name of a screen. 128 * @param action A String with the name of an action. 129 * @param redirect True if it should redirect. 130 */ 131 public RelativeDynamicURI(RunData data, 132 String screen, 133 String action, 134 boolean redirect) 135 { 136 super(data, screen, action, redirect); 137 } 138 139 /*** 140 * Constructor sets up some variables. 141 * 142 * @param data A Turbine RunData object. 143 * @param screen A String with the name of a screen. 144 * @param redirect True if it should redirect. 145 */ 146 public RelativeDynamicURI(RunData data, 147 String screen, 148 boolean redirect) 149 { 150 super(data, screen, redirect); 151 } 152 153 /*** 154 * Constructor sets up some variables. 155 * 156 * @param data A Turbine RunData object. 157 * @param redirect True if it should redirect. 158 */ 159 public RelativeDynamicURI(RunData data, boolean redirect) 160 { 161 super(data, redirect); 162 } 163 164 /*** 165 * Main constructor for RelativeDynamicURI. Uses ServerData. 166 * 167 * @param sd A ServerData. 168 */ 169 public RelativeDynamicURI(ServerData sd) 170 { 171 super(sd); 172 } 173 174 /*** 175 * Main constructor for RelativeDynamicURI. Uses ServerData. 176 * 177 * @param sd A ServerData. 178 * @param screen A String with the name of a screen. 179 */ 180 public RelativeDynamicURI(ServerData sd, String screen) 181 { 182 super(sd, screen); 183 } 184 185 /*** 186 * Main constructor for RelativeDynamicURI. Uses ServerData. 187 * 188 * @param sd A ServerData. 189 * @param screen A String with the name of a screen. 190 * @param action A String with the name of an action. 191 */ 192 public RelativeDynamicURI(ServerData sd, 193 String screen, 194 String action) 195 { 196 super(sd, screen, action); 197 } 198 199 /*** 200 * Main constructor for RelativeDynamicURI. Uses ServerData. 201 * 202 * @param sd A ServerData. 203 * @param screen A String with the name of a screen. 204 * @param action A String with the name of an action. 205 * @param redirect True if it should redirect. 206 */ 207 public RelativeDynamicURI(ServerData sd, 208 String screen, 209 String action, 210 boolean redirect) 211 { 212 super(sd, screen, action, redirect); 213 } 214 215 /*** 216 * Main constructor for RelativeDynamicURI. Uses ServerData. 217 * 218 * @param sd A ServerData. 219 * @param screen A String with the name of a screen. 220 * @param redirect True if it should redirect. 221 */ 222 public RelativeDynamicURI(ServerData sd, 223 String screen, 224 boolean redirect) 225 { 226 super(sd, screen, redirect); 227 } 228 229 /*** 230 * Main constructor for RelativeDynamicURI. Uses ServerData. 231 * 232 * @param sd A ServerData. 233 * @param redirect True if it should redirect. 234 */ 235 public RelativeDynamicURI(ServerData sd, boolean redirect) 236 { 237 super(sd, redirect); 238 } 239 240 /*** 241 * Builds the relative URL with all of the data URL-encoded as well as 242 * encoded using HttpServletResponse.encodeUrl(). 243 * 244 * <p> 245 * <code><pre> 246 * RelativeDynamicURI dui = new RelativeDynamicURI (data, "UserScreen" ); 247 * dui.addPathInfo("user","jon"); 248 * dui.toString(); 249 * </pre></code> 250 * 251 * The above call to toString() would return the String: 252 * 253 * <p> 254 * /servlets/Turbine/screen/UserScreen/user/jon 255 * 256 * @return A String with the built relative URL. 257 */ 258 public String toString() 259 { 260 StringBuffer output = new StringBuffer(); 261 output.append(getScriptName()); 262 if (this.hasPathInfo) 263 { 264 output.append("/"); 265 output.append(renderPathInfo(this.pathInfo)); 266 } 267 if (this.hasQueryData) 268 { 269 output.append("?"); 270 output.append(renderQueryString(this.queryData)); 271 } 272 273 // There seems to be a bug in Apache JServ 1.0 where the 274 // session id is not appended to the end of the url when a 275 // cookie has not been set. 276 if (this.res != null ) 277 { 278 if (this.redirect) 279 { 280 return res.encodeRedirectUrl(output.toString()); 281 } 282 else 283 { 284 return res.encodeUrl(output.toString()); 285 } 286 } 287 else 288 { 289 return output.toString(); 290 } 291 } 292 293 /*** 294 * Given a RunData object, get a relative URI for the request. This is 295 * necessary sometimes when you want the relative URL and don't want 296 * RelativeDynamicURI to be too smart and remove actions, screens, etc. 297 * This also returns the Query Data where RelativeDynamicURI normally 298 * would not. 299 * 300 * @param data A Turbine RunData object. 301 * @return A String with the relative URL. 302 */ 303 public static String toString(RunData data) 304 { 305 StringBuffer output = new StringBuffer(); 306 HttpServletRequest request = data.getRequest(); 307 308 output.append(data.getServerData().getScriptName()); 309 310 if (request.getPathInfo() != null) 311 { 312 output.append(request.getPathInfo()); 313 } 314 315 if (request.getQueryString() != null) 316 { 317 output.append("?"); 318 output.append(request.getQueryString()); 319 } 320 return output.toString(); 321 } 322 }

This page was automatically generated by Maven