private void prettyPrint(byte[] bytes) { boolean newLine = true; for (int i = 0; i < bytes.length; i++) { if (bytes[i] == '<') { if (i + 1 < bytes.length) { if (bytes[i + 1] == '/') { level--; } else { level++; } } for (int j = 0; newLine && j < level; j++) { config.getTraceStream().print(" "); } } config.getTraceStream().write(bytes[i]); if (bytes[i] == '>') { if (i + 1 < bytes.length && bytes[i + 1] == '<') { config.getTraceStream().println(); newLine = true; } else { newLine = false; } } } }
public TeeInputStream(ConnectorConfig config, byte[] bytes) { this.config = config; config.getTraceStream().println("------------ Response start ----------"); if (config.isPrettyPrintXml()) { prettyPrint(bytes); } else { config.getTraceStream().print(new String(bytes)); } config.getTraceStream().println(); config.getTraceStream().println("------------ Response end ----------"); }
private OutputStream wrapOutput(OutputStream output, boolean enableCompression) throws IOException { if (config.getMaxRequestSize() > 0) { output = new LimitingOutputStream(config.getMaxRequestSize(), output); } // when we are writing a zip file we don't bother with compression if (enableCompression && config.isCompression()) { output = new GZIPOutputStream(output); } if (config.isTraceMessage()) { output = new TeeOutputStream(output); } if (config.hasMessageHandlers()) { output = new MessageHandlerOutputStream(output); } return output; }
private OutputStream connectRaw( String uri, HashMap<String, String> httpHeaders, boolean enableCompression) throws IOException { url = new URL(uri); connection = createConnection(config, url, httpHeaders, enableCompression); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); if (config.useChunkedPost()) { connection.setChunkedStreamingMode(4096); } return connection.getOutputStream(); }
@Override public InputStream getContent() throws IOException { InputStream in; try { successful = true; in = connection.getInputStream(); } catch (IOException e) { successful = false; in = connection.getErrorStream(); if (in == null) { throw e; } } String encoding = connection.getHeaderField("Content-Encoding"); if (config.getMaxResponseSize() > 0) { in = new LimitingInputStream(config.getMaxResponseSize(), in); } if ("gzip".equals(encoding)) { in = new GZIPInputStream(in); } if (config.hasMessageHandlers() || config.isTraceMessage()) { byte[] bytes = FileUtil.toBytes(in); in = new ByteArrayInputStream(bytes); if (config.hasMessageHandlers()) { Iterator<MessageHandler> it = config.getMessagerHandlers(); while (it.hasNext()) { MessageHandler handler = it.next(); if (handler instanceof MessageHandlerWithHeaders) { ((MessageHandlerWithHeaders) handler) .handleResponse(url, bytes, connection.getHeaderFields()); } else { handler.handleResponse(url, bytes); } } } if (config.isTraceMessage()) { new TeeInputStream(config, bytes); } } return in; }
private static HttpURLConnection createConnection( ConnectorConfig config, URL url, HashMap<String, String> httpHeaders, boolean enableCompression) throws IOException { if (config.isTraceMessage()) { config .getTraceStream() .println( "WSC: Creating a new connection to " + url + " Proxy = " + config.getProxy() + " username " + config.getProxyUsername()); } HttpURLConnection connection = (HttpURLConnection) url.openConnection(config.getProxy()); connection.addRequestProperty("User-Agent", VersionInfo.info()); /* * Add all the client specific headers here */ if (config.getHeaders() != null) { for (Entry<String, String> ent : config.getHeaders().entrySet()) { connection.setRequestProperty(ent.getKey(), ent.getValue()); } } if (enableCompression && config.isCompression()) { connection.addRequestProperty("Content-Encoding", "gzip"); connection.addRequestProperty("Accept-Encoding", "gzip"); } if (config.getProxyUsername() != null) { String token = config.getProxyUsername() + ":" + config.getProxyPassword(); String auth = "Basic " + new String(Base64.encode(token.getBytes())); connection.addRequestProperty("Proxy-Authorization", auth); connection.addRequestProperty("Https-Proxy-Authorization", auth); } if (httpHeaders != null) { for (Map.Entry<String, String> entry : httpHeaders.entrySet()) { connection.addRequestProperty(entry.getKey(), entry.getValue()); } } if (config.getReadTimeout() != 0) { connection.setReadTimeout(config.getReadTimeout()); } if (config.getConnectionTimeout() != 0) { connection.setConnectTimeout(config.getConnectionTimeout()); } return connection; }