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