| 1 |
/* |
| 2 |
* ==================================================================== |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* ==================================================================== |
| 20 |
* |
| 21 |
* This software consists of voluntary contributions made by many |
| 22 |
* individuals on behalf of the Apache Software Foundation. For more |
| 23 |
* information on the Apache Software Foundation, please see |
| 24 |
* <http://www.apache.org/>. |
| 25 |
* |
| 26 |
*/ |
| 27 |
package org.apache.http.impl.nio.conn; |
| 28 |
|
| 29 |
import java.nio.charset.CharsetDecoder; |
| 30 |
import java.nio.charset.CharsetEncoder; |
| 31 |
|
| 32 |
import javax.net.ssl.SSLSession; |
| 33 |
|
| 34 |
import org.apache.commons.logging.Log; |
| 35 |
import org.apache.http.Header; |
| 36 |
import org.apache.http.HttpRequest; |
| 37 |
import org.apache.http.HttpResponse; |
| 38 |
import org.apache.http.config.MessageConstraints; |
| 39 |
import org.apache.http.entity.ContentLengthStrategy; |
| 40 |
import org.apache.http.impl.nio.DefaultNHttpClientConnection; |
| 41 |
import org.apache.http.nio.NHttpMessageParserFactory; |
| 42 |
import org.apache.http.nio.NHttpMessageWriterFactory; |
| 43 |
import org.apache.http.nio.conn.ManagedNHttpClientConnection; |
| 44 |
import org.apache.http.nio.reactor.IOSession; |
| 45 |
import org.apache.http.nio.reactor.ssl.SSLIOSession; |
| 46 |
import org.apache.http.nio.util.ByteBufferAllocator; |
| 47 |
import org.apache.http.util.Args; |
| 48 |
import org.apache.http.util.Asserts; |
| 49 |
|
| 50 |
class ManagedNHttpClientConnectionImpl |
| 51 |
extends DefaultNHttpClientConnection implements ManagedNHttpClientConnection { |
| 52 |
|
| 53 |
private final Log headerLog; |
| 54 |
private final Log wireLog; |
| 55 |
private final Log log; |
| 56 |
|
| 57 |
private final String id; |
| 58 |
private IOSession original; |
| 59 |
|
| 60 |
public ManagedNHttpClientConnectionImpl( |
| 61 |
final String id, |
| 62 |
final Log log, |
| 63 |
final Log headerLog, |
| 64 |
final Log wireLog, |
| 65 |
final IOSession ioSession, |
| 66 |
final int bufferSize, |
| 67 |
final int fragmentSizeHint, |
| 68 |
final ByteBufferAllocator allocator, |
| 69 |
final CharsetDecoder charDecoder, |
| 70 |
final CharsetEncoder charEncoder, |
| 71 |
final MessageConstraints constraints, |
| 72 |
final ContentLengthStrategy incomingContentStrategy, |
| 73 |
final ContentLengthStrategy outgoingContentStrategy, |
| 74 |
final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory, |
| 75 |
final NHttpMessageParserFactory<HttpResponse> responseParserFactory) { |
| 76 |
super(ioSession, bufferSize, fragmentSizeHint, allocator, charDecoder, charEncoder, constraints, |
| 77 |
incomingContentStrategy, outgoingContentStrategy, |
| 78 |
requestWriterFactory, responseParserFactory); |
| 79 |
this.id = id; |
| 80 |
this.log = log; |
| 81 |
this.headerLog = headerLog; |
| 82 |
this.wireLog = wireLog; |
| 83 |
this.original = ioSession; |
| 84 |
if (this.log.isDebugEnabled() || this.wireLog.isDebugEnabled()) { |
| 85 |
super.bind(new LoggingIOSession(ioSession, this.id, this.log, this.wireLog)); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void bind(final IOSession ioSession) { |
| 91 |
Args.notNull(ioSession, "I/O session"); |
| 92 |
Asserts.check(!ioSession.isClosed(), "I/O session is closed"); |
| 93 |
this.status = ACTIVE; |
| 94 |
this.original = ioSession; |
| 95 |
if (this.log.isDebugEnabled() || this.wireLog.isDebugEnabled()) { |
| 96 |
this.log.debug(this.id + " Upgrade session " + ioSession); |
| 97 |
super.bind(new LoggingIOSession(ioSession, this.id, this.log, this.wireLog)); |
| 98 |
} else { |
| 99 |
super.bind(ioSession); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public IOSession getIOSession() { |
| 105 |
return this.original; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public SSLSession getSSLSession() { |
| 110 |
return this.original instanceof SSLIOSession |
| 111 |
? ((SSLIOSession) this.original).getSSLSession() |
| 112 |
: null; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public String getId() { |
| 117 |
return this.id; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
protected void onResponseReceived(final HttpResponse response) { |
| 122 |
if (response != null && this.headerLog.isDebugEnabled()) { |
| 123 |
this.headerLog.debug(this.id + " << " + response.getStatusLine().toString()); |
| 124 |
final Header[] headers = response.getAllHeaders(); |
| 125 |
for (final Header header : headers) { |
| 126 |
this.headerLog.debug(this.id + " << " + header.toString()); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
protected void onRequestSubmitted(final HttpRequest request) { |
| 133 |
if (request != null && this.headerLog.isDebugEnabled()) { |
| 134 |
this.headerLog.debug(this.id + " >> " + request.getRequestLine().toString()); |
| 135 |
final Header[] headers = request.getAllHeaders(); |
| 136 |
for (final Header header : headers) { |
| 137 |
this.headerLog.debug(this.id + " >> " + header.toString()); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public String toString() { |
| 144 |
final StringBuilder buf = new StringBuilder(); |
| 145 |
buf.append(this.id); |
| 146 |
buf.append(" ["); |
| 147 |
switch (this.status) { |
| 148 |
case ACTIVE: |
| 149 |
buf.append("ACTIVE"); |
| 150 |
if (this.inbuf.hasData()) { |
| 151 |
buf.append("(").append(this.inbuf.length()).append(")"); |
| 152 |
} |
| 153 |
break; |
| 154 |
case CLOSING: |
| 155 |
buf.append("CLOSING"); |
| 156 |
break; |
| 157 |
case CLOSED: |
| 158 |
buf.append("CLOSED"); |
| 159 |
break; |
| 160 |
} |
| 161 |
buf.append("]"); |
| 162 |
return buf.toString(); |
| 163 |
} |
| 164 |
|
| 165 |
} |