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;
018
019import java.net.InetAddress;
020import java.net.URI;
021import java.util.Map;
022
023import org.apache.activemq.util.InetAddressUtil;
024import org.apache.activemq.util.IntrospectionSupport;
025import org.eclipse.jetty.security.ConstraintMapping;
026import org.eclipse.jetty.security.ConstraintSecurityHandler;
027import org.eclipse.jetty.server.Connector;
028import org.eclipse.jetty.server.Server;
029import org.eclipse.jetty.util.security.Constraint;
030
031abstract public class WebTransportServerSupport extends TransportServerSupport {
032
033    protected URI bindAddress;
034    protected Server server;
035    protected Connector connector;
036    protected SocketConnectorFactory socketConnectorFactory;
037    protected String host;
038    protected final HttpOptions httpOptions = new HttpOptions();
039
040    public WebTransportServerSupport(URI location) {
041        super(location);
042    }
043
044    private <T> void setConnectorProperty(String name, Class<T> type, T value) throws Exception {
045        connector.getClass().getMethod("set" + name, type).invoke(connector, value);
046    }
047
048    protected void createServer() {
049        server = new Server();
050        try {
051            server.getClass().getMethod("setStopTimeout", Long.TYPE).invoke(server, 500l);
052        } catch (Throwable t) {
053            //ignore, jetty 8.
054        }
055    }
056
057    public URI bind() throws Exception {
058
059        URI bind = getBindLocation();
060
061        String bindHost = bind.getHost();
062        bindHost = (bindHost == null || bindHost.length() == 0) ? "localhost" : bindHost;
063        InetAddress addr = InetAddress.getByName(bindHost);
064        host = addr.getCanonicalHostName();
065
066        setConnectorProperty("Host", String.class, host);
067        setConnectorProperty("Port", Integer.TYPE, bindAddress.getPort());
068        server.addConnector(connector);
069        if (addr.isAnyLocalAddress()) {
070            host = InetAddressUtil.getLocalHostName();
071        }
072
073        URI boundUri = new URI(bind.getScheme(), bind.getUserInfo(), host, bindAddress.getPort(), bind.getPath(), bind.getQuery(), bind.getFragment());
074        setConnectURI(boundUri);
075        return boundUri;
076    }
077
078    protected void configureTraceMethod(ConstraintSecurityHandler securityHandler,
079            boolean enableTrace) {
080        Constraint constraint = new Constraint();
081        constraint.setName("trace-security");
082        //If enableTrace is true, then we want to set authenticate to false to allow it
083        constraint.setAuthenticate(!enableTrace);
084        ConstraintMapping mapping = new ConstraintMapping();
085        mapping.setConstraint(constraint);
086        mapping.setMethod("TRACE");
087        mapping.setPathSpec("/");
088        securityHandler.addConstraintMapping(mapping);
089    }
090
091    public void setHttpOptions(Map<String, Object> options) {
092        if (options != null) {
093            IntrospectionSupport.setProperties(this.httpOptions, options);
094        }
095    }
096
097    protected static class HttpOptions {
098        private boolean enableTrace = false;
099
100        public boolean isEnableTrace() {
101            return enableTrace;
102        }
103
104        public void setEnableTrace(boolean enableTrace) {
105            this.enableTrace = enableTrace;
106        }
107    }
108}