Java Code Examples for javax.portlet.PortletRequest#getPortletSession()

The following examples show how to use javax.portlet.PortletRequest#getPortletSession() . 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: PortletHelper.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public static void setErrorMessage(PortletRequest request, String errorMsg, Throwable t)
{
	if ( errorMsg == null ) errorMsg = "null";
	PortletSession pSession = request.getPortletSession(true);
	pSession.setAttribute("error.message",errorMsg);

	OutputStream oStream = new ByteArrayOutputStream();
	PrintStream pStream = new PrintStream(oStream);

	log.error("{}", oStream);
	log.error("{}", pStream);

	// errorMsg = errorMsg .replaceAll("<","&lt;").replaceAll(">","&gt;");

	StringBuffer errorOut = new StringBuffer();
	errorOut.append("<p class=\"portlet-msg-error\">\n");
	errorOut.append(ComponentManager.get(FormattedText.class).escapeHtmlFormattedText(errorMsg));
	errorOut.append("\n</p>\n<!-- Traceback for this error\n");
	errorOut.append(oStream.toString());
	errorOut.append("\n-->\n");

	pSession.setAttribute("error.output",errorOut.toString());

	Map map = request.getParameterMap();
	pSession.setAttribute("error.map",map);
}
 
Example 2
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public static void debugPrint(PortletRequest request, String line)
{
	if ( line == null ) return;
	line = line.replaceAll("<","&lt;").replaceAll(">","&gt;");

	PortletSession pSession = request.getPortletSession(true);
	String debugOut = null;
	try {
		debugOut = (String) pSession.getAttribute("debug.print");
	} catch (Throwable t) {
		debugOut = null;
	}
	if ( debugOut == null ) {
		debugOut = line;
	} else {
		debugOut = debugOut + "\n" + line;
	}
	pSession.setAttribute("debug.print",debugOut);
}
 
Example 3
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public static void setErrorMessage(PortletRequest request, String errorMsg, Throwable t)
{
	if ( errorMsg == null ) errorMsg = "null";
	PortletSession pSession = request.getPortletSession(true);
	pSession.setAttribute("error.message",errorMsg);

	OutputStream oStream = new ByteArrayOutputStream();
	PrintStream pStream = new PrintStream(oStream);

	log.error("{}", oStream);
	log.error("{}", pStream);

	// errorMsg = errorMsg .replaceAll("<","&lt;").replaceAll(">","&gt;");

	StringBuffer errorOut = new StringBuffer();
	errorOut.append("<p class=\"portlet-msg-error\">\n");
	errorOut.append(ComponentManager.get(FormattedText.class).escapeHtmlFormattedText(errorMsg));
	errorOut.append("\n</p>\n<!-- Traceback for this error\n");
	errorOut.append(oStream.toString());
	errorOut.append("\n-->\n");

	pSession.setAttribute("error.output",errorOut.toString());

	Map map = request.getParameterMap();
	pSession.setAttribute("error.map",map);
}
 
Example 4
Source File: IMSBLTIPortlet.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private void clearSession(PortletRequest request)
{
	PortletSession pSession = request.getPortletSession(true);

	pSession.removeAttribute("sakai.url");
	pSession.removeAttribute("sakai.widget");
	pSession.removeAttribute("sakai.descriptor");
	pSession.removeAttribute("sakai.attemptdescriptor");

	for (String element : fieldList) {
		pSession.removeAttribute("sakai."+element);
	}
}
 
Example 5
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static String getDebugOutput(PortletRequest request)
{   
	PortletSession pSession = request.getPortletSession(true);
	try {
		return (String) pSession.getAttribute("debug.print");
	} catch (Throwable t) {
		return null;
	}
}
 
Example 6
Source File: TestPortlet.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
private void processStateAwarePhase(
        PortletRequest request, StateAwareResponse response) {
    String testId = getTestId(request);
    PortletTest test = (PortletTest) tests.get(testId);
    if (test != null) {
        TestResults results = test.doTest(getPortletConfig(),
                                          getPortletContext(),
                                          request,
                                          response);
        PortletSession session = request.getPortletSession();
        TestResults existingResults = (TestResults) 
            session.getAttribute(test.getClass().getName());
        if (existingResults != null) {
            for (TestResult result : results.getCollection()) {
                existingResults.add(result);
            }
        } else {
            session.setAttribute(test.getClass().getName(), results);
        }
    }
    Map<String, String[]> renderParameters = null;
    if (test != null) {
        renderParameters = test.getRenderParameters(request);
    }
    if (renderParameters == null) {
        renderParameters = new HashMap<String, String[]>();
    }
    renderParameters.put("testId", new String[] { testId });
    response.setRenderParameters(renderParameters);        
}
 
Example 7
Source File: PortletSessionBeanHolder.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the portlet session bean holder in a ThreadLocal object for the given portlet session. If no bean holder
 * exists in the session, a new one is created.
 * 
 * @param ps
 *           The portlet session.
 * @return The portlet session bean holder
 */
public static void setBeanHolder(PortletRequest req, PortletSessionScopedConfig config) {

   PortletSession ps = req.getPortletSession();
   String windowId = req.getWindowID();

   PortletSessionScopedBeanMap map = (PortletSessionScopedBeanMap) ps.getAttribute(ATTRIBNAME,
         PortletSession.APPLICATION_SCOPE);
   
   boolean createdMap = false;
   if (map == null) {
      map = new PortletSessionScopedBeanMap();
      ps.setAttribute(ATTRIBNAME, map, PortletSession.APPLICATION_SCOPE);
      createdMap = true;
   }

   PortletSessionBeanHolder holder = new PortletSessionBeanHolder(map, windowId, config);
   holders.set(holder);

   if (isTrace) {
      StringBuilder txt = new StringBuilder(80);
      txt.append("Set portlet session bean holder.");
      txt.append(" ThreadId: ").append(Thread.currentThread().getId());
      txt.append(", PortletSession: ").append(ps.getId());
      txt.append(", WindowId: ").append(windowId);
      txt.append(", Added new BeanMap to session: ").append(createdMap);
      LOG.debug(txt.toString());
   }
}
 
Example 8
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static String getDebugOutput(PortletRequest request)
{   
	PortletSession pSession = request.getPortletSession(true);
	try {
		return (String) pSession.getAttribute("debug.print");
	} catch (Throwable t) {
		return null;
	}
}
 
Example 9
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static String getErrorMessage(PortletRequest request)
{   
	PortletSession pSession = request.getPortletSession(true);
	try {
		return (String) pSession.getAttribute("error.message");
	} catch (Throwable t) {
		return null;
	}
}
 
Example 10
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static Map getErrorMap(PortletRequest request)
{   
	PortletSession pSession = request.getPortletSession(true);
	try {
		return (Map) pSession.getAttribute("error.map");
	} catch (Throwable t) {
		return null;
	}
}
 
Example 11
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static void clearErrorMessage(PortletRequest request)
{   
	PortletSession pSession = request.getPortletSession(true);
	pSession.removeAttribute("error.message");
	pSession.removeAttribute("error.output");
	pSession.removeAttribute("error.map");
}
 
Example 12
Source File: PortletUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Set the session attribute with the given name to the given value in the given scope.
 * Removes the session attribute if value is {@code null}, if a session existed at all.
 * Does not create a new session if not necessary!
 * @param request current portlet request
 * @param name the name of the session attribute
 * @param value the value of the session attribute
 * @param scope session scope of this attribute
 */
public static void setSessionAttribute(PortletRequest request, String name, Object value, int scope) {
	Assert.notNull(request, "Request must not be null");
	if (value != null) {
		request.getPortletSession().setAttribute(name, value, scope);
	}
	else {
		PortletSession session = request.getPortletSession(false);
		if (session != null) {
			session.removeAttribute(name, scope);
		}
	}
}
 
Example 13
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static void clearErrorMessage(PortletRequest request)
{   
	PortletSession pSession = request.getPortletSession(true);
	pSession.removeAttribute("error.message");
	pSession.removeAttribute("error.output");
	pSession.removeAttribute("error.map");
}
 
Example 14
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public static void clearDebugOutput(PortletRequest request)
{   
	PortletSession pSession = request.getPortletSession(true);
	pSession.removeAttribute("debug.print");
}
 
Example 15
Source File: PortletHelper.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public static void clearDebugOutput(PortletRequest request)
{   
	PortletSession pSession = request.getPortletSession(true);
	pSession.removeAttribute("debug.print");
}
 
Example 16
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest)
		throws Exception {

	PortletRequest request = webRequest.getNativeRequest(PortletRequest.class);
	PortletResponse response = webRequest.getNativeResponse(PortletResponse.class);

	if (PortletRequest.class.isAssignableFrom(parameterType) ||
			MultipartRequest.class.isAssignableFrom(parameterType)) {
		Object nativeRequest = webRequest.getNativeRequest(parameterType);
		if (nativeRequest == null) {
			throw new IllegalStateException(
					"Current request is not of type [" + parameterType.getName() + "]: " + request);
		}
		return nativeRequest;
	}
	else if (PortletResponse.class.isAssignableFrom(parameterType)) {
		Object nativeResponse = webRequest.getNativeResponse(parameterType);
		if (nativeResponse == null) {
			throw new IllegalStateException(
					"Current response is not of type [" + parameterType.getName() + "]: " + response);
		}
		return nativeResponse;
	}
	else if (PortletSession.class.isAssignableFrom(parameterType)) {
		return request.getPortletSession();
	}
	else if (PortletPreferences.class.isAssignableFrom(parameterType)) {
		return request.getPreferences();
	}
	else if (PortletMode.class.isAssignableFrom(parameterType)) {
		return request.getPortletMode();
	}
	else if (WindowState.class.isAssignableFrom(parameterType)) {
		return request.getWindowState();
	}
	else if (PortalContext.class.isAssignableFrom(parameterType)) {
		return request.getPortalContext();
	}
	else if (Principal.class.isAssignableFrom(parameterType)) {
		return request.getUserPrincipal();
	}
	else if (Locale.class == parameterType) {
		return request.getLocale();
	}
	else if (InputStream.class.isAssignableFrom(parameterType)) {
		if (!(request instanceof ClientDataRequest)) {
			throw new IllegalStateException("InputStream can only get obtained for Action/ResourceRequest");
		}
		return ((ClientDataRequest) request).getPortletInputStream();
	}
	else if (Reader.class.isAssignableFrom(parameterType)) {
		if (!(request instanceof ClientDataRequest)) {
			throw new IllegalStateException("Reader can only get obtained for Action/ResourceRequest");
		}
		return ((ClientDataRequest) request).getReader();
	}
	else if (OutputStream.class.isAssignableFrom(parameterType)) {
		if (!(response instanceof MimeResponse)) {
			throw new IllegalStateException("OutputStream can only get obtained for Render/ResourceResponse");
		}
		return ((MimeResponse) response).getPortletOutputStream();
	}
	else if (Writer.class.isAssignableFrom(parameterType)) {
		if (!(response instanceof MimeResponse)) {
			throw new IllegalStateException("Writer can only get obtained for Render/ResourceResponse");
		}
		return ((MimeResponse) response).getWriter();
	}
	else if (Event.class == parameterType) {
		if (!(request instanceof EventRequest)) {
			throw new IllegalStateException("Event can only get obtained from EventRequest");
		}
		return ((EventRequest) request).getEvent();
	}
	return super.resolveStandardArgument(parameterType, webRequest);
}
 
Example 17
Source File: DefineObjectsTag286.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
/**
 * Processes the <CODE>defineObjects</CODE> tag.
 * @return <CODE>SKIP_BODY</CODE>
 */
public int doStartTag() throws JspException {
	
	ServletRequest servletRequest = pageContext.getRequest();
	
	PortletRequest portletRequest = 
		(PortletRequest) servletRequest.getAttribute(Constants.PORTLET_REQUEST);
	
	PortletResponse portletResponse = 
		(PortletResponse) servletRequest.getAttribute(Constants.PORTLET_RESPONSE);
	
	PortletConfig portletConfig = 
		(PortletConfig) servletRequest.getAttribute(Constants.PORTLET_CONFIG);
	
	PortletSession portletSession = portletRequest.getPortletSession(false);
	
	Map<String, Object> portletSessionScope = null;
	if(portletSession != null){
		portletSessionScope = (Map<String, Object>)portletSession.getAttributeMap();// getMap();
	}
	else{
		portletSessionScope = new HashMap<String, Object>();
	}
	
	PortletPreferences portletPreferences = portletRequest.getPreferences();
	
	Map<String, String[]> portletPreferencesValues = null;
	if(portletPreferences != null){
		portletPreferencesValues = portletPreferences.getMap();
	}
	else{
		portletPreferencesValues = new HashMap<String, String[]>();
	}
	
	// set attributes render and request
	setPortletRequestResponseAttribute(portletRequest, portletResponse);
	
	// set attribute portletConfig
	setAttribute(portletConfig, "portletConfig");
	
	// set attribute portletSession
	setAttribute(portletSession, "portletSession");
	
	//set attribute portletSession
	setAttribute(portletSessionScope, "portletSessionScope");
	
	// set attribute portletPreferences
	setAttribute(portletPreferences, "portletPreferences");
	
	// set attribute portletPreferences
	setAttribute(portletPreferencesValues, "portletPreferencesValues");    	
	
    return SKIP_BODY;
}
 
Example 18
Source File: AddlEnvironmentTests_SPEC2_18_Sessions_invalidate2.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
protected void processTCKReq(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

  PortletRequest portletReq = (PortletRequest) request.getAttribute("javax.portlet.request");
  PortletResponse portletResp = (PortletResponse) request.getAttribute("javax.portlet.response");
  PortletSession portletSession = portletReq.getPortletSession();
  

  PrintWriter writer = ((MimeResponse) portletResp).getWriter();

  JSR286SpecTestCaseDetails tcd = new JSR286SpecTestCaseDetails();

  /* TestCase: V2AddlEnvironmentTests_SPEC2_18_Sessions_httpSession5 */
  /* Details: "If the PortletSession object is invalidated by a */
  /* portlet, the portlet container must invalidate the associated */
  /* HttpSession object" */
  {
     String tcid = portletReq.getParameter(BUTTON_PARAM_NAME);
     if (tcid == null || !tcid.equals(V2ADDLENVIRONMENTTESTS_SPEC2_18_SESSIONS_HTTPSESSION5)) {
        
        // generate test link 
        
        PortletURL rurl = ((MimeResponse)portletResp).createRenderURL();
        rurl.setParameter(BUTTON_PARAM_NAME, V2ADDLENVIRONMENTTESTS_SPEC2_18_SESSIONS_HTTPSESSION5);
        TestLink tl = new TestLink(V2ADDLENVIRONMENTTESTS_SPEC2_18_SESSIONS_HTTPSESSION5, rurl);
        tl.writeTo(writer);
     } else {
        
        // perform test
        
        TestResult result = tcd.getTestResultFailed(V2ADDLENVIRONMENTTESTS_SPEC2_18_SESSIONS_HTTPSESSION5);
        portletSession.invalidate();
        if (!request.isRequestedSessionIdValid()) {
          result.setTcSuccess(true);
        } else {
          result.appendTcDetail("Failed because session is not invalidated.");
        }
        result.writeTo(writer);
     }
  }
  
}
 
Example 19
Source File: PortletContentGenerator.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Check and prepare the given request and response according to the settings
 * of this generator. Checks for a required session, and applies the number of
 * cache seconds configured for this generator (if it is a render request/response).
 * @param request current portlet request
 * @param response current portlet response
 * @throws PortletException if the request cannot be handled because a check failed
 */
protected final void check(PortletRequest request, PortletResponse response) throws PortletException {
	if (this.requireSession) {
		if (request.getPortletSession(false) == null) {
			throw new PortletSessionRequiredException("Pre-existing session required but none found");
		}
	}
}
 
Example 20
Source File: PortletUtils.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Check the given request for a session attribute of the given name in the given scope.
 * Returns {@code null} if there is no session or if the session has no such attribute in that scope.
 * Does not create a new session if none has existed before!
 * @param request current portlet request
 * @param name the name of the session attribute
 * @param scope session scope of this attribute
 * @return the value of the session attribute, or {@code null} if not found
 */
public static Object getSessionAttribute(PortletRequest request, String name, int scope) {
	Assert.notNull(request, "Request must not be null");
	PortletSession session = request.getPortletSession(false);
	return (session != null ? session.getAttribute(name, scope) : null);
}