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.component.rest;
018
019import java.util.Map;
020
021import org.apache.camel.Endpoint;
022import org.apache.camel.impl.UriEndpointComponent;
023import org.apache.camel.util.FileUtil;
024import org.apache.camel.util.ObjectHelper;
025
026/**
027 * Rest component.
028 */
029public class RestComponent extends UriEndpointComponent {
030
031    private String componentName;
032    private String apiDoc;
033    private String host;
034
035    public RestComponent() {
036        super(RestEndpoint.class);
037    }
038
039    @Override
040    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
041        RestEndpoint answer = new RestEndpoint(uri, this);
042        answer.setComponentName(componentName);
043        answer.setApiDoc(apiDoc);
044
045        // if no explicit host was given, then fallback and use default configured host
046        String h = getAndRemoveOrResolveReferenceParameter(parameters, "host", String.class, host);
047        if (h == null && getCamelContext().getRestConfiguration() != null) {
048            h = getCamelContext().getRestConfiguration().getHost();
049            int port = getCamelContext().getRestConfiguration().getPort();
050            // is there a custom port number
051            if (port > 0 && port != 80 && port != 443) {
052                h += ":" + port;
053            }
054        }
055        // host must start with http:// or https://
056        if (h != null && !(h.startsWith("http://") || h.startsWith("https://"))) {
057            h = "http://" + h;
058        }
059        answer.setHost(h);
060
061        String query = ObjectHelper.after(uri, "?");
062        if (query != null) {
063            answer.setQueryParameters(query);
064        }
065
066        setProperties(answer, parameters);
067        answer.setParameters(parameters);
068
069        if (!remaining.contains(":")) {
070            throw new IllegalArgumentException("Invalid syntax. Must be rest:method:path[:uriTemplate] where uriTemplate is optional");
071        }
072
073        String method = ObjectHelper.before(remaining, ":");
074        String s = ObjectHelper.after(remaining, ":");
075
076        String path;
077        String uriTemplate;
078        if (s != null && s.contains(":")) {
079            path = ObjectHelper.before(s, ":");
080            uriTemplate = ObjectHelper.after(s, ":");
081        } else {
082            path = s;
083            uriTemplate = null;
084        }
085
086        // remove trailing slashes
087        path = FileUtil.stripTrailingSeparator(path);
088        uriTemplate = FileUtil.stripTrailingSeparator(uriTemplate);
089
090        answer.setMethod(method);
091        answer.setPath(path);
092        answer.setUriTemplate(uriTemplate);
093
094        // if no explicit component name was given, then fallback and use default configured component name
095        if (answer.getComponentName() == null && getCamelContext().getRestConfiguration() != null) {
096            String name = getCamelContext().getRestConfiguration().getProducerComponent();
097            if (name == null) {
098                // fallback and use the consumer name
099                name = getCamelContext().getRestConfiguration().getComponent();
100            }
101            answer.setComponentName(name);
102        }
103        // if no explicit producer api was given, then fallback and use default configured
104        if (answer.getApiDoc() == null && getCamelContext().getRestConfiguration() != null) {
105            answer.setApiDoc(getCamelContext().getRestConfiguration().getProducerApiDoc());
106        }
107
108        return answer;
109    }
110
111    public String getComponentName() {
112        return componentName;
113    }
114
115    /**
116     * The Camel Rest component to use for the REST transport, such as restlet, spark-rest.
117     * If no component has been explicit configured, then Camel will lookup if there is a Camel component
118     * that integrates with the Rest DSL, or if a org.apache.camel.spi.RestConsumerFactory (consumer)
119     * or org.apache.camel.spi.RestProducerFactory (producer) is registered in the registry.
120     * If either one is found, then that is being used.
121     */
122    public void setComponentName(String componentName) {
123        this.componentName = componentName;
124    }
125
126    public String getApiDoc() {
127        return apiDoc;
128    }
129
130    /**
131     * The swagger api doc resource to use.
132     * The resource is loaded from classpath by default and must be in JSon format.
133     */
134    public void setApiDoc(String apiDoc) {
135        this.apiDoc = apiDoc;
136    }
137
138    public String getHost() {
139        return host;
140    }
141
142    /**
143     * Host and port of HTTP service to use (override host in swagger schema)
144     */
145    public void setHost(String host) {
146        this.host = host;
147    }
148
149}