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 */
017 package org.apache.camel.component.restlet;
018
019 import java.util.List;
020 import java.util.Map;
021 import java.util.concurrent.atomic.AtomicBoolean;
022
023 import org.apache.camel.Consumer;
024 import org.apache.camel.ExchangePattern;
025 import org.apache.camel.Processor;
026 import org.apache.camel.Producer;
027 import org.apache.camel.impl.DefaultEndpoint;
028 import org.apache.camel.spi.HeaderFilterStrategy;
029 import org.apache.camel.spi.HeaderFilterStrategyAware;
030 import org.apache.commons.logging.Log;
031 import org.apache.commons.logging.LogFactory;
032 import org.restlet.data.Method;
033
034 /**
035 * Represents a <a href="http://www.restlet.org/"> endpoint</a>
036 *
037 * @version $Revision: 799322 $
038 */
039 public class RestletEndpoint extends DefaultEndpoint implements HeaderFilterStrategyAware {
040 private static final Log LOG = LogFactory.getLog(RestletEndpoint.class);
041
042 private static final int DEFAULT_PORT = 80;
043 private static final String DEFAULT_PROTOCOL = "http";
044 private static final String DEFAULT_HOST = "localhost";
045
046 private Method restletMethod = Method.GET;
047
048 // Optional and for consumer only. This allows a single route to service multiple
049 // methods. If it is non-null, restletMethod is ignored.
050 private Method[] restletMethods;
051
052 private String protocol = DEFAULT_PROTOCOL;
053 private String host = DEFAULT_HOST;
054 private int port = DEFAULT_PORT;
055 private String uriPattern;
056
057 // Optional and for consumer only. This allows a single route to service multiple
058 // URI patterns. The URI pattern defined in the endpoint will still be honored.
059 private List<String> restletUriPatterns;
060
061 private Map<String, String> restletRealm;
062 private HeaderFilterStrategy headerFilterStrategy;
063 private RestletBinding restletBinding;
064 private AtomicBoolean bindingInitialized = new AtomicBoolean(false);
065
066 public RestletEndpoint(RestletComponent component, String remaining) throws Exception {
067 super(remaining, component);
068 }
069
070 public boolean isSingleton() {
071 return true;
072 }
073
074 @Override
075 public boolean isLenientProperties() {
076 // true to allow dynamic URI options to be configured and passed to external system.
077 return true;
078 }
079
080 public Consumer createConsumer(Processor processor) throws Exception {
081 return new RestletConsumer(this, processor);
082 }
083
084 public Producer createProducer() throws Exception {
085 return new RestletProducer(this);
086 }
087
088 public void connect(RestletConsumer restletConsumer) throws Exception {
089 ((RestletComponent)getComponent()).connect(restletConsumer);
090 }
091
092 public void disconnect(RestletConsumer restletConsumer) throws Exception {
093 ((RestletComponent)getComponent()).disconnect(restletConsumer);
094 }
095
096 public Method getRestletMethod() {
097 return restletMethod;
098 }
099
100 public void setRestletMethod(Method restletMethod) {
101 this.restletMethod = restletMethod;
102 }
103
104 public String getProtocol() {
105 return protocol;
106 }
107
108 public void setProtocol(String protocol) {
109 this.protocol = protocol;
110 }
111
112 public String getHost() {
113 return host;
114 }
115
116 public void setHost(String host) {
117 this.host = host;
118 }
119
120 public int getPort() {
121 return port;
122 }
123
124 public void setPort(int port) {
125 this.port = port;
126 }
127
128 public String getUriPattern() {
129 return uriPattern;
130 }
131
132 public void setUriPattern(String uriPattern) {
133 this.uriPattern = uriPattern;
134 }
135
136 public RestletBinding getRestletBinding() {
137 if (restletBinding == null) {
138 restletBinding = new DefaultRestletBinding();
139 if (LOG.isDebugEnabled()) {
140 LOG.debug("Create default Restlet Binding " + restletBinding);
141 }
142 }
143
144 if (!bindingInitialized.getAndSet(true)
145 && restletBinding instanceof HeaderFilterStrategyAware) {
146 ((HeaderFilterStrategyAware)restletBinding).setHeaderFilterStrategy(getHeaderFilterStrategy());
147 }
148 return restletBinding;
149 }
150
151 public void setRestletBinding(RestletBinding restletBinding) {
152 this.restletBinding = restletBinding;
153 bindingInitialized.set(false);
154 }
155
156 public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
157 this.headerFilterStrategy = headerFilterStrategy;
158 if (restletBinding instanceof HeaderFilterStrategyAware) {
159 ((HeaderFilterStrategyAware)restletBinding).setHeaderFilterStrategy(headerFilterStrategy);
160 }
161 }
162
163 public HeaderFilterStrategy getHeaderFilterStrategy() {
164 if (headerFilterStrategy == null) {
165 headerFilterStrategy = new RestletHeaderFilterStrategy();
166 if (LOG.isDebugEnabled()) {
167 LOG.debug("Create Restlet default header filter strategy " + headerFilterStrategy);
168 }
169 }
170 return headerFilterStrategy;
171 }
172
173 public void setRestletRealm(Map<String, String> restletRealm) {
174 this.restletRealm = restletRealm;
175 }
176
177 public Map<String, String> getRestletRealm() {
178 return restletRealm;
179 }
180
181 @Override
182 public ExchangePattern getExchangePattern() {
183 // should always use in out for restlet
184 return ExchangePattern.InOut;
185 }
186
187 /**
188 * @param restletMethods the restletMethods to set
189 */
190 public void setRestletMethods(Method[] restletMethods) {
191 this.restletMethods = restletMethods;
192 }
193
194 /**
195 * @return the restletMethods
196 */
197 public Method[] getRestletMethods() {
198 return restletMethods;
199 }
200
201 /**
202 * @param restletUriPatterns the restletUriPatterns to set
203 */
204 public void setRestletUriPatterns(List<String> restletUriPatterns) {
205 this.restletUriPatterns = restletUriPatterns;
206 }
207
208 /**
209 * @return the restletUriPatterns
210 */
211 public List<String> getRestletUriPatterns() {
212 return restletUriPatterns;
213 }
214
215 }