View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.headerresource;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Collection;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.apache.jetspeed.container.url.BasePortalURL;
26  import org.apache.jetspeed.request.RequestContext;
27  
28  /***
29   * HeaderResourceLib static utility methods
30   * 
31   * @author <a href="mailto:smilek@apache.org">Steve Milek</a>
32   * @version $Id: HeaderResourceLib.java 188569 2006-10-21 13:35:18Z smilek $
33   */
34  public class HeaderResourceLib
35  {
36      protected final static String EOL = "\r\n";   // html eol
37      private final static String MAILTO_URL_SCHEME = "mailto";
38      private final static int MAILTO_URL_SCHEME_LEN = MAILTO_URL_SCHEME.length();
39      
40      public static int getHeaderTypeId( String headerType )
41      {
42          int headerTypeNumber = -1;
43          if ( headerType != null )
44          {
45              if ( headerType.equals( HeaderResource.HEADER_TYPE_SCRIPT_BLOCK ) )
46              {
47                  headerTypeNumber = HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK;
48              }
49              else if ( headerType.equals( HeaderResource.HEADER_TYPE_SCRIPT_BLOCK_START ) )
50              {
51                  headerTypeNumber = HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_START;
52              }
53              else if ( headerType.equals( HeaderResource.HEADER_TYPE_SCRIPT_TAG ) )
54              {
55                  headerTypeNumber = HeaderResource.HEADER_TYPE_ID_SCRIPT_TAG;
56              }
57              else if ( headerType.equals( HeaderResource.HEADER_TYPE_SCRIPT_BLOCK_END ) )
58              {
59                  headerTypeNumber = HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_END;
60              }
61              else if ( headerType.equals( HeaderResource.HEADER_TYPE_STYLE_BLOCK ) )
62              {
63                  headerTypeNumber = HeaderResource.HEADER_TYPE_ID_STYLE_BLOCK;
64              }
65              else if ( headerType.equals( HeaderResource.HEADER_TYPE_LINK_TAG ) )
66              {
67                  headerTypeNumber = HeaderResource.HEADER_TYPE_ID_LINK_TAG;
68              }
69              else if ( headerType.equals( HeaderResource.HEADER_TYPE_BASE_TAG ) )
70              {
71                  headerTypeNumber = HeaderResource.HEADER_TYPE_ID_BASE_TAG;
72              }
73          }
74          return headerTypeNumber;
75      }
76      
77      public static String getHeaderType( Integer headerTypeId )
78      {
79          String headerType = null;
80          if ( headerTypeId != null )
81          {
82              int typeid = headerTypeId.intValue();
83              if ( typeid == HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK )
84              {
85                  headerType = HeaderResource.HEADER_TYPE_SCRIPT_BLOCK;
86              }
87              else if ( typeid == HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_START )
88              {
89                  headerType = HeaderResource.HEADER_TYPE_SCRIPT_BLOCK_START ;
90              }
91              else if ( typeid == HeaderResource.HEADER_TYPE_ID_SCRIPT_TAG )
92              {
93                  headerType = HeaderResource.HEADER_TYPE_SCRIPT_TAG;
94              }
95              else if ( typeid == HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_END )
96              {
97                  headerType = HeaderResource.HEADER_TYPE_SCRIPT_BLOCK_END;
98              }
99              else if ( typeid == HeaderResource.HEADER_TYPE_ID_STYLE_BLOCK )
100             {
101                 headerType = HeaderResource.HEADER_TYPE_STYLE_BLOCK;
102             }
103             else if ( typeid == HeaderResource.HEADER_TYPE_ID_LINK_TAG )
104             {
105                 headerType = HeaderResource.HEADER_TYPE_LINK_TAG;
106             }
107             else if ( typeid == HeaderResource.HEADER_TYPE_ID_BASE_TAG )
108             {
109                 headerType = HeaderResource.HEADER_TYPE_BASE_TAG;
110             }
111         }
112         return headerType;
113     }
114     
115     // get portal urls - these are here as an attempt to reduce as much code duplication as possible
116     //                 - some of the methods are constructed oddly due to their dual goal of reducing
117     //                   duplication while allowing for caller caching
118     
119     /***
120      * Portal base url ( e.g. http://localhost:8080/jetspeed )
121      * 
122      * @return portal base url
123      */
124     public static String getPortalBaseUrl( RequestContext requestContext )
125     {
126         return getPortalBaseUrl( requestContext, null );
127     }
128     
129     /***
130      * Portal base url ( e.g. http://localhost:8080/jetspeed )
131      * 
132      * The optional BasePortalURL argument is provided to allow the common BasePortalURL usage by various jetspeed components 
133      * to be properly supported in this url generation
134      * 
135      * @return portal base url
136      */
137     public static String getPortalBaseUrl( RequestContext requestContext, BasePortalURL baseUrlAccessOverride )
138     {
139         return getPortalBaseUrl(requestContext, baseUrlAccessOverride, false);
140     }
141     
142     /***
143      * Portal base url ( e.g. http://localhost:8080/jetspeed )
144      * 
145      * The optional BasePortalURL argument is provided to allow the common BasePortalURL usage by various jetspeed components 
146      * to be properly supported in this url generation
147      * 
148      * When the fullUrl parameter is true, the scheme, servername and port will be provided in the baseUrl,
149      * regardless if global property portalurl.relative.only is set to true in jetspeed.properties.
150      * This is needed for HeaderResourceImpl.jetspeedGenerateBasetag() for rendering a valid base tag (for which IE requires an absolute url to work).
151      * <br/>
152      * Note: if portalurl.relative.only is set to true to support a Proxy based front end, better remove de (default) "header.basetag" rendering setting
153      * from assembly/headtag.xml, otherwise the desktop still won't work properly behind the Proxy.
154      * 
155      * @return portal base url
156      */
157     public static String getPortalBaseUrl( RequestContext requestContext, BasePortalURL baseUrlAccessOverride, boolean fullUrl )
158     {
159         HttpServletRequest request = requestContext.getRequest();
160         StringBuffer baseurl = new StringBuffer();
161         if ( fullUrl || !requestContext.getPortalURL().isRelativeOnly() )
162         {
163             if ( baseUrlAccessOverride == null )
164             {
165                 baseurl.append( request.getScheme() ).append( "://" ).append( request.getServerName() ).append( ":" ).append( request.getServerPort() );
166             }
167             else
168             {
169                 baseurl.append( baseUrlAccessOverride.getServerScheme() ).append( "://" ).append( baseUrlAccessOverride.getServerName() ).append( ":" ).append( baseUrlAccessOverride.getServerPort() );
170             }
171         }
172         baseurl.append(request.getContextPath());
173         return baseurl.toString();
174     }
175     
176     /***
177      * Portal base servlet url ( e.g. http://localhost:8080/jetspeed/desktop/ )
178      * Expects portalBaseUrl argument to be defined (ie. it does not call getPortalBaseUrl)
179      * 
180      * @return portal base servlet url
181      */
182     public static String getPortalUrl( String portalBaseUrl, RequestContext requestContext )
183     {
184         HttpServletRequest request = requestContext.getRequest();
185         StringBuffer portalurl = new StringBuffer();
186         return portalurl.append( portalBaseUrl ).append( request.getServletPath() ).toString();
187     }
188     
189     /***
190      * Portal base servlet url ( e.g. http://localhost:8080/jetspeed/desktop/ )
191      * Expects portalBaseUrl argument to be defined (ie. it does not call getPortalBaseUrl)
192      * Also expects servletPath argument to be defined
193      * 
194      * @return portal base servlet url
195      */
196     public static String getPortalUrl( String portalBaseUrl, RequestContext requestContext, String servletPath )
197     {
198         HttpServletRequest request = requestContext.getRequest();
199         StringBuffer portalurl = new StringBuffer();
200         return portalurl.append( portalBaseUrl ).append( ( servletPath == null ) ? request.getServletPath() : servletPath ).toString();
201     }
202     
203     /***
204      * Portal base servlet url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/desktop/default-page.psml )
205      * Expects portalUrl argument to be defined (ie. it does not call getPortalUrl)
206      * 
207      * @return portal base servlet url with relativePath argument appended
208      */
209     public static String getPortalUrl( String relativePath, String portalUrl )
210     {
211         return getPortalUrl( relativePath, portalUrl, false, null );
212     }
213     
214     /***
215      * Portal base servlet url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/desktop/default-page.psml )
216      * Expects portalUrl argument to be defined (ie. it does not call getPortalUrl)
217      * RequestContext argument is needed only when encode argument is true (it's needed to call HttpServletResponse.encodeURL())
218      * 
219      * Method signature/behavior is a bit strange because this is a static method trying to accomodate
220      * callers that lazy cache portalUrl string
221      * 
222      * @return portal base servlet url with relativePath argument appended
223      */
224     public static String getPortalUrl( String relativePath, String portalUrl, boolean encode, RequestContext requestContext )
225     {
226         if ( relativePath == null )
227             relativePath = "";
228         if ( relativePath.indexOf( "://" ) == -1 && relativePath.indexOf( "mailto:" ) == -1 )
229         {
230             StringBuffer path = new StringBuffer();
231             String portalurl = path.append( portalUrl ).append( relativePath ).toString();
232             if ( encode && requestContext != null )
233             {
234                 return requestContext.getResponse().encodeURL( portalurl );
235             }
236             else
237             {
238                 return portalurl;
239             }
240         }
241         return relativePath;
242     }
243     
244     /***
245      * Portal base url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/javascript/dojo/ )
246      * Expects portalBaseUrl argument to be defined (ie. it does not call getPortalBaseUrl)
247      * 
248      * @return portal base url with relativePath argument appended
249      */
250     public static String getPortalResourceUrl( String relativePath, String portalBaseUrl )
251     {
252         return getPortalResourceUrl( relativePath, portalBaseUrl, false, null );
253     }
254     
255     /***
256      * Portal base url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/javascript/dojo/ )
257      * Expects portalBaseUrl argument to be defined (ie. it does not call getPortalBaseUrl)
258      * RequestContext argument is needed only when encode argument is true (it's needed to call HttpServletResponse.encodeURL())
259      * 
260      * Method signature/behavior is a bit strange because this is a static method trying to accomodate
261      * callers that lazy cache portalBaseUrl string
262      * 
263      * @return portal base url with relativePath argument appended
264      */
265     public static String getPortalResourceUrl( String relativePath, String portalBaseUrl, boolean encode, RequestContext requestContext )
266     {
267         if ( relativePath == null )
268             relativePath = "";
269         boolean isPathRelative = true;
270         int colonPos = relativePath.indexOf( ':' );
271         if ( colonPos != -1 )
272         {
273             int pathLen = relativePath.length();
274             if ( colonPos <= ( pathLen - 3 ) && relativePath.charAt( colonPos + 1 ) == '/' && relativePath.charAt( colonPos + 2 ) == '/' )
275             {
276                 isPathRelative = false;
277             }
278             else if ( colonPos >= MAILTO_URL_SCHEME_LEN && relativePath.substring( colonPos - MAILTO_URL_SCHEME_LEN, colonPos ).equals( MAILTO_URL_SCHEME ) )
279             {
280                 isPathRelative = false;
281             }
282         }
283         if ( isPathRelative )
284         {
285             StringBuffer path = new StringBuffer();
286             String resourceurl = path.append( portalBaseUrl ).append( relativePath.startsWith( "/" ) ? "" : "/" ).append( relativePath ).toString();
287             if ( encode && requestContext != null )
288             {
289                 return requestContext.getResponse().encodeURL( resourceurl );
290             }
291             else
292             {
293                 return resourceurl;
294             }
295         }
296         return relativePath;
297     }
298     
299     
300     public static String makeJavascriptStatement( String statement, String indent, boolean addEOL )
301     {
302         StringBuffer statementOut = new StringBuffer();
303         if ( statement != null )
304         {
305             statement = statement.trim();
306             if ( statement.length() > 0 )
307             {
308                 if ( indent != null )
309                 {
310                     statementOut.append( indent );
311                 }
312                 statementOut.append( statement );
313                 if ( statement.charAt( statement.length()-1 ) != ';' )
314                 {
315                     statementOut.append( ";" );
316                 }
317                 if ( addEOL )
318                 {
319                     statementOut.append( EOL );
320                 }
321             }
322         }
323         return statementOut.toString();
324     }
325     public static String makeJSONStringArray( Collection stringList )
326     {
327         return makeJSONStringArray( stringList, null );
328     }
329     public static String makeJSONStringArray( Collection stringList, List compiledUniqueValues )
330     {
331         if ( stringList != null && stringList.size() > 0 )
332         {
333             StringBuffer stringListContent = new StringBuffer();
334             Iterator stringListIter = stringList.iterator();
335             while ( stringListIter.hasNext() )
336             {
337                 String value = (String)stringListIter.next();
338                 if ( value != null && value.length() > 0 )
339                 {
340                     if ( stringListContent.length() > 0 )
341                     {
342                         stringListContent.append( ", " );
343                     }
344                     else
345                     {
346                         stringListContent.append( "[ " );
347                     }
348                     stringListContent.append( "\"" ).append( value ).append( "\"" );
349                     if ( compiledUniqueValues != null )
350                     {
351                         if ( ! compiledUniqueValues.contains( value ) )
352                         {
353                             compiledUniqueValues.add( value );
354                         }
355                     }
356                 }
357             }
358             if ( stringListContent.length() > 0 )
359             {
360                 stringListContent.append( " ]" );
361                 return stringListContent.toString();
362             }
363         }
364         return null;
365     }
366     public static String makeJSONInteger( Object source, boolean quote )
367     {
368         String sourceStr = ( ( source == null ) ? (String)null : source.toString() );
369         if ( sourceStr != null )
370         {
371             try
372             {
373                 Integer.parseInt( sourceStr );
374                 if ( quote )
375                 {
376                     sourceStr = "\"" + sourceStr + "\"";
377                 }
378             }
379             catch ( NumberFormatException nex )
380             {
381                 sourceStr = null;
382             }
383         }
384         return sourceStr;
385     }
386     
387     public static String makeJSONBoolean( Object source )
388     {
389         String boolStr = ( ( source == null ) ? (String)null : source.toString() );
390         if ( boolStr != null && ( ! boolStr.equals( "false" ) ) && ( ! boolStr.equals( "true" ) ) )
391         {
392             boolStr = null;
393         }
394         return boolStr;
395     }
396 }