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 org.apache.camel.Exchange; 020 import org.apache.camel.component.http.HttpEndpoint; 021 import org.apache.camel.component.http.HttpMethods; 022 023 /** 024 * Helper methods for HTTP producers. 025 * 026 * @version $Revision: 833429 $ 027 */ 028 public final class HttpProducerHelper { 029 030 private HttpProducerHelper() { 031 } 032 033 /** 034 * Creates the URL to invoke. 035 * 036 * @param exchange the exchange 037 * @param endpoint the endpoint 038 * @return the URL to invoke 039 */ 040 public static String createURL(Exchange exchange, HttpEndpoint endpoint) { 041 String uri = null; 042 if (!(endpoint.isBridgeEndpoint())) { 043 uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class); 044 } 045 if (uri == null) { 046 uri = endpoint.getHttpUri().toString(); 047 } 048 049 // append HTTP_PATH to HTTP_URI if it is provided in the header 050 // when the endpoint is not working as a bridge 051 String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class); 052 if (path != null) { 053 // make sure that there is exactly one "/" between HTTP_URI and 054 // HTTP_PATH 055 if (!uri.endsWith("/")) { 056 uri = uri + "/"; 057 } 058 if (path.startsWith("/")) { 059 path = path.substring(1); 060 } 061 uri = uri.concat(path); 062 } 063 064 return uri; 065 } 066 067 /** 068 * Creates the HttpMethod to use to call the remote server, often either its GET or POST. 069 * 070 * @param exchange the exchange 071 * @return the created method 072 */ 073 public static HttpMethods createMethod(Exchange exchange, HttpEndpoint endpoint, boolean hasPayload) { 074 // is a query string provided in the endpoint URI or in a header (header 075 // overrules endpoint) 076 String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class); 077 if (queryString == null) { 078 queryString = endpoint.getHttpUri().getQuery(); 079 } 080 081 // compute what method to use either GET or POST 082 HttpMethods answer; 083 HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, HttpMethods.class); 084 if (m != null) { 085 // always use what end-user provides in a header 086 answer = m; 087 } else if (queryString != null) { 088 // if a query string is provided then use GET 089 answer = HttpMethods.GET; 090 } else { 091 // fallback to POST if we have payload, otherwise GET 092 answer = hasPayload ? HttpMethods.POST : HttpMethods.GET; 093 } 094 095 return answer; 096 } 097 098 }