List of usage examples for org.apache.http.nio.protocol BasicAsyncResponseProducer BasicAsyncResponseProducer
public BasicAsyncResponseProducer(final HttpResponse response)
From source file:com.mycompany.allasync.NHttpRequestHandler.java
public void handle(final HttpRequest request, final HttpAsyncExchange httpexchange, final HttpContext context) throws HttpException, IOException { HttpResponse response = httpexchange.getResponse(); handleInternal(request, response, context); httpexchange.submitResponse(new BasicAsyncResponseProducer(response)); }
From source file:talkeeg.server.BarcodeProvider.java
@Override public void handle(HttpRequest data, HttpAsyncExchange httpExchange, HttpContext context) throws HttpException, IOException { final BinaryData barcodeData = this.helloProvider.get().helloAsBinaryData(); final BitMatrix matrix = this.barcodeServiceProvider.get().encode(barcodeData); final BufferedImage image = BarcodeUtilsSE.toBufferedImage(matrix); ByteArrayOutputStream tmp = new ByteArrayOutputStream(); ImageIO.write(image, "png", tmp); final HttpResponse response = httpExchange.getResponse(); response.setHeader("Content-Type", "image/png"); response.setEntity(new ByteArrayEntity(tmp.toByteArray())); response.setStatusCode(HttpStatus.SC_OK); httpExchange.submitResponse(new BasicAsyncResponseProducer(response)); }
From source file:org.opcfoundation.ua.transport.https.HttpsServerPendingRequest.java
void sendResponse(int statusCode, IEncodeable responseObject) { try {//w w w .j av a 2 s . co m HttpResponse responseHandle = httpExchange.getResponse(); responseHandle.setHeader("Content-Type", "application/octet-stream"); responseHandle.setStatusCode(statusCode); if (responseObject != null) { try { logger.trace("sendResponse: requestId={} statusCode={} responseObject={}", requestId, statusCode, responseObject); logger.debug("sendResponse: requestId={} statusCode={} responseObject={}", requestId, statusCode, responseObject.getClass().getSimpleName()); //Check isDebugEnabled() here for possible performance reasons. if (logger.isDebugEnabled() && channel.getConnection() != null) { NHttpServerConnection nHttpServerConnection = ((HttpsServerConnection) channel .getConnection()).getNHttpServerConnection(); logger.debug("sendResponse: timeout={} {} context={}", httpExchange.getTimeout(), nHttpServerConnection.getSocketTimeout(), nHttpServerConnection.getContext()); } EncoderCalc calc = new EncoderCalc(); calc.setEncoderContext(endpoint.getEncoderContext()); calc.putMessage(responseObject); int len = calc.getLength(); byte[] data = new byte[len]; BinaryEncoder enc = new BinaryEncoder(data); enc.setEncoderContext(endpoint.getEncoderContext()); enc.setEncoderMode(EncoderMode.NonStrict); enc.putMessage(responseObject); responseHandle.setEntity(new NByteArrayEntity(data)); } catch (EncodingException e) { logger.info("sendResponse: Encoding failed", e); // Internal Error if (responseObject instanceof ErrorMessage == false) { responseHandle.setStatusCode(500); } } } logger.debug("sendResponse: {} length={}", responseHandle, responseHandle.getEntity().getContentLength()); httpExchange.submitResponse(new BasicAsyncResponseProducer(responseHandle)); } finally { endpoint.pendingRequests.remove(requestId); } }