7

I am trying to develop a service.

The point is that my index.xhtml should get parameters (either POST and GET) and cookies from HTTP Request.

I tried combination with <f:metadata> and <f:event type="preRenderView"> like this:

<f:metadata>  
    <f:event type="preRenderView" listener="#{deConversation.start}"/>  
</f:metadata>

Code for deConversation.start:

public void start(ComponentSystemEvent event) {
    System.out.println("checkLogin");
    HttpServletRequest request = SsoHelper.getRequest();
    String requestSessId = SsoHelper.getRequestSessionId(request);
    String requestRedirect = SsoHelper.getRequestRedirect(request);

    System.out.println("sessId " + requestSessId);

    if (requestRedirect == null || requestRedirect.isEmpty()) {
        requestRedirect = "self";
    }

    if (requestSessId != null) {
        trySessId(requestSessId, requestRedirect);
    }

    externalResourcesHandler.setExternalRedirect(requestRedirect);
    tryToBeginConversation();

    if (!isAuthorized()) {
        SsoHelper.performNavigation("auth");
    }
}

SsoHelper just provides api like this:

public static String getRequestSessionId(HttpServletRequest request) {
    Map<String, Object> cookieMap = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap();
    String requestDeSessionId = null;
    if (cookieMap.containsKey("de_session_id")) {
        requestDeSessionId = ((Cookie) cookieMap.get("de_session_id")).getValue();
    }
    return requestDeSessionId;
}

public static String getRequestRedirect(HttpServletRequest request) {
    return getRequestParam(request, "redirect", "self");
}

public static String getRequestExternalCss(HttpServletRequest request) {
    return getRequestParam(request, "externalcss", null);
}

public static String getRequestParam(HttpServletRequest request, String name, String defaultValue) {
    String[] paramValues = HttpServletRequestHelper.getParamValues(request, name);
    String paramValue = null;
    if (paramValues != null && paramValues.length != 0) {
        paramValue = paramValues[0];
    }
    if(paramValue == null){
        paramValue = defaultValue;
    }
    return paramValue;
}

public static HttpServletRequest getRequest() {
    return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
}

public static void performNavigation(String destination) {
    FacesContext context = FacesContext.getCurrentInstance();
    ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler();
    handler.performNavigation(destination);
}

The point is that I could not get any POST parameters or cookie in method start(). I can get only GET parameters.

Is there any possibilty to read cookies and POST parameters using <f:event type="preRenderView">?

Indigo23
  • 3
  • 3
Jerome
  • 2,397
  • 2
  • 12
  • 8
  • You can do it [in this way](http://stackoverflow.com/questions/550448/get-request-and-session-parameters-and-attributes-from-jsf-pages] but I think you should consider before [some recommendations](https://forums.oracle.com/forums/thread.jspa?threadID=2218557). Regards, – Rodmar Conde May 21 '13 at 09:58

2 Answers2

0

I think you should call init method with @PostConstruct so that the init() will be called before page rendered & take global variable to assign Session or Cookies values in variable(s) by that you can achieve your requirements.

Example:

@PostConstruct
public void init(){
    // Your Response
    Map request = (Map)FacesContext.getCurrentInstance().getExternalContext().getResponse();
    request.get("Your Key");

    // Cookies
    Map cookie = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap();
    cookie.get("Your Key");

    Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
    session.get("Your Key"); 
}

Try this i think it will help you.

0

You can also use the f:viewParam tag to get a POST/GET parameter and bind it to a property of your backing bean.

More precisely the tag causes a UIViewParameter to be attached as metadata for the current view. Assuming your POST parameter name is 'mypostparam' basically you can do something like:

<f:metadata> 
    <f:viewParam id="mypostparam" name="mypostparam" value="#{deConversation.myPostParam}"/>
    <f:event type="preRenderView" listener="#{deConversation.start}"/>  
</f:metadata>

In your backing bean you should declare the property myPostParam and the standard setter/getter methods as well. As a benefit you can nest standard converters and validators to let JSF take care of it.

The only limit is that this is not working during the render response phase (you can get the POST param while navigating via an action method returning a String). It will work on the view which is the target of the POST submit.

elbuild
  • 4,869
  • 4
  • 24
  • 31