Java 类org.apache.http.nio.protocol.HttpAsyncExchange 实例源码
项目:oap
文件:NioHandlerAdapter.java
@Override
public void handle( final HttpRequest httpRequest, final HttpAsyncExchange httpAsyncExchange,
final HttpContext httpContext ) throws HttpException, IOException {
LOGGER.trace( "handling [{}]", httpRequest );
final HttpInetConnection connection = ( HttpInetConnection ) httpContext
.getAttribute( HttpCoreContext.HTTP_CONNECTION );
final InetAddress remoteAddress = connection.getRemoteAddress();
final HttpResponse response = httpAsyncExchange.getResponse();
final String httpContextProtocol = String.valueOf( httpContext.getAttribute( "protocol" ) );
if( Protocol.LOCAL.equals( this.protocol ) && !Inet.isLocalAddress( remoteAddress ) ) {
response.setStatusCode( HTTP_FORBIDDEN );
} else {
Request request = new Request( httpRequest, new Context( location, remoteAddress, httpContextProtocol ) );
handler.handle( request, new Response( response, corsPolicy.getCors( request ) ) );
}
httpAsyncExchange.submitResponse();
}
项目:OpsDev
文件:NHttpReverseProxy.java
public void failed(final Exception ex) {
synchronized (this.httpExchange) {
if (this.completed) {
return;
}
this.completed = true;
this.httpExchange.setException(ex);
HttpAsyncExchange responseTrigger = this.httpExchange.getResponseTrigger();
if (responseTrigger != null && !responseTrigger.isCompleted()) {
System.out.println("[client<-proxy] " + this.httpExchange.getId() + " " + ex);
ConsoleFactory.printToConsole("[client<-proxy] " + this.httpExchange.getId() + " " + ex,true);
int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, status,
EnglishReasonPhraseCatalog.INSTANCE.getReason(status, Locale.US));
String message = ex.getMessage();
if (message == null) {
message = "Unexpected error";
}
response.setEntity(new NStringEntity(message, ContentType.DEFAULT_TEXT));
responseTrigger.submitResponse(new BasicAsyncResponseProducer(response));
}
}
}
项目:oap
文件:NioClasspathResourceHandler.java
@Override
public void handle( HttpRequest req, HttpAsyncExchange httpExchange,
HttpContext context ) throws HttpException, IOException {
String resource = location + Strings.substringAfter( req.getRequestLine().getUri(), prefix );
if( logger.isTraceEnabled() ) logger.trace( req.getRequestLine().toString() + " -> " + resource );
Optional<byte[]> file = Resources.read( getClass(), resource );
if( file.isPresent() ) {
ByteArrayEntity entity = new ByteArrayEntity( file.get() );
entity.setContentType( mimeTypes.getContentType( req.getRequestLine().getUri() ) );
httpExchange.getResponse().setEntity( entity );
} else httpExchange.getResponse().setStatusCode( 404 );
}
项目:nio-benchmark
文件:SlowHelloRequestHandler.java
@Override
public void handle(final HttpRequest request, final HttpAsyncExchange httpexchange, final HttpContext context) {
executor.schedule(new Runnable() {
@Override
public void run() {
HttpResponse response = httpexchange.getResponse();
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(new NStringEntity(("Slow hello world"), ContentType.create("text/html", "UTF-8")));
httpexchange.submitResponse();
}
}, 50, TimeUnit.MILLISECONDS);
}
项目:nio-benchmark
文件:TestRequestHandler.java
private void sendError(Exception e, HttpAsyncExchange httpexchange) {
e.printStackTrace();
HttpResponse response = httpexchange.getResponse();
response.setStatusCode(500);
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
response.setEntity(new NStringEntity(stringWriter.toString(),
ContentType.create("text/plain", "UTF-8")));
httpexchange.submitResponse();
}
项目:nio-benchmark
文件:HelloRequestHandler.java
@Override
public void handle(final HttpRequest request, final HttpAsyncExchange httpexchange, final HttpContext context) {
HttpResponse response = httpexchange.getResponse();
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(new NStringEntity("Hello world", ContentType.create("text/html", "UTF-8")));
httpexchange.submitResponse();
}
项目:OpsDev
文件:NHttpReverseProxy.java
public void responseReceived(final HttpResponse response) {
synchronized (this.httpExchange) {
System.out.println("[proxy<-origin] " + this.httpExchange.getId() + " " + response.getStatusLine());
ConsoleFactory.printToConsole("[proxy<-origin] " + this.httpExchange.getId() + " " + response.getStatusLine(),true);
this.httpExchange.setResponse(response);
HttpAsyncExchange responseTrigger = this.httpExchange.getResponseTrigger();
if (responseTrigger != null && !responseTrigger.isCompleted()) {
System.out.println("[client<-proxy] " + this.httpExchange.getId() + " response triggered");
ConsoleFactory.printToConsole("[client<-proxy] " + this.httpExchange.getId() + " response triggered",true);
responseTrigger.submitResponse(new ProxyResponseProducer(this.httpExchange));
}
}
}
项目:holico
文件:HttpStack.java
/**
* Send simple http response.
*
* @param httpExchange
* the http exchange
* @param httpCode
* the http code
*/
private void sendSimpleHttpResponse(HttpAsyncExchange httpExchange, int httpCode) {
// get the empty response from the exchange
HttpResponse httpResponse = httpExchange.getResponse();
// create and set the status line
StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, httpCode, EnglishReasonPhraseCatalog.INSTANCE.getReason(httpCode, Locale.ENGLISH));
httpResponse.setStatusLine(statusLine);
// send the error response
httpExchange.submitResponse();
}
项目:OpsDev
文件:NHttpReverseProxy.java
public HttpAsyncExchange getResponseTrigger() {
return this.responseTrigger;
}
项目:OpsDev
文件:NHttpReverseProxy.java
public void setResponseTrigger(final HttpAsyncExchange responseTrigger) {
this.responseTrigger = responseTrigger;
}
项目:holico
文件:HttpStack.java
/**
* Instantiates a new coap response worker.
*
* @param name
* the name
* @param coapRequest
* the coap request
* @param httpExchange
* the http exchange
* @param httpRequest
* the http request
*/
public CoapResponseWorker(String name, Request coapRequest, HttpAsyncExchange httpExchange, HttpRequest httpRequest) {
super(name);
this.coapRequest = coapRequest;
this.httpExchange = httpExchange;
this.httpRequest = httpRequest;
}