List of usage examples for java.io StringWriter flush
public void flush()
From source file:com.msopentech.odatajclient.engine.it.AbstractTest.java
protected void debugFeed(final FeedResource feed, final String message) { if (LOG.isDebugEnabled()) { final StringWriter writer = new StringWriter(); client.getSerializer().feed(feed, writer); writer.flush(); LOG.debug(message + "\n{}", writer.toString()); }//from w w w . ja v a 2 s . co m }
From source file:com.msopentech.odatajclient.engine.it.AbstractTest.java
protected void debugEntry(final EntryResource entry, final String message) { if (LOG.isDebugEnabled()) { final StringWriter writer = new StringWriter(); client.getSerializer().entry(entry, writer); writer.flush(); LOG.debug(message + "\n{}", writer.toString()); }//from www . j a va 2 s . c o m }
From source file:com.adaptris.core.AdaptrisMessageCase.java
@Test public void testReader() throws Exception { AdaptrisMessage msg1 = createMessage(); Reader in = msg1.getReader(); StringWriter out = new StringWriter(); IOUtils.copy(in, out);/*from w w w.j a va2s. c om*/ out.flush(); assertEquals(PAYLOAD, out.toString()); }
From source file:com.msopentech.odatajclient.engine.it.AbstractTestITCase.java
protected void debugFeed(final Feed feed, final String message) { if (LOG.isDebugEnabled()) { final StringWriter writer = new StringWriter(); client.getSerializer().feed(feed, writer); writer.flush(); LOG.debug(message + "\n{}", writer.toString()); }//w ww.ja v a2 s . com }
From source file:com.msopentech.odatajclient.engine.it.AbstractTestITCase.java
protected void debugEntry(final Entry entry, final String message) { if (LOG.isDebugEnabled()) { final StringWriter writer = new StringWriter(); client.getSerializer().entry(entry, writer); writer.flush(); LOG.debug(message + "\n{}", writer.toString()); }//from w w w .jav a 2 s . com }
From source file:com.cts.ptms.tracking.UPSTracking.java
@Override public CustomTrackingResponse getTrackingDetails(CustomTrackingRequest customTrackingRequest) throws TrackingException { CustomTrackingResponse customTrackingResponse = null; try {/*from ww w.j ava2 s . c om*/ com.cts.ptms.model.gls.AccessRequest accessRequest = null; com.cts.ptms.model.ups.generated.trackrequest.TrackRequest trackRequest = null; com.cts.ptms.model.ups.generated.trackresponse.TrackResponse trackResponse = null; //Create JAXBContext and marshaller for AccessRequest object JAXBContext accessRequestJAXBC = JAXBContext.newInstance(AccessRequest.class.getPackage().getName()); Marshaller accessRequestMarshaller = accessRequestJAXBC.createMarshaller(); com.cts.ptms.model.gls.ObjectFactory accessRequestObjectFactory = new com.cts.ptms.model.gls.ObjectFactory(); accessRequest = accessRequestObjectFactory.createAccessRequest(); AccessRequest receivedAccessReq = customTrackingRequest.getAccessRequest(); //Populate the access request accessRequest.setAccessLicenseNumber(receivedAccessReq.getAccessLicenseNumber()); accessRequest.setUserId(receivedAccessReq.getUserId()); accessRequest.setPassword(receivedAccessReq.getPassword()); TrackRequestDetails inputRequestDtls = customTrackingRequest.getTrackRequestDetails(); if (inputRequestDtls == null || inputRequestDtls.getTrackingNumber() == null) { throw new TrackingException("No Tracking Tracking number found in the request."); } //Create JAXBContext and marshaller for TrackRequest object JAXBContext trackRequestJAXBC = JAXBContext.newInstance( com.cts.ptms.model.ups.generated.trackrequest.TrackRequest.class.getPackage().getName()); Marshaller trackRequestMarshaller = trackRequestJAXBC.createMarshaller(); com.cts.ptms.model.ups.generated.trackrequest.ObjectFactory requestObjectFactory = new com.cts.ptms.model.ups.generated.trackrequest.ObjectFactory(); trackRequest = requestObjectFactory.createTrackRequest(); //Populate the Track request com.cts.ptms.model.ups.generated.trackrequest.Request request = new com.cts.ptms.model.ups.generated.trackrequest.Request(); com.cts.ptms.model.ups.generated.trackrequest.TransactionReference transReference = new com.cts.ptms.model.ups.generated.trackrequest.TransactionReference(); transReference.setCustomerContext("Tracking customer shipment Details"); request.setTransactionReference(transReference); request.setRequestAction("Track"); request.getRequestOption().addAll(inputRequestDtls.getRequestOptions()); trackRequest.setRequest(request); trackRequest.setTrackingNumber(inputRequestDtls.getTrackingNumber()); //Get String out of access request and track request objects. StringWriter strWriter = new StringWriter(); accessRequestMarshaller.marshal(accessRequest, strWriter); trackRequestMarshaller.marshal(trackRequest, strWriter); strWriter.flush(); strWriter.close(); System.out.println("Request: " + strWriter.getBuffer().toString()); if (properties == null) { throw new TrackingException("Error while loading the ups properties."); } URLConnection httpUrlConnection = ShipmentCommonUtilities.contactService( strWriter.getBuffer().toString(), new URL(properties.getProperty("INTEGRATION_URL"))); String strResults = null; if (httpUrlConnection != null) { strResults = ShipmentCommonUtilities.readURLConnection(httpUrlConnection); } else throw new TrackingException("Exception occured while contacting the service.."); //Parse response object JAXBContext trackResponseJAXBC = JAXBContext.newInstance(TrackResponse.class.getPackage().getName()); Unmarshaller trackUnmarshaller = trackResponseJAXBC.createUnmarshaller(); ByteArrayInputStream input = new ByteArrayInputStream(strResults.getBytes()); Object objResponse = trackUnmarshaller.unmarshal(input); trackResponse = (com.cts.ptms.model.ups.generated.trackresponse.TrackResponse) objResponse; List<TrackingError> trackingErrors = null; if (trackResponse.getResponse() != null) { String responseCode = trackResponse.getResponse().getResponseStatusCode(); customTrackingResponse = populateResponse(trackResponse); if (responseCode != null && responseCode.equals("1")) { System.out.println("Response Status: " + trackResponse.getResponse().getResponseStatusCode()); System.out.println("Response Status Description: " + trackResponse.getResponse().getResponseStatusDescription()); } else if (responseCode != null && responseCode.equals("0")) { trackingErrors = new ArrayList<TrackingError>(0); List<com.cts.ptms.model.ups.generated.trackresponse.Error> resErrors = trackResponse .getResponse().getError(); if (resErrors != null && !resErrors.isEmpty()) { for (com.cts.ptms.model.ups.generated.trackresponse.Error resError : resErrors) { TrackingError trackingError = new TrackingError(); trackingError.setErrorSeverity(resError.getErrorSeverity()); trackingError.setErrorCode(resError.getErrorCode()); trackingError.setErrorDescription(resError.getErrorDescription()); trackingErrors.add(trackingError); } } customTrackingResponse.setError(trackingErrors); } } } catch (TrackingException e) { System.out.println("Exception occurred at : " + e.getMessage()); } catch (Exception e) { System.out.println("Exception occurred at : " + e.getMessage()); throw new TrackingException(e.getMessage()); } return customTrackingResponse; }
From source file:com.glaf.template.engine.FreemarkerTemplateEngine.java
public void evaluate(Template template, Map<String, Object> context, Writer writer) { try {//from ww w. ja v a 2s .com context.put("currentDate", new java.util.Date()); StringWriter out = new StringWriter(); Reader reader = new BufferedReader(new StringReader(template.getContent())); freemarker.template.Template t = new freemarker.template.Template(template.getDataFile(), reader, configuration); long startTime = System.currentTimeMillis(); t.process(context, out); long endTime = System.currentTimeMillis(); long renderTime = (endTime - startTime); logger.debug("Rendered [" + template.getTemplateId() + "] in " + renderTime + " milliseconds"); out.flush(); out.close(); String text = out.toString(); writer.write(text); } catch (Exception ex) { logger.debug("error template:" + template.getDataFile()); throw new RuntimeException(ex); } }
From source file:com.jaspersoft.jasperserver.war.OlapPrint.java
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * * @param request servlet request/*w ww . jav a 2 s . c o m*/ * @param response servlet response */ protected void processRequest(RequestContext context) throws ServletException, IOException { HttpServletRequest request = context.getRequest(); HttpServletResponse response = context.getResponse(); HttpSession session = request.getSession(); String type = request.getParameter("type"); int identifiedType = -1; boolean xslCache = true; String view = request.getParameter("view"); if (view == null || type == null) { throw new ServletException("view and type parameters not supplied"); } //context.getFileParameters(); OutputStream outStream = response.getOutputStream(); PrintWriter out = null; try { String xslUri = null; if (type.equalsIgnoreCase("XLS")) { xslUri = "/WEB-INF/jpivot/table/xls_mdxtable.xsl"; RendererParameters.setParameter(context.getRequest(), "mode", "excel", "request"); response.setContentType("application/vnd.ms-excel"); filename = "xls_export.xls"; identifiedType = XLS; } else if (type.equalsIgnoreCase("PDF")) { xslUri = "/WEB-INF/jpivot/table/fo_mdxtable.xsl"; RendererParameters.setParameter(context.getRequest(), "mode", "print", "request"); response.setContentType("application/pdf"); filename = "xls_export.pdf"; identifiedType = PDF; } else { throw new ServletException("Unknown file type: " + type); } // Get references to needed elements. We expect them to be in the current // session String tableRef = view + "/table"; String chartRef = view + "/chart"; String printRef = view + "/print"; // get TableComponent TableComponent table = (TableComponent) context.getModelReference(tableRef); // only proceed if table component exists if (table == null) { return; } Map parameters = getPrintParameters(printRef, context); parameters.putAll(getChartParameters(chartRef, request)); //parameters.put("message",table.getReportTitle()); // add "context" and "renderId" to parameter map //parameters.put("renderId", renderId); parameters.put("context", context.getRequest().getContextPath()); // Some FOP-PDF versions require a complete URL, not a path //parameters.put("contextUrl", createContextURLValue(context)); // set up filename for download. response.setHeader("Content-Disposition", "attachment; filename=" + filename); table.setDirty(true); Document document = table.render(context); table.setDirty(true); DOMSource source = new DOMSource(document); Transformer transformer = XmlUtils.getTransformer(session, xslUri, xslCache); for (Iterator it = parameters.keySet().iterator(); it.hasNext();) { String name = (String) it.next(); Object value = parameters.get(name); transformer.setParameter(name, value); } // if this is XLS, then we are done, so output xml file. if (identifiedType == XLS) { logger.debug("Creating XLS"); // Get table rendered as XML that Excel understands StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); //do transform transformer.transform(source, result); sw.flush(); response.setContentLength(sw.toString().length()); out = new PrintWriter(outStream); out.write(sw.toString()); RendererParameters.removeParameter(context.getRequest(), "mode", "excel", "request"); } else { // if this is PDF, then need to generate PDF from the FO xml logger.debug("Creating PDF"); logger.debug(resources.getString("jpivot.PrintServlet.message.encoding", new Object[] { resources.getCharacterEncoding() })); FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); // configure foUserAgent as desired // Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream); // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); // Start XSLT transformation and FOP processing transformer.transform(source, res); /* ByteArrayInputStream bain = new ByteArrayInputStream(sw.toString() .getBytes(resources.getEncodingProvider().getCharacterEncoding())); ByteArrayOutputStream baout = new ByteArrayOutputStream(16384); // process FO to PDF convertFO2PDF(bain, baout); final byte[] content = baout.toByteArray(); response.setContentLength(content.length); outStream.write(content); */ RendererParameters.removeParameter(context.getRequest(), "mode", "print", "request"); } } catch (Exception e) { logger.error(e); throw new ServletException(e); } finally { //close output streams if (out != null) { try { out.flush(); out.close(); } catch (Exception ex) { } } if (outStream != null) { try { outStream.flush(); outStream.close(); } catch (Exception ex) { } } } }
From source file:com.msopentech.odatajclient.engine.data.impl.AbstractODataBinder.java
@Override public ODataEntitySet getODataEntitySet(final Feed resource, final URI defaultBaseURI) { if (LOG.isDebugEnabled()) { final StringWriter writer = new StringWriter(); client.getSerializer().feed(resource, writer); writer.flush(); LOG.debug("FeedResource -> ODataEntitySet:\n{}", writer.toString()); }// w w w .j av a2 s . c om final URI base = defaultBaseURI == null ? resource.getBaseURI() : defaultBaseURI; final URI next = resource.getNext(); final ODataEntitySet entitySet = next == null ? client.getObjectFactory().newEntitySet() : client.getObjectFactory().newEntitySet(URIUtils.getURI(base, next.toASCIIString())); if (resource.getCount() != null) { entitySet.setCount(resource.getCount()); } for (Entry entryResource : resource.getEntries()) { entitySet.addEntity(getODataEntity(entryResource)); } return entitySet; }
From source file:com.msopentech.odatajclient.engine.data.AbstractODataBinder.java
@Override public ODataEntitySet getODataEntitySet(final FeedResource resource, final URI defaultBaseURI) { if (LOG.isDebugEnabled()) { final StringWriter writer = new StringWriter(); client.getSerializer().feed(resource, writer); writer.flush(); LOG.debug("FeedResource -> ODataEntitySet:\n{}", writer.toString()); }//www .j av a 2s .com final URI base = defaultBaseURI == null ? resource.getBaseURI() : defaultBaseURI; final URI next = resource.getNext(); final ODataEntitySet entitySet = next == null ? ODataObjectFactory.newEntitySet() : ODataObjectFactory.newEntitySet(URIUtils.getURI(base, next.toASCIIString())); if (resource.getCount() != null) { entitySet.setCount(resource.getCount()); } for (EntryResource entryResource : resource.getEntries()) { entitySet.addEntity(getODataEntity(entryResource)); } return entitySet; }