View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.util.template;
18  
19  import java.lang.reflect.Method;
20  import org.apache.turbine.util.RunData;
21  import org.apache.turbine.util.DynamicURI;
22  import org.apache.turbine.services.pull.ApplicationTool;
23  import org.apache.jetspeed.services.resources.JetspeedResources;
24  
25  /***
26   * A customized version of the DynamicURI for linking to non-servlet
27   * webapp resources.
28   *
29   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
30   * @author <a href="mailto:sgala@apache.org">Santiago Gala</a>
31   * @version $Id: ContentTemplateLink.java,v 1.7 2004/02/23 03:20:45 jford Exp $
32   */
33  public class ContentTemplateLink
34      extends DynamicURI
35      implements ApplicationTool
36  {
37          
38      /*** the servlet 2.2+ webapp context */
39      private String contextPath;
40  
41      /*** the webapp relative URI to find */
42      private String pathToContent;
43  
44      /*** decide whether we should output full external URIs or simple
45      absolute URIs */
46      private boolean useExternalForm = false;
47      
48      /*** Empty Constructor for introspection */
49      public ContentTemplateLink ()
50      {
51      }
52  
53      /*** Constructor */
54      public ContentTemplateLink (RunData data)
55      {
56          super(data);
57          initForceSecure();
58          initContextPath(data);
59      }
60  
61      /***
62       * This will initialise a ContentTemplateLink object that was
63       * constructed with the default constructor (ApplicationTool
64       * method).
65       *
66       * @param data assumed to be a RunData object
67       */
68      public void init(Object data)
69      {
70          super.init((RunData)data);
71          initForceSecure();
72          initContextPath(data);
73      }
74  
75      /*** Inits the contextPath for this object 
76       *
77       * @param data the RunData to use 
78       */
79      protected void initContextPath(Object data)
80      {
81          try
82          {
83              Class runDataClass = RunData.class;
84              Method meth = runDataClass.getDeclaredMethod("getContextPath", null);
85              contextPath = (String)meth.invoke(data, null);
86          }
87          catch (Exception e)
88          {
89              /*
90               * Ignore a NoSuchMethodException because it means we are
91               * using Servlet API 2.0.  Make sure scriptName is not
92               * null.
93               */
94              contextPath = "";
95          }
96      }
97  
98      /***
99       * Inits the force secure setting.
100      */
101     protected void initForceSecure()
102     {
103         // check if we need to force to a secure (https) link
104         if (JetspeedResources.getBoolean("force.ssl", false))
105         {
106             setSecure();
107         }
108     }
109 
110     /***
111      * Refresh method - does nothing
112      */
113     public void refresh()
114     {
115         // empty
116     }
117 
118     /***
119      * Specify the link should be expressed in external form (ie 
120      * with protocol, server name and server port)
121      * @return a self reference for easy link construction in templates
122      */
123     public ContentTemplateLink getExternal() {
124         this.useExternalForm = true;
125         return this;
126     }
127     
128     /***
129      * Specify the link should be expressed in absolute form (ie 
130      * only a URI and not a full URL)
131      * @return a self reference for easy link construction in templates
132      */
133     public ContentTemplateLink getAbsolute() {
134         this.useExternalForm = false;
135         return this;
136     }
137     
138     /***
139      * Specify the webapp resource to link to.
140      *
141      * @param pathToContent the path to resource, assumed to be relative to the
142      * web application context
143      * @return a self reference for easy link construction in templates
144      */
145     public ContentTemplateLink setURI(String pathToContent)
146     {
147         this.pathToContent = pathToContent;
148         return this;
149     }
150 
151     /***
152      * Returns the URI. After rendering the URI, it clears the 
153      * pathInfo and QueryString portions of the DynamicURI.
154      *
155      * @return A String with the URI in either external or absolute form
156      */
157     public String toString()
158     {
159         
160         StringBuffer sb = new StringBuffer();
161 
162         // we want external URL form so include protocol and server name
163         if (useExternalForm)
164         {
165 	    String scheme = getServerScheme();
166             sb.append ( getServerScheme() ); //http
167             sb.append ("://");
168             sb.append (getServerName()); //www.foo.com
169 	    int port = getServerPort();
170 	    if( ( "http".equals( scheme ) && port != 80 ) ||
171 		( "https".equals( scheme ) && port != 443 ) ) { //only for non-default ports, to preserve session tracking.
172 		sb.append (":");
173 		sb.append ( port ); //port webserver running on (8080 for TDK)
174 	    }
175         }
176         //the context for tomcat adds a / so no need to add another
177         sb.append (contextPath); //the tomcat context
178         sb.append ("/");
179         if (pathToContent!=null) sb.append (pathToContent);
180 
181         // This was added to allow multilple $link variables in one
182         // template.
183         removePathInfo();
184         removeQueryData();
185         this.pathToContent=null;
186         
187         return (sb.toString());
188     }
189 
190 }