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.support;
018
019import org.apache.camel.Exchange;
020import org.apache.camel.impl.cloud.ServiceCallConstants;
021import org.apache.camel.util.ExchangeHelper;
022import org.apache.camel.util.ObjectHelper;
023
024public abstract class ServiceCallExpressionSupport extends ExpressionAdapter {
025    private String hostHeader;
026    private String portHeader;
027
028    public ServiceCallExpressionSupport() {
029        this(ServiceCallConstants.SERVICE_HOST, ServiceCallConstants.SERVICE_PORT);
030    }
031
032    public ServiceCallExpressionSupport(String hostHeader, String portHeader) {
033        this.hostHeader = hostHeader;
034        this.portHeader = portHeader;
035    }
036
037    public String getHostHeader() {
038        return hostHeader;
039    }
040
041    public void setHostHeader(String hostHeader) {
042        this.hostHeader = hostHeader;
043    }
044
045    public String getPortHeader() {
046        return portHeader;
047    }
048
049    public void setPortHeader(String portHeader) {
050        this.portHeader = portHeader;
051    }
052
053    @Override
054    public Object evaluate(Exchange exchange) {
055        try {
056            return buildCamelEndpointUri(
057                ExchangeHelper.getMandatoryHeader(exchange, ServiceCallConstants.SERVICE_NAME, String.class),
058                ExchangeHelper.getMandatoryHeader(exchange, hostHeader, String.class),
059                exchange.getIn().getHeader(portHeader, Integer.class),
060                exchange.getIn().getHeader(ServiceCallConstants.SERVICE_CALL_URI, String.class),
061                exchange.getIn().getHeader(ServiceCallConstants.SERVICE_CALL_CONTEXT_PATH, String.class),
062                exchange.getIn().getHeader(ServiceCallConstants.SERVICE_CALL_SCHEME, String.class));
063        } catch (Exception e) {
064            throw ObjectHelper.wrapRuntimeCamelException(e);
065        }
066    }
067
068    protected abstract String buildCamelEndpointUri(String name, String host, Integer port, String uri, String contextPath, String scheme);
069}