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.hc.core5.http.nio; 28 29 import java.io.IOException; 30 31 import org.apache.hc.core5.annotation.Contract; 32 import org.apache.hc.core5.annotation.ThreadingBehavior; 33 import org.apache.hc.core5.http.EntityDetails; 34 import org.apache.hc.core5.http.HttpException; 35 import org.apache.hc.core5.http.HttpRequest; 36 import org.apache.hc.core5.http.HttpResponse; 37 import org.apache.hc.core5.http.protocol.HttpContext; 38 39 /** 40 * AsyncServerRequestHandler represents a routine for processing of a specific group 41 * of HTTP requests. Request execution filters are designed to take care of protocol 42 * specific aspects, whereas individual request handlers are expected to take care 43 * of application specific HTTP processing. The main purpose of a request handler 44 * is to generate a response object with a content entity to be sent back to 45 * the client in response to the given request. 46 * 47 * @param <T> request representation. 48 * 49 * @since 5.0 50 */ 51 @Contract(threading = ThreadingBehavior.STATELESS) 52 public interface AsyncServerRequestHandler<T> { 53 54 /** 55 * Response trigger that can be used to submit a final HTTP response 56 * and terminate HTTP request processing. 57 */ 58 interface ResponseTrigger { 59 60 /** 61 * Sends an intermediate informational HTTP response to the client. 62 * 63 * @param response the intermediate (1xx) HTTP response 64 * @param context the actual execution context. 65 */ 66 void sendInformation(HttpResponse response, HttpContext context) throws HttpException, IOException; 67 68 /** 69 * Sends a final HTTP response to the client. 70 * 71 * @param responseProducer the HTTP response message producer. 72 * @param context the actual execution context. 73 */ 74 void submitResponse(AsyncResponseProducer responseProducer, HttpContext context) throws HttpException, IOException; 75 76 /** 77 * Pushes a request message head as a promise to deliver a response message. 78 * 79 * @param promise the request message header used as a promise. 80 * @param context the actual execution context. 81 * @param responseProducer the push response message producer. 82 */ 83 void pushPromise(HttpRequest promise, HttpContext context, AsyncPushProducer responseProducer) throws HttpException, IOException; 84 85 } 86 87 /** 88 * Triggered to signal new incoming request. The handler can create a {@link AsyncRequestConsumer} based on 89 * properties of the request head and entity details and let it process the request data stream. The request 90 * handler will be used to generate an object that represents request data. 91 * 92 * @param request the incoming request head. 93 * @param entityDetails the request entity details or {@code null} if the request 94 * does not enclose an entity. 95 * @param context the actual execution context. 96 * @return the request handler. 97 */ 98 AsyncRequestConsumer<T> prepare(HttpRequest request, EntityDetails entityDetails, HttpContext context) throws HttpException; 99 100 /** 101 * Triggered to handles the request object produced by the {@link AsyncRequestConsumer} returned 102 * from the {@link #prepare(HttpRequest, EntityDetails, HttpContext)} method. The handler can choose 103 * to send response messages immediately inside the call or asynchronously at some later point. 104 * 105 * @param requestObject the request object. 106 * @param responseTrigger the response trigger. 107 * @param context the actual execution context. 108 */ 109 void handle(T requestObject, ResponseTrigger responseTrigger, HttpContext context) throws HttpException, IOException; 110 111 }