@SuppressWarnings({"rawtypes", "unchecked"}) public byte[] gerarRelatorioJasper( String nomeJasper, List<?> dataSource, Map parametros, String tipo) throws JRException, IOException { FacesContext fContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = fContext.getExternalContext(); // Busca o local onde esta o jasper InputStream fis = externalContext.getResourceAsStream(PATH + nomeJasper); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(fis); // Abre a figura e seta o parâmetro do relatório InputStream imgInputStream = this.getClass().getResourceAsStream("/resources/images/logo/" + "fmb.gif"); parametros.put("IMG_BRASAO", imgInputStream); JRDataSource ds = new JRBeanCollectionDataSource(dataSource); byte[] bytes = null; if (tipo.equals("P")) { bytes = this.gerarRelatorioJasperPDF(jasperReport, parametros, ds); } else if (tipo.equals("X")) { bytes = this.gerarRelatorioJasperXLS(jasperReport, parametros, ds); } else if (tipo.equals("D")) { bytes = this.gerarRelatorioJasperDoc(jasperReport, parametros, ds); } return bytes; }
@SuppressWarnings({"unchecked", "rawtypes"}) public void gerarRelatorio( String nomeJasper, String attachmentName, List<?> dataSource, Map parametros, String tipo) throws JRException, IOException { FacesContext fContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = fContext.getExternalContext(); // Busca o local onde esta o jasper InputStream fis = externalContext.getResourceAsStream(PATH + nomeJasper); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(fis); HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); // Abre a figura e seta o parâmetro do relatório InputStream imgInputStream = this.getClass().getResourceAsStream("resources/images/logo/" + "fmb.gif"); parametros.put("IMG_BRASAO", imgInputStream); StringBuffer filename = new StringBuffer(attachmentName); JRDataSource ds = new JRBeanCollectionDataSource(dataSource); byte[] bytes = null; if (tipo.equals("P")) { bytes = this.gerarRelatorioJasperPDF(jasperReport, parametros, ds); response.setContentType("application/pdf"); filename.append(".pdf").toString(); response.addHeader("content-disposition", "attachment;filename=" + filename); } else if (tipo.equals("X")) { bytes = this.gerarRelatorioJasperXLS(jasperReport, parametros, ds); response.setContentType("application/vnd.ms-excel"); filename.append(".xls").toString(); response.addHeader("content-disposition", "attachment;filename=" + filename); } else if (tipo.equals("D")) { bytes = this.gerarRelatorioJasperDoc(jasperReport, parametros, ds); response.setContentType("application/rtf"); filename.append(".rtf").toString(); response.addHeader("content-disposition", "attachment;filename=" + filename); } if (bytes != null && bytes.length > 0) { response.setContentLength(bytes.length); ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); fContext.responseComplete(); } }
public void process(OutputStream outStream, Object data) throws Exception { colors = ((MediaData) data).getNewColors(); ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext(); BufferedInputStream inStream = new BufferedInputStream( extContext.getResourceAsStream(RICHFACES_MEDIA_OUTPUT_IMAGE_SOURCE), BUFFER_SIZE); try { // skip 8-bytes of header byte[] bs = new byte[8]; if (inStream.read(bs) < bs.length) { throw new IllegalArgumentException(); } outStream.write(bs); Section section = null; while ((section = readNextSection(inStream)) != null) { section.write(inStream, outStream); } } finally { inStream.close(); outStream.close(); } }
public void onComponentCreated(FaceletContext ctx, UIComponent c, UIComponent parent) { try { final FacesContext facesContext = ctx.getFacesContext(); final Object pathValue = pathAttribute.getValueExpression(ctx, String.class).getValue(ctx); final String path; if (pathValue == null) { throw new FacesException("Value for properties file path is null"); } if (!pathValue.toString().startsWith("/")) { path = "/WEB-INF/" + pathValue; } else { path = pathValue.toString(); } @SuppressWarnings("unused") final String charsetName; if (charsetAttribute == null) { charsetName = "UTF-8"; } else { final Object encodingValue = charsetAttribute.getValueExpression(ctx, String.class).getValue(ctx); if (encodingValue == null) { charsetName = "UTF-8"; } else { charsetName = encodingValue.toString(); } } final ExternalContext externalContext = facesContext.getExternalContext(); final Properties properties; final InputStream stream = externalContext.getResourceAsStream(path); try { properties = new Properties(); try { properties.load(stream); } catch (IOException e) { final FacesException ex = new FacesException("Unable to load properties: " + e.getMessage()); ex.setStackTrace(e.getStackTrace()); throw ex; } } finally { try { stream.close(); } catch (IOException e) { log.warning("Failed to close resource stream: " + e.getMessage()); } } final NodeMap nodeMap = new NodeHashMap(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { NodeMap loc = nodeMap; for (String part : new DelimitedStringList(entry.getKey().toString(), '.')) { if (loc.containsKey(part)) { loc = loc.get(part); } else { final NodeMap newMap = new NodeHashMap(); loc.put(part, newMap); loc = newMap; } } loc.setNodeValue(entry.getValue().toString()); } nodeMap.lock(); final UIProperties uiProperties = ((UIProperties) c); uiProperties.setPropertiesMap(nodeMap); final Object varValue = varAttribute.getValueExpression(ctx, String.class).getValue(ctx); if (varValue == null) { throw new TagException(tag, "Value of 'var' attribute cannot be null"); } uiProperties.setVar(varValue.toString()); uiProperties.updatePropertyMap(facesContext); } catch (TagException tex) { throw tex; } catch (RuntimeException rex) { TagException tex = new TagException( tag, "An exception of type " + rex.getClass().getName() + " occurred: " + rex.getMessage()); tex.setStackTrace(rex.getStackTrace()); throw tex; } }
public static RequestContextBean parseConfigFile(ExternalContext externalContext) { RequestContextBean bean = new RequestContextBean(); InputStream in = externalContext.getResourceAsStream(_CONFIG_FILE); if (in != null) { try { InputSource input = new InputSource(); input.setByteStream(in); input.setPublicId(_CONFIG_FILE); XMLReader reader = _SAX_PARSER_FACTORY.newSAXParser().getXMLReader(); reader.setContentHandler(new Handler(bean, externalContext)); reader.parse(input); } catch (IOException ioe) { _LOG.warning(ioe); } catch (ParserConfigurationException pce) { _LOG.warning(pce); } catch (SAXException saxe) { _LOG.warning(saxe); } finally { try { in.close(); } catch (IOException ioe) { // Ignore ; } } } String classNameString = (String) bean.getProperty(RequestContextBean.UPLOADED_FILE_PROCESSOR_KEY); if (classNameString != null) { classNameString = classNameString.trim(); // check if this contains multiple class names for chained processors usecase. // Usually the class named are separated by space char. String classNames[] = classNameString.split("[ ]+"); if (classNames.length == 1) { // This could be a single processor full override usecase or a chained // processor usecase that has only one processor. try { Class<UploadedFileProcessor> clazz = (Class<UploadedFileProcessor>) ClassLoaderUtils.loadClass(classNames[0]); if (ChainedUploadedFileProcessor.class.isAssignableFrom(clazz)) { // this single chained processor case ChainedUploadedFileProcessor cufp[] = { (ChainedUploadedFileProcessor) clazz.newInstance() }; bean.setProperty( RequestContextBean.UPLOADED_FILE_PROCESSOR_KEY, new CompositeUploadedFileProcessorImpl(Arrays.asList(cufp))); } else { // this is full override usecase bean.setProperty(RequestContextBean.UPLOADED_FILE_PROCESSOR_KEY, clazz.newInstance()); } } catch (Exception e) { _LOG.severe("CANNOT_INSTANTIATE_UPLOADEDFILEPROCESSOR", e); bean.setProperty( RequestContextBean.UPLOADED_FILE_PROCESSOR_KEY, new CompositeUploadedFileProcessorImpl()); } } else { try { // chained processors usecase, Multiple processors List<ChainedUploadedFileProcessor> processors = new ArrayList<ChainedUploadedFileProcessor>(classNames.length); for (String className : classNames) { Class<ChainedUploadedFileProcessor> clazz = (Class<ChainedUploadedFileProcessor>) ClassLoaderUtils.loadClass(className); processors.add(clazz.newInstance()); } bean.setProperty( RequestContextBean.UPLOADED_FILE_PROCESSOR_KEY, new CompositeUploadedFileProcessorImpl(processors)); } catch (Exception e) { _LOG.severe("CANNOT_INSTANTIATE_UPLOADEDFILEPROCESSOR", e); bean.setProperty( RequestContextBean.UPLOADED_FILE_PROCESSOR_KEY, new CompositeUploadedFileProcessorImpl()); } } } else { // nothing specified, hence use default. bean.setProperty( RequestContextBean.UPLOADED_FILE_PROCESSOR_KEY, new CompositeUploadedFileProcessorImpl()); } UploadedFileProcessor ufp = (UploadedFileProcessor) bean.getProperty(RequestContextBean.UPLOADED_FILE_PROCESSOR_KEY); ufp.init(externalContext.getContext()); if (_LOG.isInfo()) { Object debug = bean.getProperty(RequestContextBean.DEBUG_OUTPUT_KEY); if (Boolean.TRUE.equals(debug)) _LOG.info("RUNNING_IN_DEBUG_MODE", _CONFIG_FILE); } return bean; }