/** * Recursively retrieve PortletContextResources that match the given pattern, * adding them to the given result set. * @param portletContext the PortletContext to work on * @param fullPattern the pattern to match against, * with preprended root directory path * @param dir the current directory * @param result the Set of matching Resources to add to * @throws IOException if directory contents could not be retrieved * @see org.springframework.web.portlet.context.PortletContextResource * @see javax.portlet.PortletContext#getResourcePaths */ protected void doRetrieveMatchingPortletContextResources( PortletContext portletContext, String fullPattern, String dir, Set<Resource> result) throws IOException { Set<String> candidates = portletContext.getResourcePaths(dir); if (candidates != null) { boolean dirDepthNotFixed = fullPattern.contains("**"); for (Iterator<String> it = candidates.iterator(); it.hasNext();) { String currPath = it.next(); if (currPath.endsWith("/") && (dirDepthNotFixed || StringUtils.countOccurrencesOf(currPath, "/") <= StringUtils.countOccurrencesOf(fullPattern, "/"))) { doRetrieveMatchingPortletContextResources(portletContext, fullPattern, currPath, result); } if (getPathMatcher().match(fullPattern, currPath)) { result.add(new PortletContextResource(portletContext, currPath)); } } } }
/** * Find the root {@link WebApplicationContext} for this web app, typically * loaded via {@link org.springframework.web.context.ContextLoaderListener}. * <p>Will rethrow an exception that happened on root context startup, * to differentiate between a failed context startup and no context at all. * @param pc PortletContext to find the web application context for * @return the root WebApplicationContext for this web app, or {@code null} if none * (typed to ApplicationContext to avoid a Servlet API dependency; can usually * be casted to WebApplicationContext, but there shouldn't be a need to) * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE */ public static ApplicationContext getWebApplicationContext(PortletContext pc) { Assert.notNull(pc, "PortletContext must not be null"); Object attr = pc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (attr == null) { return null; } if (attr instanceof RuntimeException) { throw (RuntimeException) attr; } if (attr instanceof Error) { throw (Error) attr; } if (!(attr instanceof ApplicationContext)) { throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr); } return (ApplicationContext) attr; }
/** * Register web-specific scopes ("request", "session", "globalSession") * with the given BeanFactory, as used by the Portlet ApplicationContext. * @param bf the BeanFactory to configure * @param pc the PortletContext that we're running within */ static void registerPortletApplicationScopes(ConfigurableListableBeanFactory bf, PortletContext pc) { bf.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope()); bf.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope(false)); bf.registerScope(WebApplicationContext.SCOPE_GLOBAL_SESSION, new SessionScope(true)); if (pc != null) { PortletContextScope appScope = new PortletContextScope(pc); bf.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope); // Register as PortletContext attribute, for ContextCleanupListener to detect it. pc.setAttribute(PortletContextScope.class.getName(), appScope); } bf.registerResolvableDependency(PortletRequest.class, new RequestObjectFactory()); bf.registerResolvableDependency(PortletResponse.class, new ResponseObjectFactory()); bf.registerResolvableDependency(PortletSession.class, new SessionObjectFactory()); bf.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory()); }
/** * Serve the resource as specified in the given request to the given response, * using the PortletContext's request dispatcher. * <p>This is roughly equivalent to Portlet 2.0 GenericPortlet. * @param request the current resource request * @param response the current resource response * @param context the current Portlet's PortletContext * @throws PortletException propagated from Portlet API's forward method * @throws IOException propagated from Portlet API's forward method */ public static void serveResource(ResourceRequest request, ResourceResponse response, PortletContext context) throws PortletException, IOException { String id = request.getResourceID(); if (id != null) { if (!PortletUtils.isProtectedResource(id)) { PortletRequestDispatcher rd = context.getRequestDispatcher(id); if (rd != null) { rd.forward(request, response); return; } } response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "404"); } }
/** * Register web-specific scopes ("request", "session", "globalSession") * with the given BeanFactory, as used by the Portlet ApplicationContext. * @param beanFactory the BeanFactory to configure * @param pc the PortletContext that we're running within */ static void registerPortletApplicationScopes(ConfigurableListableBeanFactory beanFactory, PortletContext pc) { beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope()); beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope(false)); beanFactory.registerScope(WebApplicationContext.SCOPE_GLOBAL_SESSION, new SessionScope(true)); if (pc != null) { PortletContextScope appScope = new PortletContextScope(pc); beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope); // Register as PortletContext attribute, for ContextCleanupListener to detect it. pc.setAttribute(PortletContextScope.class.getName(), appScope); } beanFactory.registerResolvableDependency(PortletRequest.class, new RequestObjectFactory()); beanFactory.registerResolvableDependency(PortletSession.class, new SessionObjectFactory()); beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory()); }
public void testRequiredInitParameterNotSetOtherParameterNotSet() throws Exception { PortletContext portletContext = new MockPortletContext(); MockPortletConfig portletConfig = new MockPortletConfig(portletContext); String testParam = "testParam"; String testValue = "testValue"; portletConfig.addInitParameter(testParam, testValue); TestPortletBean portletBean = new TestPortletBean(); portletBean.addRequiredProperty("anotherParam"); assertNull(portletBean.getTestParam()); try { portletBean.init(portletConfig); fail("should have thrown PortletException"); } catch (PortletException ex) { // expected } assertNull(portletBean.getTestParam()); }
public void testUnknownRequiredInitParameter() throws Exception { PortletContext portletContext = new MockPortletContext(); MockPortletConfig portletConfig = new MockPortletConfig(portletContext); String testParam = "testParam"; String testValue = "testValue"; portletConfig.addInitParameter(testParam, testValue); TestPortletBean portletBean = new TestPortletBean(); portletBean.addRequiredProperty("unknownParam"); assertNull(portletBean.getTestParam()); try { portletBean.init(portletConfig); fail("should have thrown PortletException"); } catch (PortletException ex) { // expected } assertNull(portletBean.getTestParam()); }
/** * FIXME: should this test reside in this class? -- ZHENG Zhong */ protected TestResult checkGetContextFromSession(PortletSession session) { TestResult result = new TestResult(); result.setDescription("Ensure that the PortletContext can be retrieved " + "from the portlet session."); PortletContext context = session.getPortletContext(); if (context != null) { result.setReturnCode(TestResult.PASSED); } else { result.setReturnCode(TestResult.FAILED); result.setResultMessage("Fail to retrieve PortletContext from " + "PortletSession: null returned."); } return result; }
protected TestResult checkAddedSameNameParameter(PortletContext context, PortletRequest request, PortletResponse response) throws IOException, PortletException { // Dispatch to the companion servlet: call checkAddedSameNameParameter(). StringBuffer buffer = new StringBuffer(); buffer.append(SERVLET_PATH).append("?") .append(KEY_TARGET).append("=").append(TARGET_ADDED_SAME_NAME_PARAM) .append("&").append(KEY_RENDER).append("=").append(VALUE_ADDED1) .append("&").append(KEY_RENDER).append("=").append(VALUE_ADDED2); if (LOG.isDebugEnabled()) { LOG.debug("Dispatching to: " + buffer.toString()); } PortletRequestDispatcher dispatcher = context.getRequestDispatcher( buffer.toString()); dispatcher.include((RenderRequest) request, (RenderResponse) response); // Retrieve test result returned by the companion servlet. TestResult result = (TestResult) request.getAttribute(RESULT_KEY); request.removeAttribute(RESULT_KEY); return result; }
protected TestResult checkEnumerationContainsNames( PortletContext context) { TestResult result = new TestResult(); result.setDescription("Ensure that the expected init parameter name " + "exists in the portlet context's init parameters."); result.setSpecPLT("10.3.1"); boolean found = false; for (Enumeration<String> en = context.getInitParameterNames(); !found && en.hasMoreElements(); ) { String name = en.nextElement(); if (TEST_PARAM_NAME.equals(name)) { found = true; } } if (found) { result.setReturnCode(TestResult.PASSED); } else { result.setReturnCode(TestResult.FAILED); result.setResultMessage("Expected init parameter '" + TEST_PARAM_NAME + "' not found in portlet context."); } return result; }
protected TestResult checkParameters(PortletContext context, PortletRequest request, PortletResponse response) throws IOException, PortletException { // Dispatch to the companion servlet: call checkParameters(). StringBuffer buffer = new StringBuffer(); buffer.append(SERVLET_PATH).append("?") .append(KEY_TARGET).append("=").append(TARGET_PARAMS) .append("&").append(KEY_A).append("=").append(VALUE_A) .append("&").append(KEY_B).append("=").append(VALUE_B); if (LOG.isDebugEnabled()) { LOG.debug("Dispatching to: " + buffer.toString()); } PortletRequestDispatcher dispatcher = context.getRequestDispatcher( buffer.toString()); dispatcher.include((RenderRequest) request, (RenderResponse) response); // Retrieve test result returned by the companion servlet. TestResult result = (TestResult) request.getAttribute(RESULT_KEY); request.removeAttribute(RESULT_KEY); return result; }
/** * Renders the given template * * @param request The request * @param response The response * @param ctx The template context * @param templatePath The path of the template, relative to the templates path given in the init parameters * * @throws PortletException Thrown if there is an error during template processing * @throws IOException Thrown if there is an error while writing the response */ public void renderTemplate(RenderRequest request, RenderResponse response, Map<String, Object> ctx, String templatePath) throws PortletException, IOException { PortletContext portletCtx = this.getPortletContext(); String templateResourcePath = String.format(TEMPLATE_PATH_FORMAT, portletCtx.getPortletContextName(), this.templatesPath, templatePath); if(this.templateResourceLoader.hasTemplateResource(templateResourcePath)) { try { this.writeTemplate(request, response, ctx, templateResourcePath); } catch(TemplateException e) { throw new PortletException(e); } } else { throw new TemplateNotFoundException(templateResourcePath); } }
private static final ContextInfo _getPortletContextInfo(final Object context) { assert(context instanceof PortletContext); final PortletContext pContext = (PortletContext)context; return new ContextInfo( new PortletInitParameterMap(pContext), new PortletApplicationMap(pContext)); }
/** * Create a new PortletContextAwareProcessor for the given context and config. */ public PortletContextAwareProcessor(PortletContext portletContext, PortletConfig portletConfig) { this.portletContext = portletContext; this.portletConfig = portletConfig; if (portletContext == null && portletConfig != null) { this.portletContext = portletConfig.getPortletContext(); } }
/** * Overridden version which checks for PortletContextResource * and uses {@code PortletContext.getResourcePaths} to find * matching resources below the web application root directory. * In case of other resources, delegates to the superclass version. * @see #doRetrieveMatchingPortletContextResources * @see PortletContextResource * @see javax.portlet.PortletContext#getResourcePaths */ @Override protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { if (rootDirResource instanceof PortletContextResource) { PortletContextResource pcResource = (PortletContextResource) rootDirResource; PortletContext pc = pcResource.getPortletContext(); String fullPattern = pcResource.getPath() + subPattern; Set<Resource> result = new HashSet<Resource>(); doRetrieveMatchingPortletContextResources(pc, fullPattern, pcResource.getPath(), result); return result; } return super.doFindPathMatchingFileResources(rootDirResource, subPattern); }
/** * Create a new PortletContextResource. * <p>The Portlet spec requires that resource paths start with a slash, * even if many containers accept paths without leading slash too. * Consequently, the given path will be prepended with a slash if it * doesn't already start with one. * @param portletContext the PortletContext to load from * @param path the path of the resource */ public PortletContextResource(PortletContext portletContext, String path) { // check PortletContext Assert.notNull(portletContext, "Cannot resolve PortletContextResource without PortletContext"); this.portletContext = portletContext; // check path Assert.notNull(path, "Path is required"); String pathToUse = StringUtils.cleanPath(path); if (!pathToUse.startsWith("/")) { pathToUse = "/" + pathToUse; } this.path = pathToUse; }
/** * Return the current PortletContext. * @throws IllegalStateException if not running within a PortletContext */ protected final PortletContext getPortletContext() throws IllegalStateException { if (this.portletContext == null) { throw new IllegalStateException( "PortletApplicationObjectSupport instance [" + this + "] does not run within a PortletContext"); } return this.portletContext; }
/** * Return the real path of the given path within the web application, * as provided by the portlet container. * <p>Prepends a slash if the path does not already start with a slash, * and throws a {@link java.io.FileNotFoundException} if the path cannot * be resolved to a resource (in contrast to * {@link javax.portlet.PortletContext#getRealPath PortletContext's {@code getRealPath}}, * which simply returns {@code null}). * @param portletContext the portlet context of the web application * @param path the relative path within the web application * @return the corresponding real path * @throws FileNotFoundException if the path cannot be resolved to a resource * @see javax.portlet.PortletContext#getRealPath */ public static String getRealPath(PortletContext portletContext, String path) throws FileNotFoundException { Assert.notNull(portletContext, "PortletContext must not be null"); // Interpret location as relative to the web application root directory. if (!path.startsWith("/")) { path = "/" + path; } String realPath = portletContext.getRealPath(path); if (realPath == null) { throw new FileNotFoundException( "PortletContext resource [" + path + "] cannot be resolved to absolute file path - " + "web application archive not expanded?"); } return realPath; }
private ConfigurablePortletApplicationContext initApplicationContext(String scope) { MockServletContext sc = new MockServletContext(); GenericWebApplicationContext rac = new GenericWebApplicationContext(sc); rac.refresh(); PortletContext pc = new ServletWrappingPortletContext(sc); StaticPortletApplicationContext ac = new StaticPortletApplicationContext(); ac.setParent(rac); ac.setPortletContext(pc); GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(DerivedTestBean.class); bd.setScope(scope); ac.registerBeanDefinition(NAME, bd); ac.refresh(); return ac; }
@Test public void portletContextAwareWithNullPortletContext() { PortletContextAwareProcessor processor = new PortletContextAwareProcessor((PortletContext) null); assertNull(bean.getPortletContext()); processor.postProcessBeforeInitialization(bean, "testBean"); assertNull(bean.getPortletContext()); }
@Test public void portletConfigAwareWithNullPortletContext() { PortletContextAwareProcessor processor = new PortletContextAwareProcessor((PortletContext) null); PortletConfigAwareBean bean = new PortletConfigAwareBean(); assertNull(bean.getPortletConfig()); processor.postProcessBeforeInitialization(bean, "testBean"); assertNull(bean.getPortletConfig()); }
public void testGetRealPathInterpretsLocationAsRelativeToWebAppRootIfPathDoesNotBeginWithALeadingSlash() throws Exception { final String originalPath = "web/foo"; final String expectedRealPath = "/" + originalPath; PortletContext ctx = mock(PortletContext.class); given(ctx.getRealPath(expectedRealPath)).willReturn(expectedRealPath); String actualRealPath = PortletUtils.getRealPath(ctx, originalPath); assertEquals(expectedRealPath, actualRealPath); verify(ctx); }
/** * Create a new MockPortletRequest. * @param portalContext the PortalContext that the request runs in * @param portletContext the PortletContext that the request runs in */ public MockPortletRequest(PortalContext portalContext, PortletContext portletContext) { this.portalContext = (portalContext != null ? portalContext : new MockPortalContext()); this.portletContext = (portletContext != null ? portletContext : new MockPortletContext()); this.responseContentTypes.add("text/html"); this.locales.add(Locale.ENGLISH); this.attributes.put(LIFECYCLE_PHASE, getLifecyclePhase()); }
/** * Create a new MockPortletRequest. * * @param portalContext the PortalContext that the request runs in * @param portletContext the PortletContext that the request runs in */ public MockPortletRequest(PortalContext portalContext, PortletContext portletContext) { this.portalContext = (portalContext != null ? portalContext : new MockPortalContext()); this.portletContext = (portletContext != null ? portletContext : new MockPortletContext()); this.responseContentTypes.add("text/html"); this.locales.add(Locale.ENGLISH); this.attributes.put(LIFECYCLE_PHASE, getLifecyclePhase()); }
@Override public void destroy() { PortletContext portletContext = getPortletContext(); ServletContextPool.remove(portletContext.getPortletContextName()); super.destroy(); }
public static PluginPackage getPluginPackage(PortletConfig portletConfig) { if (portletConfig == null) { return null; } PortletContext portletContext = portletConfig.getPortletContext(); String portletContextName = portletContext.getPortletContextName(); return DeployManagerUtil.getInstalledPluginPackage(portletContextName); }
public TwitterPortletRequest(RenderRequest request, RenderResponse response, PortletContext portletContext, OAuthProvider oauthProvider) { this.request = request; this.response = response; this.portletContext = portletContext; this.oauthProvider = oauthProvider; }
public FacebookPortletRequest(RenderRequest request, RenderResponse response, PortletContext portletContext, OAuthProvider oauthPr) { this.request = request; this.response = response; this.portletContext = portletContext; this.oauthProvider = oauthPr; }
FacebookClientWrapper(RenderRequest request, RenderResponse response, PortletContext portletContext, OAuthProvider oauthPr, String accessToken) { this.request = request; this.response = response; this.portletContext = portletContext; this.oauthProvider = oauthPr; this.facebookClient = new DefaultFacebookClient(accessToken); }
public GooglePortletRequest(RenderRequest request, RenderResponse response, PortletContext portletContext, OAuthProvider oauthPr, String requiredScope) { this.request = request; this.response = response; this.portletContext = portletContext; this.oauthProvider = oauthPr; this.requiredScope = requiredScope; }
public static void sendToJSP(PortletContext pContext, RenderRequest request, RenderResponse response, String jspPage) throws PortletException { response.setContentType(request.getResponseContentType()); if (jspPage != null && jspPage.length() != 0) { try { PortletRequestDispatcher dispatcher = pContext .getRequestDispatcher(jspPage); dispatcher.include(request, response); } catch (IOException e) { throw new PortletException("Sakai Dispatch unabble to use " + jspPage, e); } } }
public AbstractPortletConfigImpl(PortletContext portletContext, PortletDefinition portlet) { this.portletContext = portletContext; this.portlet = portlet; this.supportedContainerRuntimeOptions = new HashSet<String>(); for (Enumeration<String> e = portletContext.getContainerRuntimeOptions(); e.hasMoreElements(); ) { supportedContainerRuntimeOptions.add(e.nextElement()); } }
protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException { PortletContext context = getPortletContext(); PortletRequestDispatcher requestDispatcher = context.getRequestDispatcher(getHelpPage(request)); requestDispatcher.include(request, response); }