javax.portlet.PortletException Java Examples

The following examples show how to use javax.portlet.PortletException. 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: PortletUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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");
	}
}
 
Example #2
Source File: RegisterEmployeePortlet.java    From journaldev with MIT License 6 votes vote down vote up
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException{
	// Create request dispatcher
	PortletRequestDispatcher dispatcher =  this.getPortletContext().getNamedDispatcher("RegisterEmployeeServlet");
	try {
		// Include
		dispatcher.include(request, response);
		// Set render parameter
		response.setRenderParameter("status", "success");
	}
	catch(Exception ex){
		// Set render parameter
		response.setRenderParameter("status", "failed");
		response.setRenderParameter("exception", ex.getMessage());
	}

}
 
Example #3
Source File: DefaultAnnotationHandlerMapping.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void validate(PortletRequest request) throws PortletException {
	if (!PortletAnnotationMappingUtils.checkHeaders(this.headers, request)) {
		throw new PortletRequestBindingException("Header conditions \"" +
				StringUtils.arrayToDelimitedString(this.headers, ", ") +
				"\" not met for actual request");
	}
	if (!this.methods.isEmpty()) {
		if (!(request instanceof ClientDataRequest)) {
			throw new PortletRequestMethodNotSupportedException(StringUtils.toStringArray(this.methods));
		}
		String method = ((ClientDataRequest) request).getMethod();
		if (!this.methods.contains(method)) {
			throw new PortletRequestMethodNotSupportedException(method, StringUtils.toStringArray(this.methods));
		}
	}
}
 
Example #4
Source File: RenderResponseNamespaceTwo.java    From journaldev with MIT License 6 votes vote down vote up
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
	response.getWriter().println("<html>"
			+ "<head>"
			+ "<script>"
			+ "var "+response.getNamespace()+"message = 'Handle Submit, Portlet Two';"
			+ "function "+response.getNamespace()+"handleSubmit(){"
			+ " alert('Handle Submit, The Portlet Is '+"+response.getNamespace()+"message);"
			+ "}"
			+ "</script>"
			+ "</head>"
			+ "<form action="+response.createActionURL()+">"
			+ "  <input type='button' value='Click On Me' onclick='"+response.getNamespace()+"handleSubmit()'/>"
			+ "</form>"
			+ "</html>");

}
 
Example #5
Source File: ActionRequestReader.java    From journaldev with MIT License 6 votes vote down vote up
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
	// Handle default MIME type request
	if(request.getContentType().equals("application/x-www-form-urlencoded")){
		String message = request.getParameter("message");
		System.out.println(message);
	}
	// Handle multipart request
	else if(request.getContentType().contains("multipart/form-data")){
		// Create FileItemFactory
		FileItemFactory factory = new DiskFileItemFactory();
		// Create PortletFileUpload instance
		PortletFileUpload fileUpload = new PortletFileUpload(factory);
		try {
			// Instead of parsing the request ourselves, let Apache PortletFileUpload do that
			List<FileItem> files = fileUpload.parseRequest(request);
			// Iterate over files
			for(FileItem item : files){
				// Print out some of information
				System.out.println("File Uploaded Name Is : "+item.getName()+" , Its Size Is :: "+item.getSize());
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		System.out.println(response.encodeURL("/index.html"));
	}
}
 
Example #6
Source File: AdapterPortlet.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
public void processAction(
    ActionRequest request,
    ActionResponse response)
    throws PortletException, IOException {
   
	PortletTracer.info(Constants.NOME_MODULO, "AdapterPortlet", "action", "Invocato");
    // set into threadLocal variables the jsr 168 portlet object 
    PortletAccess.setPortletConfig(getPortletConfig());
    PortletAccess.setPortletRequest(request);
    PortletAccess.setPortletResponse(response);
    PortletSession portletSession = request.getPortletSession();
    portletSession.setAttribute("BrowserLocale", request.getLocale());
    
    
    processService(request, response);
}
 
Example #7
Source File: SimplePortletPostProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
	if (bean instanceof Portlet) {
		PortletConfig config = this.portletConfig;
		if (config == null || !this.useSharedPortletConfig) {
			config = new DelegatingPortletConfig(beanName, this.portletContext, this.portletConfig);
		}
		try {
			((Portlet) bean).init(config);
		}
		catch (PortletException ex) {
			throw new BeanInitializationException("Portlet.init threw exception", ex);
		}
	}
	return bean;
}
 
Example #8
Source File: GenericPortletBeanTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void unknownRequiredInitParameter() throws Exception {
	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());
}
 
Example #9
Source File: GenericPortletBean.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create new PortletConfigPropertyValues.
 * @param config PortletConfig we'll use to take PropertyValues from
 * @param requiredProperties set of property names we need, where
 * we can't accept default values
 * @throws PortletException if any required properties are missing
 */
private PortletConfigPropertyValues(PortletConfig config, Set<String> requiredProperties)
	throws PortletException {

	Set<String> missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ?
			new HashSet<String>(requiredProperties) : null;

	Enumeration<String> en = config.getInitParameterNames();
	while (en.hasMoreElements()) {
		String property = en.nextElement();
		Object value = config.getInitParameter(property);
		addPropertyValue(new PropertyValue(property, value));
		if (missingProps != null) {
			missingProps.remove(property);
		}
	}

	// fail if we are still missing properties
	if (missingProps != null && missingProps.size() > 0) {
		throw new PortletException(
			"Initialization from PortletConfig for portlet '" + config.getPortletName() +
			"' failed; the following required properties were missing: " +
			StringUtils.collectionToDelimitedString(missingProps, ", "));
	}
}
 
Example #10
Source File: RenderResponseNamespaceOne.java    From journaldev with MIT License 6 votes vote down vote up
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
	response.getWriter().println("<html>"
			+ "<head>"
			+ "<script>"
			+ "var "+response.getNamespace()+"message = 'Handle Submit, Portlet One';"
			+ "function "+response.getNamespace()+"handleSubmit(){"
			+ " alert('Handle Submit, The Portlet Is '+"+response.getNamespace()+"message);"
			+ "}"
			+ "</script>"
			+ "</head>"
			+ "<form action="+response.createActionURL()+">"
			+ "  <input type='button' value='Click On Me' onclick='"+response.getNamespace()+"handleSubmit()'/>"
			+ "</form>"
			+ "</html>");

}
 
Example #11
Source File: ComplexPortletApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterRenderCompletion(
		RenderRequest request, RenderResponse response, Object handler, Exception ex)
		throws PortletException {
	if (request.getAttribute("test2-remove-after") != null) {
		throw new PortletException("Wrong interceptor order");
	}
	request.removeAttribute("test1-remove-after");
}
 
Example #12
Source File: DispatcherPortletTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void incorrectLocaleInRequest() throws Exception {
	MockRenderRequest request = new MockRenderRequest();
	MockRenderResponse response = new MockRenderResponse();
	request.setParameter("myParam", "requestLocaleChecker");
	request.addPreferredLocale(Locale.ENGLISH);
	complexDispatcherPortlet.doDispatch(request, response);
	Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE);
	Exception exception = (Exception) model.get("exception");
	assertTrue(exception.getClass().equals(PortletException.class));
	assertEquals("Incorrect Locale in RenderRequest", exception.getMessage());
	InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE);
	assertEquals("failed-default-1", view.getBeanName());
}
 
Example #13
Source File: GenericPortletBean.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Map config parameters onto bean properties of this portlet, and
 * invoke subclass initialization.
 * @throws PortletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 */
@Override
public final void init() throws PortletException {
	if (logger.isInfoEnabled()) {
		logger.info("Initializing portlet '" + getPortletName() + "'");
	}

	// Set bean properties from init parameters.
	try {
		PropertyValues pvs = new PortletConfigPropertyValues(getPortletConfig(), this.requiredProperties);
		BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
		ResourceLoader resourceLoader = new PortletContextResourceLoader(getPortletContext());
		bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
		initBeanWrapper(bw);
		bw.setPropertyValues(pvs, true);
	}
	catch (BeansException ex) {
		logger.error("Failed to set bean properties on portlet '" + getPortletName() + "'", ex);
		throw ex;
	}

	// let subclasses do whatever initialization they like
	initPortletBean();

	if (logger.isInfoEnabled()) {
		logger.info("Portlet '" + getPortletName() + "' configured successfully");
	}
}
 
Example #14
Source File: ComplexPortletApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean preHandleRender(RenderRequest request, RenderResponse response, Object handler)
	throws PortletException {
	if (request.getAttribute("test2-remove-never") != null) {
		throw new PortletException("Wrong interceptor order");
	}
	request.setAttribute("test1-remove-never", "test1-remove-never");
	request.setAttribute("test1-remove-post", "test1-remove-post");
	request.setAttribute("test1-remove-after", "test1-remove-after");
	return true;
}
 
Example #15
Source File: HelloPortlet.java    From journaldev with MIT License 5 votes vote down vote up
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException{
	// Get all attributes' names that are defined along side of Application Scope
	Enumeration<String> names = request.getPortletSession().getAttributeNames(PortletSession.APPLICATION_SCOPE);
	StringBuffer buffer = new StringBuffer();
	while(names.hasMoreElements()){
		String name = names.nextElement();
		// Get the attribute's value
		buffer.append(name+" :: "+request.getPortletSession().getAttribute(name,PortletSession.APPLICATION_SCOPE)+"\n");
	}
	response.getWriter().print("<form action="+response.createActionURL()+">"
			+ "<p>Portlet Session Attributes</p>"
			+ buffer
			+ "<input type='submit' value='Just Do Action'/>"
			+ "</form>");
}
 
Example #16
Source File: ActionRequestReader.java    From journaldev with MIT License 5 votes vote down vote up
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
	response.getWriter().println("<form action="+response.createActionURL()+" method='POST' enctype='application/x-www-form-urlencoded'>"
			+ "Enter message : <input type='text' id='message' name='message'/>"
			+ "<input type='submit' value='Submit Form Message'/><br/>"
			+ "</form>"
			+ "<form action="+response.createActionURL()+" method='POST' enctype='multipart/form-data'>"
			+ "Upload Your File: <input type='file' name='fileUpload'/>"
			+ "<input type='submit' value='Upload File'/>"
			+ "</form>");

}
 
Example #17
Source File: DispatcherPortletTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void unknownHandlerRenderRequest() throws Exception {
	MockRenderRequest request = new MockRenderRequest();
	MockRenderResponse response = new MockRenderResponse();
	request.setParameter("myParam", "unknown");
	complexDispatcherPortlet.doDispatch(request, response);
	Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE);
	Exception exception = (Exception)model.get("exception");
	assertTrue(exception.getClass().equals(PortletException.class));
	assertTrue(exception.getMessage().indexOf("No adapter for handler") != -1);
	InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE);
	assertEquals("failed-default-1", view.getBeanName());
}
 
Example #18
Source File: DispatcherPortletTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void unknownHandlerActionRequest() throws Exception {
	MockActionRequest request = new MockActionRequest();
	MockActionResponse response = new MockActionResponse();
	request.setParameter("myParam", "unknown");
	complexDispatcherPortlet.processAction(request, response);
	String exceptionParam = response.getRenderParameter(DispatcherPortlet.ACTION_EXCEPTION_RENDER_PARAMETER);
	assertNotNull(exceptionParam);
	assertTrue(exceptionParam.startsWith(PortletException.class.getName()));
	assertTrue(exceptionParam.indexOf("No adapter for handler") != -1);
}
 
Example #19
Source File: ComplexPortletApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void postHandleRender(
		RenderRequest request, RenderResponse response, Object handler, ModelAndView modelAndView)
		throws PortletException {
	if (request.getAttribute("test2-remove-post") != null) {
		throw new PortletException("Wrong interceptor order");
	}
	if (!"test1-remove-post".equals(request.getAttribute("test1-remove-post"))) {
		throw new PortletException("Incorrect request attribute");
	}
	request.removeAttribute("test1-remove-post");
}
 
Example #20
Source File: DispatcherPortletTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws PortletException {
	complexPortletConfig.addInitParameter("publishContext", "false");

	complexDispatcherPortlet.setContextClass(ComplexPortletApplicationContext.class);
	complexDispatcherPortlet.setNamespace("test");
	complexDispatcherPortlet.addRequiredProperty("publishContext");
	complexDispatcherPortlet.init(complexPortletConfig);
}
 
Example #21
Source File: ComplexPortletApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterRenderCompletion(
		RenderRequest request, RenderResponse response, Object handler, Exception ex)
		throws Exception {
	if (request.getAttribute("test1-remove-after") == null) {
		throw new PortletException("Wrong interceptor order");
	}
	request.removeAttribute("test2-remove-after");
}
 
Example #22
Source File: DispatcherPortletTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void incorrectLocaleInLocalContextHolder() throws Exception {
	MockRenderRequest request = new MockRenderRequest();
	MockRenderResponse response = new MockRenderResponse();
	request.setParameter("myParam", "contextLocaleChecker");
	request.addPreferredLocale(Locale.ENGLISH);
	complexDispatcherPortlet.doDispatch(request, response);
	Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE);
	Exception exception = (Exception) model.get("exception");
	assertTrue(exception.getClass().equals(PortletException.class));
	assertEquals("Incorrect Locale in LocaleContextHolder", exception.getMessage());
	InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE);
	assertEquals("failed-default-1", view.getBeanName());
}
 
Example #23
Source File: DispatcherPortletTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void dispatcherPortletRefresh() throws PortletException {
	MockPortletContext portletContext = new MockPortletContext("org/springframework/web/portlet/context");
	DispatcherPortlet portlet = new DispatcherPortlet();

	portlet.init(new MockPortletConfig(portletContext, "empty"));
	PortletContextAwareBean contextBean = (PortletContextAwareBean)
			portlet.getPortletApplicationContext().getBean("portletContextAwareBean");
	PortletConfigAwareBean configBean = (PortletConfigAwareBean)
			portlet.getPortletApplicationContext().getBean("portletConfigAwareBean");
	assertSame(portletContext, contextBean.getPortletContext());
	assertSame(portlet.getPortletConfig(), configBean.getPortletConfig());
	PortletMultipartResolver multipartResolver = portlet.getMultipartResolver();
	assertNotNull(multipartResolver);

	portlet.refresh();

	PortletContextAwareBean contextBean2 = (PortletContextAwareBean)
			portlet.getPortletApplicationContext().getBean("portletContextAwareBean");
	PortletConfigAwareBean configBean2 = (PortletConfigAwareBean)
			portlet.getPortletApplicationContext().getBean("portletConfigAwareBean");
	assertSame(portletContext, contextBean.getPortletContext());
	assertSame(portlet.getPortletConfig(), configBean.getPortletConfig());
	assertTrue(contextBean != contextBean2);
	assertTrue(configBean != configBean2);
	PortletMultipartResolver multipartResolver2 = portlet.getMultipartResolver();
	assertTrue(multipartResolver != multipartResolver2);

	portlet.destroy();
}
 
Example #24
Source File: ComplexPortletApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response)
	throws PortletException, IOException {
	if (!Locale.CANADA.equals(LocaleContextHolder.getLocale())) {
		throw new PortletException("Incorrect Locale in LocaleContextHolder");
	}
	response.getWriter().write("locale-ok");
	return null;
}
 
Example #25
Source File: ComplexPortletApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response)
	throws PortletException, IOException {
	if (!Locale.CANADA.equals(request.getLocale())) {
		throw new PortletException("Incorrect Locale in RenderRequest");
	}
	response.getWriter().write("locale-ok");
	return null;
}
 
Example #26
Source File: RequestParameters.java    From journaldev with MIT License 5 votes vote down vote up
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
	// Check if the loggedIn is null for initial state
	if(request.getParameter("loggedIn") == null){
		response.getWriter().println("<form action="+response.createActionURL()+">"
				+ "Enter Username : <input type='text' id='username' name='username'/>"
				+ "Enter Password : <input type='password' id='password' name='password'/>"
				+ "<input type='submit' value='Login'/>"
				+ "</form>");	
	}
	else {
		// Get loggedIn value from the request parameter
		boolean loggedIn = Boolean.parseBoolean(request.getParameter("loggedIn"));
		if(loggedIn){
			response.getWriter().println("<form action="+response.createActionURL()+">"
					+ "<p>You're logged in</p>"
					+ "</form>");
		}
		else {
			response.getWriter().println("<form action="+response.createActionURL()+">"
					+ "<span style='color:red'>Try another</span><br/>"
					+ "Enter Username : <input type='text' id='username' name='username'/>"
					+ "Enter Password : <input type='password' id='password' name='password'/>"
					+ "<input type='submit' value='Login By the Portlet'/>"
					+ "<input type='submit' value='Login By Servlet'/>"
					+ "</form>");			
		}	
	}	
}
 
Example #27
Source File: MockPortletRequestDispatcher.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void include(PortletRequest request, PortletResponse response) throws PortletException, IOException {
	Assert.notNull(request, "Request must not be null");
	Assert.notNull(response, "Response must not be null");
	if (!(response instanceof MockMimeResponse)) {
		throw new IllegalArgumentException("MockPortletRequestDispatcher requires MockMimeResponse");
	}
	((MockMimeResponse) response).setIncludedUrl(this.url);
	if (logger.isDebugEnabled()) {
		logger.debug("MockPortletRequestDispatcher: including URL [" + this.url + "]");
	}
}
 
Example #28
Source File: MockPortletRequestDispatcher.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void forward(PortletRequest request, PortletResponse response) throws PortletException, IOException {
	Assert.notNull(request, "Request must not be null");
	Assert.notNull(response, "Response must not be null");
	if (!(response instanceof MockMimeResponse)) {
		throw new IllegalArgumentException("MockPortletRequestDispatcher requires MockMimeResponse");
	}
	((MockMimeResponse) response).setForwardedUrl(this.url);
	if (logger.isDebugEnabled()) {
		logger.debug("MockPortletRequestDispatcher: forwarding to URL [" + this.url + "]");
	}
}
 
Example #29
Source File: FrameworkPortlet.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Delegate render requests to processRequest/doRenderService.
 */
@Override
protected final void doDispatch(RenderRequest request, RenderResponse response)
		throws PortletException, IOException {

	processRequest(request, response);
}
 
Example #30
Source File: FrameworkPortlet.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Delegate action requests to processRequest/doActionService.
 */
@Override
public final void processAction(ActionRequest request, ActionResponse response)
		throws PortletException, IOException {

	processRequest(request, response);
}