001: /*
002: * Copyright 2000-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018:
019: */
020:
021: package org.apache.wsrp4j.producer.provider.driver;
022:
023: import java.net.URLEncoder;
024:
025: import oasis.names.tc.wsrp.v1.types.PortletContext;
026: import oasis.names.tc.wsrp.v1.types.RuntimeContext;
027: import oasis.names.tc.wsrp.v1.types.Templates;
028: import oasis.names.tc.wsrp.v1.types.UserContext;
029:
030: import org.apache.wsrp4j.exception.ErrorCodes;
031: import org.apache.wsrp4j.exception.WSRPException;
032: import org.apache.wsrp4j.exception.WSRPXHelper;
033: import org.apache.wsrp4j.log.LogManager;
034: import org.apache.wsrp4j.log.Logger;
035: import org.apache.wsrp4j.producer.provider.Provider;
036: import org.apache.wsrp4j.producer.provider.URLComposer;
037: import org.apache.wsrp4j.util.Constants;
038:
039: /**
040: * <p>This class implements the URLComposer interface providing methods to generate
041: * URLs or namespace tokens.</p>
042: * <p>There are two kinds of methods:
043: * <ul>
044: * <li>Methods defining URLs or tokens to be rewritten by the consumer.</li>
045: * <li>Methods defining complete URLs or namespaced tokens according to the
046: * templates delivered by the Consumer.</li></p>
047: *
048: * @author <a href="mailto:stefan.behl@de.ibm.com">Stefan Behl</a>
049: */
050: public class URLComposerImpl implements URLComposer {
051: // the provider implementation
052: private Provider provider = null;
053:
054: // for logging and exception support
055: private Logger logger = LogManager.getLogManager().getLogger(
056: this .getClass());
057:
058: private URLComposerImpl() {
059: }
060:
061: public static URLComposerImpl getInstance(Provider provider) {
062: URLComposerImpl urlComposer = new URLComposerImpl();
063: urlComposer.provider = provider;
064:
065: return urlComposer;
066: }
067:
068: // methods generating rewrite urls
069:
070: /**
071: * creates a rewrite blocking-action-url
072: *
073: * @param portletMode
074: * @param navigationalState
075: * @param interactionState
076: * @param windowState
077: * @param secureURL
078: * @param runtimeContext
079: * @param portletContext
080: * @param userContext
081: *
082: * @return URL as String
083: */
084: public String createBlockingActionURL(String portletMode,
085: String navigationalState, String interactionState,
086: String windowState, boolean secureURL,
087: RuntimeContext runtimeContext,
088: PortletContext portletContext, UserContext userContext) {
089: String MN = "createBlockingActionURL";
090: if (logger.isLogging(Logger.TRACE_HIGH)) {
091: logger.entry(Logger.TRACE_HIGH, MN);
092: }
093:
094: // check if valid templates are provided
095: if (runtimeContext != null) {
096:
097: Templates templates = runtimeContext.getTemplates();
098: if (templates != null) {
099:
100: String template = null;
101: if (secureURL) {
102:
103: template = templates
104: .getSecureBlockingActionTemplate();
105: if (template == null)
106: template = templates.getSecureDefaultTemplate();
107:
108: } else {
109:
110: template = templates.getBlockingActionTemplate();
111: if (template == null)
112: template = templates.getDefaultTemplate();
113: }
114:
115: try {
116: if (template != null) {
117:
118: if (logger.isLogging(Logger.TRACE_HIGH)) {
119: logger.exit(Logger.TRACE_HIGH, MN);
120: }
121:
122: return this .createBlockingActionURLByTemplate(
123: template, portletMode,
124: navigationalState, interactionState,
125: windowState,
126: portletContext == null ? null
127: : portletContext
128: .getPortletHandle(),
129: userContext == null ? null
130: : userContext
131: .getUserContextKey(),
132: runtimeContext.getPortletInstanceKey(),
133: runtimeContext.getSessionID());
134: }
135: } catch (WSRPException e) {
136:
137: // TODO: pass exception to consumer
138: }
139: }
140: }
141:
142: if (logger.isLogging(Logger.TRACE_HIGH)) {
143: logger.exit(Logger.TRACE_HIGH, MN);
144: }
145:
146: return createRewriteURL(Constants.URL_TYPE_BLOCKINGACTION,
147: portletMode, navigationalState, interactionState,
148: windowState, null, null, null, java.lang.String
149: .valueOf(secureURL));
150: }
151:
152: /**
153: * creates a rewrite render-url
154: */
155: public String createRenderURL(String portletMode,
156: String navigationalState, String windowState,
157: boolean secureURL, RuntimeContext runtimeContext,
158: PortletContext portletContext, UserContext userContext) {
159: String MN = "createRenderURL";
160: if (logger.isLogging(Logger.TRACE_HIGH)) {
161: logger.entry(Logger.TRACE_HIGH, MN);
162: }
163:
164: // check if valid templates are provided
165: if (runtimeContext != null) {
166:
167: Templates templates = runtimeContext.getTemplates();
168: if (templates != null) {
169:
170: String template = null;
171: if (secureURL) {
172:
173: template = templates.getSecureRenderTemplate();
174: if (template == null)
175: template = templates.getDefaultTemplate();
176:
177: } else {
178:
179: template = templates.getRenderTemplate();
180: if (template == null)
181: template = templates.getDefaultTemplate();
182: }
183:
184: try {
185: if (template != null) {
186:
187: if (logger.isLogging(Logger.TRACE_HIGH)) {
188: logger.exit(Logger.TRACE_HIGH, MN);
189: }
190:
191: return this .createRenderURLByTemplate(template,
192: portletMode, navigationalState,
193: windowState,
194: portletContext == null ? null
195: : portletContext
196: .getPortletHandle(),
197: userContext == null ? null
198: : userContext
199: .getUserContextKey(),
200: runtimeContext.getPortletInstanceKey(),
201: runtimeContext.getSessionID());
202: }
203: } catch (WSRPException e) {
204:
205: // TODO: pass exception to consumer
206:
207: }
208: }
209: }
210:
211: if (logger.isLogging(Logger.TRACE_HIGH)) {
212: logger.exit(Logger.TRACE_HIGH, MN);
213: }
214:
215: return createRewriteURL(Constants.URL_TYPE_RENDER, portletMode,
216: navigationalState, null, windowState, null, null, null,
217: java.lang.String.valueOf(secureURL));
218: }
219:
220: /**
221: * creates a rewrite resource-url
222: */
223: public String createResourceURL(String url,
224: boolean rewriteResource, boolean secureURL,
225: RuntimeContext runtimeContext,
226: PortletContext portletContext, UserContext userContext) {
227: String MN = "createResourceURL";
228: if (logger.isLogging(Logger.TRACE_HIGH)) {
229: logger.entry(Logger.TRACE_HIGH, MN);
230: }
231:
232: // check if valid templates are provided
233: if (runtimeContext != null) {
234:
235: Templates templates = runtimeContext.getTemplates();
236: if (templates != null) {
237:
238: String template = null;
239: if (secureURL) {
240:
241: template = templates.getSecureResourceTemplate();
242: if (template == null)
243: template = templates.getDefaultTemplate();
244:
245: } else {
246:
247: template = templates.getResourceTemplate();
248: if (template == null)
249: template = templates.getDefaultTemplate();
250: }
251:
252: try {
253: if (template != null) {
254:
255: if (logger.isLogging(Logger.TRACE_HIGH)) {
256: logger.exit(Logger.TRACE_HIGH, MN);
257: }
258:
259: return this .createResourceURLByTemplate(
260: template, url, rewriteResource,
261: portletContext == null ? null
262: : portletContext
263: .getPortletHandle(),
264: userContext == null ? null
265: : userContext
266: .getUserContextKey(),
267: runtimeContext.getPortletInstanceKey(),
268: runtimeContext.getSessionID());
269: }
270:
271: } catch (WSRPException e) {
272:
273: // TODO: pass exception to consumer
274:
275: }
276: }
277: }
278:
279: if (logger.isLogging(Logger.TRACE_HIGH)) {
280: logger.exit(Logger.TRACE_HIGH, MN);
281: }
282:
283: return createRewriteURL(Constants.URL_TYPE_RESOURCE, null,
284: null, null, null, url, java.lang.String
285: .valueOf(rewriteResource), null,
286: java.lang.String.valueOf(secureURL));
287: }
288:
289: /**
290: * creates a namespaced token
291: */
292: public String createNamespacedToken(String token,
293: RuntimeContext runtimeContext) {
294: String MN = "createNamespacedToken";
295: if (logger.isLogging(Logger.TRACE_HIGH)) {
296: logger.entry(Logger.TRACE_HIGH, MN);
297: }
298:
299: String nsToken = null;
300:
301: // first get the prefix
302: // this is either the prefix passed in the RuntimeContext
303: if (runtimeContext != null) {
304: nsToken = runtimeContext.getNamespacePrefix();
305: }
306: // no namespace prefix obtained?
307: // then need to put in the rewrite NS wsrp_rewrite_
308: if (nsToken == null) {
309: nsToken = Constants.REWRITE_START
310: + Constants.NAMESPACE_START;
311: }
312:
313: // append the token
314: // if token is null, simply return the namespace
315: if (token != null) {
316: nsToken = nsToken.concat(token);
317: }
318:
319: if (logger.isLogging(Logger.TRACE_HIGH)) {
320: logger.exit(Logger.TRACE_HIGH, MN);
321: }
322:
323: return nsToken;
324: }
325:
326: private String createRewriteURL(String urlType, String portletMode,
327: String navigationalState, String interactionState,
328: String windowState, String url, String rewriteResource,
329: String token, String secureURL) {
330:
331: String MN = "createRewriteURL()";
332: if (logger.isLogging(Logger.TRACE_HIGH)) {
333: logger.entry(Logger.TRACE_HIGH, MN,
334: new Object[] { urlType, portletMode,
335: navigationalState, interactionState,
336: windowState, url, rewriteResource, token,
337: secureURL });
338: }
339:
340: StringBuffer resultURL = new StringBuffer();
341:
342: resultURL.append(Constants.REWRITE_START);
343: resultURL.append(Constants.PARAMS_START);
344:
345: // urlType must be first (required)
346: if (urlType != null) {
347: resultURL.append(appendNameValuePair(Constants.URL_TYPE,
348: urlType));
349: } else {
350:
351: //TODO: Error
352: }
353:
354: // portletMode
355: if (portletMode != null && portletMode.length() > 0) {
356: resultURL.append(Constants.NEXT_PARAM_AMP);
357: resultURL.append(appendNameValuePair(
358: Constants.PORTLET_MODE, portletMode));
359: }
360:
361: // navigationalState
362: if (navigationalState != null && navigationalState.length() > 0) {
363: resultURL.append(Constants.NEXT_PARAM_AMP);
364: resultURL.append(appendNameValuePair(
365: Constants.NAVIGATIONAL_STATE, navigationalState));
366: }
367:
368: // interactionState
369: if (interactionState != null && interactionState.length() > 0) {
370: resultURL.append(Constants.NEXT_PARAM_AMP);
371: resultURL.append(appendNameValuePair(
372: Constants.INTERACTION_STATE, interactionState));
373: }
374:
375: // windowState
376: if (windowState != null && windowState.length() > 0) {
377: resultURL.append(Constants.NEXT_PARAM_AMP);
378: resultURL.append(appendNameValuePair(
379: Constants.WINDOW_STATE, windowState));
380: }
381:
382: // url
383: if (url != null && url.length() > 0) {
384: resultURL.append(Constants.NEXT_PARAM_AMP);
385: String fileEncoding = System.getProperty("file.encoding",
386: "UTF-8");
387: try {
388: resultURL.append(appendNameValuePair(Constants.URL,
389: URLEncoder.encode(url, fileEncoding)));
390: } catch (Exception e) {
391: logger.text(Logger.ERROR, MN, e,
392: "Encoding not supported.");
393: }
394: }
395:
396: // rewriteResource
397: if (rewriteResource != null && rewriteResource.length() > 0) {
398: resultURL.append(Constants.NEXT_PARAM_AMP);
399: resultURL.append(appendNameValuePair(
400: Constants.REWRITE_RESOURCE, rewriteResource));
401: }
402:
403: // secureURL
404: if (secureURL != null && secureURL.length() > 0) {
405: resultURL.append(Constants.NEXT_PARAM_AMP);
406: resultURL.append(appendNameValuePair(Constants.SECURE_URL,
407: secureURL));
408: }
409:
410: resultURL.append(Constants.REWRITE_END);
411:
412: if (logger.isLogging(Logger.TRACE_HIGH)) {
413: logger.exit(Logger.TRACE_HIGH, MN);
414: }
415:
416: String result = resultURL.toString();
417:
418: if (logger.isLogging(Logger.TRACE_HIGH)) {
419: logger.exit(Logger.TRACE_HIGH, MN, result);
420: }
421:
422: return result;
423:
424: }
425:
426: private String createNameValuePair(String name, String value) {
427:
428: StringBuffer pair = new StringBuffer(name);
429: pair.append(Constants.EQUALS);
430: pair.append(value);
431:
432: return pair.toString();
433: }
434:
435: private String appendNameValuePair(String name, String value) {
436: return createNameValuePair(name, value);
437: }
438:
439: // methods generating urls by template processing
440:
441: private String createBlockingActionURLByTemplate(
442: String blockingActionTemplate, String portletMode,
443: String navigationalState, String interactionState,
444: String windowState) throws WSRPException {
445:
446: return processTemplate(blockingActionTemplate,
447: Constants.URL_TYPE_BLOCKINGACTION, portletMode,
448: navigationalState, interactionState, windowState, null,
449: null, null, null, null, null);
450:
451: }
452:
453: private String createBlockingActionURLByTemplate(
454: String blockingActionTemplate, String portletMode,
455: String navigationalState, String interactionState,
456: String windowState, String portletHandle,
457: String userContextKey, String portletInstanceKey,
458: String sessionID) throws WSRPException {
459:
460: return processTemplate(blockingActionTemplate,
461: Constants.URL_TYPE_BLOCKINGACTION, portletMode,
462: navigationalState, interactionState, windowState, null,
463: null, portletHandle, userContextKey,
464: portletInstanceKey, sessionID);
465:
466: }
467:
468: private String createRenderURLByTemplate(String renderTemplate,
469: String portletMode, String navigationalState,
470: String windowState) throws WSRPException {
471:
472: return processTemplate(renderTemplate,
473: Constants.URL_TYPE_RENDER, portletMode,
474: navigationalState, null, windowState, null, null, null,
475: null, null, null);
476: }
477:
478: private String createRenderURLByTemplate(String renderTemplate,
479: String portletMode, String navigationalState,
480: String windowState, String portletHandle,
481: String userContextKey, String portletInstanceKey,
482: String sessionID) throws WSRPException {
483:
484: return processTemplate(renderTemplate,
485: Constants.URL_TYPE_RENDER, portletMode,
486: navigationalState, null, windowState, null, null,
487: portletHandle, userContextKey, portletInstanceKey,
488: sessionID);
489:
490: }
491:
492: private String createResourceURLByTemplate(String resourceTemplate,
493: String url, boolean rewriteResource) throws WSRPException {
494:
495: return processTemplate(resourceTemplate,
496: Constants.URL_TYPE_RESOURCE, null, null, null, null,
497: url, java.lang.String.valueOf(rewriteResource), null,
498: null, null, null);
499:
500: }
501:
502: private String createResourceURLByTemplate(String resourceTemplate,
503: String url, boolean rewriteResource, String portletHandle,
504: String userContextKey, String portletInstanceKey,
505: String sessionID) throws WSRPException {
506:
507: return processTemplate(resourceTemplate,
508: Constants.URL_TYPE_RESOURCE, null, null, null, null,
509: url, java.lang.String.valueOf(rewriteResource),
510: portletHandle, userContextKey, portletInstanceKey,
511: sessionID);
512:
513: }
514:
515: private String processTemplate(String template, String urlType,
516: String portletMode, String navigationalState,
517: String interactionState, String windowState, String url,
518: String rewriteResource, String portletHandle,
519: String userContextKey, String portletInstanceKey,
520: String sessionID) throws WSRPException {
521:
522: String MN = "processTemplate";
523: if (logger.isLogging(Logger.TRACE_HIGH)) {
524: logger.entry(Logger.TRACE_HIGH, MN);
525: }
526:
527: String replaceToken = "";
528:
529: // Any template containing "{" or "}" will be processed.
530: while ((template.indexOf(Constants.REPLACE_START) != -1)
531: | (template.indexOf(Constants.REPLACE_END) != -1)) {
532:
533: StringBuffer templateBuffer = new StringBuffer(template);
534:
535: // get positions of next left and right brackets
536: int left_br = template.indexOf(Constants.REPLACE_START);
537: int right_br = template.indexOf(Constants.REPLACE_END) + 1;
538:
539: // check if bracketing is correct
540: if ((left_br > right_br) | (left_br == -1)
541: | (right_br == -1)) {
542:
543: WSRPXHelper.throwX(logger, Logger.ERROR, MN,
544: ErrorCodes.SYNTAX_ERROR_IN_TEMPLATE);
545:
546: }
547:
548: // get replaceToken without curly brackets enclosing it
549: replaceToken = templateBuffer.substring(left_br + 1,
550: right_br - 1);
551:
552: // urlType
553: if (replaceToken.equalsIgnoreCase(Constants.URL_TYPE)) {
554: if (urlType == null) {
555: urlType = "";
556: }
557: templateBuffer.replace(left_br, right_br, urlType);
558: }
559:
560: // portletMode
561: else if (replaceToken
562: .equalsIgnoreCase(Constants.PORTLET_MODE)) {
563: if (portletMode == null) {
564: portletMode = "";
565: }
566: templateBuffer.replace(left_br, right_br, portletMode);
567: }
568:
569: // navigationalState
570: else if (replaceToken
571: .equalsIgnoreCase(Constants.NAVIGATIONAL_STATE)) {
572: if (navigationalState == null) {
573: navigationalState = "";
574: }
575: templateBuffer.replace(left_br, right_br,
576: navigationalState);
577: }
578:
579: // interactionState
580: else if (replaceToken
581: .equalsIgnoreCase(Constants.INTERACTION_STATE)) {
582: if (interactionState == null) {
583: interactionState = "";
584: }
585: templateBuffer.replace(left_br, right_br,
586: interactionState);
587: }
588:
589: // windowState
590: else if (replaceToken
591: .equalsIgnoreCase(Constants.WINDOW_STATE)) {
592: if (windowState == null) {
593: windowState = "";
594: }
595: templateBuffer.replace(left_br, right_br, windowState);
596: }
597:
598: // url
599: else if (replaceToken.equalsIgnoreCase(Constants.URL)) {
600: if (url == null) {
601: url = "";
602: }
603: templateBuffer.replace(left_br, right_br, url);
604: }
605:
606: // rewriteResource
607: else if (replaceToken
608: .equalsIgnoreCase(Constants.REWRITE_RESOURCE)) {
609: if (rewriteResource == null) {
610: rewriteResource = "";
611: }
612: templateBuffer.replace(left_br, right_br,
613: rewriteResource);
614: }
615:
616: /*
617:
618: // secureURL
619: else if (replaceToken.equalsIgnoreCase(Constants.SECURE_URL))
620: {
621: if (secureURL == null)
622: {
623: secureURL = "";
624: }
625: templateBuffer.replace(left_br, right_br, secureURL);
626: }
627:
628: // token
629: else if (replaceToken.equalsIgnoreCase(Constants.TOKEN))
630: {
631: if (token == null)
632: {
633: token = "";
634: }
635: templateBuffer.replace(left_br, right_br, token);
636: }
637: */
638: // portletHandle
639: else if (replaceToken
640: .equalsIgnoreCase(Constants.PORTLET_HANDLE)) {
641: if (portletHandle == null) {
642: portletHandle = "";
643: }
644: templateBuffer
645: .replace(left_br, right_br, portletHandle);
646: }
647:
648: // userContextKey
649: else if (replaceToken
650: .equalsIgnoreCase(Constants.USER_CONTEXT_KEY)) {
651: if (userContextKey == null) {
652: userContextKey = "";
653: }
654: templateBuffer.replace(left_br, right_br,
655: userContextKey);
656: }
657:
658: // portletInstanceKey
659: else if (replaceToken
660: .equalsIgnoreCase(Constants.PORTLET_INSTANCE_KEY)) {
661: if (portletInstanceKey == null) {
662: portletInstanceKey = "";
663: }
664: templateBuffer.replace(left_br, right_br,
665: portletInstanceKey);
666: }
667:
668: // sessionID
669: else if (replaceToken
670: .equalsIgnoreCase(Constants.SESSION_ID)) {
671: if (sessionID == null) {
672: sessionID = "";
673: }
674: templateBuffer.replace(left_br, right_br, sessionID);
675: }
676:
677: // unknown
678: else {
679:
680: WSRPXHelper.throwX(logger, Logger.ERROR, MN,
681: ErrorCodes.UNKNOWN_TOKEN_IN_TEMPLATE);
682:
683: }
684:
685: // update template
686: template = templateBuffer.toString();
687:
688: }
689:
690: if (logger.isLogging(Logger.TRACE_HIGH)) {
691: logger.exit(Logger.TRACE_HIGH, MN);
692: }
693:
694: return template;
695: }
696:
697: }
|