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 */
017package org.apache.camel.impl.cloud;
018
019import org.apache.camel.model.cloud.ServiceCallDefinition;
020import org.apache.camel.support.ServiceCallExpressionSupport;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024/**
025 * Support class for custom implementations of {@link ServiceCallDefinition ServiceCall EIP} components.
026 * <p/>
027 * Below are some examples how to call a service and what Camel endpoint URI is constructed based on the input:
028 * <pre>
029 serviceCall("myService") -> http4://hostname:port
030 serviceCall("myService/foo") -> http4://hostname:port/foo
031 serviceCall("http4:myService/foo") -> http4:hostname:port/foo
032 serviceCall("myService", "http4:myService.host:myService.port/foo") -> http4:hostname:port/foo
033 serviceCall("myService", "netty4:tcp:myService?connectTimeout=1000") -> netty:tcp:hostname:port?connectTimeout=1000
034 * </pre>
035 */
036public class DefaultServiceCallExpression extends ServiceCallExpressionSupport {
037    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultServiceCallExpression.class);
038
039    public DefaultServiceCallExpression() {
040    }
041
042    public DefaultServiceCallExpression(String hostHeader, String portHeader) {
043        super(hostHeader, portHeader);
044    }
045
046    @Override
047    protected String buildCamelEndpointUri(String name, String host, Integer port, String uri, String contextPath, String scheme) {
048        // build basic uri if none provided
049        String answer = uri;
050        if (answer == null) {
051            answer = doBuildCamelEndpointUri(host, port, contextPath, scheme);
052        } else {
053            // we have existing uri, then replace the serviceName with ip:port
054            if (answer.contains(name + ".host")) {
055                answer = answer.replaceFirst(name + "\\.host", host);
056            }
057            if (answer.contains(name + ".port") && port != null) {
058                answer = answer.replaceFirst(name + "\\.port", "" + port);
059            }
060            if (answer.contains(name) && port != null) {
061                answer = answer.replaceFirst(name, host + ":" + port);
062            }
063            if (answer.contains(name) && port == null) {
064                answer = answer.replaceFirst(name, host);
065            }
066        }
067
068        LOGGER.debug("Camel endpoint uri: {} for calling service: {} on server {}:{}", answer, name, host, port);
069        return answer;
070    }
071
072    protected String doBuildCamelEndpointUri(String host, Integer port, String contextPath, String scheme) {
073        if (scheme == null) {
074            // use http/https by default if no scheme or port have been configured
075            if (port == null) {
076                scheme = "http4";
077            } else if (port == 443) {
078                scheme = "https4";
079            } else {
080                scheme = "http4";
081            }
082        }
083
084        String answer = scheme + "://" + host;
085        if (port != null) {
086            answer = answer + ":" + port;
087        }
088        if (contextPath != null) {
089            if (!contextPath.startsWith("/")) {
090                contextPath = "/" + contextPath;
091            }
092
093            answer += contextPath;
094        }
095
096        return answer;
097    }
098}