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
026public class RestComponent extends UriEndpointComponent {
027
028    public RestComponent() {
029        super(RestEndpoint.class);
030    }
031
032    @Override
033    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
034        RestEndpoint answer = new RestEndpoint(uri, this);
035        setProperties(answer, parameters);
036        answer.setParameters(parameters);
037
038        if (!remaining.contains(":")) {
039            throw new IllegalArgumentException("Invalid syntax. Must be rest:method:path[:uriTemplate] where uriTemplate is optional");
040        }
041
042        String method = ObjectHelper.before(remaining, ":");
043        String s = ObjectHelper.after(remaining, ":");
044
045        String path;
046        String uriTemplate;
047        if (s != null && s.contains(":")) {
048            path = ObjectHelper.before(s, ":");
049            uriTemplate = ObjectHelper.after(s, ":");
050        } else {
051            path = s;
052            uriTemplate = null;
053        }
054
055        // remove trailing slashes
056        path = FileUtil.stripTrailingSeparator(path);
057        uriTemplate = FileUtil.stripTrailingSeparator(uriTemplate);
058
059        answer.setMethod(method);
060        answer.setPath(path);
061        answer.setUriTemplate(uriTemplate);
062
063        // if no explicit component name was given, then fallback and use default configured component name
064        if (answer.getComponentName() == null && getCamelContext().getRestConfiguration() != null) {
065            answer.setComponentName(getCamelContext().getRestConfiguration().getComponent());
066        }
067
068        return answer;
069    }
070
071}