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.velocity;
018:
019: import java.io.IOException;
020: import java.io.PrintWriter;
021: import java.io.Writer;
022: import java.security.Principal;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.Set;
028:
029: import javax.portlet.PortletConfig;
030: import javax.portlet.PortletMode;
031: import javax.portlet.RenderRequest;
032: import javax.portlet.RenderResponse;
033: import javax.portlet.WindowState;
034: import javax.security.auth.Subject;
035: import javax.servlet.http.HttpServletRequest;
036:
037: import org.apache.commons.configuration.Configuration;
038: import org.apache.commons.configuration.PropertiesConfiguration;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041: import org.apache.jetspeed.Jetspeed;
042: import org.apache.jetspeed.PortalReservedParameters;
043: import org.apache.jetspeed.aggregator.PortletRenderer;
044: import org.apache.jetspeed.aggregator.impl.PortletAggregatorFragmentImpl;
045: import org.apache.jetspeed.capabilities.CapabilityMap;
046: import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
047: import org.apache.jetspeed.components.portletentity.PortletEntityNotGeneratedException;
048: import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
049: import org.apache.jetspeed.container.state.NavigationalState;
050: import org.apache.jetspeed.container.url.BasePortalURL;
051: import org.apache.jetspeed.container.window.FailedToRetrievePortletWindow;
052: import org.apache.jetspeed.container.window.PortletWindowAccessor;
053: import org.apache.jetspeed.locator.LocatorDescriptor;
054: import org.apache.jetspeed.locator.TemplateDescriptor;
055: import org.apache.jetspeed.locator.TemplateLocator;
056: import org.apache.jetspeed.locator.TemplateLocatorException;
057: import org.apache.jetspeed.om.page.ContentFragment;
058: import org.apache.jetspeed.om.page.ContentFragmentImpl;
059: import org.apache.jetspeed.om.page.Fragment;
060: import org.apache.jetspeed.om.page.Page;
061: import org.apache.jetspeed.request.RequestContext;
062: import org.apache.jetspeed.services.title.DynamicTitleService;
063: import org.apache.jetspeed.util.ArgUtil;
064: import org.apache.jetspeed.util.Path;
065: import org.apache.pluto.om.entity.PortletEntity;
066: import org.apache.velocity.context.Context;
067:
068: /**
069: * <p>
070: * JetspeedPowerTool
071: * </p>
072: * <p>
073: * The JetspeedPowerTool is meant to be used by template designers to build
074: * templates for internal Jetspeed portlet applications. It hides the
075: * implementation details of the more common template actions so that future
076: * changes to said implementation have minimal effect on template.
077: * </p>
078: * <p>
079: * Where applicable, methods have been marked with a <strong>BEST PRATICES
080: * </strong> meaning that this method should be used instead the synonymous code
081: * listed within the method docuementation.
082: * </p>
083: *
084: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
085: * @version $Id: JetspeedPowerToolImpl.java 589933 2007-10-30 01:51:50Z woonsan $
086: *
087: */
088: public class JetspeedPowerToolImpl implements JetspeedVelocityPowerTool {
089:
090: private static final String DECORATOR_ID_ATTR = "decoratorId";
091:
092: private static final String ACTION_IMAGE_EXTENSION_ATTR = "actionImageExtension";
093:
094: protected static final String PORTLET_CONFIG_ATTR = "javax.portlet.config";
095:
096: protected static final String RENDER_RESPONSE_ATTR = "javax.portlet.response";
097:
098: protected static final String RENDER_REQUEST_ATTR = "javax.portlet.request";
099:
100: protected static final String COLUMNS_ATTR = "columns";
101:
102: protected static final String COLUMN_SIZES = "columnSizes";
103:
104: protected RenderRequest renderRequest;
105:
106: protected RenderResponse renderResponse;
107:
108: protected PortletConfig portletConfig;
109:
110: protected Writer templateWriter;
111:
112: protected static final Log log = LogFactory
113: .getLog(JetspeedPowerToolImpl.class);
114:
115: protected CapabilityMap capabilityMap;
116:
117: protected Locale locale;
118:
119: protected LocatorDescriptor templateLocatorDescriptor;
120:
121: protected TemplateLocator templateLocator;
122:
123: protected PortletEntityAccessComponent entityAccess;
124:
125: protected TemplateLocator decorationLocator;
126:
127: protected LocatorDescriptor decorationLocatorDescriptor;
128:
129: protected PortletWindowAccessor windowAccess;
130:
131: protected RequestContext requestContext;
132:
133: protected Context velocityContext;
134:
135: private DynamicTitleService titleService;
136:
137: private BasePortalURL baseUrlAccess;
138:
139: private PortletRenderer renderer;
140:
141: public JetspeedPowerToolImpl(RequestContext requestContext,
142: DynamicTitleService titleService, PortletRenderer renderer)
143: throws Exception {
144: HttpServletRequest request = requestContext.getRequest();
145: this .requestContext = requestContext;
146: this .titleService = titleService;
147: windowAccess = (PortletWindowAccessor) getComponent(PortletWindowAccessor.class
148: .getName());
149: entityAccess = (PortletEntityAccessComponent) getComponent(PortletEntityAccessComponent.class
150: .getName());
151: try {
152: baseUrlAccess = (BasePortalURL) getComponent("BasePortalURL");
153: } catch (Exception e) {
154: // BasePortalURL is optional: ignore (org.springframework.beans.factory.NoSuchBeanDefinitionException)
155: }
156:
157: renderRequest = (RenderRequest) request
158: .getAttribute(RENDER_REQUEST_ATTR);
159: renderResponse = (RenderResponse) request
160: .getAttribute(RENDER_RESPONSE_ATTR);
161: portletConfig = (PortletConfig) request
162: .getAttribute(PORTLET_CONFIG_ATTR);
163:
164: templateLocator = (TemplateLocator) getComponent("TemplateLocator");
165: decorationLocator = (TemplateLocator) getComponent("DecorationLocator");
166: // By using null, we create a re-useable locator
167: capabilityMap = requestContext.getCapabilityMap();
168: locale = requestContext.getLocale();
169:
170: templateLocatorDescriptor = templateLocator
171: .createLocatorDescriptor(null);
172: templateLocatorDescriptor.setMediaType(capabilityMap
173: .getPreferredMediaType().getName());
174: templateLocatorDescriptor.setCountry(locale.getCountry());
175: templateLocatorDescriptor.setLanguage(locale.getLanguage());
176:
177: decorationLocatorDescriptor = decorationLocator
178: .createLocatorDescriptor(null);
179: decorationLocatorDescriptor.setMediaType(capabilityMap
180: .getPreferredMediaType().getName());
181: decorationLocatorDescriptor.setCountry(locale.getCountry());
182: decorationLocatorDescriptor.setLanguage(locale.getLanguage());
183:
184: this .renderer = renderer;
185: }
186:
187: /**
188: * <p>
189: * getRequestContext
190: * </p>
191: *
192: * @return
193: */
194: protected final RequestContext getRequestContext() {
195: return requestContext;
196: }
197:
198: /**
199: * Gets the window state for the current portlet window (fragment)
200: *
201: * @return The window state for the current window
202: * @throws Exception
203: */
204: public WindowState getWindowState() throws Exception {
205: try {
206: NavigationalState nav = getRequestContext().getPortalURL()
207: .getNavigationalState();
208: return nav.getState(windowAccess
209: .getPortletWindow(getCurrentFragment()));
210: } catch (Exception e) {
211: handleError(e, e.toString(), getCurrentFragment());
212: return null;
213: }
214: }
215:
216: /**
217: * Gets the internal (portal) window state for the current portlet window (fragment)
218: *
219: * @return The window state for the current window
220: * @throws Exception
221: */
222: public WindowState getMappedWindowState() throws Exception {
223: try {
224: NavigationalState nav = getRequestContext().getPortalURL()
225: .getNavigationalState();
226: return nav.getMappedState(windowAccess
227: .getPortletWindow(getCurrentFragment()));
228: } catch (Exception e) {
229: handleError(e, e.toString(), getCurrentFragment());
230: return null;
231: }
232: }
233:
234: /**
235: * Gets the portlet mode for a current portlet window (fragment)
236: *
237: * @return The portlet mode of the current window
238: * @throws Exception
239: */
240: public PortletMode getPortletMode() throws Exception {
241:
242: NavigationalState nav = getRequestContext().getPortalURL()
243: .getNavigationalState();
244: try {
245: return nav.getMode(windowAccess
246: .getPortletWindow(getCurrentFragment()));
247: } catch (FailedToRetrievePortletWindow e) {
248: handleError(e, e.toString(), getCurrentFragment());
249: return null;
250: }
251: }
252:
253: /**
254: * Gets the internal (portal) portlet mode for a current portlet window (fragment)
255: *
256: * @return The portlet mode of the current window
257: * @throws Exception
258: */
259: public PortletMode getMappedPortletMode() throws Exception {
260:
261: NavigationalState nav = getRequestContext().getPortalURL()
262: .getNavigationalState();
263: try {
264: return nav.getMappedMode(windowAccess
265: .getPortletWindow(getCurrentFragment()));
266: } catch (FailedToRetrievePortletWindow e) {
267: handleError(e, e.toString(), getCurrentFragment());
268: return null;
269: }
270: }
271:
272: /**
273: *
274: * @return
275: */
276: public ContentFragment getCurrentFragment() {
277: checkState();
278: return (ContentFragment) renderRequest
279: .getAttribute(PortalReservedParameters.FRAGMENT_ATTRIBUTE);
280: }
281:
282: /**
283: *
284: * @param f
285: */
286: public void setCurrentFragment(ContentFragment f) {
287: checkState();
288: setAttribute(PortalReservedParameters.FRAGMENT_ATTRIBUTE, f);
289:
290: }
291:
292: public void setCurrentLayout() {
293: checkState();
294:
295: ContentFragment f = (ContentFragment) getRequestContext()
296: .getRequest().getAttribute(LAYOUT_ATTR);
297: setAttribute(LAYOUT_ATTR, f);
298: }
299:
300: /**
301: *
302: * @return
303: */
304: public ContentFragment getCurrentLayout() {
305: checkState();
306: return (ContentFragment) renderRequest
307: .getAttribute(LAYOUT_ATTR);
308: }
309:
310: /**
311: *
312: * @return
313: */
314: public Page getPage() {
315: checkState();
316: // return (Page)
317: // renderRequest.getAttribute(PortalReservedParameters.PAGE_ATTRIBUTE_KEY);
318: return requestContext.getPage();
319: }
320:
321: /**
322: *
323: * @return
324: */
325: public List[] getColumns() {
326: checkState();
327: return (List[]) renderRequest.getAttribute(COLUMNS_ATTR);
328: }
329:
330: public List getColumnSizes() {
331: checkState();
332: Object o = renderRequest.getAttribute(COLUMN_SIZES);
333: if (o == null)
334: return null;
335: return (List) renderRequest.getAttribute(COLUMN_SIZES);
336: }
337:
338: /**
339: *
340: * @return
341: * @throws Exception
342: */
343: public PortletEntity getCurrentPortletEntity() throws Exception {
344: try {
345: return windowAccess.getPortletWindow(getCurrentFragment())
346: .getPortletEntity();
347: } catch (Exception e) {
348: handleError(e,
349: "JetspeedPowerTool failed to retreive the current PortletEntity. "
350: + e.toString(), getCurrentFragment());
351: return null;
352: }
353: }
354:
355: /**
356: *
357: * @param f
358: * Fragment whose <code>PortletEntity</code> we want to
359: * retreive.
360: * @return The PortletEntity represented by the current fragment.
361: * @throws Exception
362: */
363: public PortletEntity getPortletEntity(ContentFragment f)
364: throws Exception {
365: PortletEntity portletEntity = windowAccess.getPortletWindow(f)
366: .getPortletEntity();
367: // This API hits the DB: PortletEntity portletEntity = entityAccess.getPortletEntityForFragment(f);
368: if (portletEntity == null) {
369: try {
370: portletEntity = entityAccess
371: .generateEntityFromFragment(f);
372: entityAccess.storePortletEntity(portletEntity);
373: } catch (PortletEntityNotGeneratedException e) {
374: String msg = "JetspeedPowerTool failed to retreive a PortletEntity for Fragment "
375: + f.getId() + ". " + e.toString();
376: handleError(e, msg, f);
377: } catch (PortletEntityNotStoredException e) {
378: String msg = "JetspeedPowerTool failed to store a PortletEntity for Fragment "
379: + f.getId() + ". " + e.toString();
380: handleError(e, msg, f);
381: }
382: }
383: return portletEntity;
384: }
385:
386: /**
387: * Checks the the visibilty of this fragment with respect to the current
388: * RenderReqeust.
389: *
390: * @param f
391: * Fragment
392: * @return whether or not the Fragment in question should be considered
393: * visible during rendering.
394: */
395: public boolean isHidden(ContentFragment f) {
396: checkState();
397: if (f == null) {
398: throw new IllegalArgumentException(
399: "Fragment cannot be null for isHidden(Fragment)");
400: }
401: return f.getState() != null && f.getState().equals(HIDDEN);
402: }
403:
404: /**
405: * Retreives a template using Jetspeed's
406: *
407: * @see org.apache.jetspeed.locator.TemplateLocator
408: *
409: *
410: * @param path
411: * Expected to the template. This may actually be changed by the
412: * TL service based the capability and localization information
413: * provided by the client.
414: * @param templateType
415: * Type off template we are interested in.
416: * @return Template object containng the pertinent information required to
417: * inlcude the request template path in the current response
418: * @throws TemplateLocatorException
419: * if the <code>path</code> does not exist.
420: */
421: public TemplateDescriptor getTemplate(String path,
422: String templateType) throws TemplateLocatorException {
423: checkState();
424: return getTemplate(path, templateType, templateLocator,
425: templateLocatorDescriptor);
426: }
427:
428: public Configuration getTypeConfiguration(String type, String name,
429: String location) throws Exception {
430: ArgUtil.assertNotNull(String.class, type, this ,
431: "getTypeConfiguration(String type, String name)");
432: ArgUtil.assertNotNull(String.class, name, this ,
433: "getTypeConfiguration(String type, String name)");
434: try {
435: TemplateDescriptor locator = null;
436: if (location.equals("templates")) {
437: locator = getTemplate(
438: name + "/" + type + ".properties", type);
439: } else if (location.equals("decorations")) {
440: locator = getDecoration(name + "/decorator.properties",
441: type);
442: } else {
443: throw new IllegalArgumentException(
444: "Location type "
445: + location
446: + " is not supported by getTypeConfiguration().");
447: }
448: return new PropertiesConfiguration(locator
449: .getAbsolutePath());
450: } catch (TemplateLocatorException e) {
451: log.warn(e.toString(), e);
452: return null;
453: }
454: }
455:
456: public TemplateDescriptor getDecoration(String path,
457: String templateType) throws TemplateLocatorException {
458: checkState();
459: return getTemplate(path, templateType, decorationLocator,
460: decorationLocatorDescriptor);
461: }
462:
463: public String includeTemplate(String template, String templateType)
464: throws IOException {
465: checkState();
466: try {
467: TemplateDescriptor useLocator = getTemplate(template,
468: templateType);
469: return useLocator.getAppRelativePath();
470: } catch (Exception e) {
471: PrintWriter directError = new PrintWriter(renderResponse
472: .getWriter());
473: directError
474: .write("Error occured process includeTemplate(): "
475: + e.toString() + "\n\n");
476: e.printStackTrace(directError);
477: directError.close();
478: return null;
479: }
480: }
481:
482: public String includeDecoration(String template, String templateType)
483: throws IOException {
484: checkState();
485: try {
486: return getDecoration(template, templateType)
487: .getAppRelativePath();
488: } catch (Exception e) {
489: PrintWriter directError = new PrintWriter(renderResponse
490: .getWriter());
491: directError
492: .write("Error occured process includeDecoration(): "
493: + e.toString() + "\n\n");
494: e.printStackTrace(directError);
495: directError.close();
496: return null;
497: }
498: }
499:
500: /**
501: * <p>
502: * Decorate and include fragment content.
503: * </p>
504: *
505: * @param f
506: * Fragment to include and decorate
507: * @throws Exception
508: * @return String path to the decorator.
509: */
510: public String decorateAndInclude(ContentFragment f)
511: throws Exception {
512: // makes sure that any previous content has been written to
513: // preserve natural HTML rendering order
514:
515: setCurrentFragment(f);
516: setCurrentLayout();
517:
518: // include decorated layout or portlet fragment
519: try {
520: return decorateAndIncludePortlet(f);
521: } catch (Exception e) {
522: renderResponse.getWriter().write(e.toString());
523: return null;
524: }
525:
526: }
527:
528: /**
529: * <p>
530: * The decorator template itself is responsible for including the content of
531: * the target Fragment which is easily acheived like so: <br />
532: * in Velocity:
533: *
534: * <pre>
535: * <code>
536: * $jetspeed.include($jetspeed.currentFragment)
537: * </code>
538: * </pre>
539: *
540: * In JSP:
541: *
542: * <pre>
543: * <code>
544: *
545: * <%
546: * JetspeedPowerTool jetspeed = new JetspeedPowerTool(renderRequest, renderResponse, portletConfig);
547: * jetspeed.include(jetspeed.getCurrentFragment());
548: * %>
549: *
550: * </code>
551: * </pre>
552: *
553: *
554: * @param f
555: * Portlet fragment to "decorate"
556: * @throws Exception
557: */
558: protected String decorateAndIncludePortlet(ContentFragment f)
559: throws Exception {
560: // make sure that any previous content has been written to
561: // preserve natural HTML rendering order
562:
563: // get fragment decorator; fallback to the default decorator
564: // if the current fragment is not specifically decorated
565: String fragmentType = f.getType();
566: String decorator = f.getDecorator();
567: if (decorator == null) {
568: decorator = getPage().getEffectiveDefaultDecorator(
569: fragmentType);
570: }
571:
572: // get fragment properties for fragmentType or generic
573: TemplateDescriptor propsTemp = getTemplate(decorator + "/"
574: + DECORATOR_TYPE + ".properties", fragmentType,
575: decorationLocator, decorationLocatorDescriptor);
576: if (propsTemp == null) {
577: fragmentType = GENERIC_TEMPLATE_TYPE;
578: propsTemp = getTemplate(decorator + "/" + DECORATOR_TYPE
579: + ".properties", fragmentType, decorationLocator,
580: decorationLocatorDescriptor);
581: }
582:
583: // get decorator template
584: Configuration decoConf = new PropertiesConfiguration(propsTemp
585: .getAbsolutePath());
586: String ext = decoConf.getString("template.extension");
587: String decoratorPath = decorator + "/" + DECORATOR_TYPE + ext;
588: TemplateDescriptor template = null;
589: try {
590: template = getDecoration(decoratorPath, fragmentType);
591: } catch (TemplateLocatorException e) {
592: String parent = decoConf.getString("extends");
593: if (parent != null) {
594: template = getDecoration(parent + "/" + DECORATOR_TYPE
595: + ext, fragmentType);
596: }
597: }
598:
599: setAttribute(DECORATOR_ID_ATTR, decoConf.getString("id"));
600: setAttribute(ACTION_IMAGE_EXTENSION_ATTR, decoConf.getString(
601: "action.image.extension", ".gif"));
602: return template.getAppRelativePath();
603: }
604:
605: /**
606: *
607: *
608: * @throws java.lang.IllegalStateException
609: * if the <code>PortletConfig</code>,
610: * <code>RenderRequest</code> or <code>RenderReponse</code>
611: * is null.
612: */
613: protected void checkState() {
614: if (portletConfig == null || renderRequest == null
615: || renderResponse == null) {
616: throw new IllegalStateException(
617: "JetspeedPowerTool has not been properly initialized. "
618: + ""
619: + "The JetspeedPowerTool generally only usuable during the rendering phase of "
620: + "internal portlet applications.");
621: }
622: }
623:
624: protected TemplateDescriptor getTemplate(String path,
625: String templateType, TemplateLocator locator,
626: LocatorDescriptor descriptor)
627: throws TemplateLocatorException {
628: checkState();
629: if (templateType == null) {
630: templateType = GENERIC_TEMPLATE_TYPE;
631: }
632: try {
633:
634: descriptor.setName(path);
635: descriptor.setType(templateType);
636:
637: TemplateDescriptor template = locator
638: .locateTemplate(descriptor);
639: // Check for defaults above the currently specified root
640: if (template == null) {
641: Path pathObject = new Path(path);
642: if (pathObject.length() > 1) {
643: template = getTemplate(pathObject.getSegment(1)
644: .toString(), templateType, locator,
645: descriptor);
646: }
647: }
648: return template;
649: } catch (TemplateLocatorException e) {
650: log.error("Unable to locate template: " + path, e);
651: // System.out.println("Unable to locate template: " + path);
652: throw e;
653: }
654: }
655:
656: /**
657: * <p>
658: * handleError
659: * </p>
660: *
661: * @param e
662: * @param msg
663: */
664: protected void handleError(Exception e, String msg,
665: ContentFragment fragment) {
666: log.error(msg, e);
667:
668: Set exceptions = (Set) renderRequest
669: .getAttribute(FRAGMENT_PROCESSING_ERROR_PREFIX
670: + fragment.getId());
671: if (exceptions == null) {
672: exceptions = new HashSet();
673: setAttribute(FRAGMENT_PROCESSING_ERROR_PREFIX
674: + fragment.getId(), exceptions);
675: }
676: exceptions.add(e);
677:
678: }
679:
680: /**
681: * Gets the list of decorator actions for a window. Each window (on each
682: * page) has its own collection of actionAccess flags associated with it.
683: *
684: * @return A list of actions available to the current window, filtered by
685: * securty access and current state.
686: * @throws Exception
687: * @deprecated
688: */
689: public List getDecoratorActions() {
690: return getCurrentFragment().getDecoration().getActions();
691: }
692:
693: /**
694: * Gets the list of decorator actions for a page. Each layout fragment on a
695: * page has its own collection of actionAccess flags associated with it.
696: *
697: * @return A list of actions available to the current window, filtered by
698: * securty access and current state.
699: * @throws Exception
700: * @deprecated
701: */
702: public List getPageDecoratorActions() throws Exception {
703: return getCurrentFragment().getDecoration().getActions();
704: }
705:
706: /**
707: *
708: * <p>
709: * getTitle
710: * </p>
711: * Returns the appropriate for the title based on locale prferences
712: *
713: * @param entity
714: * @return
715: */
716: public String getTitle(PortletEntity entity, ContentFragment f) {
717: String title = null;
718:
719: if (f != null) {
720: title = f.getTitle();
721: }
722:
723: if (title == null) {
724: try {
725:
726: return titleService.getDynamicTitle(windowAccess
727: .getPortletWindow(f), getRequestContext()
728: .getRequest());
729: } catch (Exception e) {
730: log.error("Unable to reteive portlet title: "
731: + e.getMessage(), e);
732: return "Title Error: " + e.getMessage();
733: }
734: }
735:
736: return title;
737: }
738:
739: /**
740: *
741: * <p>
742: * getTitle
743: * </p>
744: * Returns the appropriate for the title based on locale prferences
745: *
746: * @param entity
747: * @return
748: */
749: public String getTitle(PortletEntity entity) {
750: try {
751: return titleService.getDynamicTitle(windowAccess
752: .getPortletWindow(getCurrentFragment()),
753: getRequestContext().getRequest());
754: } catch (Exception e) {
755: log.error("Unable to reteive portlet title: "
756: + e.getMessage(), e);
757: return "Title Error: " + e.getMessage();
758: }
759:
760: }
761:
762: public Object getComponent(String name) {
763: return Jetspeed.getComponentManager().getComponent(name);
764: }
765:
766: public String getAbsoluteUrl(String relativePath) {
767: // only rewrite a non-absolute url
768: if (relativePath != null && relativePath.indexOf("://") == -1
769: && relativePath.indexOf("mailto:") == -1) {
770: HttpServletRequest request = getRequestContext()
771: .getRequest();
772: StringBuffer path = new StringBuffer();
773: if (!getRequestContext().getPortalURL().isRelativeOnly()) {
774: if (this .baseUrlAccess == null) {
775: path.append(request.getScheme()).append("://")
776: .append(request.getServerName())
777: .append(":")
778: .append(request.getServerPort());
779: } else {
780: path.append(baseUrlAccess.getServerScheme())
781: .append("://").append(
782: baseUrlAccess.getServerName())
783: .append(":").append(
784: baseUrlAccess.getServerPort());
785: }
786: }
787: return renderResponse.encodeURL(path.append(
788: request.getContextPath()).append(
789: request.getServletPath()).append(relativePath)
790: .toString());
791:
792: } else {
793: return relativePath;
794: }
795: }
796:
797: public Subject getSubject() {
798: return requestContext.getSubject();
799: }
800:
801: public boolean getLoggedOn() {
802: Principal principal = requestContext.getRequest()
803: .getUserPrincipal();
804: return (principal != null);
805: }
806:
807: public String getBasePath() {
808: return getRequestContext().getPortalURL().getBasePath();
809: }
810:
811: public String getPageBasePath() {
812: return getRequestContext().getPortalURL().getPageBasePath();
813: }
814:
815: public void setVelocityContext(Context velocityContext) {
816: this .velocityContext = velocityContext;
817: }
818:
819: /**
820: * Sets an attribute for use within your layout and decoration templates.
821: * The value is always stored within the current
822: * <code>javax.portlet.Renderrequest</code> and is also stored within the
823: * current <code>org.apache.velocity.Context</code> if it is available.
824: *
825: * @param name
826: * to store the attribute under.
827: * @param obj
828: * object to set.
829: */
830: protected void setAttribute(String name, Object object) {
831: renderRequest.setAttribute(name, object);
832: if (velocityContext != null) {
833: velocityContext.put(name, object);
834: }
835: }
836:
837: public String renderPortletEntity(String entityId, String portletId) {
838:
839: RequestContext context = getRequestContext();
840:
841: PortletAggregatorFragmentImpl fragment = new PortletAggregatorFragmentImpl(
842: entityId);
843: fragment.setType(Fragment.PORTLET);
844: fragment.setName(portletId);
845: ContentFragment contentFragment = new ContentFragmentImpl(
846: fragment, new HashMap(), true);
847: renderer.renderNow(contentFragment, context);
848: return contentFragment.getRenderedContent();
849: }
850:
851: }
|