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 */
017package org.apache.activemq.transport.http;
018
019import java.net.InetSocketAddress;
020import java.net.URI;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.activemq.command.BrokerInfo;
025import org.apache.activemq.transport.SocketConnectorFactory;
026import org.apache.activemq.transport.WebTransportServerSupport;
027import org.apache.activemq.transport.util.TextWireFormat;
028import org.apache.activemq.transport.xstream.XStreamWireFormat;
029import org.apache.activemq.util.ServiceStopper;
030import org.eclipse.jetty.security.ConstraintSecurityHandler;
031import org.eclipse.jetty.server.Connector;
032import org.eclipse.jetty.server.Handler;
033import org.eclipse.jetty.server.Server;
034import org.eclipse.jetty.server.handler.HandlerWrapper;
035import org.eclipse.jetty.servlet.ServletContextHandler;
036import org.eclipse.jetty.servlet.ServletHolder;
037
038public class HttpTransportServer extends WebTransportServerSupport {
039
040    private TextWireFormat wireFormat;
041    private final HttpTransportFactory transportFactory;
042    private Map<String, Object> wireFormatOptions = new HashMap<>();
043
044    public HttpTransportServer(URI uri, HttpTransportFactory factory) {
045        super(uri);
046        this.bindAddress = uri;
047        this.transportFactory = factory;
048        socketConnectorFactory = new SocketConnectorFactory();
049    }
050
051    @Override
052    public void setBrokerInfo(BrokerInfo brokerInfo) {
053    }
054
055    // Properties
056    // -------------------------------------------------------------------------
057    public TextWireFormat getWireFormat() {
058        if (wireFormat == null) {
059            wireFormat = createWireFormat();
060        }
061        return wireFormat;
062    }
063
064    public void setWireFormat(TextWireFormat wireFormat) {
065        this.wireFormat = wireFormat;
066    }
067
068    // Implementation methods
069    // -------------------------------------------------------------------------
070    protected TextWireFormat createWireFormat() {
071        return new XStreamWireFormat();
072    }
073
074    protected void setConnector(Connector connector) {
075        this.connector = connector;
076    }
077
078    @Override
079    protected void doStart() throws Exception {
080        createServer();
081        if (connector == null) {
082            connector = socketConnectorFactory.createConnector(server);
083        }
084
085        URI boundTo = bind();
086
087        ServletContextHandler contextHandler =
088            new ServletContextHandler(server, "/", ServletContextHandler.SECURITY);
089
090        ServletHolder holder = new ServletHolder();
091        holder.setServlet(new HttpTunnelServlet());
092        contextHandler.addServlet(holder, "/");
093
094        contextHandler.setAttribute("acceptListener", getAcceptListener());
095        contextHandler.setAttribute("wireFormat", getWireFormat());
096        contextHandler.setAttribute("transportFactory", transportFactory);
097        contextHandler.setAttribute("transportOptions", transportOptions);
098        contextHandler.setAttribute("wireFormatOptions", wireFormatOptions);
099
100        //AMQ-6182 - disabling trace by default
101        configureTraceMethod((ConstraintSecurityHandler) contextHandler.getSecurityHandler(),
102                httpOptions.isEnableTrace());
103
104        addGzipHandler(contextHandler);
105
106        server.start();
107
108        // Update the Connect To URI with our actual location in case the configured port
109        // was set to zero so that we report the actual port we are listening on.
110
111        int port = boundTo.getPort();
112        int p2 = getConnectorLocalPort();
113        if (p2 != -1) {
114            port = p2;
115        }
116
117        setConnectURI(new URI(boundTo.getScheme(),
118                              boundTo.getUserInfo(),
119                              boundTo.getHost(),
120                              port,
121                              boundTo.getPath(),
122                              boundTo.getQuery(),
123                              boundTo.getFragment()));
124    }
125
126    private int getConnectorLocalPort() throws Exception {
127        return (Integer)connector.getClass().getMethod("getLocalPort").invoke(connector);
128    }
129
130    private void addGzipHandler(ServletContextHandler contextHandler) throws Exception {
131        HandlerWrapper handler = null;
132        try {
133            handler = (HandlerWrapper) forName("org.eclipse.jetty.servlets.gzip.GzipHandler").newInstance();
134        } catch (Throwable t) {
135            handler = (HandlerWrapper) forName("org.eclipse.jetty.server.handler.gzip.GzipHandler").newInstance();
136        }
137        contextHandler.insertHandler(handler);
138    }
139
140    private Class<?> forName(String name) throws ClassNotFoundException {
141        Class<?> clazz = null;
142        ClassLoader loader = Thread.currentThread().getContextClassLoader();
143        if (loader != null) {
144            try {
145                clazz = loader.loadClass(name);
146            } catch (ClassNotFoundException e) {
147                // ignore
148            }
149        }
150        if (clazz == null) {
151            clazz = HttpTransportServer.class.getClassLoader().loadClass(name);
152        }
153
154        return clazz;
155    }
156
157    @Override
158    protected void doStop(ServiceStopper stopper) throws Exception {
159        Server temp = server;
160        server = null;
161        if (temp != null) {
162            temp.stop();
163        }
164    }
165
166    @Override
167    public InetSocketAddress getSocketAddress() {
168        return null;
169    }
170
171    @Override
172    public void setTransportOption(Map<String, Object> transportOptions) {
173        socketConnectorFactory.setTransportOptions(transportOptions);
174        super.setTransportOption(transportOptions);
175    }
176
177    public void setWireFormatOptions(Map<String, Object> wireFormatOptions) {
178        this.wireFormatOptions = wireFormatOptions;
179    }
180
181    @Override
182    public boolean isSslServer() {
183        return false;
184    }
185}