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 java.net.URLEncoder;
58 import java.util.Enumeration;
59 import java.util.Vector;
60 import javax.servlet.http.HttpServletRequest;
61 import javax.servlet.http.HttpServletResponse;
62 import org.apache.ecs.html.A;
63 import org.apache.turbine.services.resources.TurbineResources;
64 import org.apache.turbine.util.parser.BaseValueParser;
65
66 /***
67 * This creates a Dynamic URI for use within the Turbine system
68 *
69 * <p>If you use this class to generate all of your href tags as well
70 * as all of your URI's, then you will not need to worry about having
71 * session data setup for you or using HttpServletRequest.encodeUrl()
72 * since this class does everything for you.
73 *
74 * <code><pre>
75 * DynamicURI dui = new DynamicURI (data, "UserScreen" );
76 * dui.setName("Click Here").addPathInfo("user","jon");
77 * dui.getA();
78 * </pre></code>
79 *
80 * The above call to getA() would return the String:
81 *
82 * <A HREF="http://www.server.com:80/servlets/Turbine/screen=UserScreen&user=jon">Click Here</A>
83 *
84 * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
85 * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
86 * @version $Id: DynamicURI.java,v 1.2 2002/07/11 16:53:21 mpoeschl Exp $
87 */
88 public class DynamicURI
89 {
90 /*** HTTP protocol. */
91 public static final String HTTP = "http";
92
93 /*** HTTPS protocol. */
94 public static final String HTTPS = "https";
95
96 /*** The ServerData object. */
97 protected ServerData sd = null;
98
99 /*** The RunData object. */
100 protected RunData data = null;
101
102 /*** ie: http or https. */
103 protected String serverScheme = null;
104
105 /*** ie: www.foo.com. */
106 protected String serverName = null;
107
108 /*** ie: 80 or 443. */
109 protected int serverPort = 80;
110
111 /*** /foo/Turbine. */
112 protected String scriptName = null;
113
114
115 // Used with RunData constructors to provide a way around a JServ
116 // 1.0 bug.
117
118 /*** Servlet response interface. */
119 public HttpServletResponse res = null;
120
121 /*** A Vector that contains all the path info if any. */
122 protected Vector pathInfo = null;
123
124 /*** A Vectory that contains all the query data if any. */
125 protected Vector queryData = null;
126
127 /***
128 * Fast shortcut to determine if there is any data in the path
129 * info.
130 */
131 protected boolean hasPathInfo = false;
132
133 /***
134 * Fast shortcut to determine if there is any data in the query
135 * data.
136 */
137 protected boolean hasQueryData = false;
138
139 /*** Whether we want to redirect or not. */
140 protected boolean redirect = false;
141
142 /*** P = 0 for path info. */
143 protected static final int PATH_INFO = 0;
144
145 /*** Q = 1 for query data. */
146 protected static final int QUERY_DATA = 1;
147
148 /***
149 * Constructor sets up some variables.
150 *
151 * @param data A Turbine RunData object.
152 */
153 public DynamicURI( RunData data )
154 {
155 init(data);
156 }
157
158 /***
159 * Default constructor - one of the init methods must be called
160 * before use.
161 */
162 public DynamicURI()
163 {
164 }
165
166 /***
167 * Constructor sets up some variables.
168 *
169 * @param data A Turbine RunData object.
170 * @param screen A String with the name of a screen.
171 */
172 public DynamicURI( RunData data,
173 String screen )
174 {
175 this(data);
176 setScreen(screen);
177 }
178
179 /***
180 * Constructor sets up some variables.
181 *
182 * @param data A Turbine RunData object.
183 * @param screen A String with the name of a screen.
184 * @param action A String with the name of an action.
185 */
186 public DynamicURI( RunData data,
187 String screen,
188 String action )
189 {
190 this( data, screen );
191 setAction(action);
192 }
193
194 /***
195 * Constructor sets up some variables.
196 *
197 * @param data A Turbine RunData object.
198 * @param screen A String with the name of a screen.
199 * @param action A String with the name of an action.
200 * @param redirect True if it should redirect.
201 */
202 public DynamicURI( RunData data,
203 String screen,
204 String action,
205 boolean redirect )
206 {
207 this( data, screen, action );
208 this.redirect = redirect;
209 }
210
211 /***
212 * Constructor sets up some variables.
213 *
214 * @param data A Turbine RunData object.
215 * @param screen A String with the name of a screen.
216 * @param redirect True if it should redirect.
217 */
218 public DynamicURI( RunData data,
219 String screen,
220 boolean redirect )
221 {
222 this( data, screen );
223 this.redirect = redirect;
224 }
225
226 /***
227 * Constructor sets up some variables.
228 *
229 * @param data A Turbine RunData object.
230 * @param redirect True if it should redirect.
231 */
232 public DynamicURI( RunData data,
233 boolean redirect )
234 {
235 this( data );
236 this.redirect = redirect;
237 }
238
239 /***
240 * Main constructor for DynamicURI. Uses ServerData.
241 *
242 * @param sd A ServerData.
243 */
244 public DynamicURI( ServerData sd )
245 {
246 init(sd);
247 }
248
249 /***
250 * Main constructor for DynamicURI. Uses ServerData.
251 *
252 * @param sd A ServerData.
253 * @param screen A String with the name of a screen.
254 */
255 public DynamicURI( ServerData sd, String screen )
256 {
257 this(sd);
258 setScreen(screen);
259 }
260
261 /***
262 * Main constructor for DynamicURI. Uses ServerData.
263 *
264 * @param sd A ServerData.
265 * @param screen A String with the name of a screen.
266 * @param action A String with the name of an action.
267 */
268 public DynamicURI( ServerData sd,
269 String screen,
270 String action )
271 {
272 this( sd, screen );
273 setAction(action);
274 }
275
276 /***
277 * Main constructor for DynamicURI. Uses ServerData.
278 *
279 * @param sd A ServerData.
280 * @param screen A String with the name of a screen.
281 * @param action A String with the name of an action.
282 * @param redirect True if it should redirect.
283 */
284 public DynamicURI( ServerData sd,
285 String screen,
286 String action,
287 boolean redirect )
288 {
289 this( sd, screen, action );
290 this.redirect = redirect;
291 }
292
293 /***
294 * Main constructor for DynamicURI. Uses ServerData.
295 *
296 * @param sd A ServerData.
297 * @param screen A String with the name of a screen.
298 * @param redirect True if it should redirect.
299 */
300 public DynamicURI( ServerData sd,
301 String screen,
302 boolean redirect )
303 {
304 this( sd, screen );
305 this.redirect = redirect;
306 }
307
308 /***
309 * Main constructor for DynamicURI. Uses ServerData.
310 *
311 * @param sd A ServerData.
312 * @param redirect True if it should redirect.
313 */
314 public DynamicURI( ServerData sd,
315 boolean redirect )
316 {
317 this( sd );
318 this.redirect = redirect;
319 }
320
321 /***
322 * Initialize with a RunData object
323 *
324 * @param RunData
325 */
326 public void init( RunData data )
327 {
328 init(data.getServerData());
329 this.data = data;
330 this.res = data.getResponse();
331 }
332
333 /***
334 * Initialize with a ServerData object.
335 *
336 * @param ServerData
337 */
338 public void init ( ServerData sd )
339 {
340 this.setServerData( sd );
341 init();
342 }
343
344 /***
345 * <p>If the type is P (0), then add name/value to the pathInfo
346 * hashtable.
347 *
348 * <p>If the type is Q (1), then add name/value to the queryData
349 * hashtable.
350 *
351 * @param type Type (P or Q) of insertion.
352 * @param name A String with the name to add.
353 * @param value A String with the value to add.
354 */
355 protected void add ( int type,
356 String name,
357 String value )
358 {
359 Object[] tmp = new Object[2];
360 tmp[0] = (Object) BaseValueParser.convertAndTrim(name);
361 tmp[1] = (Object) value;
362 switch (type)
363 {
364 case PATH_INFO:
365 this.pathInfo.addElement ( tmp );
366 this.hasPathInfo = true;
367 break;
368 case QUERY_DATA:
369 this.queryData.addElement ( tmp );
370 this.hasQueryData = true;
371 break;
372 }
373 }
374
375 /***
376 * Method for a quick way to add all the parameters in a
377 * ParameterParser.
378 *
379 * <p>If the type is P (0), then add name/value to the pathInfo
380 * hashtable.
381 *
382 * <p>If the type is Q (1), then add name/value to the queryData
383 * hashtable.
384 *
385 * @param type Type (P or Q) of insertion.
386 * @param pp A ParameterParser.
387 */
388 protected void add( int type,
389 ParameterParser pp )
390 {
391 Enumeration e = pp.keys();
392 while ( e.hasMoreElements() )
393 {
394 String key = (String)e.nextElement();
395 if ( !key.equalsIgnoreCase("action") &&
396 !key.equalsIgnoreCase("screen") &&
397 !key.equalsIgnoreCase("template") )
398 {
399 String[] values = pp.getStrings(key);
400 for ( int i=0; i<values.length; i++ )
401 {
402 add( type, key, values[i] );
403 }
404 }
405 }
406 }
407
408 /***
409 * Adds a name=value pair to the path_info string.
410 *
411 * @param name A String with the name to add.
412 * @param value An Object with the value to add.
413 */
414 public DynamicURI addPathInfo ( String name, Object value )
415 {
416 add ( PATH_INFO, name, value.toString() );
417 return this;
418 }
419
420 /***
421 * Adds a name=value pair to the path_info string.
422 *
423 * @param name A String with the name to add.
424 * @param value A String with the value to add.
425 */
426 public DynamicURI addPathInfo ( String name, String value )
427 {
428 add ( PATH_INFO, name, value );
429 return this;
430 }
431
432 /***
433 * Adds a name=value pair to the path_info string.
434 *
435 * @param name A String with the name to add.
436 * @param value A double with the value to add.
437 */
438 public DynamicURI addPathInfo ( String name, double value )
439 {
440 add ( PATH_INFO, name, Double.toString(value) );
441 return this;
442 }
443
444 /***
445 * Adds a name=value pair to the path_info string.
446 *
447 * @param name A String with the name to add.
448 * @param value An int with the value to add.
449 */
450 public DynamicURI addPathInfo ( String name, int value )
451 {
452 add ( PATH_INFO, name, new Integer(value).toString() );
453 return this;
454 }
455
456 /***
457 * Adds a name=value pair to the path_info string.
458 *
459 * @param name A String with the name to add.
460 * @param value A long with the value to add.
461 */
462 public DynamicURI addPathInfo ( String name, long value )
463 {
464 add ( PATH_INFO, name, new Long(value).toString() );
465 return this;
466 }
467
468 /***
469 * Adds a name=value pair for every entry in a ParameterParser
470 * object to the path_info string.
471 *
472 * @param pp A ParameterParser.
473 */
474 public DynamicURI addPathInfo ( ParameterParser pp )
475 {
476 add ( PATH_INFO, pp );
477 return this;
478 }
479
480 /***
481 * Adds a name=value pair to the query string.
482 *
483 * @param name A String with the name to add.
484 * @param value An Object with the value to add.
485 */
486 public DynamicURI addQueryData ( String name, Object value )
487 {
488 add ( QUERY_DATA, name, value.toString() );
489 return this;
490 }
491
492 /***
493 * Adds a name=value pair to the query string.
494 *
495 * @param name A String with the name to add.
496 * @param value A String with the value to add.
497 */
498 public DynamicURI addQueryData ( String name, String value )
499 {
500 add ( QUERY_DATA, name, value );
501 return this;
502 }
503
504 /***
505 * Adds a name=value pair to the query string.
506 *
507 * @param name A String with the name to add.
508 * @param value A double with the value to add.
509 */
510 public DynamicURI addQueryData ( String name, double value )
511 {
512 add ( QUERY_DATA, name, Double.toString(value) );
513 return this;
514 }
515
516 /***
517 * Adds a name=value pair to the query string.
518 *
519 * @param name A String with the name to add.
520 * @param value An int with the value to add.
521 */
522 public DynamicURI addQueryData ( String name, int value )
523 {
524 add ( QUERY_DATA, name, new Integer(value).toString() );
525 return this;
526 }
527
528 /***
529 * Adds a name=value pair to the query string.
530 *
531 * @param name A String with the name to add.
532 * @param value A long with the value to add.
533 */
534 public DynamicURI addQueryData ( String name, long value )
535 {
536 add ( QUERY_DATA, name, new Long(value).toString() );
537 return this;
538 }
539
540 /***
541 * Adds a name=value pair for every entry in a ParameterParser
542 * object to the query string.
543 *
544 * @param pp A ParameterParser.
545 */
546 public DynamicURI addQueryData ( ParameterParser pp )
547 {
548 add ( QUERY_DATA, pp );
549 return this;
550 }
551
552 /***
553 * Create an anchor object. This call to getA():
554 *
555 * <code><pre>
556 * DynamicURI dui = new DynamicURI (data, "UserScreen" );
557 * dui.setName("Click Here").addPathInfo("user","jon");
558 * dui.getA();
559 * </pre></code>
560 *
561 * would return the String:
562 *
563 * <p><A HREF="http://www.server.com:80/servlets/Turbine/screen=UserScreen&user=jon">Click Here</A>
564 *
565 * @param name A String with the name for the anchor.
566 * @return The anchor as a <A HREF="">name</A>.
567 */
568 public String getA( String name )
569 {
570 return new A(this.toString(), name).toString();
571 }
572
573 /***
574 * Gets the script name (/servlets/Turbine).
575 *
576 * @return A String with the script name.
577 */
578 public String getScriptName ()
579 {
580 if ( this.scriptName == null )
581 {
582 return "";
583 }
584 return this.scriptName;
585 }
586
587 /***
588 * Gets the server name.
589 *
590 * @return A String with the server name.
591 */
592 public String getServerName ()
593 {
594 if ( this.serverName == null )
595 {
596 return "";
597 }
598 return(this.serverName);
599 }
600
601 /***
602 * Gets the server port.
603 *
604 * @return A String with the server port.
605 */
606 public int getServerPort ()
607 {
608 if ( this.serverPort == 0 )
609 {
610 return 80;
611 }
612 return this.serverPort;
613 }
614
615 /***
616 * Gets the server scheme (HTTP or HTTPS).
617 *
618 * @return A String with the server scheme.
619 */
620 public String getServerScheme ()
621 {
622 if ( this.serverScheme == null )
623 {
624 return "";
625 }
626 return (this.serverScheme);
627 }
628
629 /***
630 * Initializes some common variables.
631 */
632 protected void init()
633 {
634 this.serverScheme = this.getServerData().getServerScheme();
635 this.serverName = this.getServerData().getServerName();
636 this.serverPort = this.getServerData().getServerPort();
637 this.scriptName = this.getServerData().getScriptName();
638 this.pathInfo = new Vector();
639 this.queryData = new Vector();
640 }
641
642 /***
643 * <p>If the type is P (0), then remove name/value from the
644 * pathInfo hashtable.
645 *
646 * <p>If the type is Q (1), then remove name/value from the
647 * queryData hashtable.
648 *
649 * @param type Type (P or Q) of removal.
650 * @param name A String with the name to be removed.
651 */
652 protected void remove ( int type,
653 String name )
654 {
655 try
656 {
657 switch (type)
658 {
659 case PATH_INFO:
660 for (Enumeration e = this.pathInfo.elements() ;
661 e.hasMoreElements() ;)
662 {
663 Object[] tmp = (Object[]) e.nextElement();
664 if ( BaseValueParser.convertAndTrim(name)
665 .equals ( (String)tmp[0] ) )
666 {
667 this.pathInfo.removeElement ( tmp );
668 }
669 }
670 if ( hasPathInfo && this.pathInfo.size() == 0 )
671 {
672 this.hasPathInfo = false;
673 }
674 break;
675 case QUERY_DATA:
676 for (Enumeration e = this.queryData.elements() ;
677 e.hasMoreElements() ;)
678 {
679 Object[] tmp = (Object[]) e.nextElement();
680 if ( BaseValueParser.convertAndTrim(name)
681 .equals ( (String)tmp[0] ) )
682 {
683 this.queryData.removeElement ( tmp );
684 }
685 }
686 if ( hasQueryData && this.queryData.size() == 0 )
687 {
688 this.hasQueryData = false;
689 }
690 break;
691 }
692 }
693 catch ( Exception e )
694 {
695 }
696 }
697
698 /***
699 * Removes all the path info elements.
700 */
701 public void removePathInfo ()
702 {
703 this.pathInfo.removeAllElements();
704 this.hasPathInfo = false;
705 }
706
707 /***
708 * Removes a name=value pair from the path info.
709 *
710 * @param name A String with the name to be removed.
711 */
712 public void removePathInfo ( String name )
713 {
714 remove ( PATH_INFO, name );
715 }
716
717 /***
718 * Removes all the query string elements.
719 */
720 public void removeQueryData ()
721 {
722 this.queryData.removeAllElements();
723 this.hasQueryData = false;
724 }
725
726 /***
727 * Removes a name=value pair from the query string.
728 *
729 * @param name A String with the name to be removed.
730 */
731 public void removeQueryData ( String name )
732 {
733 remove ( QUERY_DATA, name );
734 }
735
736 /***
737 * This method takes a Vector of key/value arrays and converts it
738 * into a URL encoded querystring format.
739 *
740 * @param data A Vector of key/value arrays.
741 * @return A String with the URL encoded data.
742 */
743 protected String renderPathInfo ( Vector data )
744 {
745 String key = null;
746 String value = null;
747 String tmp = null;
748 StringBuffer out = new StringBuffer();
749 Enumeration keys = data.elements();
750 while(keys.hasMoreElements())
751 {
752 Object[] stuff = (Object[]) keys.nextElement();
753 key = URLEncoder.encode((String)stuff[0]);
754 tmp = (String) stuff[1];
755 if (tmp == null || tmp.length() == 0)
756 {
757 value = "null";
758 }
759 else
760 {
761 value = URLEncoder.encode(tmp);
762 }
763
764 if (out.length() > 0)
765 {
766 out.append ( "/" );
767 }
768 out.append ( key );
769 out.append ( "/" );
770 out.append ( value );
771 }
772 return out.toString();
773 }
774
775 /***
776 * This method takes a Vector of key/value arrays and converts it
777 * into a URL encoded querystring format.
778 *
779 * @param data A Vector of key/value arrays.
780 * @return A String with the URL encoded data.
781 */
782 protected String renderQueryString ( Vector data )
783 {
784 String key = null;
785 String value = null;
786 String tmp = null;
787 StringBuffer out = new StringBuffer();
788 Enumeration keys = data.elements();
789 while(keys.hasMoreElements())
790 {
791 Object[] stuff = (Object[]) keys.nextElement();
792 key = URLEncoder.encode((String) stuff[0]);
793 tmp = (String) stuff[1];
794 if (tmp == null || tmp.length() == 0)
795 {
796 value = "null";
797 }
798 else
799 {
800 value = URLEncoder.encode(tmp);
801 }
802
803 if ( out.length() > 0)
804 {
805 out.append ( "&" );
806 }
807 out.append ( key );
808 out.append ( "=" );
809 out.append ( value );
810 }
811 return out.toString();
812 }
813
814 /***
815 * Sets the action= value for this URL.
816 *
817 * <p>By default it adds the information to the path_info instead
818 * of the query data.
819 *
820 * @param action A String with the action value.
821 * @return A DynamicURI (self).
822 */
823 public DynamicURI setAction ( String action )
824 {
825 add ( PATH_INFO, "action", action );
826 return this;
827 }
828
829 /***
830 * Sets the screen= value for this URL.
831 *
832 * <p>By default it adds the information to the path_info instead
833 * of the query data.
834 *
835 * @param action A String with the screen value.
836 * @return A DynamicURI (self).
837 */
838 public DynamicURI setScreen ( String screen )
839 {
840 add ( PATH_INFO, "screen", screen );
841 return this;
842 }
843
844 /***
845 * Sets the script name (/servlets/Turbine).
846 *
847 * @param name A String with the script name.
848 * @return A DynamicURI (self).
849 */
850 public DynamicURI setScriptName ( String name )
851 {
852 this.scriptName = name;
853 return this;
854 }
855
856 /***
857 * Sets the server name.
858 *
859 * @param name A String with the server name.
860 * @return A DynamicURI (self).
861 */
862 public DynamicURI setServerName ( String name )
863 {
864 this.serverName = name;
865 return this;
866 }
867
868 /***
869 * Sets the server port.
870 *
871 * @param port An int with the port.
872 * @return A DynamicURI (self).
873 */
874 public DynamicURI setServerPort ( int port )
875 {
876 this.serverPort = port;
877 return this;
878 }
879
880 /***
881 * Method to specify that a URI should use SSL. Whether or not it
882 * does is determined from TurbineResources.properties. Port
883 * number is 443.
884 *
885 * @return A DynamicURI (self).
886 */
887 public DynamicURI setSecure()
888 {
889 return setSecure(443);
890 }
891
892 /***
893 * Method to specify that a URI should use SSL. Whether or not it
894 * does is determined from TurbineResources.properties.
895 *
896 * @param port An int with the port number.
897 * @return A DynamicURI (self).
898 */
899 public DynamicURI setSecure(int port)
900 {
901 boolean isSSL = TurbineResources.getBoolean("use.ssl", true);
902 if (isSSL)
903 {
904 setServerScheme(DynamicURI.HTTPS);
905 setServerPort(port);
906 }
907 return this;
908 }
909
910 /***
911 * Sets the scheme (HTTP or HTTPS).
912 *
913 * @param scheme A String with the scheme.
914 * @return A DynamicURI (self).
915 */
916 public DynamicURI setServerScheme ( String scheme )
917 {
918 this.serverScheme = scheme;
919 return this;
920 }
921
922 /***
923 * Builds the URL with all of the data URL-encoded as well as
924 * encoded using HttpServletResponse.encodeUrl().
925 *
926 * <p>
927 * <code><pre>
928 * DynamicURI dui = new DynamicURI (data, "UserScreen" );
929 * dui.addPathInfo("user","jon");
930 * dui.toString();
931 * </pre></code>
932 *
933 * The above call to toString() would return the String:
934 *
935 * <p>
936 * http://www.server.com/servlets/Turbine/screen/UserScreen/user/jon
937 *
938 * @return A String with the built URL.
939 */
940 public String toString()
941 {
942 StringBuffer output = new StringBuffer();
943 output.append ( getServerScheme() );
944 output.append ( "://" );
945 output.append ( getServerName() );
946 if ( (getServerScheme().equals(HTTP) && getServerPort() != 80)
947 || (getServerScheme().equals(HTTPS) && getServerPort() != 443)
948 )
949 {
950 output.append (":");
951 output.append ( getServerPort() );
952 }
953 output.append ( getScriptName() );
954 if ( this.hasPathInfo )
955 {
956 output.append ( "/" );
957 output.append ( renderPathInfo(this.pathInfo) );
958 }
959 if ( this.hasQueryData )
960 {
961 output.append ( "?" );
962 output.append ( renderQueryString(this.queryData) );
963 }
964
965 // There seems to be a bug in Apache JServ 1.0 where the
966 // session id is not appended to the end of the url when a
967 // cookie has not been set.
968 if ( this.res != null )
969 {
970 if ( this.redirect )
971 return res.encodeRedirectUrl (output.toString());
972 else
973 return res.encodeUrl (output.toString());
974 }
975 else
976 {
977 return output.toString();
978 }
979 }
980
981 /***
982 * Given a RunData object, get a URI for the request. This is
983 * necessary sometimes when you want the exact URL and don't want
984 * DynamicURI to be too smart and remove actions, screens, etc.
985 * This also returns the Query Data where DynamicURI normally
986 * would not.
987 *
988 * @param data A Turbine RunData object.
989 * @return A String with the URL representing the RunData.
990 */
991 public static String toString(RunData data)
992 {
993 StringBuffer output = new StringBuffer();
994 HttpServletRequest request = data.getRequest();
995
996 output.append ( data.getServerScheme() );
997 output.append ( "://" );
998 output.append ( data.getServerName() );
999
1000 if ( (data.getServerScheme().equals(HTTP) &&
1001 data.getServerPort() != 80) ||
1002 (data.getServerScheme().equals(HTTPS) &&
1003 data.getServerPort() != 443) )
1004 {
1005 output.append (":");
1006 output.append ( data.getServerPort() );
1007 }
1008
1009 output.append ( data.getServerData().getScriptName() );
1010
1011 if ( request.getPathInfo() != null )
1012 {
1013 output.append( request.getPathInfo() );
1014 }
1015
1016 if ( request.getQueryString() != null )
1017 {
1018 output.append ( "?" );
1019 output.append ( request.getQueryString() );
1020 }
1021 return output.toString();
1022 }
1023
1024 /***
1025 * Returns the ServerData used to initialize this DynamicURI.
1026 *
1027 * @return A ServerData used to initialize this DynamicURI.
1028 */
1029 public ServerData getServerData()
1030 {
1031 return this.sd;
1032 }
1033
1034 /***
1035 * Sets the ServerData used to initialize this DynamicURI.
1036 *
1037 * @param sd A ServerData used to initialize this DynamicURI.
1038 */
1039 public void setServerData( ServerData sd )
1040 {
1041 this.sd = sd;
1042 }
1043 }
This page was automatically generated by Maven