001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.http;
019    
020    import org.apache.camel.impl.DefaultMessage;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.RuntimeCamelException;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import java.util.Map;
026    import java.util.Enumeration;
027    import java.io.IOException;
028    
029    /**
030     * @version $Revision: 550444 $
031     */
032    public class HttpMessage extends DefaultMessage {
033        private HttpServletRequest request;
034    
035    
036        public HttpMessage(HttpExchange exchange, HttpServletRequest request) {
037            setExchange(exchange);
038            this.request = request;
039    
040            // lets force a parse of the body and headers
041            getBody();
042            getHeaders();
043        }
044    
045        @Override
046        public HttpExchange getExchange() {
047            return (HttpExchange) super.getExchange();
048        }
049    
050        public HttpServletRequest getRequest() {
051            return request;
052        }
053    
054        @Override
055        protected Object createBody() {
056            try {
057                return getExchange().getEndpoint().getBinding().parseBody(this);
058            }
059            catch (IOException e) {
060                throw new RuntimeCamelException(e);
061            }
062        }
063    
064        @Override
065        protected void populateInitialHeaders(Map<String, Object> map) {
066            Enumeration names = request.getHeaderNames();
067            while (names.hasMoreElements()) {
068                String name = (String) names.nextElement();
069                Object value = request.getHeader(name);
070                map.put(name, value);
071            }
072        }
073    }