javax.portlet.PortletContext Java Examples

The following examples show how to use javax.portlet.PortletContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PortletApplicationContextUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: DispatcherRenderParameterTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: PortletRequestImplTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
protected void setUp( ) throws Exception
    {
        super.setUp();

        // Create mocks
        mockServices = mock( ContainerServices.class );
        mockCCPPProfileService = mock( CCPPProfileService.class );
        mockPortalContext = mock( PortalContext.class );
        mockPortletContext = mock( PortletContext.class );
        mockPortletURLProvider = mock(PortletURLProvider.class);
        mockContainer = mock( PortletContainerImpl.class,
                new Class[] { String.class, ContainerServices.class },
                new Object[] { "Mock Pluto Container", (ContainerServices) mockServices.proxy() } );
        window = (PortletWindow) mock( PortletWindow.class ).proxy();
        mockHttpServletRequest = mock( HttpServletRequest.class );
        mockPortletRequestContext = mock ( PortletRequestContext.class );
        mockPortletResponseContext = mock ( PortletRenderResponseContext.class );
        mock ( CacheControl.class );

        // Constructor expectations for RenderRequestImpl
//        mockContainer.expects( atLeastOnce() ).method( "getOptionalContainerServices" ).will( returnValue( mockOptionalServices.proxy() ) );
//        mockServices.expects( once() ).method( "getPortalContext" ).will( returnValue( mockPortalContext.proxy() ) );
    }
 
Example #4
Source File: SimpleAttributeTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
protected TestResult checkSetAttribute(PortletContext context) {
    TestResult res = new TestResult();
    res.setName("Set Attribute Test");
    res.setDescription("Sets and retrieves portlet contextuest attribute.");

    context.setAttribute(KEY, VAL);
    Object val = context.getAttribute(KEY);
    if(!VAL.equals(val)) {
        res.setReturnCode(TestResult.FAILED);
        res.setResultMessage("Retrieved value: '"+val+"' - Expected '"+VAL+"'");
    }
    else {
        res.setReturnCode(TestResult.PASSED);
    }

    context.removeAttribute(KEY);
    return res;
}
 
Example #5
Source File: SimpleAttributeTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
protected TestResult checkRemoveAttribute(PortletContext context) {
    TestResult res = new TestResult();
    res.setName("Remove Context Attribute Test");
    res.setDescription("Sets, removes and retrieves portlet request attribute.");

    context.setAttribute(KEY, VAL);
    context.removeAttribute(KEY);
    Object val = context.getAttribute(KEY);
    if(val!=null) {
        res.setReturnCode(TestResult.FAILED);
        res.setResultMessage("Retrieved value: '"+val+"' - Expected '"+VAL+"'");
    }
    else {
        res.setReturnCode(TestResult.PASSED);
    }

    return res;
}
 
Example #6
Source File: PortletContextResourcePatternResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
			}
		}
	}
}
 
Example #7
Source File: ContextInitParameterTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #8
Source File: PortletContextResourcePatternResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #9
Source File: MiscTest.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
protected TestResult checkContextMajorVersion(PortletContext context) {
    TestResult result = new TestResult();
    result.setDescription("Ensure the expected major version number is returned.");

    String majorVersion = String.valueOf(context.getMajorVersion());
    ExpectedResults expectedResults = ExpectedResults.getInstance();
    String expected = expectedResults.getMajorVersion();
    if (majorVersion != null && majorVersion.equals(expected)) {
    	result.setReturnCode(TestResult.PASSED);
    } else {
    	TestUtils.failOnAssertion("major version", majorVersion, expected, result);
    }
    return result;
}
 
Example #10
Source File: JSPHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
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);
		}
	}
}
 
Example #11
Source File: JSPHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
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);
		}
	}
}
 
Example #12
Source File: AboutPortlet.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
	response.setContentType("text/html");
    PortletContext context = getPortletContext();
    PortletRequestDispatcher requestDispatcher =
    		context.getRequestDispatcher(VIEW_PAGE);
    requestDispatcher.include(request, response);
}
 
Example #13
Source File: PortletContextAwareProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void portletConfigAwareWithNullPortletContext() {
	PortletContextAwareProcessor processor = new PortletContextAwareProcessor((PortletContext) null);
	PortletConfigAwareBean bean = new PortletConfigAwareBean();
	assertNull(bean.getPortletConfig());
	processor.postProcessBeforeInitialization(bean, "testBean");
	assertNull(bean.getPortletConfig());
}
 
Example #14
Source File: AboutPortlet.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
protected void doEdit(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
	response.setContentType("text/html");
    PortletContext context = getPortletContext();
    PortletRequestDispatcher requestDispatcher =
    		context.getRequestDispatcher(EDIT_PAGE);
    requestDispatcher.include(request, response);
}
 
Example #15
Source File: PortletApplicationObjectSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #16
Source File: MockPortletRequest.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #17
Source File: SimpleAttributeTest.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
protected TestResult checkGetNullAttribute(PortletContext context) {
    TestResult res = new TestResult();
    res.setName("Retrieve Missing Context Attribute Test");
    res.setDescription("Retrieves an attribute bound to an invalid key set are retrieved as null");

    Object val = context.getAttribute(KEY);
    if(val != null) {
        res.setReturnCode(TestResult.FAILED);
        res.setResultMessage("Retrieved value: '"+val+"' for attribute '"+KEY+"'");
    }
    else {
        res.setReturnCode(TestResult.PASSED);
    }
    return res;
}
 
Example #18
Source File: MiscTest.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
protected TestResult checkContextServerInfo(PortletContext context) {
    TestResult result = new TestResult();
    result.setDescription("Ensure the expected server info is returned.");

    String serverInfo = context.getServerInfo();
    ExpectedResults expectedResults = ExpectedResults.getInstance();
    String expected = expectedResults.getServerInfo();
    if (serverInfo != null && serverInfo.equals(expected)) {
    	result.setReturnCode(TestResult.PASSED);
    } else {
    	TestUtils.failOnAssertion("server info", serverInfo, expected, result);
    }
    return result;
}
 
Example #19
Source File: AboutPortlet.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
protected void doHelp(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
	response.setContentType("text/html");
	PortletContext context = getPortletContext();
	PortletRequestDispatcher requestDispatcher =
			context.getRequestDispatcher(HELP_PAGE);
	requestDispatcher.include(request, response);
}
 
Example #20
Source File: ContextInitParameterTest.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
protected TestResult checkGetInitParameter(PortletContext context) {
    TestResult result = new TestResult();
    result.setDescription("Ensure that init parameters are retrieveable.");
    result.setSpecPLT("10.3.1");

    String value = context.getInitParameter(TEST_PARAM_NAME);
    if (TEST_PARAM_VALUE.equals(value)) {
    	result.setReturnCode(TestResult.PASSED);
    } else {
    	TestUtils.failOnAssertion("init parameter", value, TEST_PARAM_VALUE, result);
    }
    return result;
}
 
Example #21
Source File: JSPHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
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);
		}
	}
}
 
Example #22
Source File: FilterChainImpl.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
public void processFilter(ActionRequest req, ActionResponse res, Portlet portlet, PortletContext portletContext)
      throws IOException, PortletException {
   this.portlet = portlet;
   this.loader = Thread.currentThread().getContextClassLoader();
   this.portletContext = portletContext;
   doFilter(req, res);
}
 
Example #23
Source File: FilterChainImpl.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
public void processFilter(RenderRequest req, RenderResponse res, Portlet portlet, PortletContext portletContext)
      throws IOException, PortletException {
   this.portlet = portlet;
   this.loader = Thread.currentThread().getContextClassLoader();
   this.portletContext = portletContext;
   doFilter(req, res);
}
 
Example #24
Source File: PortletUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #25
Source File: PortletUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #26
Source File: MiscTest.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
protected TestResult checkContextMinorVersion(PortletContext context) {
    TestResult result = new TestResult();
    result.setDescription("Ensure the expected minor version number is returned.");

    String minorVersion = String.valueOf(context.getMinorVersion());
    ExpectedResults expectedResults = ExpectedResults.getInstance();
    String expected = expectedResults.getMinorVersion();
    if (minorVersion != null && minorVersion.equals(expected)) {
    	result.setReturnCode(TestResult.PASSED);
    } else {
    	TestUtils.failOnAssertion("minor version", minorVersion, expected, result);
    }
    return result;
}
 
Example #27
Source File: JSPHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
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);
		}
	}
}
 
Example #28
Source File: AbstractReflectivePortletTest.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
public void doHeaders(PortletConfig config, PortletContext context,
        RenderRequest request, RenderResponse response) {
}
 
Example #29
Source File: ResourceRequestWrapperChecker.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
@Override
public PortletContext getPortletContext() {
   return null;
}
 
Example #30
Source File: FilterManagerImpl.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.apache.pluto.container.FilterManager#processFilter(javax.portlet.RenderRequest, javax.portlet.RenderResponse, javax.portlet.Portlet, javax.portlet.PortletContext)
 */
@Override
public void processFilter(RenderRequest req, RenderResponse res, Portlet portlet,PortletContext portletContext) throws PortletException, IOException{
    filterchain.processFilter(req, res, portlet, portletContext);
}