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.io.File; 020import java.net.InetAddress; 021import java.net.URI; 022import java.util.Map; 023 024import org.apache.activemq.util.InetAddressUtil; 025import org.apache.activemq.util.IntrospectionSupport; 026import org.eclipse.jetty.security.ConstraintMapping; 027import org.eclipse.jetty.security.ConstraintSecurityHandler; 028import org.eclipse.jetty.server.Connector; 029import org.eclipse.jetty.server.Server; 030import org.eclipse.jetty.util.security.Constraint; 031import org.eclipse.jetty.xml.XmlConfiguration; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035abstract public class WebTransportServerSupport extends TransportServerSupport { 036 037 private final static Logger LOG = LoggerFactory.getLogger(WebTransportServerSupport.class); 038 039 protected URI bindAddress; 040 protected Server server; 041 protected Connector connector; 042 protected SocketConnectorFactory socketConnectorFactory; 043 protected String host; 044 protected final HttpOptions httpOptions = new HttpOptions(); 045 protected final JettyOptions jettyOptions = new JettyOptions(); 046 047 public WebTransportServerSupport(URI location) { 048 super(location); 049 } 050 051 private <T> void setConnectorProperty(String name, Class<T> type, T value) throws Exception { 052 connector.getClass().getMethod("set" + name, type).invoke(connector, value); 053 } 054 055 protected void createServer() { 056 LOG.info("Starting Jetty server"); 057 if (jettyOptions.getConfig() != null) { 058 try { 059 LOG.info("Configuring Jetty server using {}", jettyOptions.getConfig()); 060 File file = new File(jettyOptions.getConfig()); 061 if (!file.exists()) { 062 throw new IllegalArgumentException("Jetty XML not found: " + file.getAbsolutePath()); 063 } 064 XmlConfiguration xmlConfiguration = new XmlConfiguration(file.toURI().toURL()); 065 server = (Server) xmlConfiguration.configure(); 066 } catch (Throwable t) { 067 throw new IllegalStateException("Jetty configuration can't be loaded", t); 068 } 069 } else { 070 server = new Server(); 071 } 072 try { 073 server.getClass().getMethod("setStopTimeout", Long.TYPE).invoke(server, 500l); 074 } catch (Throwable t) { 075 //ignore, jetty 8. 076 } 077 } 078 079 public URI bind() throws Exception { 080 URI bind = getBindLocation(); 081 String bindHost = bind.getHost(); 082 bindHost = (bindHost == null || bindHost.length() == 0) ? "localhost" : bindHost; 083 InetAddress addr = InetAddress.getByName(bindHost); 084 host = addr.getCanonicalHostName(); 085 if (server.getConnectors().length == 0) { 086 LOG.info("Creating Jetty connector"); 087 setConnectorProperty("Host", String.class, host); 088 setConnectorProperty("Port", Integer.TYPE, bindAddress.getPort()); 089 server.addConnector(connector); 090 } else { 091 LOG.info("Using Jetty configured connector"); 092 connector = server.getConnectors()[0]; 093 for (Connector c : server.getConnectors()) { 094 if (c.getName() != null && c.getName().equalsIgnoreCase("activemq")) { 095 connector = c; 096 } 097 } 098 setConnectorProperty("Host", String.class, host); 099 setConnectorProperty("Port", Integer.TYPE, bindAddress.getPort()); 100 server.addConnector(connector); 101 } 102 if (addr.isAnyLocalAddress()) { 103 host = InetAddressUtil.getLocalHostName(); 104 } 105 URI boundUri = new URI(bind.getScheme(), bind.getUserInfo(), host, bindAddress.getPort(), bind.getPath(), bind.getQuery(), bind.getFragment()); 106 setConnectURI(boundUri); 107 return boundUri; 108 } 109 110 protected void configureTraceMethod(ConstraintSecurityHandler securityHandler, 111 boolean enableTrace) { 112 Constraint constraint = new Constraint(); 113 constraint.setName("trace-security"); 114 //If enableTrace is true, then we want to set authenticate to false to allow it 115 constraint.setAuthenticate(!enableTrace); 116 ConstraintMapping mapping = new ConstraintMapping(); 117 mapping.setConstraint(constraint); 118 mapping.setMethod("TRACE"); 119 mapping.setPathSpec("/"); 120 securityHandler.addConstraintMapping(mapping); 121 } 122 123 public void setHttpOptions(Map<String, Object> options) { 124 if (options != null) { 125 IntrospectionSupport.setProperties(this.httpOptions, options); 126 } 127 } 128 129 public void setJettyOptions(Map<String, Object> options) { 130 if (options != null) { 131 IntrospectionSupport.setProperties(this.jettyOptions, options); 132 } 133 } 134 135 protected static class HttpOptions { 136 private boolean enableTrace = false; 137 138 public boolean isEnableTrace() { 139 return enableTrace; 140 } 141 142 public void setEnableTrace(boolean enableTrace) { 143 this.enableTrace = enableTrace; 144 } 145 } 146 147 protected static class JettyOptions { 148 private String config; 149 150 public String getConfig() { 151 return config; 152 } 153 154 public void setConfig(String config) { 155 this.config = config; 156 } 157 } 158 159}