List of usage examples for java.io StringWriter flush
public void flush()
From source file:org.apache.olingo.server.core.debug.AbstractDebugTabTest.java
protected String createHtml(DebugTab tab) throws Exception { StringWriter writer = new StringWriter(); tab.appendHtml(writer);//from w w w . j ava 2 s . com writer.flush(); byte[] bytes = writer.toString().getBytes("UTF-8"); return IOUtils.toString(new ByteArrayInputStream(bytes)); }
From source file:org.apiwatch.util.IO.java
public static void putAPIData(APIScope scope, String format, String encoding, String location, String username, String password) throws SerializationError, IOException, HttpException { if (URL_RX.matcher(location).matches()) { DefaultHttpClient client = new DefaultHttpClient(); if (username != null && password != null) { client.getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username, password)); }/*ww w . jav a 2 s .c o m*/ HttpPost req = new HttpPost(location); StringWriter writer = new StringWriter(); Serializers.dumpAPIScope(scope, writer, format); HttpEntity entity = new StringEntity(writer.toString(), encoding); req.setEntity(entity); req.setHeader("content-type", format); req.setHeader("content-encoding", encoding); HttpResponse response = client.execute(req); client.getConnectionManager().shutdown(); if (response.getStatusLine().getStatusCode() >= 400) { throw new HttpException(response.getStatusLine().getReasonPhrase()); } LOGGER.info("Sent results to URL: " + location); } else { File dir = new File(location); dir.mkdirs(); File file = new File(dir, "api." + format); OutputStream out = new FileOutputStream(file); Writer writer = new OutputStreamWriter(out, encoding); Serializers.dumpAPIScope(scope, writer, format); writer.flush(); writer.close(); out.close(); LOGGER.info("Wrote results to file: " + file); } }
From source file:org.carewebframework.smart.SmartAPIResource.java
/** * Returns the content of the imbedded resource to the caller. *//*from w w w .j a va2s. co m*/ @Override public String handleAPI(Map<String, String> params) { InputStream is = SmartAPIResource.class.getClassLoader().getResourceAsStream(resourcePath); StringWriter sw = new StringWriter(); try { IOUtils.copy(is, sw); sw.flush(); is.close(); } catch (IOException e) { log.error("Error retrieving SMART API resource.", e); throw new RuntimeException(e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(sw); } return sw.toString(); }
From source file:net.jcreate.e3.resource.handler.CSSMinResourceHandler.java
public void handle(Resource pResource) throws HandleResourceException { String charset = pResource.getCharset(); if (charset == null) { charset = HttpHolder.getResponse().getCharacterEncoding(); }/*from ww w . j a va 2 s . c o m*/ logger.info("?:" + pResource.getUri() + "css min..."); try { InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(pResource.getTreatedData()), charset); CssCompressor cssc = new CssCompressor(isr); StringWriter sw = new StringWriter(); cssc.compress(sw, LINEBREAK_AFTER_CHARACTERS); sw.flush(); sw.toString(); sw.close(); logger.info("css min?:" + pResource.getUri() + "?."); } catch (Exception ex) { final String MSG = "css min?:" + pResource.getUri() + "?"; logger.warn(MSG, ex); return; } }
From source file:org.calrissian.mango.json.deser.NodeDeserializerTest.java
@Test public void testEqDeserialize() throws Exception { String json = "{\"eq\":{\"key\":\"k1\",\"type\":\"string\",\"value\":\"v1\"}}"; Node node = objectMapper.readValue(json, Node.class); StringWriter writer = new StringWriter(); node.accept(new PrintNodeVisitor(writer)); writer.flush(); writer.close();//from ww w . j a v a2s .c o m String eq = writer.toString(); assertEquals("Equals[k1,v1],", eq); }
From source file:org.calrissian.mango.json.deser.NodeDeserializerTest.java
@Test public void testAndEqDeserialize() throws Exception { String json = "{\"and\":{\"children\":[{\"eq\":{\"key\":\"k1\",\"type\":\"string\",\"value\":\"v1\"}},{\"eq\":{\"key\":\"k2\",\"type\":\"string\",\"value\":\"v2\"}}]}}"; Node node = objectMapper.readValue(json, Node.class); StringWriter writer = new StringWriter(); node.accept(new PrintNodeVisitor(writer)); writer.flush(); writer.close();//from w w w .j a v a 2 s.c o m String eq = writer.toString(); assertEquals("AndNode(Equals[k1,v1],Equals[k2,v2],),", eq); }
From source file:org.calrissian.mango.json.deser.NodeDeserializerTest.java
@Test public void testOrEqDeserialize() throws Exception { String json = "{\"or\":{\"children\":[{\"eq\":{\"key\":\"k1\",\"type\":\"string\",\"value\":\"v1\"}},{\"eq\":{\"key\":\"k2\",\"type\":\"string\",\"value\":\"v2\"}}]}}"; Node node = objectMapper.readValue(json, Node.class); StringWriter writer = new StringWriter(); node.accept(new PrintNodeVisitor(writer)); writer.flush(); writer.close();//ww w . j a v a 2s . c om String eq = writer.toString(); assertEquals("OrNode(Equals[k1,v1],Equals[k2,v2],),", eq); }
From source file:org.calrissian.mango.json.deser.NodeDeserializerTest.java
@Test public void testAndOrEqNeqDeserialize() throws Exception { String json = "{\"or\":{\"children\":[{\"and\":{\"children\":[{\"eq\":{\"key\":\"k1\",\"type\":\"string\",\"value\":\"v1\"}},{\"neq\":{\"key\":\"k2\",\"type\":\"ipv4\",\"value\":\"1.2.3.4\"}}]}},{\"and\":{\"children\":[{\"eq\":{\"key\":\"k3\",\"type\":\"integer\",\"value\":\"1234\"}}]}}]}}"; Node node = objectMapper.readValue(json, Node.class); StringWriter writer = new StringWriter(); node.accept(new PrintNodeVisitor(writer)); writer.flush(); writer.close();/*from w w w .j a v a 2 s. co m*/ String eq = writer.toString(); assertEquals("OrNode(AndNode(Equals[k1,v1],NotEquals[k2,1.2.3.4],),AndNode(Equals[k3,1234],),),", eq); }
From source file:org.calrissian.mango.json.deser.NodeDeserializerTest.java
@Test public void testRangeDeserialize() throws Exception { String json = "{\"range\":{\"key\":\"k1\",\"type\":\"string\",\"start\":\"v1\",\"end\":\"v2\"}}"; Node node = objectMapper.readValue(json, Node.class); StringWriter writer = new StringWriter(); node.accept(new PrintNodeVisitor(writer)); writer.flush(); writer.close();// w w w . j ava 2s.c om String eq = writer.toString(); assertEquals("Range[k1,(v1,v2)],", eq); }
From source file:com.dhenton9000.jersey.client.velocity.VelocityTests.java
@Test public void testVelocity() throws Exception { VelocityEngineFactory vFactory = new VelocityEngineFactory(); VelocityEngine ve = vFactory.getEngine(); VelocityContext context = new VelocityContext(); context.put("alpha", "alpha"); context.put("beta", 55); StringWriter writer = new StringWriter(); ve.mergeTemplate("/velocity_templates/test.vm", "UTF-8", context, writer); writer.flush(); writer.close();//w ww .j av a2s . c om String tString = writer.toString(); LOG.debug("tString " + tString); ObjectMapper mapper = new ObjectMapper(); JsonNode sampleTree = mapper.readTree(tString); assertEquals(55, sampleTree.get("beta").asInt()); }