@Override public final String toString() { return "URLConnectionSender(" + endpoint() + ")"; }
void send(byte[] body, String mediaType) throws IOException { // intentionally not closing the connection, so as to use keep-alives HttpURLConnection connection = (HttpURLConnection) endpoint().openConnection(); connection.setConnectTimeout(connectTimeout()); connection.setReadTimeout(readTimeout()); connection.setRequestMethod("POST"); connection.addRequestProperty("Content-Type", mediaType); if (compressionEnabled()) { connection.addRequestProperty("Content-Encoding", "gzip"); ByteArrayOutputStream gzipped = new ByteArrayOutputStream(); GZIPOutputStream compressor = new GZIPOutputStream(gzipped); try { compressor.write(body); } finally { compressor.close(); } body = gzipped.toByteArray(); } connection.setDoOutput(true); connection.setFixedLengthStreamingMode(body.length); connection.getOutputStream().write(body); skipAllContent(connection); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof URLConnectionSender) { URLConnectionSender that = (URLConnectionSender) o; return (this.encoding.equals(that.encoding())) && (this.messageMaxBytes == that.messageMaxBytes()) && (this.encoder.equals(that.encoder())) && (this.endpoint.equals(that.endpoint())) && (this.connectTimeout == that.connectTimeout()) && (this.readTimeout == that.readTimeout()) && (this.compressionEnabled == that.compressionEnabled()) && (this.mediaType.equals(that.mediaType())); } return false; }
private Builder(URLConnectionSender source) { this.encoding = source.encoding(); this.messageMaxBytes = source.messageMaxBytes(); this.encoder = source.encoder(); this.endpoint = source.endpoint(); this.connectTimeout = source.connectTimeout(); this.readTimeout = source.readTimeout(); this.compressionEnabled = source.compressionEnabled(); this.mediaType = source.mediaType(); } @Override