Saher Ahwal
Saher Ahwal

Reputation: 9237

get HttpServletResponse from liferay portal

I am trying to get the HttpServletResponse from liferay portal. I am also working with icefaces.

PortletResponse response1 = (PortletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        HttpServletResponse response = (HttpServletResponse)response1;

I get the following Exception:

Caused by: java.lang.ClassCastException: com.liferay.portlet.RenderResponseImpl cannot be cast to javax.servlet.http.HttpServletResponse

Upvotes: 2

Views: 9610

Answers (4)

dtruong
dtruong

Reputation: 311

Try PortalUtil.getHttpServletResponse(portletResponse)

Upvotes: 7

Septimiu
Septimiu

Reputation: 1

Try getting the native response and then casting it:

FilterServletResponseWrapper filterResponse = (FilterServletResponseWrapper) RequestContextHolder.getRequestContext().getExternalContext().getNativeResponse();
HttpServletResponse response = (HttpServletResponse)filterResponse.getResponse();

Upvotes: 0

BCG
BCG

Reputation: 1170

FacesContext will return PortletRequest/Response objects if you are using the portlet bridge.

What are you trying to do with the HttpServletResponse?

If you are trying to generate binary content, this will only work in the Resource phase of a JSR 286 portlet (otherwise you cannot set headers). It will never work in a JSR 168 portlet. If you need to do this in a JSR 168 portlet, you need to use a helper servlet.

In any case, if you are trying to use a third party lib that requires HttpServletResponse for its API, you can use a PortletRequestDispatcher to dispatch to a JSP or a servlet and then use the HttpServletResponse that is available to you there.

Upvotes: 1

Feras Odeh
Feras Odeh

Reputation: 9296

May be this Helps

Generate PDF File in Portlet

Upvotes: 1

Related Questions