5

I've created a RenderingPlugin, for use in WebSphere Portal, which is invoked serverside before sending markup to client. The plugin loops through all cookies and if 'test' is not found, I'd like to set that cookie.

I know this is possible with a HttpServletResponse but the RenderingPlugin doesn't have access to that object. It only has a HttpServletRequest.

Is there another way to do this?

public class Request implements com.ibm.workplace.wcm.api.plugin.RenderingPlugin {

    @Override
    public boolean render(RenderingPluginModel rpm) throws RenderingPluginException {

        boolean found = false;

        HttpServletRequest servletRequest = (HttpServletRequest) rpm.getRequest();
        Cookie[] cookie = servletRequest.getCookies();

        // loop through cookies
        for (int i = 0; i < cookie.length; i++) {

            // if test found
            if (cookie[i].getName().equals("test")) {

                found = true;
            }
        }

        if (!found){

            // set cookie here
        }
    }
}
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150

2 Answers2

1

Did you try using javascript code to set the cookie ?

<script>
document.cookie = "test=1;path=/";
</script>

you send this as part of the content you give to the Writer rpm.getWriter() and it will be executed by the browser.

Ahmed M
  • 71
  • 1
  • 4
  • the problem is if i embed the rendering plugin multiple times on the same page, the cookie won't have been created yet after initial rendering plugin is resolved. – bobbyrne01 May 05 '15 at 20:38
  • 1
    I think this also would happen if you had access to the http response object and called addCookie. So this problem is not specific for javascript solution. You can use the Session object to store stateful information like is this the first time the plugin is rendered in the scope of the current request in order to avoid setting the cookie many times (if it is ever a problem). – Ahmed M May 06 '15 at 06:26
0

I had a problem to simulate a cookie, that is only sending in production, in my test environment. I solve with HttpServletRequestWrapper in a filter.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        Cookie cookie = new Cookie("Key", "Value");
        chain.doFilter(new CustomRequest((HttpServletRequest) request, cookie), response);
    }
}

class CustomRequest extends HttpServletRequestWrapper {
     private final Cookie cookie;

        public CustomRequest(HttpServletRequest request, Cookie cookie) {
            super(request);
            this.cookie = cookie;
        }

        @Override
        public Cookie[] getCookies() {
         //This is a example, get all cookies here and put your with a new Array
            return new Cookie[] {cookie};
        }
    }

This filter is only started in test environment. My class WebConfig take care of this:

 @HandlesTypes(WebApplicationInitializer.class)
 public class WebConfig implements WebApplicationInitializer
Guilherme
  • 877
  • 9
  • 16