001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.ahc;
018    
019    import java.io.ByteArrayOutputStream;
020    
021    import com.ning.http.client.HttpResponseHeaders;
022    import com.ning.http.client.HttpResponseStatus;
023    import com.ning.http.client.Request;
024    import org.apache.camel.Exchange;
025    
026    /**
027     * Binding from Camel to/from {@link com.ning.http.client.AsyncHttpClient}
028     */
029    public interface AhcBinding {
030    
031        /**
032         * Prepares the AHC {@link Request} to be send.
033         *
034         * @param endpoint the endpoint
035         * @param exchange the exchange
036         * @return the request to send using the {@link com.ning.http.client.AsyncHttpClient}
037         * @throws Exception is thrown if error occurred preparing the request
038         */
039        Request prepareRequest(AhcEndpoint endpoint, Exchange exchange) throws Exception;
040    
041        /**
042         * Callback from the {@link com.ning.http.client.AsyncHttpClient} when an exception occurred sending the request.
043         *
044         * @param endpoint the endpoint
045         * @param exchange the exchange
046         * @param t        the thrown exception
047         * @throws Exception is thrown if error occurred in the callback
048         */
049        void onThrowable(AhcEndpoint endpoint, Exchange exchange, Throwable t) throws Exception;
050    
051        /**
052         * Callback from the {@link com.ning.http.client.AsyncHttpClient} when the HTTP response status was received
053         *
054         * @param endpoint       the endpoint
055         * @param exchange       the exchange
056         * @param responseStatus the HTTP response status
057         * @throws Exception is thrown if error occurred in the callback
058         */
059        void onStatusReceived(AhcEndpoint endpoint, Exchange exchange, HttpResponseStatus responseStatus) throws Exception;
060    
061        /**
062         * Callback from the {@link com.ning.http.client.AsyncHttpClient} when the HTTP headers was received
063         *
064         * @param endpoint the endpoint
065         * @param exchange the exchange
066         * @param headers  the HTTP headers
067         * @throws Exception is thrown if error occurred in the callback
068         */
069        void onHeadersReceived(AhcEndpoint endpoint, Exchange exchange, HttpResponseHeaders headers) throws Exception;
070    
071        /**
072         * Callback from the {@link com.ning.http.client.AsyncHttpClient} when complete and all the response has been received.
073         *
074         *
075         * @param endpoint      the endpoint
076         * @param exchange      the exchange
077         * @param url           the url requested
078         * @param os            output stream with the HTTP response body
079         * @param contentLength length of the response body
080         * @param statusCode    the http response code
081         * @param statusText    the http status text
082         * @throws Exception is thrown if error occurred in the callback
083         */
084        void onComplete(AhcEndpoint endpoint, Exchange exchange, String url, ByteArrayOutputStream os, int contentLength,
085                        int statusCode, String statusText) throws Exception;
086    }