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.http;
018    
019    import java.io.IOException;
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.camel.Message;
024    import org.apache.camel.spi.HeaderFilterStrategy;
025    
026    /**
027     * A plugable strategy for configuring the http binding so reading request and writing response
028     * can be customized using the Java Servlet API.
029     * <p/>
030     * This is used by the camel-jetty.
031     *
032     * @version $Revision: 724293 $
033     */
034    public interface HttpBinding {
035    
036        /**
037         * Startegy to read the given request and bindings it to the given message.
038         *
039         * @param request  the request
040         * @param message  the message to populate with data from request
041         */
042        void readRequest(HttpServletRequest request, HttpMessage message);
043    
044        /**
045         * Parses the body from a {@link org.apache.camel.component.http.HttpMessage}
046         *
047         * @return the parsed body returned as either a {@link java.io.InputStream} or a {@link java.io.Reader}
048         * depending on the {@link #setUseReaderForPayload(boolean)} property.
049         * @throws java.io.IOException can be thrown
050         */
051        Object parseBody(HttpMessage httpMessage) throws IOException;
052    
053        /**
054         * Writes the exchange to the servlet response.
055         * <p/>
056         * Default implementation will delegate to the following methods depending on the status of the exchange
057         * <ul>
058         *   <li>doWriteResponse - processing returns a OUT message </li>
059         *   <li>doWriteFaultResponse - processing returns a fault message</li>
060         *   <li>doWriteResponse - processing returns an exception and status code 500</li>
061         * </ul>
062         *
063         * @param exchange the exchange
064         * @param response the http response
065         * @throws java.io.IOException can be thrown from http response
066         */
067        void writeResponse(HttpExchange exchange, HttpServletResponse response) throws IOException;
068    
069        /**
070         * Strategy method that writes the response to the http response stream when an exception occuerd
071         *
072         * @param exception  the exception occured
073         * @param response   the http response
074         * @throws java.io.IOException can be thrown from http response
075         */
076        void doWriteExceptionResponse(Throwable exception, HttpServletResponse response) throws IOException;
077    
078        /**
079         * Strategy method that writes the response to the http response stream for a fault message
080         *
081         * @param message  the fault message
082         * @param response   the http response
083         * @throws java.io.IOException can be thrown from http response
084         */
085        void doWriteFaultResponse(Message message, HttpServletResponse response) throws IOException;
086    
087        /**
088         * Strategy method that writes the response to the http response stream for an OUT message
089         *
090         * @param message  the OUT message
091         * @param response   the http response
092         * @throws java.io.IOException can be thrown from http response
093         */
094        void doWriteResponse(Message message, HttpServletResponse response) throws IOException;
095    
096        boolean isUseReaderForPayload();
097    
098        /**
099         * Should the {@link javax.servlet.http.HttpServletRequest#getReader()} be exposed as the payload of input messages in the Camel
100         * {@link org.apache.camel.Message#getBody()} or not. If false then the {@link javax.servlet.http.HttpServletRequest#getInputStream()} will be exposed.
101         */
102        void setUseReaderForPayload(boolean useReaderForPayload);
103    
104        HeaderFilterStrategy getHeaderFilterStrategy();
105    
106        /**
107         * Sets the header filter stratety to use.
108         * <p/>
109         * Will default use {@link org.apache.camel.component.http.HttpHeaderFilterStrategy}
110         */
111        void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy);
112    
113    }