Refine search
public void download() throws IOException { // Prepare. byte[] pdfData = getItSomehow(); FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); // Initialize response. response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide. response.setContentType("application/pdf"); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename. response.setHeader("Content-disposition", "attachment; filename=\"name.pdf\""); // The Save As popup magic is done here. You can give it any filename you want, this only won't work in MSIE, it will use current request URL as filename instead. // Write file to response. OutputStream output = response.getOutputStream(); output.write(pdfData); output.close(); // Inform JSF to not take the response in hands. facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it's already written with a file and closed. }
public void download() throws IOException { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide. response.setContentType("application/xml"); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename. response.setHeader("Content-disposition", "attachment; filename=\"name.xml\""); // The Save As popup magic is done here. You can give it any filename you want, this only won't work in MSIE, it will use current request URL as filename instead. BufferedInputStream input = null; BufferedOutputStream output = null; try { input = new BufferedInputStream(getYourXmlAsInputStream()); output = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[10240]; for (int length; (length = input.read(buffer)) > 0;) { output.write(buffer, 0, length); } } finally { close(output); close(input); } facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it's already written with a file and closed. }
ExternalContext externalContext = facesContext.getExternalContext(); HttpServletResponse response = (HttpServletResponse) externalContext .getResponse();
/** * @see Faces#getResponse() */ public static HttpServletResponse getResponse(FacesContext context) { return (HttpServletResponse) context.getExternalContext().getResponse(); }
/** * Gets the current response instance. * * @return the current response. */ private HttpServletResponse getResponse(final FacesContext context) { return (HttpServletResponse)context.getExternalContext().getResponse(); }
/** * @see Faces#getResponse() */ public static HttpServletResponse getResponse(FacesContext context) { return (HttpServletResponse) context.getExternalContext().getResponse(); }
/** * Determine if we are processing a portlet RenderResponse. * * @param facesContext The current FacesContext. * @return <code>true</code> if we are processing a RenderResponse, * <code>false</code> otherwise. */ public static boolean isRenderResponse(FacesContext facesContext) { return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getResponse() instanceof RenderResponse; }
private static HttpServletResponse getHttpServletResponse() { ServletResponse response = null; final FacesContext facesContext = FacesContext.getCurrentInstance(); if (facesContext != null) { response = (ServletResponse) facesContext.getExternalContext().getResponse(); } if (response != null && response instanceof HttpServletResponse) { return (HttpServletResponse) response; } return null; }
private static HttpServletResponse getHttpServletResponse() { final FacesContext facesContext = FacesContext.getCurrentInstance(); return facesContext == null ? null : (HttpServletResponse) facesContext.getExternalContext().getResponse(); }
public static void ensureXFrameOptionsHeader(final FacesContext facesContext) { final Object response = facesContext.getExternalContext().getResponse(); if (response instanceof HttpServletResponse) { ((HttpServletResponse) response).setHeader("X-Frame-Options", "DENY"); } } }
protected void addCookie(Cookie cookie) { ( (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext() .getResponse() ).addCookie(cookie); }
... FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); HttpServletResponse hp = (HttpServletResponse) ec.getResponse(); hp.setStatus(hp.getStatus(), "my new message"); ...
private void setCookieValue(String value) { FacesContext ctx = FacesContext.getCurrentInstance(); if (ctx == null) return; HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse(); Cookie cookie = new Cookie(COOKIE_NAME, value); cookie.setMaxAge(DEFAULT_MAX_AGE); cookie.setPath(COOKIE_PATH); response.addCookie(cookie); }
public String forceTreeRefresh() throws IOException { needsInvalidation = true; FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType("application/xml; charset=UTF-8"); response.getWriter().write("<response>OK</response>"); context.responseComplete(); return null; }
public static void writeFeed(SyndFeed feed, FeedType feedType, FacesContext context) throws FeedException, IOException { HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType(XML_MIME_TYPE); writeFeed(feed, feedType, response.getWriter()); } }
protected PortletResponse getPortletResponse(FacesContext facesContext) { PortletResponse portletResponse = null; Object response = facesContext.getExternalContext().getResponse(); if (response instanceof PortletResponse) { portletResponse = (PortletResponse) response; } else if (response instanceof HttpServletResponseAdapter) { HttpServletResponseAdapter httpServletResponseAdapter = (HttpServletResponseAdapter) response; portletResponse = httpServletResponseAdapter.getWrapped(); } return portletResponse; }
public void send404(final FacesContext facesContext) { try { HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); response.sendError(HttpServletResponse.SC_NOT_FOUND); } catch (IOException e) { throw new PrettyException(e); } }
protected PortletResponse getPortletResponse(FacesContext facesContext) { PortletResponse portletResponse = null; Object response = facesContext.getExternalContext().getResponse(); if (response instanceof PortletResponse) { portletResponse = (PortletResponse) response; } else if (response instanceof HttpServletResponseAdapter) { HttpServletResponseAdapter httpServletResponseAdapter = (HttpServletResponseAdapter) response; portletResponse = httpServletResponseAdapter.getWrapped(); } return portletResponse; }
/** * Determine if we are processing a portlet RenderResponse. * * @param facesContext The current FacesContext. * @return <code>true</code> if we are processing a RenderResponse, * <code>false</code> otherwise. */ public static boolean isRenderResponse(FacesContext facesContext) { if (!isPortletRequest(facesContext)) return false; return facesContext.getExternalContext().getResponse() instanceof RenderResponse; }
/** * Check if the thread is processing render method. * * @param facesContext * @return */ public static boolean isRender(FacesContext facesContext) { if (isPortlet(facesContext)) { return facesContext.getExternalContext().getResponse() instanceof RenderResponse; } return false; }