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.http4.helper;
018    
019    import java.net.URI;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.RuntimeCamelException;
023    import org.apache.camel.component.http4.HttpEndpoint;
024    import org.apache.camel.component.http4.HttpMethods;
025    import org.apache.http.HttpVersion;
026    import org.apache.http.ProtocolException;
027    
028    /**
029     * Helper methods for HTTP producers.
030     *
031     * @version $Revision: 980636 $
032     */
033    public final class HttpProducerHelper {
034    
035        private HttpProducerHelper() {
036        }
037    
038        /**
039         * Creates the URL to invoke.
040         *
041         * @param exchange the exchange
042         * @param endpoint the endpoint
043         * @return the URL to invoke
044         */
045        public static String createURL(Exchange exchange, HttpEndpoint endpoint) {
046            String uri = null;
047            if (!(endpoint.isBridgeEndpoint())) {
048                uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
049            }
050            if (uri == null) {
051                uri = endpoint.getHttpUri().toASCIIString();
052            }
053    
054            // append HTTP_PATH to HTTP_URI if it is provided in the header
055            String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
056            if (path != null) {
057                if (path.startsWith("/")) {
058                    URI baseURI;
059                    String baseURIString = exchange.getIn().getHeader(Exchange.HTTP_BASE_URI, String.class);
060                    try {
061                        if (baseURIString == null) {
062                            if (exchange.getFromEndpoint() != null) {
063                                baseURIString = exchange.getFromEndpoint().getEndpointUri();
064                            } else {
065                                // will set a default one for it
066                                baseURIString = "/";
067                            }
068                        }
069                        baseURI = new URI(baseURIString);
070                        String basePath = baseURI.getRawPath();
071                        if (path.startsWith(basePath)) {
072                            path = path.substring(basePath.length());
073                            if (path.startsWith("/")) {
074                                path = path.substring(1);
075                            }
076                        } else {
077                            throw new RuntimeCamelException("Cannot analyze the Exchange.HTTP_PATH header, due to: cannot find the right HTTP_BASE_URI");
078                        }
079                    } catch (Throwable t) {
080                        throw new RuntimeCamelException("Cannot analyze the Exchange.HTTP_PATH header, due to: "
081                                                        + t.getMessage(), t);
082                    }
083    
084                }
085                if (path.length() > 0) {
086                    // make sure that there is exactly one "/" between HTTP_URI and
087                    // HTTP_PATH
088                    if (!uri.endsWith("/")) {
089                        uri = uri + "/";
090                    }
091                    uri = uri.concat(path);
092                }
093            }
094            return uri;
095        }
096    
097        /**
098         * Creates the HttpMethod to use to call the remote server, often either its GET or POST.
099         *
100         * @param exchange the exchange
101         * @return the created method
102         */
103        public static HttpMethods createMethod(Exchange exchange, HttpEndpoint endpoint, boolean hasPayload) {
104            // is a query string provided in the endpoint URI or in a header (header
105            // overrules endpoint)
106            String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
107            if (queryString == null) {
108                queryString = endpoint.getHttpUri().getRawQuery();
109            }
110    
111            // compute what method to use either GET or POST
112            HttpMethods answer;
113            HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, HttpMethods.class);
114            if (m != null) {
115                // always use what end-user provides in a header
116                answer = m;
117            } else if (queryString != null) {
118                // if a query string is provided then use GET
119                answer = HttpMethods.GET;
120            } else {
121                // fallback to POST if we have payload, otherwise GET
122                answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
123            }
124    
125            return answer;
126        }
127        
128        public static HttpVersion parserHttpVersion(String s) throws ProtocolException {
129            int major;
130            int minor;
131            if (s == null) {
132                throw new IllegalArgumentException("String may not be null");
133            }
134            if (!s.startsWith("HTTP/")) {
135                throw new ProtocolException("Invalid HTTP version string: " + s);
136            }
137            int i1 = "HTTP/".length();
138            int i2 = s.indexOf(".", i1);
139            if (i2 == -1) {
140                throw new ProtocolException("Invalid HTTP version number: " + s);
141            }
142            try {
143                major = Integer.parseInt(s.substring(i1, i2)); 
144            } catch (NumberFormatException e) {
145                throw new ProtocolException("Invalid HTTP major version number: " + s);
146            }
147            i1 = i2 + 1;
148            i2 = s.length();
149            try {
150                minor = Integer.parseInt(s.substring(i1, i2)); 
151            } catch (NumberFormatException e) {
152                throw new ProtocolException("Invalid HTTP minor version number: " + s);
153            }
154            return new HttpVersion(major, minor);
155            
156        }
157    
158    }