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