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.http;
018    
019    import java.net.URI;
020    import java.net.URISyntaxException;
021    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
025    import org.apache.camel.Consumer;
026    import org.apache.camel.Processor;
027    import org.apache.camel.impl.DefaultEndpoint;
028    
029    /**
030     * Represents a <a href="http://activemq.apache.org/camel/http.html">HTTP
031     * endpoint</a>
032     * 
033     * @version $Revision: 563665 $
034     */
035    public class HttpEndpoint extends DefaultEndpoint<HttpExchange> {
036    
037        private HttpBinding binding;
038        private HttpComponent component;
039        private URI httpUri;
040    
041        protected HttpEndpoint(String uri, HttpComponent component) throws URISyntaxException {
042            super(uri, component);
043            this.component = component;
044            this.httpUri = new URI(uri);
045        }
046    
047        public HttpProducer createProducer() throws Exception {
048            return new HttpProducer(this);
049        }
050    
051        public Consumer<HttpExchange> createConsumer(Processor processor) throws Exception {
052            return new HttpConsumer(this, processor);
053        }
054    
055        public HttpExchange createExchange() {
056            return new HttpExchange(this);
057        }
058    
059        public HttpExchange createExchange(HttpServletRequest request, HttpServletResponse response) {
060            return new HttpExchange(this, request, response);
061        }
062    
063        public HttpBinding getBinding() {
064            if (binding == null) {
065                binding = new HttpBinding();
066            }
067            return binding;
068        }
069    
070        public void setBinding(HttpBinding binding) {
071            this.binding = binding;
072        }
073    
074        public boolean isSingleton() {
075            return true;
076        }
077    
078        public void connect(HttpConsumer consumer) throws Exception {
079            component.connect(consumer);
080        }
081    
082        public void disconnect(HttpConsumer consumer) throws Exception {
083            component.disconnect(consumer);
084        }
085    
086        public String getPath() {
087            return httpUri.getPath();
088        }
089    
090        public int getPort() {
091            if (httpUri.getPort() == -1) {
092                if ("https".equals(getProtocol())) {
093                    return 443;
094                } else {
095                    return 80;
096                }
097            }
098            return httpUri.getPort();
099        }
100    
101        public String getProtocol() {
102            return httpUri.getScheme();
103        }
104    }