Dear Wiki user, You have subscribed to a wiki page or wiki category on "Httpcomponents Wiki" for change notification.
The following page has been changed by OlegKalnichevski: http://wiki.apache.org/HttpComponents/HttpCoreTutorial ------------------------------------------------------------------------------ === HTTP service === - !HttpService is a server side HTTP protocol handler based in the blocking I/O model that implements the essential requirements of the HTTP protocol for the server side message processing as described by RFC 2616. !HttpService relies on !HttpProcessor to take care of the cross-cutting protocol aspects that apply to all incoming and outgoing messages. + !HttpService is a server side HTTP protocol handler based in the blocking I/O model that implements the essential requirements of the HTTP protocol for the server side message processing as described by RFC 2616. + + !HttpService relies on !HttpProcessor to generate mandatory protocol headers for all outgoing messages and apply common, cross-cutting message transformations to all all incoming and outgoing messages, whereas HTTP request executor is expected to take care of application specific content generation and processing. {{{ HttpParams params; @@ -1430, +1432 @@ === Asynchronous HTTP service handler === - !AsyncNHttpServiceHandler is fully asynchronous HTTP service handler implementation that works with !ConsumingNHttpEntity and !ProducingNHttpEntity. The contents of HTTP headers are stored in memory, HTTP entities are streamed directly from the entities to the underlying channel (and vice versa). + !AsyncNHttpServiceHandler is a fully asynchronous HTTP server side protocol handler that implements the essential requirements of the HTTP protocol for the server side message processing as described by RFC 2616. !AsyncNHttpServiceHandler is capable of processin HTTP requests with nearly constant memory footprint. Only HTTP message heads are stored in memory, while content of message bodies is streamed directly from the entity to the underlying channel (and vice versa) using !ConsumingNHttpEntity and !ProducingNHttpEntity interfaces. When using this implementation, it is important to ensure that entities supplied for writing implement !ProducingNHttpEntity. Doing so will allow the entity to be written out asynchronously. If entities supplied for writing do not implement the !ProducingNHttpEntity interface, a delegate is added that buffers the entire contents in memory. Additionally, the buffering might take place in the I/O dispatch thread, which could cause I/O to block temporarily. For best results, one must ensure that all entities set on !HttpResponses from !NHttpRequestHandlers implement !ProducingNHttpEntity. - If incoming requests enclose a content entity, !NHttpRequestHandlers are expected to return a !ConsumingNHttpEntity for reading the content. After the entity is finished reading the data, !NHttpRequestHandler#handle() method is called to generate a response. Similarly to other HTTP protocol handlers !AsyncNHttpServiceHandler relies on !HttpProcessor to take care of the cross-cutting protocol aspects that apply to all incoming and outgoing messages. + If incoming requests enclose a content entity, !NHttpRequestHandlers are expected to return a !ConsumingNHttpEntity for reading the content. After the entity is finished reading the data, !NHttpRequestHandler#handle() method is called to generate a response. + + !AsyncNHttpServiceHandler relies on !HttpProcessor to generate mandatory protocol headers for all outgoing messages and apply common, cross-cutting message transformations to all all incoming and outgoing messages, whereas individual HTTP request handlers are expected to take care of application specific content generation amd processing. {{{ HttpParams params; @@ -1541, +1545 @@ Users are encouraged to provide more sophisticated implementations of !NHttpRequestHandlerResolver, for instance, based on regular expressions. + === Asynchronous HTTP client handler === + + !AsyncNHttpClientHandler is a fully asynchronous HTTP client side protocol handler that implements the essential requirements of the HTTP protocol for the client side message processing as described by RFC 2616. !AsyncNHttpClientHandler is capable of executing HTTP requests with nearly constant memory footprint. Only HTTP message heads are stored in memory, while content of message bodies is streamed directly from the entity to the underlying channel (and vice versa) using !ConsumingNHttpEntity and !ProducingNHttpEntity interfaces. + + When using this implementation, it is important to ensure that entities supplied for writing implement !ProducingNHttpEntity. Doing so will allow the entity to be written out asynchronously. If entities supplied for writing do not implement the !ProducingNHttpEntity interface, a delegate is added that buffers the entire contents in memory. Additionally, the buffering might take place in the I/O dispatch thread, which could cause I/O to block temporarily. For best results, one must ensure that all entities set on !HttpRequests from !NHttpRequestExecutionHandler implement !ProducingNHttpEntity. + + If incoming responses enclose a content entity, !NHttpRequestExecutionHandler are expected to return a !ConsumingNHttpEntity for reading the content. After the entity is finished reading the data, !NHttpRequestExecutionHandler#handleResponse() method is called to process the response. + + !AsyncNHttpClientHandler relies on !HttpProcessor to generate mandatory protocol headers for all outgoing messages and apply common, cross-cutting message transformations to all all incoming and outgoing messages, whereas HTTP request executor is expected to take care of application specific content generation and processing. + + {{{ + // Initialize HTTP parameters + HttpParams params; + //Initialize HTTP processor + HttpProcessor httpproc; + //Create HTTP request execution handler + NHttpRequestExecutionHandler execHandler; + + AsyncNHttpClientHandler handler = new AsyncNHttpClientHandler( + httpproc, + execHandler, + new DefaultConnectionReuseStrategy(), + params); + }}} + + === Asynchronous HTTP request execution handler === + + Asynchronous HTTP request execution handler can be used by client-side protocol handlers to trigger the submission of a new HTTP request and the processing of an HTTP response. + + HTTP request execution exents as defined by the !NHttpRequestExecutionHandler interface: + + * initalizeContext: triggered when a new connection has been established and the HTTP context needs to be initialized. The attachment object passed to this method is the same object which was passed to the connecting I/O reactor when the connection request was made. The attachment may optionally contain some state information required in order to correctly initalize the HTTP context. + + * submitRequest: triggered when the underlying connection is ready to send a new HTTP request to the target host. This method may returt null if the client is not yet ready to send a request. In this case the connection will remain open and can be activated at a later point. If the request encloses an entity, the entity must be an instance of !ProducingNHttpEntity. + + * responseEntity: triggered when a response is received with an entity. This method should return a !ConsumingNHttpEntity that will be used to consume the entity. Null is a valid response value, and will indicate that the entity should be silently ignored. After the entity is fully consumed, #handleResponse method is called to notify a full response & entity are ready to be processed. + + * handleResponse: triggered when an HTTP response is ready to be processed. + + * finalizeContext: tiggered when the connection is terminated. This event can be used to release objects stored in the context or perform some other kind of cleanup. + + {{{ + NHttpRequestExecutionHandler execHandler = new NHttpRequestExecutionHandler() { + + private final static String DONE_FLAG = "done"; + + public void initalizeContext(HttpContext context, Object attachment) { + if (attachment != null) { + HttpHost virtualHost = (HttpHost) attachment; + context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, virtualHost); + } + } + + public void finalizeContext(HttpContext context) { + context.removeAttribute(DONE_FLAG); + } + + public HttpRequest submitRequest(HttpContext context) { + // Submit HTTP GET once + Object done = context.getAttribute(DONE_FLAG); + if (done == null) { + context.setAttribute(DONE_FLAG, Boolean.TRUE); + return new BasicHttpRequest("GET", "/"); + } else { + return null; + } + } + + public ConsumingNHttpEntity responseEntity(HttpResponse response, HttpContext context) throws IOException { + // Buffer imcoming content in memory for simplicity + return new BufferingNHttpEntity(response.getEntity(), + new HeapByteBufferAllocator()); + } + + public void handleResponse(HttpResponse response, HttpContext context) throws IOException { + System.out.println(response.getStatusLine()); + if (response.getEntity() != null) { + System.out.println(EntityUtils.toString(response.getEntity())); + } + } + + }; + }}} + === Buffering protocol handlers === Protocol handler implementations that buffer the content of HTTP messages @@ -1562, +1650 @@ HeaderValueParser / HeaderValueFormatter interfaces - == Example of building a simple HTTP server (using blocking I/O model) == - - Code sample with a code walk-through - - == Example of building a simple HTTP client (using blocking I/O model) == - - Code sample with a code walk-through - - == Example of building a simple HTTP server (using non-blocking I/O model) == - - Code sample with a code walk-through - - == Example of building a simple HTTP client (using non-blocking I/O model) == - - Code sample with a code walk-through - --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
