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