/** * Connects to the given {@code address} and {@code port}. Data can be sent to the remote server through the * {@link OutputStream} returned by {@link #getOutputStream()}, and data can be read from the remote server * through the {@link InputStream} returned by {@link #getInputStream()}. */ public TcpClient(InetAddress address, int port) throws IOException { this.socket = new Socket(address, port); LOGGER.fine("Connected to " + this.socket.getRemoteSocketAddress()); InputStream in = this.socket.getInputStream(); OutputStream out = this.socket.getOutputStream(); boolean logData = LOGGER.isLoggable(FINEST); this.fromServer = ( logData ? IoUtil.wye(in, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, "<From Server< "))) : in ); this.toServer = ( logData ? IoUtil.tee(out, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, ">To Server> "))) : out ); }
/** * Connects to the given <var>address</var> and <var>port</var>. Data can be sent to the remote server through the * {@link OutputStream} returned by {@link #getOutputStream()}, and data can be read from the remote server * through the {@link InputStream} returned by {@link #getInputStream()}. */ public TcpClient(InetAddress address, int port) throws IOException { this.socket = new Socket(address, port); LOGGER.fine("Connected to " + this.socket.getRemoteSocketAddress()); InputStream in = this.socket.getInputStream(); OutputStream out = this.socket.getOutputStream(); boolean logData = LOGGER.isLoggable(FINEST); this.fromServer = ( logData ? InputStreams.wye(in, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, "<From Server< "))) : in ); this.toServer = ( logData ? OutputStreams.tee(out, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, ">To Server> "))) : out ); }
@Override public void run() throws IOException { InputStream in = new ByteArrayInputStream(buffer.toByteArray()); // Process "Content-Encoding" header. if ("gzip".equalsIgnoreCase(HttpMessage.this.getHeader("Content-Encoding"))) { LOGGER.fine(loggingPrefix + "GZIP-encoded content"); in = new GZIPInputStream(in); } if (LOGGER.isLoggable(FINE)) { LOGGER.fine(loggingPrefix + "Reading message body"); Writer lw = LogUtil.logWriter(LOGGER, FINE, loggingPrefix); in = InputStreams.wye(in, ( HttpMessage.this.contentTypeIsXmlish() ? new WriterOutputStream(new XMLFormatterWriter(lw)) : new HexOutputStream(lw) )); } HttpMessage.this.setBody(HttpMessage.body(in)); finished.run(); } };
in = IoUtil.wye( in, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, "<From Client< ")) ); out = IoUtil.tee( out, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, ">To Client> ")) );
in = InputStreams.wye( in, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, "<From Client< ")) ); out = OutputStreams.tee( out, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, ">To Client> ")) );
/** * Writes the body of this message <em>synchronously</em> to the given {@link OutputStream}. */ private void writeBody(String prefix, OutputStream out) throws IOException { // Check "Content-Encoding: gzip" GZIPOutputStream finishable = null; if ("gzip".equalsIgnoreCase(this.getHeader("Content-Encoding"))) { LOGGER.fine(prefix + "GZIP-encoded contents"); out = (finishable = new GZIPOutputStream(out)); } final boolean isXml; { String ct = this.getHeader("Content-Type"); isXml = ct != null && ct.indexOf("text/xml") != -1; } if (LOGGER.isLoggable(FINE)) { LOGGER.fine(prefix + "Writing message body:"); Writer lw = LogUtil.logWriter(LOGGER, FINE, prefix); out = IoUtil.tee(out, ( isXml ? new WriterOutputStream(new XMLFormatterWriter(lw)) : new HexOutputStream(lw) )); } this.body.write(out); if (finishable != null) finishable.finish(); out.flush(); }
/** * Writes the body of this message <em>synchronously</em> to the given {@link OutputStream}. * * @param loggingPrefix E.g. {@code "<<< "} */ private void writeBody(String loggingPrefix, OutputStream out) throws IOException { // Check "Content-Encoding: gzip" GZIPOutputStream finishable = null; if ("gzip".equalsIgnoreCase(this.getHeader("Content-Encoding"))) { LOGGER.fine(loggingPrefix + "GZIP-encoded contents"); out = (finishable = new GZIPOutputStream(out)); } if (LOGGER.isLoggable(FINE)) { LOGGER.fine(loggingPrefix + "Writing message body:"); Writer lw = LogUtil.logWriter(LOGGER, FINE, loggingPrefix); out = OutputStreams.tee(out, ( this.contentTypeIsXmlish() ? new WriterOutputStream(new XMLFormatterWriter(lw)) : new HexOutputStream(lw) )); } this.body.write(out); if (finishable != null) finishable.finish(); out.flush(); }
@Override public void handleConnection( InputStream clientIn, OutputStream clientOut, InputStream serverIn, OutputStream serverOut, InetSocketAddress clientLocalSocketAddress, InetSocketAddress clientRemoteSocketAddress, InetSocketAddress serverLocalSocketAddress, InetSocketAddress serverRemoteSocketAddress, Stoppable stoppable ) throws IOException { ThreadUtil.parallel( IoUtil.copyRunnable(clientIn, OutputStreams.tee( serverOut, new HexOutputStream(LogUtil.logWriter(LOGGER, FINER, "> ")) )), IoUtil.copyRunnable(serverIn, OutputStreams.tee( clientOut, new HexOutputStream(LogUtil.logWriter(LOGGER, FINER, "< ")) )), stoppable ); } }
@Override public void run() throws IOException { InputStream in = new ByteArrayInputStream(buffer.toByteArray()); // Process "Content-Encoding" header. if ("gzip".equalsIgnoreCase(HttpMessage.this.getHeader("Content-Encoding"))) { LOGGER.fine("GZIP-encoded content"); in = new GZIPInputStream(in); } if (LOGGER.isLoggable(FINE)) { boolean isXml = false; LOGGER.fine("Reading message body"); String contentType = HttpMessage.this.getHeader("Content-Type"); if (contentType != null) { ParametrizedHeaderValue phv = new ParametrizedHeaderValue(contentType); if ("text/xml".equalsIgnoreCase(phv.getToken())) isXml = true; } Writer logWriter = LogUtil.logWriter(LOGGER, FINE, ">>> "); in = IoUtil.wye(in, ( isXml ? new WriterOutputStream(new XMLFormatterWriter(logWriter)) : new HexOutputStream(logWriter) )); } HttpMessage.this.setBody(HttpMessage.body(in)); finished.run(); } };
@Override public void handleConnection( InputStream clientIn, OutputStream clientOut, InputStream serverIn, OutputStream serverOut, InetSocketAddress clientLocalSocketAddress, InetSocketAddress clientRemoteSocketAddress, InetSocketAddress serverLocalSocketAddress, InetSocketAddress serverRemoteSocketAddress, Stoppable stoppable ) throws IOException { ThreadUtil.parallel( IoUtil.copyRunnable(clientIn, IoUtil.tee( serverOut, new HexOutputStream(LogUtil.logWriter(LOGGER, FINER, "> ")) )), IoUtil.copyRunnable(serverIn, IoUtil.tee( clientOut, new HexOutputStream(LogUtil.logWriter(LOGGER, FINER, "< ")) )), stoppable ); } }
isXml ? new WriterOutputStream(new XMLFormatterWriter(logWriter)) : new HexOutputStream(logWriter) ));
this.contentTypeIsXmlish() ? new WriterOutputStream(new XMLFormatterWriter(lw)) : new HexOutputStream(lw) ));