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