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(); facesContext.responseComplete();
public void getReportData() throws IOException { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue(0.0); FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); externalContext.setResponseContentType("application/vnd.ms-excel"); externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"my.xls\""); workbook.write(externalContext.getResponseOutputStream()); facesContext.responseComplete(); }
@Override public void handle(FacesContext context) throws IOException { Map<String, String> params = context.getExternalContext().getRequestParameterMap(); ExternalContext externalContext = context.getExternalContext(); String sessionKey = params.get(Constants.DYNAMIC_CONTENT_PARAM); Map<String, Object> session = externalContext.getSessionMap(); context.responseComplete();
@Override public void handle(FacesContext context) throws IOException { Map<String, String> params = context.getExternalContext().getRequestParameterMap(); ExternalContext externalContext = context.getExternalContext(); String sessionKey = params.get(Constants.DYNAMIC_CONTENT_PARAM); Map<String, Object> session = externalContext.getSessionMap(); Map<String, String> barcodeMapping = (Map) session.get(Constants.BARCODE_MAPPING); String value = barcodeMapping.get(sessionKey); if (value != null) { boolean cache = Boolean.parseBoolean(params.get(Constants.DYNAMIC_CONTENT_CACHE_PARAM)); externalContext.setResponseStatus(200); externalContext.setResponseContentType("image/png"); handleCache(externalContext, cache); ErrorCorrectionLevel ecl = ErrorCorrectionLevel.L; String errorCorrection = params.get("qrec"); if (!LangUtils.isValueBlank(errorCorrection)) { ecl = ErrorCorrectionLevel.valueOf(errorCorrection); } QRCode.from(value).to(ImageType.PNG).withErrorCorrection(ecl).withCharset("UTF-8") .writeTo(externalContext.getResponseOutputStream()); externalContext.responseFlushBuffer(); context.responseComplete(); } }
@Override public void handle(FacesContext context) throws IOException { Map<String, String> params = context.getExternalContext().getRequestParameterMap(); String library = params.get("ln"); String resourceKey = params.get(Constants.DYNAMIC_CONTENT_PARAM); ExternalContext externalContext = context.getExternalContext(); Map<String, Object> session = externalContext.getSessionMap(); Map<String, String> dynamicResourcesMapping = (Map) session.get(Constants.DYNAMIC_RESOURCES_MAPPING); context.responseComplete();
ExternalContext externalContext = context.getExternalContext(); String contentDispositionValue = contentDisposition != null ? (String) contentDisposition.getValue(elContext) : "attachment"; String monitorKeyValue = monitorKey != null ? "_" + (String) monitorKey.getValue(elContext) : ""; context.responseComplete();
@ManagedBean @ViewScoped public class MyBean { private Long id; public void validate(FacesContext context, UIComponent component, Object object) { // Do some validation // And if failed: context.getExternalContext().setResponseStatus(404); context.responseComplete(); } public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
protected void handleRedirect(FacesContext context, Throwable rootCause, ExceptionInfo info, boolean responseResetted) throws IOException { ExternalContext externalContext = context.getExternalContext(); externalContext.getSessionMap().put(ExceptionInfo.ATTRIBUTE_NAME, info); context.responseComplete();
protected void handleAjaxException(FacesContext context, Throwable rootCause, ExceptionInfo info) throws Exception { ExternalContext externalContext = context.getExternalContext(); PartialResponseWriter writer = context.getPartialViewContext().getPartialResponseWriter(); writer.endDocument(); context.responseComplete();
FacesContext context = FacesContext.getCurrentInstance(); ExternalContext externalContext = context.getExternalContext(); externalContext.responseReset(); externalContext.setResponseContentType("application/vnd.ms-excel"); externalContext.setResponseHeader("Content-Disposition", "attachment;filename=export.xls"); workbook.write(externalContext.getResponseOutputStream()); context.responseComplete(); // Prevent JSF from performing navigation.
public void startDownload(Garbage garbage) { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); externalContext.setResponseHeader("Content-Type", garbage.getContentType()); externalContext.setResponseHeader("Content-Length", garbage.getContent().length); externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"" + garbage.getFileName() + "\""); externalContext.getResponseOutputStream().write(garbage.getContent()); facesContext.responseComplete(); }
public void download(Arquivo arquivo) throws Exception { FacesContext facesContext = FacesContext.getCurrentInstance(); pushArquivo(facesContext.getExternalContext(), ContextoBean.CAMINHO_ARQUIVOS + arquivo.getNomeArquivo(), arquivo.getNomeArquivo()); facesContext.responseComplete(); }
public String doThis(){ String url = "url of your servlet"; FacesContext context = FacesContext.getCurrentInstance(); try { context.getExternalContext().dispatch(url); }catch (Exception e) { e.printStackTrace(); } finally{ context.responseComplete(); return ""; }
try { String xml = "<person>damian</person>"; FacesContext ctx = FacesContext.getCurrentInstance(); final HttpServletResponse resp = (HttpServletResponse)ctx.getExternalContext().getResponse(); resp.setContentType("text/xml"); resp.setContentLength(xml.length()); resp.getOutputStream().write(xml.getBytes()); resp.getOutputStream().flush(); resp.getOutputStream().close(); ctx.responseComplete(); } catch (IOException e) { e.printStackTrace(); }
private String sendError(int errorCode) { FacesContext context = FacesContext.getCurrentInstance(); try { context.getExternalContext().responseSendError(errorCode,null); } catch (IOException ex) { Logger.getLogger(PermissionsWrapper.class.getName()).log(Level.SEVERE, null, ex); } context.responseComplete(); return ""; }
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 void sendHtmlJSONResponse() throws JSONException { FacesContext context = FacesContext.getCurrentInstance(); ExternalContext econtext = context.getExternalContext(); HttpServletResponse response = (HttpServletResponse) econtext.getResponse(); response.setContentType("text/html"); try { response.getWriter().write(getCollectedData()); response.flushBuffer(); } catch (IOException e) { } context.responseComplete(); }
public String forceTreeRefresh() throws IOException { resetCurrentDocumentData(); 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; }