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