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 used by the camel-jetty.
032     *
033     * @version $Revision: 787384 $
034     */
035    public interface HttpBinding {
036    
037        /**
038         * Startegy 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         * @return the parsed body returned as either a {@link java.io.InputStream} or a {@link java.io.Reader}
049         * depending on the {@link #setUseReaderForPayload(boolean)} property.
050         * @throws java.io.IOException can be thrown
051         */
052        Object parseBody(HttpMessage httpMessage) throws IOException;
053    
054        /**
055         * Writes the exchange to the servlet response.
056         * <p/>
057         * Default implementation will delegate to the following methods depending on the status of the exchange
058         * <ul>
059         *   <li>doWriteResponse - processing returns a OUT message </li>
060         *   <li>doWriteFaultResponse - processing returns a fault message</li>
061         *   <li>doWriteResponse - processing returns an exception and status code 500</li>
062         * </ul>
063         *
064         * @param exchange the exchange
065         * @param response the http response
066         * @throws java.io.IOException can be thrown from http response
067         */
068        void writeResponse(Exchange exchange, HttpServletResponse response) throws IOException;
069    
070        /**
071         * Strategy method that writes the response to the http response stream when an exception occuerd
072         *
073         * @param exception  the exception occured
074         * @param response   the http response
075         * @throws java.io.IOException can be thrown from http response
076         */
077        void doWriteExceptionResponse(Throwable exception, HttpServletResponse response) throws IOException;
078    
079        /**
080         * Strategy method that writes the response to the http response stream for a fault message
081         *
082         * @param message  the fault message
083         * @param response the http response
084         * @param exchange the exchange to provide context for header filtering
085         * @throws java.io.IOException can be thrown from http response
086         */
087        void doWriteFaultResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException;
088    
089        /**
090         * Strategy method that writes the response to the http response stream for an OUT message
091         *
092         * @param message  the OUT message
093         * @param response the http response
094         * @param exchange the exchange to provide context for header filtering
095         * @throws java.io.IOException can be thrown from http response
096         */
097        void doWriteResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException;
098    
099        boolean isUseReaderForPayload();
100    
101        /**
102         * Should the {@link javax.servlet.http.HttpServletRequest#getReader()} be exposed as the payload of input messages in the Camel
103         * {@link org.apache.camel.Message#getBody()} or not. If false then the {@link javax.servlet.http.HttpServletRequest#getInputStream()} will be exposed.
104         */
105        void setUseReaderForPayload(boolean useReaderForPayload);
106    
107        HeaderFilterStrategy getHeaderFilterStrategy();
108    
109        /**
110         * Sets the header filter stratety to use.
111         * <p/>
112         * Will default use {@link org.apache.camel.component.http.HttpHeaderFilterStrategy}
113         */
114        void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy);
115    
116    }