001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.http;
019    
020    import java.net.URI;
021    import java.net.URISyntaxException;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    import org.apache.camel.Consumer;
027    import org.apache.camel.Processor;
028    import org.apache.camel.Producer;
029    import org.apache.camel.RuntimeCamelException;
030    import org.apache.camel.impl.DefaultEndpoint;
031    
032    /**
033     * Represents a <a href="http://activemq.apache.org/camel/http.html">HTTP endpoint</a>
034     *
035     * @version $Revision: 550760 $
036     */
037    public class HttpEndpoint extends DefaultEndpoint<HttpExchange> {
038    
039        private HttpBinding binding;
040            private HttpComponent component;
041            private URI httpUri;
042            
043        protected HttpEndpoint(String uri, HttpComponent component) throws URISyntaxException {
044            super(uri, component);
045                    this.component = component;
046                    this.httpUri = new URI(uri);
047        }
048    
049        public HttpProducer createProducer() throws Exception {
050            return new HttpProducer(this);
051        }
052    
053        public Consumer<HttpExchange> createConsumer(Processor processor) throws Exception {
054            return new HttpConsumer(this, processor);
055        }
056    
057        public HttpExchange createExchange() {
058            return new HttpExchange(this);
059        }
060    
061        public HttpExchange createExchange(HttpServletRequest request, HttpServletResponse response) {
062            return new HttpExchange(this, request, response);
063        }
064    
065        public HttpBinding getBinding() {
066            if (binding == null) {
067                binding = new HttpBinding();
068            }
069            return binding;
070        }
071    
072        public void setBinding(HttpBinding binding) {
073            this.binding = binding;
074        }
075        
076            public boolean isSingleton() {
077                    return true;
078            }
079    
080            public void connect(HttpConsumer consumer) throws Exception {
081                    component.connect(consumer);
082            }
083    
084            public void disconnect(HttpConsumer consumer) throws Exception {
085                    component.disconnect(consumer);
086            }
087    
088            public String getPath() {
089                    return httpUri.getPath();
090            }
091    
092            public int getPort() {
093                    if( httpUri.getPort() == -1 ) {
094                            if( "https".equals(getProtocol() ) ) {
095                                    return 443;
096                            } else {
097                                    return 80;
098                            }
099                    }
100                    return httpUri.getPort();
101            }
102    
103            public String getProtocol() {
104                    return httpUri.getScheme(); 
105            }
106    }