001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.headerresource;
018:
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Collection;
023:
024: import javax.servlet.http.HttpServletRequest;
025:
026: import org.apache.jetspeed.container.url.BasePortalURL;
027: import org.apache.jetspeed.request.RequestContext;
028:
029: /**
030: * HeaderResourceLib static utility methods
031: *
032: * @author <a href="mailto:smilek@apache.org">Steve Milek</a>
033: * @version $Id: HeaderResourceLib.java 188569 2006-10-21 13:35:18Z smilek $
034: */
035: public class HeaderResourceLib {
036: protected final static String EOL = "\r\n"; // html eol
037: private final static String MAILTO_URL_SCHEME = "mailto";
038: private final static int MAILTO_URL_SCHEME_LEN = MAILTO_URL_SCHEME
039: .length();
040:
041: public static int getHeaderTypeId(String headerType) {
042: int headerTypeNumber = -1;
043: if (headerType != null) {
044: if (headerType
045: .equals(HeaderResource.HEADER_TYPE_SCRIPT_BLOCK)) {
046: headerTypeNumber = HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK;
047: } else if (headerType
048: .equals(HeaderResource.HEADER_TYPE_SCRIPT_BLOCK_START)) {
049: headerTypeNumber = HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_START;
050: } else if (headerType
051: .equals(HeaderResource.HEADER_TYPE_SCRIPT_TAG)) {
052: headerTypeNumber = HeaderResource.HEADER_TYPE_ID_SCRIPT_TAG;
053: } else if (headerType
054: .equals(HeaderResource.HEADER_TYPE_SCRIPT_BLOCK_END)) {
055: headerTypeNumber = HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_END;
056: } else if (headerType
057: .equals(HeaderResource.HEADER_TYPE_STYLE_BLOCK)) {
058: headerTypeNumber = HeaderResource.HEADER_TYPE_ID_STYLE_BLOCK;
059: } else if (headerType
060: .equals(HeaderResource.HEADER_TYPE_LINK_TAG)) {
061: headerTypeNumber = HeaderResource.HEADER_TYPE_ID_LINK_TAG;
062: } else if (headerType
063: .equals(HeaderResource.HEADER_TYPE_BASE_TAG)) {
064: headerTypeNumber = HeaderResource.HEADER_TYPE_ID_BASE_TAG;
065: }
066: }
067: return headerTypeNumber;
068: }
069:
070: public static String getHeaderType(Integer headerTypeId) {
071: String headerType = null;
072: if (headerTypeId != null) {
073: int typeid = headerTypeId.intValue();
074: if (typeid == HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK) {
075: headerType = HeaderResource.HEADER_TYPE_SCRIPT_BLOCK;
076: } else if (typeid == HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_START) {
077: headerType = HeaderResource.HEADER_TYPE_SCRIPT_BLOCK_START;
078: } else if (typeid == HeaderResource.HEADER_TYPE_ID_SCRIPT_TAG) {
079: headerType = HeaderResource.HEADER_TYPE_SCRIPT_TAG;
080: } else if (typeid == HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_END) {
081: headerType = HeaderResource.HEADER_TYPE_SCRIPT_BLOCK_END;
082: } else if (typeid == HeaderResource.HEADER_TYPE_ID_STYLE_BLOCK) {
083: headerType = HeaderResource.HEADER_TYPE_STYLE_BLOCK;
084: } else if (typeid == HeaderResource.HEADER_TYPE_ID_LINK_TAG) {
085: headerType = HeaderResource.HEADER_TYPE_LINK_TAG;
086: } else if (typeid == HeaderResource.HEADER_TYPE_ID_BASE_TAG) {
087: headerType = HeaderResource.HEADER_TYPE_BASE_TAG;
088: }
089: }
090: return headerType;
091: }
092:
093: // get portal urls - these are here as an attempt to reduce as much code duplication as possible
094: // - some of the methods are constructed oddly due to their dual goal of reducing
095: // duplication while allowing for caller caching
096:
097: /**
098: * Portal base url ( e.g. http://localhost:8080/jetspeed )
099: *
100: * @return portal base url
101: */
102: public static String getPortalBaseUrl(RequestContext requestContext) {
103: return getPortalBaseUrl(requestContext, null);
104: }
105:
106: /**
107: * Portal base url ( e.g. http://localhost:8080/jetspeed )
108: *
109: * The optional BasePortalURL argument is provided to allow the common BasePortalURL usage by various jetspeed components
110: * to be properly supported in this url generation
111: *
112: * @return portal base url
113: */
114: public static String getPortalBaseUrl(
115: RequestContext requestContext,
116: BasePortalURL baseUrlAccessOverride) {
117: return getPortalBaseUrl(requestContext, baseUrlAccessOverride,
118: false);
119: }
120:
121: /**
122: * Portal base url ( e.g. http://localhost:8080/jetspeed )
123: *
124: * The optional BasePortalURL argument is provided to allow the common BasePortalURL usage by various jetspeed components
125: * to be properly supported in this url generation
126: *
127: * When the fullUrl parameter is true, the scheme, servername and port will be provided in the baseUrl,
128: * regardless if global property portalurl.relative.only is set to true in jetspeed.properties.
129: * This is needed for HeaderResourceImpl.jetspeedGenerateBasetag() for rendering a valid base tag (for which IE requires an absolute url to work).
130: * <br/>
131: * Note: if portalurl.relative.only is set to true to support a Proxy based front end, better remove de (default) "header.basetag" rendering setting
132: * from assembly/headtag.xml, otherwise the desktop still won't work properly behind the Proxy.
133: *
134: * @return portal base url
135: */
136: public static String getPortalBaseUrl(
137: RequestContext requestContext,
138: BasePortalURL baseUrlAccessOverride, boolean fullUrl) {
139: HttpServletRequest request = requestContext.getRequest();
140: StringBuffer baseurl = new StringBuffer();
141: if (fullUrl || !requestContext.getPortalURL().isRelativeOnly()) {
142: if (baseUrlAccessOverride == null) {
143: baseurl.append(request.getScheme()).append("://")
144: .append(request.getServerName()).append(":")
145: .append(request.getServerPort());
146: } else {
147: baseurl.append(baseUrlAccessOverride.getServerScheme())
148: .append("://").append(
149: baseUrlAccessOverride.getServerName())
150: .append(":").append(
151: baseUrlAccessOverride.getServerPort());
152: }
153: }
154: baseurl.append(request.getContextPath());
155: return baseurl.toString();
156: }
157:
158: /**
159: * Portal base servlet url ( e.g. http://localhost:8080/jetspeed/desktop/ )
160: * Expects portalBaseUrl argument to be defined (ie. it does not call getPortalBaseUrl)
161: *
162: * @return portal base servlet url
163: */
164: public static String getPortalUrl(String portalBaseUrl,
165: RequestContext requestContext) {
166: HttpServletRequest request = requestContext.getRequest();
167: StringBuffer portalurl = new StringBuffer();
168: return portalurl.append(portalBaseUrl).append(
169: request.getServletPath()).toString();
170: }
171:
172: /**
173: * Portal base servlet url ( e.g. http://localhost:8080/jetspeed/desktop/ )
174: * Expects portalBaseUrl argument to be defined (ie. it does not call getPortalBaseUrl)
175: * Also expects servletPath argument to be defined
176: *
177: * @return portal base servlet url
178: */
179: public static String getPortalUrl(String portalBaseUrl,
180: RequestContext requestContext, String servletPath) {
181: HttpServletRequest request = requestContext.getRequest();
182: StringBuffer portalurl = new StringBuffer();
183: return portalurl.append(portalBaseUrl).append(
184: (servletPath == null) ? request.getServletPath()
185: : servletPath).toString();
186: }
187:
188: /**
189: * Portal base servlet url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/desktop/default-page.psml )
190: * Expects portalUrl argument to be defined (ie. it does not call getPortalUrl)
191: *
192: * @return portal base servlet url with relativePath argument appended
193: */
194: public static String getPortalUrl(String relativePath,
195: String portalUrl) {
196: return getPortalUrl(relativePath, portalUrl, false, null);
197: }
198:
199: /**
200: * Portal base servlet url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/desktop/default-page.psml )
201: * Expects portalUrl argument to be defined (ie. it does not call getPortalUrl)
202: * RequestContext argument is needed only when encode argument is true (it's needed to call HttpServletResponse.encodeURL())
203: *
204: * Method signature/behavior is a bit strange because this is a static method trying to accomodate
205: * callers that lazy cache portalUrl string
206: *
207: * @return portal base servlet url with relativePath argument appended
208: */
209: public static String getPortalUrl(String relativePath,
210: String portalUrl, boolean encode,
211: RequestContext requestContext) {
212: if (relativePath == null)
213: relativePath = "";
214: if (relativePath.indexOf("://") == -1
215: && relativePath.indexOf("mailto:") == -1) {
216: StringBuffer path = new StringBuffer();
217: String portalurl = path.append(portalUrl).append(
218: relativePath).toString();
219: if (encode && requestContext != null) {
220: return requestContext.getResponse()
221: .encodeURL(portalurl);
222: } else {
223: return portalurl;
224: }
225: }
226: return relativePath;
227: }
228:
229: /**
230: * Portal base url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/javascript/dojo/ )
231: * Expects portalBaseUrl argument to be defined (ie. it does not call getPortalBaseUrl)
232: *
233: * @return portal base url with relativePath argument appended
234: */
235: public static String getPortalResourceUrl(String relativePath,
236: String portalBaseUrl) {
237: return getPortalResourceUrl(relativePath, portalBaseUrl, false,
238: null);
239: }
240:
241: /**
242: * Portal base url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/javascript/dojo/ )
243: * Expects portalBaseUrl argument to be defined (ie. it does not call getPortalBaseUrl)
244: * RequestContext argument is needed only when encode argument is true (it's needed to call HttpServletResponse.encodeURL())
245: *
246: * Method signature/behavior is a bit strange because this is a static method trying to accomodate
247: * callers that lazy cache portalBaseUrl string
248: *
249: * @return portal base url with relativePath argument appended
250: */
251: public static String getPortalResourceUrl(String relativePath,
252: String portalBaseUrl, boolean encode,
253: RequestContext requestContext) {
254: if (relativePath == null)
255: relativePath = "";
256: boolean isPathRelative = true;
257: int colonPos = relativePath.indexOf(':');
258: if (colonPos != -1) {
259: int pathLen = relativePath.length();
260: if (colonPos <= (pathLen - 3)
261: && relativePath.charAt(colonPos + 1) == '/'
262: && relativePath.charAt(colonPos + 2) == '/') {
263: isPathRelative = false;
264: } else if (colonPos >= MAILTO_URL_SCHEME_LEN
265: && relativePath.substring(
266: colonPos - MAILTO_URL_SCHEME_LEN, colonPos)
267: .equals(MAILTO_URL_SCHEME)) {
268: isPathRelative = false;
269: }
270: }
271: if (isPathRelative) {
272: StringBuffer path = new StringBuffer();
273: String resourceurl = path.append(portalBaseUrl).append(
274: relativePath.startsWith("/") ? "" : "/").append(
275: relativePath).toString();
276: if (encode && requestContext != null) {
277: return requestContext.getResponse().encodeURL(
278: resourceurl);
279: } else {
280: return resourceurl;
281: }
282: }
283: return relativePath;
284: }
285:
286: public static StringBuffer makeJSONObject(Map objectMap,
287: boolean whenEmptyReturnNewObject) {
288: return makeJSONObject(null, new Map[] { objectMap },
289: whenEmptyReturnNewObject);
290: }
291:
292: public static StringBuffer makeJSONObject(Map[] objectMaps,
293: boolean whenEmptyReturnNewObject) {
294: return makeJSONObject(null, objectMaps,
295: whenEmptyReturnNewObject);
296: }
297:
298: public static StringBuffer makeJSONObject(StringBuffer jsonBuffer,
299: Map objectMap, boolean whenEmptyReturnNewObject) {
300: return makeJSONObject(jsonBuffer, new Map[] { objectMap },
301: whenEmptyReturnNewObject);
302: }
303:
304: public static StringBuffer makeJSONObject(StringBuffer jsonBuffer,
305: Map[] objectMaps, boolean whenEmptyReturnNewObject) {
306: if (jsonBuffer == null)
307: jsonBuffer = new StringBuffer();
308:
309: int added = 0;
310: int objMapsLen = (objectMaps == null ? 0 : objectMaps.length);
311: if (objMapsLen > 0) {
312: for (int i = 0; i < objMapsLen; i++) {
313: Map objectMap = objectMaps[i];
314: if (objectMap != null && objectMap.size() > 0) {
315: if (added == 0)
316: jsonBuffer.append("{");
317: Map.Entry objEntry;
318: Object objKey, objVal;
319: Iterator objMapIter = objectMap.entrySet()
320: .iterator();
321: while (objMapIter.hasNext()) {
322: objEntry = (Map.Entry) objMapIter.next();
323: objKey = objEntry.getKey();
324: if (objKey != null) {
325: if (added > 0)
326: jsonBuffer.append(", ");
327: jsonBuffer.append("\"").append(
328: objKey.toString()).append("\":");
329: objVal = objEntry.getValue();
330: if (objVal == null)
331: objVal = "";
332: jsonBuffer.append("\"").append(
333: objVal.toString()).append("\"");
334: added++;
335: }
336: }
337: }
338: }
339: }
340: if (added > 0) {
341: jsonBuffer.append("}");
342: } else if (whenEmptyReturnNewObject) {
343: jsonBuffer.append("{}");
344: } else {
345: return null;
346: }
347: return jsonBuffer;
348: }
349:
350: public static String makeJavascriptStatement(String statement,
351: String indent, boolean addEOL) {
352: StringBuffer statementOut = new StringBuffer();
353: if (statement != null) {
354: statement = statement.trim();
355: if (statement.length() > 0) {
356: if (indent != null) {
357: statementOut.append(indent);
358: }
359: statementOut.append(statement);
360: if (statement.charAt(statement.length() - 1) != ';') {
361: statementOut.append(";");
362: }
363: if (addEOL) {
364: statementOut.append(EOL);
365: }
366: }
367: }
368: return statementOut.toString();
369: }
370:
371: public static String makeJSONStringArray(Collection stringList) {
372: return makeJSONStringArray(stringList, null);
373: }
374:
375: public static String makeJSONStringArray(Collection stringList,
376: List compiledUniqueValues) {
377: if (stringList != null && stringList.size() > 0) {
378: StringBuffer stringListContent = new StringBuffer();
379: Iterator stringListIter = stringList.iterator();
380: while (stringListIter.hasNext()) {
381: String value = (String) stringListIter.next();
382: if (value != null && value.length() > 0) {
383: if (stringListContent.length() > 0) {
384: stringListContent.append(", ");
385: } else {
386: stringListContent.append("[ ");
387: }
388: stringListContent.append("\"").append(value)
389: .append("\"");
390: if (compiledUniqueValues != null) {
391: if (!compiledUniqueValues.contains(value)) {
392: compiledUniqueValues.add(value);
393: }
394: }
395: }
396: }
397: if (stringListContent.length() > 0) {
398: stringListContent.append(" ]");
399: return stringListContent.toString();
400: }
401: }
402: return null;
403: }
404:
405: public static String makeJSONInteger(Object source, boolean quote) {
406: String sourceStr = ((source == null) ? (String) null : source
407: .toString());
408: if (sourceStr != null) {
409: try {
410: Integer.parseInt(sourceStr);
411: if (quote) {
412: sourceStr = "\"" + sourceStr + "\"";
413: }
414: } catch (NumberFormatException nex) {
415: sourceStr = null;
416: }
417: }
418: return sourceStr;
419: }
420:
421: public static String makeJSONBoolean(Object source) {
422: String boolStr = ((source == null) ? (String) null : source
423: .toString());
424: if (boolStr != null && (!boolStr.equals("false"))
425: && (!boolStr.equals("true"))) {
426: boolStr = null;
427: }
428: return boolStr;
429: }
430: }
|