Source Code Cross Referenced for DynamicURI.java in  » Web-Framework » TURBINE » org » apache » turbine » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » TURBINE » org.apache.turbine.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.turbine.util;
002:
003:        /*
004:         * Licensed to the Apache Software Foundation (ASF) under one
005:         * or more contributor license agreements.  See the NOTICE file
006:         * distributed with this work for additional information
007:         * regarding copyright ownership.  The ASF licenses this file
008:         * to you under the Apache License, Version 2.0 (the
009:         * "License"); you may not use this file except in compliance
010:         * with the License.  You may obtain a copy of the License at
011:         *
012:         *   http://www.apache.org/licenses/LICENSE-2.0
013:         *
014:         * Unless required by applicable law or agreed to in writing,
015:         * software distributed under the License is distributed on an
016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017:         * KIND, either express or implied.  See the License for the
018:         * specific language governing permissions and limitations
019:         * under the License.
020:         */
021:
022:        import java.net.URLEncoder;
023:
024:        import java.util.ArrayList;
025:        import java.util.Iterator;
026:        import java.util.List;
027:
028:        import javax.servlet.http.HttpServletRequest;
029:        import javax.servlet.http.HttpServletResponse;
030:
031:        import org.apache.commons.lang.StringUtils;
032:
033:        import org.apache.commons.logging.Log;
034:        import org.apache.commons.logging.LogFactory;
035:
036:        import org.apache.ecs.html.A;
037:
038:        import org.apache.turbine.Turbine;
039:        import org.apache.turbine.TurbineConstants;
040:        import org.apache.turbine.util.parser.ParameterParser;
041:        import org.apache.turbine.util.parser.ParserUtils;
042:        import org.apache.turbine.util.uri.URI;
043:        import org.apache.turbine.util.uri.URIConstants;
044:
045:        /**
046:         * This creates a Dynamic URI for use within the Turbine system
047:         *
048:         * <p>If you use this class to generate all of your href tags as well
049:         * as all of your URI's, then you will not need to worry about having
050:         * session data setup for you or using HttpServletRequest.encodeUrl()
051:         * since this class does everything for you.
052:         *
053:         * <code><pre>
054:         * DynamicURI dui = new DynamicURI (data, "UserScreen" );
055:         * dui.setName("Click Here").addPathInfo("user","jon");
056:         * dui.getA();
057:         * </pre></code>
058:         *
059:         * The above call to getA() would return the String:
060:         *
061:         * &lt;A HREF="http://www.server.com:80/servlets/Turbine/screen=UserScreen&amp;amp;user=jon"&gt;Click Here&lt;/A&gt;
062:         *
063:         * @todo Add support for returning the correct URI when mod_rewrite is
064:         *       being used.
065:         *
066:         * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
067:         * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
068:         * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
069:         * @version $Id: DynamicURI.java 534527 2007-05-02 16:10:59Z tv $
070:         * @deprecated Use {@link org.apache.turbine.util.uri.TurbineURI} instead.
071:         */
072:        public class DynamicURI implements  URI {
073:            /** @deprecated Use URIConstants.HTTP */
074:            public static final String HTTP = URIConstants.HTTP;
075:
076:            /** @deprecated Use URIConstants.HTTPS */
077:            public static final String HTTPS = URIConstants.HTTPS;
078:
079:            /** Logging */
080:            private static Log log = LogFactory.getLog(DynamicURI.class);
081:
082:            /** The ServerData object. */
083:            protected ServerData sd = null;
084:
085:            /** The RunData object. */
086:            protected RunData data = null;
087:
088:            /** #ref */
089:            protected String reference = null;
090:
091:            // Used with RunData constructors to provide a way around a JServ
092:            // 1.0 bug.
093:
094:            /** Servlet response interface. */
095:            public HttpServletResponse res = null;
096:
097:            /** A List that contains all the path info if any. */
098:            protected List pathInfo = null;
099:
100:            /** A List that contains all the query data if any. */
101:            protected List queryData = null;
102:
103:            /** Fast shortcut to determine if there is any data in the path info. */
104:            protected boolean hasPathInfo = false;
105:
106:            /** Fast shortcut to determine if there is any data in the query data. */
107:            protected boolean hasQueryData = false;
108:
109:            /** Whether we want to redirect or not. */
110:            protected boolean redirect = false;
111:
112:            /** P = 0 for path info. */
113:            protected static final int PATH_INFO = 0;
114:
115:            /** Q = 1 for query data. */
116:            protected static final int QUERY_DATA = 1;
117:
118:            /** Has the object been initialized? */
119:            private boolean initialized = false;
120:
121:            /**
122:             * Constructor sets up some variables.
123:             *
124:             * @param data A Turbine RunData object.
125:             */
126:            public DynamicURI(RunData data) {
127:                init(data);
128:            }
129:
130:            /**
131:             * Default constructor - one of the init methods must be called
132:             * before use.
133:             */
134:            public DynamicURI() {
135:            }
136:
137:            /**
138:             * Constructor sets up some variables.
139:             *
140:             * @param data A Turbine RunData object.
141:             * @param screen A String with the name of a screen.
142:             */
143:            public DynamicURI(RunData data, String screen) {
144:                this (data);
145:                setScreen(screen);
146:            }
147:
148:            /**
149:             * Constructor sets up some variables.
150:             *
151:             * @param data A Turbine RunData object.
152:             * @param screen A String with the name of a screen.
153:             * @param action A String with the name of an action.
154:             */
155:            public DynamicURI(RunData data, String screen, String action) {
156:                this (data, screen);
157:                setAction(action);
158:            }
159:
160:            /**
161:             * Constructor sets up some variables.
162:             *
163:             * @param data A Turbine RunData object.
164:             * @param screen A String with the name of a screen.
165:             * @param action A String with the name of an action.
166:             * @param redirect True if it should redirect.
167:             */
168:            public DynamicURI(RunData data, String screen, String action,
169:                    boolean redirect) {
170:                this (data, screen, action);
171:                this .redirect = redirect;
172:            }
173:
174:            /**
175:             * Constructor sets up some variables.
176:             *
177:             * @param data A Turbine RunData object.
178:             * @param screen A String with the name of a screen.
179:             * @param redirect True if it should redirect.
180:             */
181:            public DynamicURI(RunData data, String screen, boolean redirect) {
182:                this (data, screen);
183:                this .redirect = redirect;
184:            }
185:
186:            /**
187:             * Constructor sets up some variables.
188:             *
189:             * @param data A Turbine RunData object.
190:             * @param redirect True if it should redirect.
191:             */
192:            public DynamicURI(RunData data, boolean redirect) {
193:                this (data);
194:                this .redirect = redirect;
195:            }
196:
197:            /**
198:             * Main constructor for DynamicURI.  Uses ServerData.
199:             *
200:             * @param sd A ServerData.
201:             */
202:            public DynamicURI(ServerData sd) {
203:                init(sd);
204:            }
205:
206:            /**
207:             * Main constructor for DynamicURI.  Uses ServerData.
208:             *
209:             * @param sd A ServerData.
210:             * @param screen A String with the name of a screen.
211:             */
212:            public DynamicURI(ServerData sd, String screen) {
213:                this (sd);
214:                setScreen(screen);
215:            }
216:
217:            /**
218:             * Main constructor for DynamicURI.  Uses ServerData.
219:             *
220:             * @param sd A ServerData.
221:             * @param screen A String with the name of a screen.
222:             * @param action A String with the name of an action.
223:             */
224:            public DynamicURI(ServerData sd, String screen, String action) {
225:                this (sd, screen);
226:                setAction(action);
227:            }
228:
229:            /**
230:             * Main constructor for DynamicURI.  Uses ServerData.
231:             *
232:             * @param sd A ServerData.
233:             * @param screen A String with the name of a screen.
234:             * @param action A String with the name of an action.
235:             * @param redirect True if it should redirect.
236:             */
237:            public DynamicURI(ServerData sd, String screen, String action,
238:                    boolean redirect) {
239:                this (sd, screen, action);
240:                this .redirect = redirect;
241:            }
242:
243:            /**
244:             * Main constructor for DynamicURI.  Uses ServerData.
245:             *
246:             * @param serverData A ServerData.
247:             * @param screen A String with the name of a screen.
248:             * @param redirect True if it should redirect.
249:             */
250:            public DynamicURI(ServerData serverData, String screen,
251:                    boolean redirect) {
252:                this (serverData, screen);
253:                this .redirect = redirect;
254:            }
255:
256:            /**
257:             * Main constructor for DynamicURI.  Uses ServerData.
258:             *
259:             * @param serverData A ServerData.
260:             * @param redirect True if it should redirect.
261:             */
262:            public DynamicURI(ServerData serverData, boolean redirect) {
263:                this (serverData);
264:                this .redirect = redirect;
265:                this .initialized = true;
266:            }
267:
268:            /**
269:             * Initialize with a RunData object
270:             *
271:             * @param data RunData instance
272:             */
273:            public void init(RunData data) {
274:                init(data.getServerData());
275:                this .data = data;
276:                this .res = data.getResponse();
277:            }
278:
279:            /**
280:             * Initialize with a ServerData object.
281:             *
282:             * @param serverData
283:             */
284:            public void init(ServerData serverData) {
285:                this .sd = (ServerData) serverData.clone();
286:                this .pathInfo = new ArrayList();
287:                this .queryData = new ArrayList();
288:                this .reference = null;
289:                this .initialized = true;
290:            }
291:
292:            /**
293:             * If the type is {@link #PATH_INFO}, then add name/value to the
294:             * pathInfo.
295:             * <p>
296:             * If the type is {@link #QUERY_DATA}, then add name/value to the
297:             * queryData.
298:             *
299:             * @param type Type of insertion.
300:             * @param name A String with the name to add.
301:             * @param value A String with the value to add.
302:             */
303:            protected void add(int type, String name, String value) {
304:                assertInitialized();
305:                Object[] tmp = new Object[2];
306:                tmp[0] = ParserUtils.convertAndTrim(name);
307:                tmp[1] = value;
308:                switch (type) {
309:                case PATH_INFO:
310:                    this .pathInfo.add(tmp);
311:                    this .hasPathInfo = true;
312:                    break;
313:                case QUERY_DATA:
314:                    this .queryData.add(tmp);
315:                    this .hasQueryData = true;
316:                    break;
317:                }
318:            }
319:
320:            /**
321:             * Method for a quick way to add all the parameters in a
322:             * ParameterParser.
323:             * <p>
324:             * If the type is {@link #PATH_INFO}, then add name/value to the
325:             * pathInfo.
326:             * <p>
327:             * If the type is {@link #QUERY_DATA}, then add name/value to the
328:             * queryData.
329:             *
330:             * @param type Type of insertion.
331:             * @param pp A ParameterParser.
332:             */
333:            protected void add(int type, ParameterParser pp) {
334:                for (Iterator iter = pp.keySet().iterator(); iter.hasNext();) {
335:                    String key = (String) iter.next();
336:                    if (!key.equalsIgnoreCase(URIConstants.CGI_ACTION_PARAM)
337:                            && !key
338:                                    .equalsIgnoreCase(URIConstants.CGI_SCREEN_PARAM)
339:                            && !key
340:                                    .equalsIgnoreCase(URIConstants.CGI_TEMPLATE_PARAM)) {
341:                        String[] values = pp.getStrings(key);
342:                        for (int i = 0; i < values.length; i++) {
343:                            add(type, key, values[i]);
344:                        }
345:                    }
346:                }
347:            }
348:
349:            /**
350:             * Adds a name=value pair to the path_info string.
351:             *
352:             * @param name A String with the name to add.
353:             * @param value An Object with the value to add.
354:             * @return A DynamicURI (self).
355:             */
356:            public DynamicURI addPathInfo(String name, Object value) {
357:                add(PATH_INFO, name, value.toString());
358:                return this ;
359:            }
360:
361:            /**
362:             * Adds a name=value pair to the path_info string.
363:             *
364:             * @param name A String with the name to add.
365:             * @param value A String with the value to add.
366:             * @return A DynamicURI (self).
367:             */
368:            public DynamicURI addPathInfo(String name, String value) {
369:                add(PATH_INFO, name, value);
370:                return this ;
371:            }
372:
373:            /**
374:             * Adds a name=value pair to the path_info string.
375:             *
376:             * @param name A String with the name to add.
377:             * @param value A double with the value to add.
378:             * @return A DynamicURI (self).
379:             */
380:            public DynamicURI addPathInfo(String name, double value) {
381:                add(PATH_INFO, name, Double.toString(value));
382:                return this ;
383:            }
384:
385:            /**
386:             * Adds a name=value pair to the path_info string.
387:             *
388:             * @param name A String with the name to add.
389:             * @param value An int with the value to add.
390:             * @return A DynamicURI (self).
391:             */
392:            public DynamicURI addPathInfo(String name, int value) {
393:                add(PATH_INFO, name, String.valueOf(value));
394:                return this ;
395:            }
396:
397:            /**
398:             * Adds a name=value pair to the path_info string.
399:             *
400:             * @param name A String with the name to add.
401:             * @param value A long with the value to add.
402:             * @return A DynamicURI (self).
403:             */
404:            public DynamicURI addPathInfo(String name, long value) {
405:                add(PATH_INFO, name, String.valueOf(value));
406:                return this ;
407:            }
408:
409:            /**
410:             * Adds a name=value pair for every entry in a ParameterParser
411:             * object to the path_info string.
412:             *
413:             * @param pp A ParameterParser.
414:             * @return A DynamicURI (self).
415:             */
416:            public DynamicURI addPathInfo(ParameterParser pp) {
417:                add(PATH_INFO, pp);
418:                return this ;
419:            }
420:
421:            /**
422:             * Adds a name=value pair to the query string.
423:             *
424:             * @param name A String with the name to add.
425:             * @param value An Object with the value to add.
426:             * @return A DynamicURI (self).
427:             */
428:            public DynamicURI addQueryData(String name, Object value) {
429:                add(QUERY_DATA, name, value.toString());
430:                return this ;
431:            }
432:
433:            /**
434:             * Adds a name=value pair to the query string.
435:             *
436:             * @param name A String with the name to add.
437:             * @param value A String with the value to add.
438:             * @return A DynamicURI (self).
439:             */
440:            public DynamicURI addQueryData(String name, String value) {
441:                add(QUERY_DATA, name, value);
442:                return this ;
443:            }
444:
445:            /**
446:             * Adds a name=value pair to the query string.
447:             *
448:             * @param name A String with the name to add.
449:             * @param value A double with the value to add.
450:             * @return A DynamicURI (self).
451:             */
452:            public DynamicURI addQueryData(String name, double value) {
453:                add(QUERY_DATA, name, Double.toString(value));
454:                return this ;
455:            }
456:
457:            /**
458:             * Adds a name=value pair to the query string.
459:             *
460:             * @param name A String with the name to add.
461:             * @param value An int with the value to add.
462:             * @return A DynamicURI (self).
463:             */
464:            public DynamicURI addQueryData(String name, int value) {
465:                add(QUERY_DATA, name, String.valueOf(value));
466:                return this ;
467:            }
468:
469:            /**
470:             * Adds a name=value pair to the query string.
471:             *
472:             * @param name A String with the name to add.
473:             * @param value A long with the value to add.
474:             * @return A DynamicURI (self).
475:             */
476:            public DynamicURI addQueryData(String name, long value) {
477:                add(QUERY_DATA, name, String.valueOf(value));
478:                return this ;
479:            }
480:
481:            /**
482:             * Adds a name=value pair for every entry in a ParameterParser
483:             * object to the query string.
484:             *
485:             * @param pp A ParameterParser.
486:             * @return A DynamicURI (self).
487:             */
488:            public DynamicURI addQueryData(ParameterParser pp) {
489:                add(QUERY_DATA, pp);
490:                return this ;
491:            }
492:
493:            /**
494:             * Create an anchor object.  This call to getA():
495:             *
496:             * <code><pre>
497:             * DynamicURI dui = new DynamicURI (data, "UserScreen" );
498:             * dui.setName("Click Here").addPathInfo("user","jon");
499:             * dui.getA();
500:             * </pre></code>
501:             *
502:             * would return the String:
503:             *
504:             * <p>&lt;A HREF="http://www.server.com:80/servlets/Turbine/screen=UserScreen&amp;amp;user=jon"&gt;Click Here&lt;/A&gt;
505:             *
506:             * @param name A String with the name for the anchor.
507:             * @return The anchor as a &lt;A HREF=""&gt;name&lt;/A&gt;.
508:             */
509:            public String getA(String name) {
510:                return new A(this .toString(), name).toString();
511:            }
512:
513:            /**
514:             * Gets the script name (/servlets/Turbine).
515:             *
516:             * @return A String with the script name.
517:             */
518:            public String getScriptName() {
519:                String result = getServerData().getScriptName();
520:                return (StringUtils.isEmpty(result) ? "" : result);
521:            }
522:
523:            /**
524:             * Gets the context path
525:             *
526:             * @return A String with the servlet context path
527:             */
528:            public String getContextPath() {
529:                String result = getServerData().getContextPath();
530:                return (StringUtils.isEmpty(result) ? "" : result);
531:            }
532:
533:            /**
534:             * Gets the reference (#ref).
535:             *
536:             * @return A String containing the reference.
537:             */
538:            public String getReference() {
539:                assertInitialized();
540:                return (StringUtils.isEmpty(this .reference) ? ""
541:                        : this .reference);
542:            }
543:
544:            /**
545:             * Gets the server name.
546:             *
547:             * @return A String with the server name.
548:             */
549:            public String getServerName() {
550:                String result = getServerData().getServerName();
551:                return (StringUtils.isEmpty(result) ? "" : result);
552:            }
553:
554:            /**
555:             * Gets the server port.
556:             *
557:             * @return A String with the server port.
558:             */
559:            public int getServerPort() {
560:                int result = getServerData().getServerPort();
561:                return (result == 0 ? URIConstants.HTTP_PORT : result);
562:            }
563:
564:            /**
565:             * Gets the server scheme (HTTP or HTTPS).
566:             *
567:             * @return A String with the server scheme.
568:             */
569:            public String getServerScheme() {
570:                String result = getServerData().getServerScheme();
571:                return (StringUtils.isEmpty(result) ? "" : result);
572:            }
573:
574:            /**
575:             * <p>If the type is {@link #PATH_INFO}, then remove name/value from the
576:             * pathInfo.
577:             *
578:             * <p>If the type is {@link #QUERY_DATA}, then remove name/value from the
579:             * queryData.
580:             *
581:             * @param type Type of removal.
582:             * @param name A String with the name to be removed.
583:             */
584:            protected void remove(int type, String name) {
585:                assertInitialized();
586:                try {
587:                    switch (type) {
588:                    case PATH_INFO:
589:                        for (Iterator iter = this .pathInfo.iterator(); iter
590:                                .hasNext();) {
591:                            Object[] tmp = (Object[]) iter.next();
592:                            if (ParserUtils.convertAndTrim(name).equals(
593:                                    (String) tmp[0])) {
594:                                iter.remove();
595:                            }
596:                        }
597:                        if (hasPathInfo && this .pathInfo.size() == 0) {
598:                            this .hasPathInfo = false;
599:                        }
600:                        break;
601:                    case QUERY_DATA:
602:                        for (Iterator iter = this .pathInfo.iterator(); iter
603:                                .hasNext();) {
604:                            Object[] tmp = (Object[]) iter.next();
605:                            if (ParserUtils.convertAndTrim(name).equals(
606:                                    (String) tmp[0])) {
607:                                iter.remove();
608:                            }
609:                        }
610:                        if (hasQueryData && this .queryData.size() == 0) {
611:                            this .hasQueryData = false;
612:                        }
613:                        break;
614:                    }
615:                } catch (Exception e) {
616:                    log.error("Could not remove " + name, e);
617:                }
618:            }
619:
620:            /**
621:             * Removes all the path info elements.
622:             */
623:            public void removePathInfo() {
624:                assertInitialized();
625:                this .pathInfo.clear();
626:                this .hasPathInfo = false;
627:            }
628:
629:            /**
630:             * Removes a name=value pair from the path info.
631:             *
632:             * @param name A String with the name to be removed.
633:             */
634:            public void removePathInfo(String name) {
635:                remove(PATH_INFO, name);
636:            }
637:
638:            /**
639:             * Removes all the query string elements.
640:             */
641:            public void removeQueryData() {
642:                assertInitialized();
643:                this .queryData.clear();
644:                this .hasQueryData = false;
645:            }
646:
647:            /**
648:             * Removes a name=value pair from the query string.
649:             *
650:             * @param name A String with the name to be removed.
651:             */
652:            public void removeQueryData(String name) {
653:                remove(QUERY_DATA, name);
654:            }
655:
656:            /**
657:             * This method takes a List of key/value arrays and converts it
658:             * into a URL encoded querystring format.
659:             *
660:             * @param data A List of key/value arrays.
661:             * @return A String with the URL encoded data.
662:             */
663:            protected String renderPathInfo(List data) {
664:                String key = null;
665:                String value = null;
666:                String tmp = null;
667:                StringBuffer out = new StringBuffer();
668:                for (Iterator iter = data.iterator(); iter.hasNext();) {
669:                    Object[] stuff = (Object[]) iter.next();
670:                    key = URLEncoder.encode((String) stuff[0]);
671:                    tmp = (String) stuff[1];
672:                    if (tmp == null || tmp.length() == 0) {
673:                        value = "null";
674:                    } else {
675:                        value = URLEncoder.encode(tmp);
676:                    }
677:
678:                    if (out.length() > 0) {
679:                        out.append("/");
680:                    }
681:                    out.append(key);
682:                    out.append("/");
683:                    out.append(value);
684:                }
685:                return out.toString();
686:            }
687:
688:            /**
689:             * This method takes a List of key/value arrays and converts it
690:             * into a URL encoded querystring format.
691:             *
692:             * @param data A List of key/value arrays.
693:             * @return A String with the URL encoded data.
694:             */
695:            protected String renderQueryString(List data) {
696:                String key = null;
697:                String value = null;
698:                String tmp = null;
699:                StringBuffer out = new StringBuffer();
700:                for (Iterator iter = data.iterator(); iter.hasNext();) {
701:                    Object[] stuff = (Object[]) iter.next();
702:                    key = URLEncoder.encode((String) stuff[0]);
703:                    tmp = (String) stuff[1];
704:                    if (tmp == null || tmp.length() == 0) {
705:                        value = "null";
706:                    } else {
707:                        value = URLEncoder.encode(tmp);
708:                    }
709:
710:                    if (out.length() > 0) {
711:                        out.append("&");
712:                    }
713:                    out.append(key);
714:                    out.append("=");
715:                    out.append(value);
716:                }
717:                return out.toString();
718:            }
719:
720:            /**
721:             * Sets the action= value for this URL.
722:             *
723:             * <p>By default it adds the information to the path_info instead
724:             * of the query data.
725:             *
726:             * @param action A String with the action value.
727:             * @return A DynamicURI (self).
728:             */
729:            public DynamicURI setAction(String action) {
730:                add(PATH_INFO, URIConstants.CGI_ACTION_PARAM, action);
731:                return this ;
732:            }
733:
734:            /**
735:             * Sets the action= value for this URL and added eventSubmit_[eventName]
736:             * to the path_info.  The value of eventSubmit_[eventName] will be
737:             * [eventName].
738:             *
739:             * @param actionName name of the action to call
740:             * @param eventName name of the event.
741:             * @return A DynamicURI (self).
742:             */
743:            public DynamicURI setActionEvent(String actionName, String eventName) {
744:                setAction(actionName).addPathInfo("eventSubmit_" + eventName,
745:                        eventName);
746:                return this ;
747:            }
748:
749:            /**
750:             * Sets the screen= value for this URL.
751:             *
752:             * <p>By default it adds the information to the path_info instead
753:             * of the query data.
754:             *
755:             * @param screen A String with the screen value.
756:             * @return A DynamicURI (self).
757:             */
758:            public DynamicURI setScreen(String screen) {
759:                add(PATH_INFO, URIConstants.CGI_SCREEN_PARAM, screen);
760:                return this ;
761:            }
762:
763:            /**
764:             * Sets the script name (/servlets/Turbine).
765:             *
766:             * @param name A String with the script name.
767:             * @return A DynamicURI (self).
768:             */
769:            public DynamicURI setScriptName(String name) {
770:                getServerData().setScriptName(name);
771:                return this ;
772:            }
773:
774:            /**
775:             * Sets the context path
776:             *
777:             * @param contextPath A String with the servlet context path
778:             * @return A DynamicURI (self).
779:             */
780:            public DynamicURI setContextPath(String contextPath) {
781:                getServerData().setContextPath(contextPath);
782:                return this ;
783:            }
784:
785:            /**
786:             * Sets the reference  (#ref).
787:             *
788:             * @param reference A String containing the reference.
789:             * @return A DynamicURI (self).
790:             */
791:            public DynamicURI setReference(String reference) {
792:                this .reference = reference;
793:                return this ;
794:            }
795:
796:            /**
797:             * Sets the server name.
798:             *
799:             * @param name A String with the server name.
800:             * @return A DynamicURI (self).
801:             */
802:            public DynamicURI setServerName(String name) {
803:                getServerData().setServerName(name);
804:                return this ;
805:            }
806:
807:            /**
808:             * Sets the server port.
809:             *
810:             * @param port An int with the port.
811:             * @return A DynamicURI (self).
812:             */
813:            public DynamicURI setServerPort(int port) {
814:                getServerData().setServerPort(port);
815:                return this ;
816:            }
817:
818:            /**
819:             * Method to specify that a URI should use SSL.  Whether or not it
820:             * does is determined from TurbineResources.properties.  Port
821:             * number is 443.
822:             *
823:             * @return A DynamicURI (self).
824:             */
825:            public DynamicURI setSecure() {
826:                return setSecure(443);
827:            }
828:
829:            /**
830:             * Method to specify that a URI should use SSL.  Whether or not it
831:             * does is determined from TurbineResources.properties.
832:             *
833:             * @param port An int with the port number.
834:             * @return A DynamicURI (self).
835:             */
836:            public DynamicURI setSecure(int port) {
837:                boolean useSSL = Turbine.getConfiguration().getBoolean(
838:                        TurbineConstants.USE_SSL_KEY,
839:                        TurbineConstants.USE_SSL_DEFAULT);
840:
841:                setServerScheme(useSSL ? URIConstants.HTTPS : URIConstants.HTTP);
842:                setServerPort(port);
843:
844:                return this ;
845:            }
846:
847:            /**
848:             * Sets the scheme (HTTP or HTTPS).
849:             *
850:             * @param scheme A String with the scheme.
851:             * @return A DynamicURI (self).
852:             */
853:            public DynamicURI setServerScheme(String scheme) {
854:                getServerData().setServerScheme(scheme);
855:                return this ;
856:            }
857:
858:            /**
859:             * Builds the URL with all of the data URL-encoded as well as
860:             * encoded using HttpServletResponse.encodeUrl().
861:             *
862:             * <p>
863:             * <code><pre>
864:             * DynamicURI dui = new DynamicURI (data, "UserScreen" );
865:             * dui.addPathInfo("user","jon");
866:             * dui.toString();
867:             * </pre></code>
868:             *
869:             *  The above call to toString() would return the String:
870:             *
871:             * <p>
872:             * http://www.server.com/servlets/Turbine/screen/UserScreen/user/jon
873:             *
874:             * @return A String with the built URL.
875:             */
876:            public String toString() {
877:                assertInitialized();
878:                StringBuffer output = new StringBuffer();
879:                output.append(getServerScheme());
880:                output.append(URIConstants.URI_SCHEME_SEPARATOR);
881:                output.append(getServerName());
882:                if ((getServerScheme().equals(URIConstants.HTTP) && getServerPort() != URIConstants.HTTP_PORT)
883:                        || (getServerScheme().equals(URIConstants.HTTPS) && getServerPort() != URIConstants.HTTPS_PORT)) {
884:                    output.append(":");
885:                    output.append(getServerPort());
886:                }
887:                output.append(getContextPath());
888:                output.append(getScriptName());
889:                if (this .hasPathInfo) {
890:                    output.append("/");
891:                    output.append(renderPathInfo(this .pathInfo));
892:                }
893:                if (this .hasQueryData) {
894:                    output.append("?");
895:                    output.append(renderQueryString(this .queryData));
896:                }
897:                if (this .reference != null) {
898:                    output.append("#");
899:                    output.append(this .getReference());
900:                }
901:
902:                // There seems to be a bug in Apache JServ 1.0 where the
903:                // session id is not appended to the end of the url when a
904:                // cookie has not been set.
905:                if (this .res != null) {
906:                    if (this .redirect)
907:                        return res.encodeRedirectURL(output.toString());
908:                    else
909:                        return res.encodeURL(output.toString());
910:                } else {
911:                    return output.toString();
912:                }
913:            }
914:
915:            /**
916:             * Given a RunData object, get a URI for the request.  This is
917:             * necessary sometimes when you want the exact URL and don't want
918:             * DynamicURI to be too smart and remove actions, screens, etc.
919:             * This also returns the Query Data where DynamicURI normally
920:             * would not.
921:             *
922:             * @param data A Turbine RunData object.
923:             * @return A String with the URL representing the RunData.
924:             */
925:            public static String toString(RunData data) {
926:                StringBuffer output = new StringBuffer();
927:                HttpServletRequest request = data.getRequest();
928:
929:                output.append(data.getServerScheme());
930:                output.append(URIConstants.URI_SCHEME_SEPARATOR);
931:                output.append(data.getServerName());
932:
933:                if ((data.getServerScheme().equals(URIConstants.HTTP) && data
934:                        .getServerPort() != URIConstants.HTTP_PORT)
935:                        || (data.getServerScheme().equals(URIConstants.HTTPS) && data
936:                                .getServerPort() != URIConstants.HTTPS_PORT)) {
937:                    output.append(":");
938:                    output.append(data.getServerPort());
939:                }
940:
941:                output.append(data.getServerData().getContextPath());
942:
943:                output.append(data.getServerData().getScriptName());
944:
945:                if (request.getPathInfo() != null) {
946:                    output.append(request.getPathInfo());
947:                }
948:
949:                if (request.getQueryString() != null) {
950:                    output.append("?");
951:                    output.append(request.getQueryString());
952:                }
953:                return output.toString();
954:            }
955:
956:            /**
957:             * Returns the ServerData used to initialize this DynamicURI.
958:             *
959:             * @return A ServerData used to initialize this DynamicURI.
960:             */
961:            public ServerData getServerData() {
962:                assertInitialized();
963:                return this .sd;
964:            }
965:
966:            /**
967:             * Sets the ServerData used to initialize this DynamicURI.
968:             *
969:             * @param serverData A ServerData used to initialize this DynamicURI.
970:             * @deprecated no replacement.  This value is set during initialization
971:             *             and should not be changed.
972:             */
973:            public void setServerData(ServerData serverData) {
974:                this .sd = serverData;
975:            }
976:
977:            /**
978:             * Verifies that one of the init() methods has been called
979:             */
980:            protected void assertInitialized() {
981:                if (!this .initialized) {
982:                    throw new IllegalStateException("Not initialized");
983:                }
984:            }
985:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.