List of usage examples for java.io StringWriter close
public void close() throws IOException
From source file:it.geosolutions.geobatch.migrationmonitor.utils.DS2DSTokenResolver.java
private String loadOutputTemplate() throws IOException { StringWriter writer = null; InputStream is = null;/*from w w w. j a va 2 s .c om*/ Reader r = null; String output = null; try { is = getClass().getResourceAsStream("template.txt"); r = new InputStreamReader(is); writer = new StringWriter(); IOUtils.copy(is, writer); output = writer.toString(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); throw new IOException("Error while loading the DS2DSTemplate..."); } finally { try { r.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } try { is.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } try { writer.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } return output; }
From source file:com.xpn.xwiki.plugin.activitystream.impl.ActivityStreamImpl.java
/** * {@inheritDoc}/*from w ww. j av a 2 s .c o m*/ */ public String getFeedOutput(SyndFeed feed, String type) { feed.setFeedType(type); StringWriter writer = new StringWriter(); SyndFeedOutput output = new SyndFeedOutput(); try { output.output(feed, writer); writer.close(); return writer.toString(); } catch (Exception e) { return ""; } }
From source file:org.ikasan.filetransfer.xml.transform.DefaultXSLTransformer.java
/** * Performs the transformation to the output result as String. * * @param in - the XML document.//www .j a va 2 s . com * @return String * @throws TransformerException * @throws IOException */ public String transformToString(Source in) throws TransformerException, IOException { // Create a Writer instance to hold transformation result StringWriter writer = new StringWriter(); StreamResult out = new StreamResult(writer); // Transformation this.transform(in, out); // Flush and close the stream writer.flush(); writer.close(); // Here is the newly transformed string return new String(writer.getBuffer().toString()); }
From source file:org.jboss.processFlow.knowledgeService.SessionPerPInstanceBean.java
public String printActiveProcessInstanceVariables(Long processInstanceId, Integer ksessionId) { Map<String, Object> vHash = null; try {/*w w w. j a va 2 s . c o m*/ try { if (ksessionId == null) ksessionId = sessionPool.getSessionId(processInstanceId); vHash = getActiveProcessInstanceVariables(processInstanceId, ksessionId); } finally { disposeStatefulKnowledgeSessionAndExtras(ksessionId); } } catch (Exception x) { throw new RuntimeException(x); } if (vHash.size() == 0) log.error( "printActiveProcessInstanceVariables() no process instance variables for :\n\tprocessInstanceId = " + processInstanceId); StringWriter sWriter = null; try { sWriter = new StringWriter(); ObjectMapper jsonMapper = new ObjectMapper(); jsonMapper.writeValue(sWriter, vHash); return sWriter.toString(); } catch (Exception x) { throw new RuntimeException(x); } finally { if (sWriter != null) { try { sWriter.close(); } catch (Exception x) { x.printStackTrace(); } } } }
From source file:com.mobicage.rogerthat.xmpp.CallBackApiXMPPListener.java
/** * Establish an XMPP connection to XmppService and listen for Rogerthat API callbacks. *///from ww w . ja va2 s . c om public void startListening() { if (connectionThread != null) { throw new RuntimeException("Previous connection has not yet been closed!"); } if (xmppUsername == null || xmppService == null || xmppPassword == null || sik == null) throw new RuntimeException("Not enough information present to setup an xmpp connection"); final ConnectionListener connectionListener = new ConnectionListener() { @Override public void reconnectionSuccessful() { log.info("Reconnection to jabber server succeeded."); status = XmppConnectionStatus.Connected; } @Override public void reconnectionFailed(Exception e) { log.info("Reconnection to jabber server failed."); } @Override public void reconnectingIn(int seconds) { log.info("Reconnecting to jabber in " + seconds + " seconds ..."); status = XmppConnectionStatus.Reconnecting; } @Override public void connectionClosedOnError(Exception e) { log.info("Connection closed to jabber due to " + e.toString()); } @Override public void connectionClosed() { log.info("Connection to jabber closed."); } }; tasks.clear(); connectionThread = new Thread(new Runnable() { @Override public void run() { try { while (true) { Runnable task = tasks.take(); task.run(); } } catch (StopListeningException e) { disconnect(connectionListener); status = XmppConnectionStatus.Closed; statusLine = ""; } catch (Throwable e) { disconnect(connectionListener); status = XmppConnectionStatus.Closed; statusLine = "Connection interrupted."; } finally { connectionThread = null; } } }); connectionThread.setName("Rogerthat callback listener"); connectionThread.setDaemon(true); connectionThread.start(); tasks.add(new Runnable() { @Override public void run() { ConnectionConfiguration conf = new ConnectionConfiguration(xmppService); status = XmppConnectionStatus.Connecting; log.info("Connecting to jabber server ..."); conn = new XMPPConnection(conf); try { conn.connect(); } catch (XMPPException e) { status = XmppConnectionStatus.ConnectionFailed; statusLine = "Failed to reach Rogerthat servers.\n" + e.getMessage(); conn = null; connectionThread = null; if (onConnectionFailed != null) try { onConnectionFailed.run(); } catch (Throwable t) { log.log(Level.WARNING, "Failure in onConnectionFailed handler.", t); } throw new RuntimeException(e); // Stop thread. } if (onConnected != null) try { onConnected.run(); } catch (Throwable t) { log.log(Level.WARNING, "Failure in onConnected handler.", t); } conn.addConnectionListener(connectionListener); SASLAuthentication.supportSASLMechanism("PLAIN", 0); PacketFilter filter = new PacketFilter() { @Override public boolean accept(Packet packet) { boolean accept = packet instanceof Message && ROGERTHAT_CALLBACK_BOT.equals(packet.getFrom()); if (!accept) log.info("Dropping packet:\n" + packet.toXML()); return accept; } }; conn.addPacketListener(new PacketListener() { @Override public void processPacket(Packet packet) { log.info("Processing packet:\n" + packet.toXML()); if (!(packet instanceof Message)) { log.info("Ignoring non message packet."); return; } Message message = (Message) packet; PacketExtension extension = packet.getExtension("call", "mobicage:comm"); if (extension == null || !(extension instanceof CallbackRequestExtension)) { log.info("Ignoring incomplete packet."); return; } CallbackRequestExtension call = (CallbackRequestExtension) extension; if (!sik.equals(call.getSik())) { log.info("Ignoring packet with incorrect sik."); return; } String json; try { json = new String(DatatypeConverter.parseBase64Binary(call.getBase64Body()), "UTF-8"); } catch (UnsupportedEncodingException e) { log.log(Level.WARNING, "Could not decode base64 packet.", e); return; } final JSONObject request = (JSONObject) JSONValue.parse(json); if (logTraffic) log.info(String.format("Incoming Rogerthat API Callback.\nSIK: %s\n\n%s", sik, json)); final String id = (String) request.get("id"); if (callbackDedup != null) { byte[] response = callbackDedup.getResponse(id); if (response != null) { Message resultMessage = new Message(message.getFrom()); resultMessage.setFrom(message.getTo()); resultMessage.addExtension(new CallbackResponseExtension(sik, DatatypeConverter.printBase64Binary(response))); log.info("Sending message:\n" + resultMessage.toXML()); conn.sendPacket(resultMessage); return; } } final JSONObject result = new JSONObject(); final RequestContext requestContext = new RequestContext(id, sik); try { processor.process(request, result, requestContext); } finally { try { StringWriter writer = new StringWriter(); try { result.writeJSONString(writer); writer.flush(); json = writer.toString(); if (logTraffic) log.info("Returning result:\n" + json); } finally { writer.close(); } } catch (IOException e) { log.log(Level.SEVERE, "Could not write json object to string", e); return; } Message resultMessage = new Message(message.getFrom()); resultMessage.setFrom(message.getTo()); try { byte[] response = json.getBytes("UTF-8"); resultMessage.addExtension(new CallbackResponseExtension(sik, DatatypeConverter.printBase64Binary(response))); if (callbackDedup != null) { callbackDedup.storeResponse(id, response); } } catch (UnsupportedEncodingException e) { log.log(Level.SEVERE, "Could not add result to message packet", e); return; } log.info("Sending message:\n" + resultMessage.toXML()); conn.sendPacket(resultMessage); } } }, filter); try { conn.login(xmppUsername, xmppPassword); } catch (XMPPException e1) { status = XmppConnectionStatus.ConnectionFailed; statusLine = "Failed to authenticate jabber connection. Verify your configuration.\n" + e1.getMessage(); conn = null; connectionThread = null; if (onAuthenticationFailed != null) try { onAuthenticationFailed.run(); } catch (Throwable t) { log.log(Level.WARNING, "Failure in onAuthenticationFailed handler.", t); } throw new RuntimeException(); // Stop thread. } status = XmppConnectionStatus.Connected; if (onAuthenticated != null) try { onAuthenticated.run(); } catch (Throwable t) { log.log(Level.WARNING, "Failure in onAuthenticated handler.", t); } } }); }
From source file:com.xpn.xwiki.plugin.feed.FeedPlugin.java
/** * @see FeedPluginApi#getFeedOutput(SyndFeed, String) *//* w ww .jav a2 s . c o m*/ public String getFeedOutput(SyndFeed feed, String type, XWikiContext context) { feed.setFeedType(type); StringWriter writer = new StringWriter(); SyndFeedOutput output = new SyndFeedOutput(); try { output.output(feed, writer); writer.close(); return writer.toString(); } catch (Exception e) { e.printStackTrace(); return ""; } }
From source file:TDS.Shared.Messages.MessageJson.java
public String create(ContextType contextType, List<String> contexts) throws ReturnStatusException { MessageContextType messageContextType = null; StringWriter sw = new StringWriter(); JsonFactory jsonFactory = new JsonFactory(); JsonGenerator jsonWriter;//from w ww. j ava 2s .c o m try { if (_messageSystem != null) { messageContextType = _messageSystem.getMessageContextType(contextType); } if (messageContextType == null) return "{}"; jsonWriter = jsonFactory.createGenerator(sw); jsonWriter.writeStartObject(); jsonWriter.writeStringField("c_l", _language); // "c_l": _language jsonWriter.writeFieldName("c_a"); // "c_a" : jsonWriter.writeStartArray(); // [ for (String context : contexts) { MessageContext messageContext = messageContextType.getContext(context); writeContextElement(messageContext, jsonWriter); } jsonWriter.writeEndArray(); // ] jsonWriter.writeEndObject(); // } jsonWriter.close(); sw.close(); } catch (IOException e) { ReturnStatus rs = new ReturnStatus("failed", "Serialization failed: " + e.getMessage()); throw new ReturnStatusException(rs); } return sw.getBuffer().toString(); }
From source file:com.yahoo.flowetl.core.Plumber.java
/** * Translates a set of roots into a runnable object. * /* www . j av a 2s. co m*/ * @param roots * * @return the pipe runner * * @throws PipeException */ public PipeRunner translate(final Set<Pipe> roots) throws PipeException { if (roots == null || roots.isEmpty()) { throw new IllegalArgumentException("No valid pipes provided"); } // first translate to a graph final DefaultDirectedGraph<Pipe, PipeEdge> runGraph = new DefaultDirectedGraph<Pipe, PipeEdge>( new EdgeFactory<Pipe, PipeEdge>() { @Override public PipeEdge createEdge(Pipe src, Pipe tgt) { StringBuilder tmp = new StringBuilder(); tmp.append("{" + src.getName() + "}"); tmp.append("->"); tmp.append("{" + tgt.getName() + "}"); return new PipeEdge(tmp.toString()); } }); // find all reachable pipes from the given roots final Set<Pipe> reachableInputs = new HashSet<Pipe>(); Set<Pipe> reachablePipesTmp = new HashSet<Pipe>(); for (Pipe p : roots) { discoverReachable(p, reachablePipesTmp); reachableInputs.addAll(reachablePipesTmp); reachableInputs.add(p); reachablePipesTmp.clear(); } // add as vertexes.. for (Pipe p : reachableInputs) { runGraph.addVertex(p); } // connect together for (Pipe v : reachableInputs) { List<Pipe> outs = v.getOutputs(); if (outs != null) { int max = v.maxOutputs(); int cur = outs.size(); if (max != -1 && (max < cur)) { throw new PipeException( "Pipe " + v + " is only allowed " + max + " outputs but it has " + cur + " outputs"); } for (Pipe t : outs) { if (t == null) { continue; } PipeEdge edgeName = runGraph.addEdge(v, t); if (logger.isEnabled(Level.INFO)) { logger.log(Level.INFO, "Connected " + v + " to " + t + " with edge " + edgeName); } } } } // do cycle detection CycleDetector<Pipe, PipeEdge> cycleDetect = new CycleDetector<Pipe, PipeEdge>(runGraph); Set<Pipe> cycleNodes = cycleDetect.findCycles(); if (cycleNodes != null && cycleNodes.isEmpty() == false) { StringBuilder msg = new StringBuilder("The following pipes are causing cycles ["); msg.append(StringUtils.join(cycleNodes, ",")); msg.append("]"); throw new PipeException(msg.toString()); } // check connected components ConnectivityInspector<Pipe, PipeEdge> cInspector = new ConnectivityInspector<Pipe, PipeEdge>(runGraph); if (cInspector.isGraphConnected() == false) { throw new PipeException( "The pipes provided have occurences which do not actually connect to other pipes"); } // display if (logger.isEnabled(Level.DEBUG)) { StringWriter w = new StringWriter(); DOTExporter<Pipe, PipeEdge> d = new DOTExporter<Pipe, PipeEdge>(new VertexNameProvider<Pipe>() { @Override public String getVertexName(Pipe p) { return p.getName(); } }, new VertexNameProvider<Pipe>() { @Override public String getVertexName(Pipe p) { return p.getName(); } }, new EdgeNameProvider<PipeEdge>() { @Override public String getEdgeName(PipeEdge e) { return String.valueOf(e); } }); d.export(w, runGraph); try { w.close(); } catch (IOException e1) { // should be ok to ignore this... } logger.log(Level.DEBUG, w.toString()); } // all verified, yippe PipeRunner out = new PipeRunner() { @Override public void run() { // use topological order to figure out // how to run this graph in a way // that will ensure the inputs are satisfied // before a vertex is ran... GraphIterator<Pipe, PipeEdge> it = makeTraversalIterator(runGraph); // get the ordering first // which doesn't involve activating any of the pipes // just seeing what the iteration order will be... final List<Pipe> order = IterUtils.toList(it, ArrayList.class); // now make the real run iterator it = makeTraversalIterator(runGraph); it.addTraversalListener(new TraversalListenerAdapter<Pipe, PipeEdge>() { @Override public void vertexTraversed(VertexTraversalEvent<Pipe> v) { if (logger.isEnabled(Level.INFO)) { logger.log(Level.INFO, "Vertex " + v.getVertex() + " was visited"); } } }); StopWatch overallTimer = new StopWatch(); overallTimer.start(); notifyStart(order); // keep track of which ones we exec'ed // maybe for use later?? final List<Pipe> curExecd = new ArrayList<Pipe>(order.size()); // iterate StopWatch perRunTimer = new StopWatch(); List<Pipe> pipeOutputs = null; PipeResult pipeRes = null; while (it.hasNext()) { Pipe toRun = it.next(); perRunTimer.reset(); perRunTimer.start(); notifyStartGenerate(toRun); { pipeRes = toRun.generateOutput(); } perRunTimer.stop(); curExecd.add(toRun); pipeOutputs = toRun.getOutputs(); if (pipeOutputs != null) { for (Pipe tmp : pipeOutputs) { if (tmp == null) { continue; } tmp.attachInput(pipeRes); } } notifyFinishGenerate(toRun, pipeRes, perRunTimer.getTime()); // now clear it toRun.clearInputs(); } overallTimer.stop(); notifyComplete(overallTimer.getTime()); } }; return out; }
From source file:com.qwazr.compiler.JavaCompiler.java
private void compile(javax.tools.JavaCompiler compiler, Collection<File> javaFiles) throws IOException { final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); try {/*from ww w .jav a 2 s .co m*/ Iterable<? extends JavaFileObject> sourceFileObjects = fileManager .getJavaFileObjectsFromFiles(javaFiles); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); final List<String> options = new ArrayList<String>(); if (classPath != null) { options.add("-classpath"); options.add(classPath); } options.add("-d"); options.add(javaClassesDirectory.getAbsolutePath()); options.add("-sourcepath"); options.add(javaSourceDirectory.getAbsolutePath()); javax.tools.JavaCompiler.CompilationTask task = compiler.getTask(pw, fileManager, diagnostics, options, null, sourceFileObjects); if (!task.call()) { for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) pw.format("Error on line %d in %s%n%s%n", diagnostic.getLineNumber(), diagnostic.getSource().toUri(), diagnostic.getMessage(null)); pw.flush(); pw.close(); sw.close(); throw new IOException(sw.toString()); } } finally { IOUtils.close(fileManager); } }
From source file:com.google.acre.script.AcreFetch.java
@SuppressWarnings("boxing") public void fetch(boolean system, String response_encoding, boolean log_to_user, boolean no_redirect) { if (request_url.length() > 2047) { throw new AcreURLFetchException("fetching URL failed - url is too long"); }//from w ww . ja v a2 s . co m DefaultHttpClient client = new DefaultHttpClient(_connectionManager, null); HttpParams params = client.getParams(); // pass the deadline down to the invoked service. // this will be ignored unless we are fetching from another // acre server. // note that we may send a deadline that is already passed: // it's not our job to throw here since we don't know how // the target service will interpret the quota header. // NOTE: this is done *after* the user sets the headers to overwrite // whatever settings they might have tried to change for this value // (which could be a security hazard) long sub_deadline = (HostEnv.LIMIT_EXECUTION_TIME) ? _deadline - HostEnv.SUBREQUEST_DEADLINE_ADVANCE : System.currentTimeMillis() + HostEnv.ACRE_URLFETCH_TIMEOUT; int reentries = _reentries + 1; request_headers.put(HostEnv.ACRE_QUOTAS_HEADER, "td=" + sub_deadline + ",r=" + reentries); // if this is not an internal call, we need to invoke the call thru a proxy if (!_internal) { // XXX No sense wasting the resources to gzip inside the network. // XXX seems that twitter gets upset when we do this /* if (!request_headers.containsKey("accept-encoding")) { request_headers.put("accept-encoding", "gzip"); } */ String proxy_host = Configuration.Values.HTTP_PROXY_HOST.getValue(); int proxy_port = -1; if (!(proxy_host.length() == 0)) { proxy_port = Configuration.Values.HTTP_PROXY_PORT.getInteger(); HttpHost proxy = new HttpHost(proxy_host, proxy_port, "http"); params.setParameter(AllClientPNames.DEFAULT_PROXY, proxy); } } params.setParameter(AllClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // in msec long timeout = _deadline - System.currentTimeMillis(); if (timeout < 0) timeout = 0; params.setParameter(AllClientPNames.CONNECTION_TIMEOUT, (int) timeout); params.setParameter(AllClientPNames.SO_TIMEOUT, (int) timeout); // we're not streaming the request so this should be a win. params.setParameter(AllClientPNames.TCP_NODELAY, true); // reuse an existing socket if it is in TIME_WAIT state. params.setParameter(AllClientPNames.SO_REUSEADDR, true); // set the encoding of our POST payloads to UTF-8 params.setParameter(AllClientPNames.HTTP_CONTENT_CHARSET, "UTF-8"); BasicCookieStore cstore = new BasicCookieStore(); for (AcreCookie cookie : request_cookies.values()) { cstore.addCookie(cookie.toClientCookie()); } client.setCookieStore(cstore); HttpRequestBase method; HashMap<String, String> logmsg = new HashMap<String, String>(); logmsg.put("Method", request_method); logmsg.put("URL", request_url); params.setParameter(AllClientPNames.HANDLE_REDIRECTS, !no_redirect); logmsg.put("Redirect", Boolean.toString(!no_redirect)); try { if (request_method.equals("GET")) { method = new HttpGet(request_url); } else if (request_method.equals("POST")) { method = new HttpPost(request_url); } else if (request_method.equals("HEAD")) { method = new HttpHead(request_url); } else if (request_method.equals("PUT")) { method = new HttpPut(request_url); } else if (request_method.equals("DELETE")) { method = new HttpDelete(request_url); } else if (request_method.equals("PROPFIND")) { method = new HttpPropFind(request_url); } else { throw new AcreURLFetchException("Failed: unsupported (so far) method " + request_method); } method.getParams().setBooleanParameter(AllClientPNames.USE_EXPECT_CONTINUE, false); } catch (java.lang.IllegalArgumentException e) { throw new AcreURLFetchException("Unable to fetch URL; this is most likely an issue with URL encoding."); } catch (java.lang.IllegalStateException e) { throw new AcreURLFetchException("Unable to fetch URL; possibly an illegal protocol?"); } StringBuilder request_header_log = new StringBuilder(); for (Map.Entry<String, String> header : request_headers.entrySet()) { String key = header.getKey(); String value = header.getValue(); // XXX should suppress cookie headers? // content-type and length? if ("content-type".equalsIgnoreCase(key)) { Matcher m = contentTypeCharsetPattern.matcher(value); if (m.find()) { content_type = m.group(1); content_type_charset = m.group(2); } else { content_type_charset = "utf-8"; } method.addHeader(key, value); } else if ("content-length".equalsIgnoreCase(key)) { // ignore user-supplied content-length, which is // probably wrong due to chars vs bytes and is // redundant anyway ArrayList<String> msg = new ArrayList<String>(); msg.add("User-supplied content-length header is ignored"); _acre_response.log("warn", msg); } else if ("user-agent".equalsIgnoreCase(key)) { params.setParameter(AllClientPNames.USER_AGENT, value); } else { method.addHeader(key, value); } if (!("x-acre-auth".equalsIgnoreCase(key))) { request_header_log.append(key + ": " + value + "\r\n"); } } logmsg.put("Headers", request_header_log.toString()); // XXX need more detailed error checking if (method instanceof HttpEntityEnclosingRequestBase && request_body != null) { HttpEntityEnclosingRequestBase em = (HttpEntityEnclosingRequestBase) method; try { if (request_body instanceof String) { StringEntity ent = new StringEntity((String) request_body, content_type_charset); em.setEntity(ent); } else if (request_body instanceof JSBinary) { ByteArrayEntity ent = new ByteArrayEntity(((JSBinary) request_body).get_data()); em.setEntity(ent); } } catch (UnsupportedEncodingException e) { throw new AcreURLFetchException( "Failed to fetch URL. " + " - Unsupported charset: " + content_type_charset); } } if (!system && log_to_user) { ArrayList<Object> msg = new ArrayList<Object>(); msg.add("urlfetch request"); msg.add(logmsg); _acre_response.log("debug", msg); } _logger.info("urlfetch.request", logmsg); long startTime = System.currentTimeMillis(); try { // this sends the http request and waits HttpResponse hres = client.execute(method); status = hres.getStatusLine().getStatusCode(); HashMap<String, String> res_logmsg = new HashMap<String, String>(); res_logmsg.put("URL", request_url); res_logmsg.put("Status", ((Integer) status).toString()); Header content_type_header = null; // translate response headers StringBuilder response_header_log = new StringBuilder(); Header[] rawheaders = hres.getAllHeaders(); for (Header rawheader : rawheaders) { String headername = rawheader.getName().toLowerCase(); if (headername.equalsIgnoreCase("content-type")) { content_type_header = rawheader; // XXX should strip everything after ; content_type = rawheader.getValue(); // XXX don't set content_type_parameters, deprecated? } else if (headername.equalsIgnoreCase("x-metaweb-cost")) { _costCollector.merge(rawheader.getValue()); } else if (headername.equalsIgnoreCase("x-metaweb-tid")) { res_logmsg.put("ITID", rawheader.getValue()); } headers.put(headername, rawheader.getValue()); response_header_log.append(headername + ": " + rawheader.getValue() + "\r\n"); } res_logmsg.put("Headers", response_header_log.toString()); if (!system && log_to_user) { ArrayList<Object> msg = new ArrayList<Object>(); msg.add("urlfetch response"); msg.add(res_logmsg); _acre_response.log("debug", msg); } _logger.info("urlfetch.response", res_logmsg); // read cookies for (Cookie c : cstore.getCookies()) { cookies.put(c.getName(), new AcreCookie(c)); } // get body encoding String charset = null; if (content_type_header != null) { HeaderElement values[] = content_type_header.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } if (charset == null) charset = response_encoding; // read body HttpEntity ent = hres.getEntity(); if (ent != null) { InputStream res_stream = ent.getContent(); Header cenc = ent.getContentEncoding(); if (cenc != null && res_stream != null) { HeaderElement[] codecs = cenc.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase("gzip")) { res_stream = new GZIPInputStream(res_stream); } } } long firstByteTime = 0; long endTime = 0; if (content_type != null && (content_type.startsWith("image/") || content_type.startsWith("application/octet-stream") || content_type.startsWith("multipart/form-data"))) { // HttpClient's InputStream doesn't support mark/reset, so // wrap it with one that does. BufferedInputStream bufis = new BufferedInputStream(res_stream); bufis.mark(2); bufis.read(); firstByteTime = System.currentTimeMillis(); bufis.reset(); byte[] data = IOUtils.toByteArray(bufis); endTime = System.currentTimeMillis(); body = new JSBinary(); ((JSBinary) body).set_data(data); try { if (res_stream != null) { res_stream.close(); } } catch (IOException e) { // ignore } } else if (res_stream == null || charset == null) { firstByteTime = endTime = System.currentTimeMillis(); body = ""; } else { StringWriter writer = new StringWriter(); Reader reader = new InputStreamReader(res_stream, charset); int i = reader.read(); firstByteTime = System.currentTimeMillis(); writer.write(i); IOUtils.copy(reader, writer); endTime = System.currentTimeMillis(); body = writer.toString(); try { reader.close(); writer.close(); } catch (IOException e) { // ignore } } long waitingTime = firstByteTime - startTime; long readingTime = endTime - firstByteTime; _logger.debug("urlfetch.timings", "waiting time: " + waitingTime + "ms"); _logger.debug("urlfetch.timings", "reading time: " + readingTime + "ms"); Statistics.instance().collectUrlfetchTime(startTime, firstByteTime, endTime); _costCollector.collect((system) ? "asuc" : "auuc").collect((system) ? "asuw" : "auuw", waitingTime) .collect((system) ? "asub" : "auub", waitingTime); } } catch (IllegalArgumentException e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw new AcreURLFetchException("failed to fetch URL. " + " - Request Error: " + cause.getMessage()); } catch (IOException e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw new AcreURLFetchException("Failed to fetch URL. " + " - Network Error: " + cause.getMessage()); } catch (RuntimeException e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw new AcreURLFetchException("Failed to fetch URL. " + " - Network Error: " + cause.getMessage()); } finally { method.abort(); } }