use of org.apache.http.HttpInetConnection in project platform_external_apache-http by android.
the class RequestTargetHost method process.
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
if (!request.containsHeader(HTTP.TARGET_HOST)) {
HttpHost targethost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (targethost == null) {
HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
if (conn instanceof HttpInetConnection) {
// Populate the context with a default HTTP host based on the
// inet address of the target host
InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
int port = ((HttpInetConnection) conn).getRemotePort();
if (address != null) {
targethost = new HttpHost(address.getHostName(), port);
}
}
if (targethost == null) {
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
} else {
throw new ProtocolException("Target host missing");
}
}
}
request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
}
}
use of org.apache.http.HttpInetConnection in project robovm by robovm.
the class RequestTargetHost method process.
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
if (!request.containsHeader(HTTP.TARGET_HOST)) {
HttpHost targethost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (targethost == null) {
HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
if (conn instanceof HttpInetConnection) {
// Populate the context with a default HTTP host based on the
// inet address of the target host
InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
int port = ((HttpInetConnection) conn).getRemotePort();
if (address != null) {
targethost = new HttpHost(address.getHostName(), port);
}
}
if (targethost == null) {
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
} else {
throw new ProtocolException("Target host missing");
}
}
}
request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
}
}
use of org.apache.http.HttpInetConnection in project wso2-synapse by wso2.
the class ClientHandler method shutdownConnection.
/**
* Shutdown the connection ignoring any IO errors during the process
*
* @param conn the connection to be shutdown
* @param isError whether shutdown is due to an error
* @param errorMsg error message if shutdown happens on error
*/
private void shutdownConnection(final NHttpClientConnection conn, boolean isError, String errorMsg) {
if (conn instanceof HttpInetConnection) {
HttpInetConnection inetConnection = (HttpInetConnection) conn;
if (log.isWarnEnabled() && (isError || log.isDebugEnabled())) {
String msg = "Connection from local address : " + inetConnection.getLocalAddress() + ":" + inetConnection.getLocalPort() + " to remote address : " + inetConnection.getRemoteAddress() + ":" + inetConnection.getRemotePort() + " is closed!" + (errorMsg != null ? " - On error : " + errorMsg : "");
if (isError) {
log.warn(msg);
} else {
log.debug(msg);
}
}
if (countConnections) {
removeConnectionRecord(inetConnection);
}
}
HttpContext context = conn.getContext();
NhttpSharedOutputBuffer outputBuffer = (NhttpSharedOutputBuffer) context.getAttribute(REQUEST_SOURCE_BUFFER);
if (outputBuffer != null) {
outputBuffer.close();
}
SharedInputBuffer inputBuffer = (SharedInputBuffer) context.getAttribute(RESPONSE_SINK_BUFFER);
if (inputBuffer != null) {
inputBuffer.close();
}
try {
conn.shutdown();
} catch (IOException ignore) {
}
context.removeAttribute(RESPONSE_SINK_BUFFER);
context.removeAttribute(REQUEST_SOURCE_BUFFER);
context.removeAttribute(CLIENT_CONNECTION_DEBUG);
context.removeAttribute(CONNECTION_CREATION_TIME);
}
use of org.apache.http.HttpInetConnection in project wso2-synapse by wso2.
the class ServerWorker method processHttpRequestUri.
/**
* Get Uri of underlying SourceRequest and calculate service prefix and add to message context
* create response buffers for HTTP GET, DELETE, OPTION and HEAD methods
* @param msgContext Axis2MessageContext of the request
* @param method HTTP Method of the request
*/
public void processHttpRequestUri(MessageContext msgContext, String method) {
String servicePrefixIndex = "://";
ConfigurationContext cfgCtx = sourceConfiguration.getConfigurationContext();
msgContext.setProperty(Constants.Configuration.HTTP_METHOD, request.getMethod());
// String uri = request.getUri();
String oriUri = request.getUri();
String restUrlPostfix = NhttpUtil.getRestUrlPostfix(oriUri, cfgCtx.getServicePath());
String servicePrefix = oriUri.substring(0, oriUri.indexOf(restUrlPostfix));
if (servicePrefix.indexOf(servicePrefixIndex) == -1) {
HttpInetConnection inetConn = (HttpInetConnection) request.getConnection();
InetAddress localAddr = inetConn.getLocalAddress();
if (localAddr != null) {
servicePrefix = sourceConfiguration.getScheme().getName() + servicePrefixIndex + localAddr.getHostAddress() + ":" + inetConn.getLocalPort() + servicePrefix;
}
}
msgContext.setProperty(PassThroughConstants.SERVICE_PREFIX, servicePrefix);
msgContext.setTo(new EndpointReference(restUrlPostfix));
msgContext.setProperty(PassThroughConstants.REST_URL_POSTFIX, restUrlPostfix);
if (PassThroughConstants.HTTP_GET.equals(method) || PassThroughConstants.HTTP_HEAD.equals(method) || PassThroughConstants.HTTP_OPTIONS.equals(method)) {
HttpResponse response = sourceConfiguration.getResponseFactory().newHttpResponse(request.getVersion(), HttpStatus.SC_OK, request.getConnection().getContext());
// create a basic HttpEntity using the source channel of the response pipe
BasicHttpEntity entity = new BasicHttpEntity();
if (request.getVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
entity.setChunked(true);
}
response.setEntity(entity);
httpGetRequestProcessor.process(request.getRequest(), response, msgContext, request.getConnection(), os, isRestDispatching);
}
}
Aggregations