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