You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by mo...@apache.org on 2017/09/01 13:17:00 UTC
[02/64] [partial] knox git commit: KNOX-998 - Refactoring save 1
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
deleted file mode 100644
index 2fa803f..0000000
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
+++ /dev/null
@@ -1,1029 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hadoop.gateway;
-
-import net.lingala.zip4j.core.ZipFile;
-import net.lingala.zip4j.exception.ZipException;
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.hadoop.gateway.audit.api.Action;
-import org.apache.hadoop.gateway.audit.api.ActionOutcome;
-import org.apache.hadoop.gateway.audit.api.AuditServiceFactory;
-import org.apache.hadoop.gateway.audit.api.Auditor;
-import org.apache.hadoop.gateway.audit.api.ResourceType;
-import org.apache.hadoop.gateway.audit.log4j.audit.AuditConstants;
-import org.apache.hadoop.gateway.config.GatewayConfig;
-import org.apache.hadoop.gateway.config.impl.GatewayConfigImpl;
-import org.apache.hadoop.gateway.deploy.DeploymentException;
-import org.apache.hadoop.gateway.deploy.DeploymentFactory;
-import org.apache.hadoop.gateway.filter.CorrelationHandler;
-import org.apache.hadoop.gateway.filter.PortMappingHelperHandler;
-import org.apache.hadoop.gateway.filter.RequestUpdateHandler;
-import org.apache.hadoop.gateway.i18n.messages.MessagesFactory;
-import org.apache.hadoop.gateway.i18n.resources.ResourcesFactory;
-import org.apache.hadoop.gateway.services.GatewayServices;
-import org.apache.hadoop.gateway.services.registry.ServiceRegistry;
-import org.apache.hadoop.gateway.services.security.SSLService;
-import org.apache.hadoop.gateway.services.topology.TopologyService;
-import org.apache.hadoop.gateway.topology.Application;
-import org.apache.hadoop.gateway.topology.Topology;
-import org.apache.hadoop.gateway.topology.TopologyEvent;
-import org.apache.hadoop.gateway.topology.TopologyListener;
-import org.apache.hadoop.gateway.trace.AccessHandler;
-import org.apache.hadoop.gateway.trace.ErrorHandler;
-import org.apache.hadoop.gateway.trace.TraceHandler;
-import org.apache.hadoop.gateway.util.Urls;
-import org.apache.hadoop.gateway.util.XmlUtils;
-import org.apache.hadoop.gateway.websockets.GatewayWebsocketHandler;
-import org.apache.log4j.PropertyConfigurator;
-import org.eclipse.jetty.server.ConnectionFactory;
-import org.eclipse.jetty.server.Connector;
-import org.eclipse.jetty.server.HttpConfiguration;
-import org.eclipse.jetty.server.HttpConnectionFactory;
-import org.eclipse.jetty.server.NetworkConnector;
-import org.eclipse.jetty.server.SecureRequestCustomizer;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.server.ServerConnector;
-import org.eclipse.jetty.server.handler.ContextHandler;
-import org.eclipse.jetty.server.handler.ContextHandlerCollection;
-import org.eclipse.jetty.server.handler.HandlerCollection;
-import org.eclipse.jetty.server.handler.RequestLogHandler;
-import org.eclipse.jetty.servlets.gzip.GzipHandler;
-import org.eclipse.jetty.util.ssl.SslContextFactory;
-import org.eclipse.jetty.util.thread.QueuedThreadPool;
-import org.eclipse.jetty.webapp.Configuration;
-import org.eclipse.jetty.webapp.WebAppContext;
-import org.jboss.shrinkwrap.api.exporter.ExplodedExporter;
-import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
-
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerException;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Serializable;
-import java.net.InetSocketAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.UnknownHostException;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
-import java.security.cert.CertificateException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.ServiceLoader;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.regex.Pattern;
-
-public class GatewayServer {
- private static final GatewayResources res = ResourcesFactory.get(GatewayResources.class);
- private static final GatewayMessages log = MessagesFactory.get(GatewayMessages.class);
- private static final Auditor auditor = AuditServiceFactory.getAuditService().getAuditor(AuditConstants.DEFAULT_AUDITOR_NAME,
- AuditConstants.KNOX_SERVICE_NAME, AuditConstants.KNOX_COMPONENT_NAME);
- private static final String DEFAULT_CONNECTOR_NAME = "default";
-
- private static GatewayServer server;
- private static GatewayServices services;
-
- private static Properties buildProperties;
-
- private Server jetty;
- private GatewayConfig config;
- private ContextHandlerCollection contexts;
- private TopologyService monitor;
- private TopologyListener listener;
- private Map<String, WebAppContext> deployments;
-
- public static void main( String[] args ) {
- try {
- configureLogging();
- logSysProps();
- CommandLine cmd = GatewayCommandLine.parse( args );
- if( cmd.hasOption( GatewayCommandLine.HELP_LONG ) ) {
- GatewayCommandLine.printHelp();
- } else if( cmd.hasOption( GatewayCommandLine.VERSION_LONG ) ) {
- printVersion();
- } else if( cmd.hasOption( GatewayCommandLine.REDEPLOY_LONG ) ) {
- redeployTopologies( cmd.getOptionValue( GatewayCommandLine.REDEPLOY_LONG ) );
- } else {
- buildProperties = loadBuildProperties();
- services = instantiateGatewayServices();
- if (services == null) {
- log.failedToInstantiateGatewayServices();
- }
- GatewayConfig config = new GatewayConfigImpl();
- if (config.isHadoopKerberosSecured()) {
- configureKerberosSecurity( config );
- }
- Map<String,String> options = new HashMap<>();
- options.put(GatewayCommandLine.PERSIST_LONG, Boolean.toString(cmd.hasOption(GatewayCommandLine.PERSIST_LONG)));
- services.init(config, options);
- if (!cmd.hasOption(GatewayCommandLine.NOSTART_LONG)) {
- startGateway( config, services );
- }
- }
- } catch ( ParseException e ) {
- log.failedToParseCommandLine( e );
- GatewayCommandLine.printHelp();
- } catch ( Exception e ) {
- log.failedToStartGateway( e );
- // Make sure the process exits.
- System.exit(1);
- }
- }
-
- private static void printVersion() {
- System.out.println( res.gatewayVersionMessage( // I18N not required.
- getBuildVersion(),
- getBuildHash() ) );
- }
-
- public static String getBuildHash() {
- String hash = "unknown";
- if( buildProperties != null ) {
- hash = buildProperties.getProperty( "build.hash", hash );
- }
- return hash;
- }
-
- public static String getBuildVersion() {
- String version = "unknown";
- if( buildProperties != null ) {
- version = buildProperties.getProperty( "build.version", version );
- }
- return version;
- }
-
- private static GatewayServices instantiateGatewayServices() {
- ServiceLoader<GatewayServices> loader = ServiceLoader.load( GatewayServices.class );
- Iterator<GatewayServices> services = loader.iterator();
- if (services.hasNext()) {
- return services.next();
- }
- return null;
- }
-
- public static synchronized GatewayServices getGatewayServices() {
- return services;
- }
-
- private static void logSysProp( String name ) {
- log.logSysProp( name, System.getProperty( name ) );
- }
-
- private static void logSysProps() {
- logSysProp( "user.name" );
- logSysProp( "user.dir" );
- logSysProp( "java.runtime.name" );
- logSysProp( "java.runtime.version" );
- logSysProp( "java.home" );
- }
-
- private static void configureLogging() {
- PropertyConfigurator.configure( System.getProperty( "log4j.configuration" ) );
-// String fileName = config.getGatewayConfDir() + File.separator + "log4j.properties";
-// File file = new File( fileName );
-// if( file.isFile() && file.canRead() ) {
-// FileInputStream stream;
-// try {
-// stream = new FileInputStream( file );
-// Properties properties = new Properties();
-// properties.load( stream );
-// PropertyConfigurator.configure( properties );
-// log.loadedLoggingConfig( fileName );
-// } catch( IOException e ) {
-// log.failedToLoadLoggingConfig( fileName );
-// }
-// }
- }
-
- private static void configureKerberosSecurity( GatewayConfig config ) {
- System.setProperty(GatewayConfig.HADOOP_KERBEROS_SECURED, "true");
- System.setProperty(GatewayConfig.KRB5_CONFIG, config.getKerberosConfig());
- System.setProperty(GatewayConfig.KRB5_DEBUG,
- Boolean.toString(config.isKerberosDebugEnabled()));
- System.setProperty(GatewayConfig.KRB5_LOGIN_CONFIG, config.getKerberosLoginConfig());
- System.setProperty(GatewayConfig.KRB5_USE_SUBJECT_CREDS_ONLY, "false");
- }
-
- private static Properties loadBuildProperties() {
- Properties properties = new Properties();
- InputStream inputStream = GatewayServer.class.getClassLoader().getResourceAsStream( "build.properties" );
- if( inputStream != null ) {
- try {
- properties.load( inputStream );
- inputStream.close();
- } catch( IOException e ) {
- // Ignore.
- }
- }
- return properties;
- }
-
- public static void redeployTopologies( String topologyName ) {
- TopologyService ts = getGatewayServices().getService(GatewayServices.TOPOLOGY_SERVICE);
- ts.reloadTopologies();
- ts.redeployTopologies(topologyName);
- }
-
- private void cleanupTopologyDeployments() {
- File deployDir = new File( config.getGatewayDeploymentDir() );
- TopologyService ts = getGatewayServices().getService(GatewayServices.TOPOLOGY_SERVICE);
- for( Topology topology : ts.getTopologies() ) {
- cleanupTopologyDeployments( deployDir, topology );
- }
- }
-
- private void cleanupTopologyDeployments( File deployDir, Topology topology ) {
- log.cleanupDeployments( topology.getName() );
- File[] files = deployDir.listFiles( new RegexFilenameFilter( topology.getName() + "\\.(war|topo)\\.[0-9A-Fa-f]+" ) );
- if( files != null ) {
- Arrays.sort( files, new FileModificationTimeDescendingComparator() );
- int verLimit = config.getGatewayDeploymentsBackupVersionLimit();
- long ageLimit = config.getGatewayDeploymentsBackupAgeLimit();
- long keepTime = System.currentTimeMillis() - ageLimit;
- for( int i=1; i<files.length; i++ ) {
- File file = files[i];
- if( ( ( verLimit >= 0 ) && ( i > verLimit ) ) ||
- ( ( ageLimit >= 0 ) && ( file.lastModified() < keepTime ) ) ) {
- log.cleanupDeployment( file.getAbsolutePath() );
- FileUtils.deleteQuietly( file );
- }
- }
- }
- }
-
- public static GatewayServer startGateway( GatewayConfig config, GatewayServices svcs ) throws Exception {
- log.startingGateway();
- server = new GatewayServer( config );
- synchronized ( server ) {
- //KM[ Commented this out because is causes problems with
- // multiple services instance used in a single test process.
- // I'm not sure what drive including this check though.
- //if (services == null) {
- services = svcs;
- //}
- //KM]
- services.start();
- DeploymentFactory.setGatewayServices(services);
- server.start();
- // Coverity CID 1352654
- URI uri = server.jetty.getURI();
-
- // Logging for topology <-> port
- InetSocketAddress[] addresses = new InetSocketAddress[server.jetty
- .getConnectors().length];
- for (int i = 0, n = addresses.length; i < n; i++) {
- NetworkConnector connector = (NetworkConnector) server.jetty
- .getConnectors()[i];
- if (connector != null) {
- for(ConnectionFactory x : connector.getConnectionFactories()) {
- if(x instanceof HttpConnectionFactory) {
- ((HttpConnectionFactory)x).getHttpConfiguration().setSendServerVersion(config.isGatewayServerHeaderEnabled());
- }
- }
- if (connector.getName() == null) {
- log.startedGateway(
- connector != null ? connector.getLocalPort() : -1);
- } else {
- log.startedGateway(connector != null ? connector.getName() : "",
- connector != null ? connector.getLocalPort() : -1);
- }
- }
- }
-
- return server;
- }
- }
-
- public GatewayServer( GatewayConfig config ) {
- this(config, null);
- }
-
- public GatewayServer( GatewayConfig config, Properties options ) {
- this.config = config;
- this.listener = new InternalTopologyListener();
- }
-
- /**
- * Create a connector for Gateway Server to listen on.
- *
- * @param server Jetty server
- * @param config GatewayConfig
- * @param port If value is > 0 then the given value is used else we
- * use the port provided in GatewayConfig.
- * @param topologyName Connector name, only used when not null
- * @return
- * @throws IOException
- * @throws CertificateException
- * @throws NoSuchAlgorithmException
- * @throws KeyStoreException
- */
- private static Connector createConnector(final Server server,
- final GatewayConfig config, final int port, final String topologyName)
- throws IOException, CertificateException, NoSuchAlgorithmException,
- KeyStoreException {
-
- ServerConnector connector;
-
- // Determine the socket address and check availability.
- InetSocketAddress address = config.getGatewayAddress();
- checkAddressAvailability( address );
-
- final int connectorPort = port > 0 ? port : address.getPort();
-
- checkPortConflict(connectorPort, topologyName, config);
-
- HttpConfiguration httpConfig = new HttpConfiguration();
- httpConfig.setRequestHeaderSize( config.getHttpServerRequestHeaderBuffer() );
- //httpConfig.setRequestBufferSize( config.getHttpServerRequestBuffer() );
- httpConfig.setResponseHeaderSize( config.getHttpServerResponseHeaderBuffer() );
- httpConfig.setOutputBufferSize( config.getHttpServerResponseBuffer() );
-
- if (config.isSSLEnabled()) {
- HttpConfiguration httpsConfig = new HttpConfiguration( httpConfig );
- httpsConfig.setSecureScheme( "https" );
- httpsConfig.setSecurePort( connectorPort );
- httpsConfig.addCustomizer( new SecureRequestCustomizer() );
- SSLService ssl = services.getService("SSLService");
- String keystoreFileName = config.getGatewaySecurityDir() + File.separatorChar + "keystores" + File.separatorChar + "gateway.jks";
- SslContextFactory sslContextFactory = (SslContextFactory)ssl.buildSslContextFactory( keystoreFileName );
- connector = new ServerConnector( server, sslContextFactory, new HttpConnectionFactory( httpsConfig ) );
- } else {
- connector = new ServerConnector( server );
- }
- connector.setHost( address.getHostName() );
- connector.setPort( connectorPort );
-
- if(!StringUtils.isBlank(topologyName)) {
- connector.setName(topologyName);
- }
-
- long idleTimeout = config.getGatewayIdleTimeout();
- if (idleTimeout > 0l) {
- connector.setIdleTimeout(idleTimeout);
- }
-
- return connector;
- }
-
- private static HandlerCollection createHandlers(
- final GatewayConfig config,
- final GatewayServices services,
- final ContextHandlerCollection contexts,
- final Map<String, Integer> topologyPortMap) {
- HandlerCollection handlers = new HandlerCollection();
- RequestLogHandler logHandler = new RequestLogHandler();
-
- logHandler.setRequestLog( new AccessHandler() );
-
- TraceHandler traceHandler = new TraceHandler();
- traceHandler.setHandler( contexts );
- traceHandler.setTracedBodyFilter( System.getProperty( "org.apache.knox.gateway.trace.body.status.filter" ) );
-
- CorrelationHandler correlationHandler = new CorrelationHandler();
- correlationHandler.setHandler( traceHandler );
-
- /* KNOX-732: Handler for GZip compression */
- GzipHandler gzipHandler = new GzipHandler();
- String[] mimeTypes = {};
- if (config.getMimeTypesToCompress() != null
- && !config.getMimeTypesToCompress().isEmpty()) {
- mimeTypes = (String[]) config.getMimeTypesToCompress().toArray();
- }
- gzipHandler.addIncludedMimeTypes(mimeTypes);
- gzipHandler.setHandler(correlationHandler);
-
- // Used to correct the {target} part of request with Topology Port Mapping feature
- final PortMappingHelperHandler portMappingHandler = new PortMappingHelperHandler(config);
- portMappingHandler.setHandler(gzipHandler);
-
- // If topology to port mapping feature is enabled then we add new Handler {RequestForwardHandler}
- // to the chain, this handler listens on the configured port (in gateway-site.xml)
- // and simply forwards requests to the correct context path.
-
- // The reason for adding ContextHandler is so that we can add a connector
- // to it on which the handler listens (exclusively).
-
-
- if (config.isGatewayPortMappingEnabled()) {
-
- for (final Map.Entry<String, Integer> entry : topologyPortMap
- .entrySet()) {
- log.createJettyHandler(entry.getKey());
- final ContextHandler topologyContextHandler = new ContextHandler();
-
- final RequestUpdateHandler updateHandler = new RequestUpdateHandler(
- config, entry.getKey(), services);
-
- topologyContextHandler.setHandler(updateHandler);
- topologyContextHandler.setVirtualHosts(
- new String[] { "@" + entry.getKey().toLowerCase() });
-
- handlers.addHandler(topologyContextHandler);
- }
-
- }
-
- handlers.addHandler(logHandler);
-
- if (config.isWebsocketEnabled()) {
- final GatewayWebsocketHandler websocketHandler = new GatewayWebsocketHandler(
- config, services);
- websocketHandler.setHandler(portMappingHandler);
-
- handlers.addHandler(websocketHandler);
-
- } else {
- handlers.addHandler(portMappingHandler);
- }
-
- return handlers;
- }
-
- /**
- * Sanity Check to make sure configured ports are free and there is not port
- * conflict.
- *
- * @param port
- * @param topologyName
- * @param config
- * @throws IOException
- */
- public static void checkPortConflict(final int port,
- final String topologyName, final GatewayConfig config)
- throws IOException {
-
- // Throw an exception if port in use
- if (isPortInUse(port)) {
- if (topologyName == null) {
- log.portAlreadyInUse(port);
- } else {
- log.portAlreadyInUse(port, topologyName);
- }
- throw new IOException(String.format(" Port %d already in use. ", port));
- }
-
- // if topology name is blank which means we have all topologies listening on this port
- if (StringUtils.isBlank(topologyName)) {
- // If we have Default Topology old and new configuration (Port Mapping) throw error.
- if (config.getGatewayPortMappings().containsValue(new Integer(port))
- && !StringUtils.isBlank(config.getDefaultTopologyName())) {
- log.portAlreadyInUse(port);
- throw new IOException(String.format(
- " Please map port %d using either \"gateway.port.mapping.sandbox\" or "
- + "\"default.app.topology.name\" property, "
- + "specifying both is not a valid configuration. ",
- port));
- }
- } else {
- // Topology name is not blank so check amongst other ports if we have a conflict
- for (final Map.Entry<String, Integer> entry : config
- .getGatewayPortMappings().entrySet()) {
- if (entry.getKey().equalsIgnoreCase(topologyName)) {
- continue;
- }
-
- if (entry.getValue() == port) {
- log.portAlreadyInUse(port, topologyName);
- throw new IOException(String.format(
- " Topologies %s and %s use the same port %d, ports for topologies (if defined) have to be unique. ",
- entry.getKey(), topologyName, port));
- }
-
- }
-
- }
-
- }
-
- private synchronized void start() throws Exception {
- // Create the global context handler.
- contexts = new ContextHandlerCollection();
-
- // A map to keep track of current deployments by cluster name.
- deployments = new ConcurrentHashMap<>();
-
- // Start Jetty.
- jetty = new Server( new QueuedThreadPool( config.getThreadPoolMax() ) );
-
- /* topologyName is null because all topology listen on this port */
- jetty.addConnector( createConnector( jetty, config, config.getGatewayPort(), null) );
-
-
- // Add Annotations processing into the Jetty server to support JSPs
- Configuration.ClassList classlist = Configuration.ClassList.setServerDefault( jetty );
- classlist.addBefore(
- "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
- "org.eclipse.jetty.annotations.AnnotationConfiguration" );
-
- // Load the current topologies.
- File topologiesDir = calculateAbsoluteTopologiesDir();
- log.loadingTopologiesFromDirectory(topologiesDir.getAbsolutePath());
- monitor = services.getService(GatewayServices.TOPOLOGY_SERVICE);
- monitor.addTopologyChangeListener(listener);
- monitor.reloadTopologies();
-
- final Collection<Topology> topologies = monitor.getTopologies();
- final Map<String, Integer> topologyPortMap = config.getGatewayPortMappings();
-
- // List of all the topology that are deployed
- final List<String> deployedTopologyList = new ArrayList<String>();
-
- for (final Topology t : topologies) {
- deployedTopologyList.add(t.getName());
- }
-
-
- // Check whether the configured topologies for port mapping exist, if not
- // log WARN message and continue
- checkMappedTopologiesExist(topologyPortMap, deployedTopologyList);
-
- final HandlerCollection handlers = createHandlers( config, services, contexts, topologyPortMap);
-
- // Check whether a topology wants dedicated port,
- // if yes then we create a connector that listens on the provided port.
-
- log.gatewayTopologyPortMappingEnabled(config.isGatewayPortMappingEnabled());
- if (config.isGatewayPortMappingEnabled()) {
- for (Map.Entry<String, Integer> entry : topologyPortMap.entrySet()) {
- // Add connector for only valid topologies, i.e. deployed topologies.
- // and NOT for Default Topology listening on standard gateway port.
- if(deployedTopologyList.contains(entry.getKey()) && (entry.getValue().intValue() != config.getGatewayPort()) ) {
- log.createJettyConnector(entry.getKey().toLowerCase(), entry.getValue());
- jetty.addConnector(createConnector(jetty, config, entry.getValue(),
- entry.getKey().toLowerCase()));
- }
- }
- }
-
- jetty.setHandler(handlers);
-
- try {
- jetty.start();
- }
- catch (IOException e) {
- log.failedToStartGateway( e );
- throw e;
- }
-
- cleanupTopologyDeployments();
-
- // Start the topology monitor.
- log.monitoringTopologyChangesInDirectory(topologiesDir.getAbsolutePath());
- monitor.startMonitor();
- }
-
- public synchronized void stop() throws Exception {
- log.stoppingGateway();
- services.stop();
- monitor.stopMonitor();
- jetty.stop();
- jetty.join();
- log.stoppedGateway();
- }
-
- /**
- * Check whether a port is free
- *
- * @param port
- * @return true if port in use else false
- */
- public static boolean isPortInUse(final int port) {
-
- Socket socket = null;
- try {
- socket = new Socket("localhost", port);
- return true;
- } catch (final UnknownHostException e) {
- return false;
- } catch (final IOException e) {
- return false;
- } finally {
- IOUtils.closeQuietly(socket);
- }
-
- }
-
- /**
- * Checks whether the topologies defined in gateway-xml as part of Topology
- * Port mapping feature exists. If it does not Log a message and move on.
- *
- * @param configTopologies
- * @param topologies
- * @return
- */
- private void checkMappedTopologiesExist(
- final Map<String, Integer> configTopologies,
- final List<String> topologies) throws IOException {
-
- for(final Map.Entry<String, Integer> entry : configTopologies.entrySet()) {
-
- // If the topologies defined in gateway-config.xml are not found in gateway
- if (!topologies.contains(entry.getKey())) {
- log.topologyPortMappingCannotFindTopology(entry.getKey(), entry.getValue());
- }
-
- }
-
- }
-
- public URI getURI() {
- return jetty.getURI();
- }
-
- public InetSocketAddress[] getAddresses() {
- InetSocketAddress[] addresses = new InetSocketAddress[ jetty.getConnectors().length ];
- for( int i=0, n=addresses.length; i<n; i++ ) {
- NetworkConnector connector = (NetworkConnector)jetty.getConnectors()[ i ];
- String host = connector.getHost();
- if( host == null ) {
- addresses[ i ] = new InetSocketAddress( connector.getLocalPort() );
- } else {
- addresses[ i ] = new InetSocketAddress( host, connector.getLocalPort() );
- }
- }
- return addresses;
- }
-
- private ErrorHandler createErrorHandler() {
- ErrorHandler errorHandler = new ErrorHandler();
- errorHandler.setShowStacks( false );
- errorHandler.setTracedBodyFilter( System.getProperty( "org.apache.knox.gateway.trace.body.status.filter" ) );
- return errorHandler;
- }
-
- private WebAppContext createWebAppContext( Topology topology, File warFile, String warPath ) throws IOException, ZipException, TransformerException, SAXException, ParserConfigurationException {
- String topoName = topology.getName();
- WebAppContext context = new WebAppContext();
- String contextPath;
- contextPath = "/" + Urls.trimLeadingAndTrailingSlashJoin( config.getGatewayPath(), topoName, warPath );
- context.setContextPath( contextPath );
- context.setWar( warFile.getAbsolutePath() );
- context.setAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, topoName );
- context.setAttribute( "org.apache.knox.gateway.frontend.uri", getFrontendUri( context, config ) );
- context.setAttribute( GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE, config );
- // Add support for JSPs.
- context.setAttribute(
- "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
- ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$" );
- context.setTempDirectory( FileUtils.getFile( warFile, "META-INF", "temp" ) );
- context.setErrorHandler( createErrorHandler() );
- context.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
-
- return context;
- }
-
- private static void explodeWar( File source, File target ) throws IOException, ZipException {
- if( source.isDirectory() ) {
- FileUtils.copyDirectory( source, target );
- } else {
- ZipFile zip = new ZipFile( source );
- zip.extractAll( target.getAbsolutePath() );
- }
- }
-
- private void mergeWebXmlOverrides( File webInfDir ) throws IOException, SAXException, ParserConfigurationException, TransformerException {
- File webXmlFile = new File( webInfDir, "web.xml" );
- Document webXmlDoc;
- if( webXmlFile.exists() ) {
- // Backup original web.xml file.
- File originalWebXmlFile = new File( webInfDir, "original-web.xml" );
- FileUtils.copyFile( webXmlFile, originalWebXmlFile );
- webXmlDoc = XmlUtils.readXml( webXmlFile );
- } else {
- webXmlDoc = XmlUtils.createDocument();
- webXmlDoc.appendChild( webXmlDoc.createElement( "web-app" ) );
- }
- File overrideWebXmlFile = new File( webInfDir, "override-web.xml" );
- if( overrideWebXmlFile.exists() ) {
- Document overrideWebXmlDoc = XmlUtils.readXml( overrideWebXmlFile );
- Element originalRoot = webXmlDoc.getDocumentElement();
- Element overrideRoot = overrideWebXmlDoc.getDocumentElement();
- NodeList overrideNodes = overrideRoot.getChildNodes();
- for( int i = 0, n = overrideNodes.getLength(); i < n; i++ ) {
- Node overrideNode = overrideNodes.item( i );
- if( overrideNode.getNodeType() == Node.ELEMENT_NODE ) {
- Node importedNode = webXmlDoc.importNode( overrideNode, true );
- originalRoot.appendChild( importedNode );
- }
- }
-
- XmlUtils.writeXml( webXmlDoc, new FileWriter(webXmlFile) );
- }
- }
-
- private synchronized void internalDeployApplications( Topology topology, File topoDir ) throws IOException, ZipException, ParserConfigurationException, TransformerException, SAXException {
- if( topology != null ) {
- Collection<Application> applications = topology.getApplications();
- if( applications != null ) {
- for( Application application : applications ) {
- List<String> urls = application.getUrls();
- if( urls == null || urls.isEmpty() ) {
- internalDeployApplication( topology, topoDir, application, application.getName() );
- } else {
- for( String url : urls ) {
- internalDeployApplication( topology, topoDir, application, url );
- }
- }
- }
- }
- }
- }
-
- private synchronized void internalDeployApplication( Topology topology, File topoDir, Application application, String url ) throws IOException, ZipException, TransformerException, SAXException, ParserConfigurationException {
- File appsDir = new File( config.getGatewayApplicationsDir() );
- File appDir = new File( appsDir, application.getName() );
- File[] implFiles = appDir.listFiles( new RegexFilenameFilter( "app|app\\..*" ) );
- if( implFiles == null || implFiles.length == 0 ) {
- throw new DeploymentException( "Failed to find application in " + appDir );
- }
- File implFile = implFiles[0];
- File warDir = new File( topoDir, Urls.encode( "/" + Urls.trimLeadingAndTrailingSlash( url ) ) );
- File webInfDir = new File( warDir, "WEB-INF" );
- explodeWar( implFile, warDir );
- mergeWebXmlOverrides( webInfDir );
- createArchiveTempDir( warDir );
- }
-
- private synchronized void internalActivateTopology( Topology topology, File topoDir ) throws IOException, ZipException, ParserConfigurationException, TransformerException, SAXException {
- log.activatingTopology( topology.getName() );
- File[] files = topoDir.listFiles( new RegexFilenameFilter( "%.*" ) );
- if( files != null ) {
- for( File file : files ) {
- internalActivateArchive( topology, file );
- }
- }
- }
-
- private synchronized void internalActivateArchive( Topology topology, File warDir ) throws IOException, ZipException, ParserConfigurationException, TransformerException, SAXException {
- log.activatingTopologyArchive( topology.getName(), warDir.getName() );
- try {
- WebAppContext newContext = createWebAppContext( topology, warDir, Urls.decode( warDir.getName() ) );
- WebAppContext oldContext = deployments.get( newContext.getContextPath() );
- deployments.put( newContext.getContextPath(), newContext );
- if( oldContext != null ) {
- contexts.removeHandler( oldContext );
- }
- contexts.addHandler( newContext );
- if( contexts.isRunning() && !newContext.isRunning() ) {
- newContext.start();
- }
-
- } catch( Exception e ) {
- auditor.audit( Action.DEPLOY, topology.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE );
- log.failedToDeployTopology( topology.getName(), e );
- }
- }
-
-
- private synchronized void internalDeactivateTopology( Topology topology ) {
-
- log.deactivatingTopology( topology.getName() );
-
- String topoName = topology.getName();
- String topoPath = "/" + Urls.trimLeadingAndTrailingSlashJoin( config.getGatewayPath(), topoName );
- String topoPathSlash = topoPath + "/";
-
- ServiceRegistry sr = getGatewayServices().getService(GatewayServices.SERVICE_REGISTRY_SERVICE);
- if (sr != null) {
- sr.removeClusterServices( topoName );
- }
-
- // Find all the deployed contexts we need to deactivate.
- List<WebAppContext> deactivate = new ArrayList<WebAppContext>();
- if( deployments != null ) {
- for( WebAppContext app : deployments.values() ) {
- String appPath = app.getContextPath();
- if( appPath.equals( topoPath ) || appPath.startsWith( topoPathSlash ) ) {
- deactivate.add( app );
- }
- }
- }
-
- // Deactivate the required deployed contexts.
- for( WebAppContext context : deactivate ) {
- String contextPath = context.getContextPath();
- deployments.remove( contextPath );
- contexts.removeHandler( context );
- try {
- context.stop();
- } catch( Exception e ) {
- auditor.audit(Action.UNDEPLOY, topology.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
- log.failedToUndeployTopology( topology.getName(), e );
- }
- }
- deactivate.clear();
-
- }
-
- // Using an inner class to hide the handleTopologyEvent method from consumers of GatewayServer.
- private class InternalTopologyListener implements TopologyListener {
-
- @Override
- public void handleTopologyEvent( List<TopologyEvent> events ) {
- synchronized ( GatewayServer.this ) {
- for( TopologyEvent event : events ) {
- Topology topology = event.getTopology();
- File deployDir = calculateAbsoluteDeploymentsDir();
- if( event.getType().equals( TopologyEvent.Type.DELETED ) ) {
- handleDeleteDeployment(topology, deployDir);
- } else {
- handleCreateDeployment(topology, deployDir);
- }
- }
- }
- }
-
- private void handleDeleteDeployment(Topology topology, File deployDir) {
- log.deletingTopology( topology.getName() );
- File[] files = deployDir.listFiles( new RegexFilenameFilter( topology.getName() + "\\.(war|topo)\\.[0-9A-Fa-f]+" ) );
- if( files != null ) {
- auditor.audit(Action.UNDEPLOY, topology.getName(), ResourceType.TOPOLOGY,
- ActionOutcome.UNAVAILABLE);
- internalDeactivateTopology( topology );
- for( File file : files ) {
- log.deletingDeployment( file.getAbsolutePath() );
- FileUtils.deleteQuietly( file );
- }
- }
- }
-
- private void handleCreateDeployment(Topology topology, File deployDir) {
- try {
- File topoDir = calculateDeploymentDir( topology );
- if( !topoDir.exists() ) {
- auditor.audit( Action.DEPLOY, topology.getName(), ResourceType.TOPOLOGY, ActionOutcome.UNAVAILABLE );
-
-// KNOX-564 - Topology should fail to deploy with no providers configured.
-//TODO:APPS:This should only fail if there are services in the topology.
- if(topology.getProviders().isEmpty()) {
- throw new DeploymentException("No providers found inside topology.");
- }
-
- log.deployingTopology( topology.getName(), topoDir.getAbsolutePath() );
- internalDeactivateTopology( topology ); // KNOX-152
-
- EnterpriseArchive ear = DeploymentFactory.createDeployment( config, topology );
- if( !deployDir.exists() && !deployDir.mkdirs() ) {
- throw new DeploymentException( "Failed to create topology deployment temporary directory: " + deployDir.getAbsolutePath() );
- }
- File tmp = ear.as( ExplodedExporter.class ).exportExploded( deployDir, topoDir.getName() + ".tmp" );
- if( !tmp.renameTo( topoDir ) ) {
- FileUtils.deleteQuietly( tmp );
- throw new DeploymentException( "Failed to create topology deployment directory: " + topoDir.getAbsolutePath() );
- }
- internalDeployApplications( topology, topoDir );
- internalActivateTopology( topology, topoDir );
- log.deployedTopology( topology.getName());
- } else {
- auditor.audit( Action.REDEPLOY, topology.getName(), ResourceType.TOPOLOGY, ActionOutcome.UNAVAILABLE );
- log.redeployingTopology( topology.getName(), topoDir.getAbsolutePath() );
- internalActivateTopology( topology, topoDir );
- log.redeployedTopology( topology.getName() );
- }
- cleanupTopologyDeployments( deployDir, topology );
- } catch( Throwable e ) {
- auditor.audit( Action.DEPLOY, topology.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE );
- log.failedToDeployTopology( topology.getName(), e );
- }
- }
-
- }
-
- private File createArchiveTempDir( File warDir ) {
- File tempDir = FileUtils.getFile( warDir, "META-INF", "temp" );
- if( !tempDir.exists() && !tempDir.mkdirs() ) {
- throw new DeploymentException( "Failed to create archive temporary directory: " + tempDir.getAbsolutePath() );
- }
- return tempDir;
- }
-
- private static File calculateAbsoluteTopologiesDir( GatewayConfig config ) {
- File topoDir = new File( config.getGatewayTopologyDir() );
- topoDir = topoDir.getAbsoluteFile();
- return topoDir;
- }
-
- private static File calculateAbsoluteDeploymentsDir( GatewayConfig config ) {
- File deployDir = new File( config.getGatewayDeploymentDir() );
- deployDir = deployDir.getAbsoluteFile();
- return deployDir;
- }
-
- private File calculateAbsoluteTopologiesDir() {
- return calculateAbsoluteTopologiesDir( config );
- }
-
- private File calculateAbsoluteDeploymentsDir() {
- return calculateAbsoluteDeploymentsDir( config );
- }
-
- private File calculateDeploymentDir( Topology topology ) {
- File dir = new File( calculateAbsoluteDeploymentsDir(), calculateDeploymentName( topology ) );
- return dir;
- }
-
- private String calculateDeploymentExtension( Topology topology ) {
- return ".topo.";
- }
-
- private String calculateDeploymentName( Topology topology ) {
- String name = topology.getName() + calculateDeploymentExtension( topology ) + Long.toHexString( topology.getTimestamp() );
- return name;
- }
-
- private static void checkAddressAvailability( InetSocketAddress address ) throws IOException {
- ServerSocket socket = new ServerSocket();
- socket.bind( address );
- socket.close();
- }
-
- private static class RegexFilenameFilter implements FilenameFilter {
-
- Pattern pattern;
-
- RegexFilenameFilter( String regex ) {
- pattern = Pattern.compile( regex );
- }
-
- @Override
- public boolean accept( File dir, String name ) {
- return pattern.matcher( name ).matches();
- }
- }
-
- public URI getFrontendUri( WebAppContext context, GatewayConfig config ) {
- URI frontendUri = null;
- String frontendStr = config.getFrontendUrl();
- if( frontendStr != null && !frontendStr.trim().isEmpty() ) {
- String topoName = (String)context.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE );
- try {
- frontendStr = frontendStr.trim();
- if( frontendStr.endsWith( "/" ) ) {
- frontendUri = new URI( frontendStr + topoName );
- } else {
- frontendUri = new URI( frontendStr + "/" + topoName );
- }
- } catch( URISyntaxException e ) {
- throw new IllegalArgumentException( e );
- }
- }
- return frontendUri;
- }
-
- private static class FileModificationTimeDescendingComparator implements Comparator<File>, Serializable {
- /**
- *
- */
- private static final long serialVersionUID = -2269785204848916823L;
-
- @Override
- public int compare( File left, File right ) {
- long leftTime = ( left == null ? Long.MIN_VALUE : left.lastModified() );
- long rightTime = ( right == null ? Long.MIN_VALUE : right.lastModified() );
- if( leftTime > rightTime ) {
- return -1;
- } else if ( leftTime < rightTime ) {
- return 1;
- } else {
- return 0;
- }
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServlet.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServlet.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServlet.java
deleted file mode 100644
index 79cab09..0000000
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServlet.java
+++ /dev/null
@@ -1,302 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hadoop.gateway;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.URISyntaxException;
-import java.util.Enumeration;
-import javax.servlet.Filter;
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.Servlet;
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.hadoop.gateway.audit.api.Action;
-import org.apache.hadoop.gateway.audit.api.ActionOutcome;
-import org.apache.hadoop.gateway.audit.api.AuditService;
-import org.apache.hadoop.gateway.audit.api.AuditServiceFactory;
-import org.apache.hadoop.gateway.audit.api.Auditor;
-import org.apache.hadoop.gateway.audit.api.ResourceType;
-import org.apache.hadoop.gateway.audit.log4j.audit.AuditConstants;
-import org.apache.hadoop.gateway.config.GatewayConfig;
-import org.apache.hadoop.gateway.descriptor.GatewayDescriptor;
-import org.apache.hadoop.gateway.descriptor.GatewayDescriptorFactory;
-import org.apache.hadoop.gateway.filter.AbstractGatewayFilter;
-import org.apache.hadoop.gateway.i18n.messages.MessagesFactory;
-import org.apache.hadoop.gateway.i18n.resources.ResourcesFactory;
-import org.apache.hadoop.gateway.services.GatewayServices;
-import org.apache.hadoop.gateway.services.metrics.MetricsService;
-
-public class GatewayServlet implements Servlet, Filter {
-
- public static final String GATEWAY_DESCRIPTOR_LOCATION_DEFAULT = "gateway.xml";
- public static final String GATEWAY_DESCRIPTOR_LOCATION_PARAM = "gatewayDescriptorLocation";
-
- private static final GatewayResources res = ResourcesFactory.get( GatewayResources.class );
- private static final GatewayMessages LOG = MessagesFactory.get( GatewayMessages.class );
-
- private static AuditService auditService = AuditServiceFactory.getAuditService();
- private static Auditor auditor = AuditServiceFactory.getAuditService()
- .getAuditor( AuditConstants.DEFAULT_AUDITOR_NAME,
- AuditConstants.KNOX_SERVICE_NAME, AuditConstants.KNOX_COMPONENT_NAME );
-
- private FilterConfigAdapter filterConfig;
- private volatile GatewayFilter filter;
-
- public GatewayServlet( GatewayFilter filter ) {
- this.filterConfig = null;
- this.filter = filter;
- }
-
- public GatewayServlet() {
- this( null );
- }
-
- public synchronized GatewayFilter getFilter() {
- return filter;
- }
-
- public synchronized void setFilter( GatewayFilter filter ) throws ServletException {
- Filter prev = filter;
- if( filterConfig != null ) {
- filter.init( filterConfig );
- }
- this.filter = filter;
- if( prev != null && filterConfig != null ) {
- prev.destroy();
- }
- }
-
- @Override
- public synchronized void init( ServletConfig servletConfig ) throws ServletException {
- try {
- if( filter == null ) {
- filter = createFilter( servletConfig );
- }
- filterConfig = new FilterConfigAdapter( servletConfig );
- if( filter != null ) {
- filter.init( filterConfig );
- }
- } catch( ServletException e ) {
- LOG.failedToInitializeServletInstace( e );
- throw e;
- } catch( RuntimeException e ) {
- LOG.failedToInitializeServletInstace( e );
- throw e;
- }
- }
-
- @Override
- public void init( FilterConfig filterConfig ) throws ServletException {
- try {
- if( filter == null ) {
- filter = createFilter( filterConfig );
- }
- if( filter != null ) {
- filter.init( filterConfig );
- }
- } catch( ServletException e ) {
- LOG.failedToInitializeServletInstace( e );
- throw e;
- } catch( RuntimeException e ) {
- LOG.failedToInitializeServletInstace( e );
- throw e;
- }
- }
-
- @Override
- public ServletConfig getServletConfig() {
- return filterConfig.getServletConfig();
- }
-
- @Override
- public void service( ServletRequest servletRequest, ServletResponse servletResponse ) throws ServletException, IOException {
- try {
- auditService.createContext();
- GatewayFilter f = filter;
- if( f != null ) {
- try {
- f.doFilter( servletRequest, servletResponse, null );
- } catch( IOException e ) {
- LOG.failedToExecuteFilter( e );
- throw e;
- } catch( ServletException e ) {
- LOG.failedToExecuteFilter( e );
- throw e;
- } catch( RuntimeException e ) {
- LOG.failedToExecuteFilter( e );
- throw e;
- }
- } else {
- ((HttpServletResponse)servletResponse).setStatus( HttpServletResponse.SC_SERVICE_UNAVAILABLE );
- }
- String requestUri = (String)servletRequest.getAttribute( AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME );
- int status = ((HttpServletResponse)servletResponse).getStatus();
- auditor.audit( Action.ACCESS, requestUri, ResourceType.URI, ActionOutcome.SUCCESS, res.responseStatus( status ) );
- } finally {
- auditService.detachContext();
- }
- }
-
- @Override
- public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain ) throws IOException, ServletException {
- try {
- auditService.createContext();
- GatewayFilter f = filter;
- if( f != null ) {
- try {
- f.doFilter( servletRequest, servletResponse );
- //TODO: This should really happen naturally somehow as part of being a filter. This way will cause problems eventually.
- chain.doFilter( servletRequest, servletResponse );
- } catch( IOException e ) {
- LOG.failedToExecuteFilter( e );
- throw e;
- } catch( ServletException e ) {
- LOG.failedToExecuteFilter( e );
- throw e;
- } catch( RuntimeException e ) {
- LOG.failedToExecuteFilter( e );
- throw e;
- }
- } else {
- ((HttpServletResponse)servletResponse).setStatus( HttpServletResponse.SC_SERVICE_UNAVAILABLE );
- }
- String requestUri = (String)servletRequest.getAttribute( AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME );
- int status = ((HttpServletResponse)servletResponse).getStatus();
- auditor.audit( Action.ACCESS, requestUri, ResourceType.URI, ActionOutcome.SUCCESS, res.responseStatus( status ) );
- } finally {
- auditService.detachContext();
- }
- }
-
- @Override
- public String getServletInfo() {
- return res.gatewayServletInfo();
- }
-
- @Override
- public synchronized void destroy() {
- if( filter != null ) {
- filter.destroy();
- }
- filter = null;
- }
-
- private static GatewayFilter createFilter( InputStream stream, ServletContext servletContext ) throws ServletException {
- try {
- GatewayFilter filter = null;
- if( stream != null ) {
- try {
- GatewayDescriptor descriptor = GatewayDescriptorFactory.load( "xml", new InputStreamReader( stream ) );
- filter = GatewayFactory.create( descriptor );
- } finally {
- stream.close();
- }
- }
- GatewayConfig gatewayConfig = (GatewayConfig) servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
- if (gatewayConfig.isMetricsEnabled()) {
- GatewayServices gatewayServices = (GatewayServices) servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
- MetricsService metricsService = gatewayServices.getService(GatewayServices.METRICS_SERVICE);
- if (metricsService != null) {
- GatewayFilter instrumentedFilter = metricsService.getInstrumented(filter);
- if (instrumentedFilter != null) {
- filter = instrumentedFilter;
- }
- }
- }
- return filter;
- } catch( IOException e ) {
- throw new ServletException( e );
- } catch( URISyntaxException e ) {
- throw new ServletException( e );
- }
- }
-
- private static GatewayFilter createFilter( FilterConfig filterConfig ) throws ServletException {
- GatewayFilter filter;
- InputStream stream;
- String location = filterConfig.getInitParameter( GATEWAY_DESCRIPTOR_LOCATION_PARAM );
- if( location != null ) {
- stream = filterConfig.getServletContext().getResourceAsStream( location );
- if( stream == null ) {
- stream = filterConfig.getServletContext().getResourceAsStream( "/WEB-INF/" + location );
- }
- } else {
- stream = filterConfig.getServletContext().getResourceAsStream( GATEWAY_DESCRIPTOR_LOCATION_DEFAULT );
- }
- filter = createFilter( stream, filterConfig.getServletContext());
- return filter;
- }
-
- private static GatewayFilter createFilter( ServletConfig servletConfig ) throws ServletException {
- GatewayFilter filter;
- InputStream stream;
- String location = servletConfig.getInitParameter( GATEWAY_DESCRIPTOR_LOCATION_PARAM );
- if( location != null ) {
- stream = servletConfig.getServletContext().getResourceAsStream( location );
- if( stream == null ) {
- stream = servletConfig.getServletContext().getResourceAsStream( "/WEB-INF/" + location );
- }
- } else {
- stream = servletConfig.getServletContext().getResourceAsStream( GATEWAY_DESCRIPTOR_LOCATION_DEFAULT );
- }
- filter = createFilter( stream, servletConfig.getServletContext());
- return filter;
- }
-
- private static class FilterConfigAdapter implements FilterConfig {
-
- private ServletConfig config;
-
- private FilterConfigAdapter( ServletConfig config ) {
- this.config = config;
- }
-
- private ServletConfig getServletConfig() {
- return config;
- }
-
- @Override
- public String getFilterName() {
- return config.getServletName();
- }
-
- @Override
- public ServletContext getServletContext() {
- return config.getServletContext();
- }
-
- @Override
- public String getInitParameter( String name ) {
- return config.getInitParameter( name );
- }
-
- @Override
- public Enumeration<String> getInitParameterNames() {
- return config.getInitParameterNames();
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-server/src/main/java/org/apache/hadoop/gateway/config/impl/GatewayConfigImpl.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/config/impl/GatewayConfigImpl.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/config/impl/GatewayConfigImpl.java
deleted file mode 100644
index 3b7d19e..0000000
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/config/impl/GatewayConfigImpl.java
+++ /dev/null
@@ -1,915 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hadoop.gateway.config.impl;
-
-import org.apache.commons.lang3.StringUtils;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.fs.Path;
-import org.apache.hadoop.gateway.GatewayMessages;
-import org.apache.hadoop.gateway.config.GatewayConfig;
-import org.apache.hadoop.gateway.i18n.messages.MessagesFactory;
-import org.joda.time.Period;
-import org.joda.time.format.PeriodFormatter;
-import org.joda.time.format.PeriodFormatterBuilder;
-
-import java.io.File;
-import java.net.InetSocketAddress;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * The configuration for the Gateway.
- *
- * The Gateway configuration variables are described in gateway-default.xml
- *
- * The Gateway specific configuration is split into two layers:
- *
- * 1. gateway-default.xml - All the configuration variables that the
- * Gateway needs. These are the defaults that ship with the app
- * and should only be changed by the app developers.
- *
- * 2. gateway-site.xml - The (possibly empty) configuration that the
- * system administrator can set variables for their Hadoop cluster.
- *
- * To find the gateway configuration files the following process is used.
- * First, if the GATEWAY_HOME system property contains a valid directory name,
- * an attempt will be made to read the configuration files from that directory.
- * Second, if the GATEWAY_HOME environment variable contains a valid directory name,
- * an attempt will be made to read the configuration files from that directory.
- * Third, an attempt will be made to load the configuration files from the directory
- * specified via the "user.dir" system property.
- * Fourth, an attempt will be made to load the configuration files from the classpath.
- * Last, defaults will be used for all values will be used.
- *
- * If GATEWAY_HOME isn't set via either the system property or environment variable then
- * a value for this will be defaulted. The default selected will be the directory that
- * contained the last loaded configuration file that was not contained in a JAR. If
- * no such configuration file is loaded the value of the "user.dir" system property will be used
- * as the value of GATEWAY_HOME. This is important to consider for any relative file names as they
- * will be resolved relative to the value of GATEWAY_HOME. One such relative value is the
- * name of the directory containing cluster topologies. This value default to "clusters".
- */
-public class GatewayConfigImpl extends Configuration implements GatewayConfig {
-
- private static final String GATEWAY_DEFAULT_TOPOLOGY_NAME_PARAM = "default.app.topology.name";
- private static final String GATEWAY_DEFAULT_TOPOLOGY_NAME = null;
-
- private static final GatewayMessages log = MessagesFactory.get( GatewayMessages.class );
-
- private static final String GATEWAY_CONFIG_DIR_PREFIX = "conf";
-
- private static final String GATEWAY_CONFIG_FILE_PREFIX = "gateway";
-
- private static final String DEFAULT_STACKS_SERVICES_DIR = "services";
-
- private static final String DEFAULT_APPLICATIONS_DIR = "applications";
-
- public static final String[] GATEWAY_CONFIG_FILENAMES = {
- GATEWAY_CONFIG_DIR_PREFIX + "/" + GATEWAY_CONFIG_FILE_PREFIX + "-default.xml",
- GATEWAY_CONFIG_DIR_PREFIX + "/" + GATEWAY_CONFIG_FILE_PREFIX + "-site.xml"
- };
-
-// private static final String[] HADOOP_CONF_FILENAMES = {
-// "core-default.xml",
-// "core-site.xml"
-//// "hdfs-default.xml",
-//// "hdfs-site.xml",
-//// "mapred-default.xml",
-//// "mapred-site.xml"
-// };
-
-// private static final String[] HADOOP_PREFIX_VARS = {
-// "HADOOP_PREFIX",
-// "HADOOP_HOME"
-// };
-
- public static final String HTTP_HOST = GATEWAY_CONFIG_FILE_PREFIX + ".host";
- public static final String HTTP_PORT = GATEWAY_CONFIG_FILE_PREFIX + ".port";
- public static final String HTTP_PATH = GATEWAY_CONFIG_FILE_PREFIX + ".path";
- public static final String DEPLOYMENT_DIR = GATEWAY_CONFIG_FILE_PREFIX + ".deployment.dir";
- public static final String SECURITY_DIR = GATEWAY_CONFIG_FILE_PREFIX + ".security.dir";
- public static final String DATA_DIR = GATEWAY_CONFIG_FILE_PREFIX + ".data.dir";
- public static final String STACKS_SERVICES_DIR = GATEWAY_CONFIG_FILE_PREFIX + ".services.dir";
- public static final String GLOBAL_RULES_SERVICES = GATEWAY_CONFIG_FILE_PREFIX + ".global.rules.services";
- public static final String APPLICATIONS_DIR = GATEWAY_CONFIG_FILE_PREFIX + ".applications.dir";
- public static final String HADOOP_CONF_DIR = GATEWAY_CONFIG_FILE_PREFIX + ".hadoop.conf.dir";
- public static final String FRONTEND_URL = GATEWAY_CONFIG_FILE_PREFIX + ".frontend.url";
- private static final String TRUST_ALL_CERTS = GATEWAY_CONFIG_FILE_PREFIX + ".trust.all.certs";
- private static final String CLIENT_AUTH_NEEDED = GATEWAY_CONFIG_FILE_PREFIX + ".client.auth.needed";
- private static final String TRUSTSTORE_PATH = GATEWAY_CONFIG_FILE_PREFIX + ".truststore.path";
- private static final String TRUSTSTORE_TYPE = GATEWAY_CONFIG_FILE_PREFIX + ".truststore.type";
- private static final String KEYSTORE_TYPE = GATEWAY_CONFIG_FILE_PREFIX + ".keystore.type";
- private static final String XFORWARDED_ENABLED = GATEWAY_CONFIG_FILE_PREFIX + ".xforwarded.enabled";
- private static final String EPHEMERAL_DH_KEY_SIZE = GATEWAY_CONFIG_FILE_PREFIX + ".jdk.tls.ephemeralDHKeySize";
- private static final String HTTP_CLIENT_MAX_CONNECTION = GATEWAY_CONFIG_FILE_PREFIX + ".httpclient.maxConnections";
- private static final String HTTP_CLIENT_CONNECTION_TIMEOUT = GATEWAY_CONFIG_FILE_PREFIX + ".httpclient.connectionTimeout";
- private static final String HTTP_CLIENT_SOCKET_TIMEOUT = GATEWAY_CONFIG_FILE_PREFIX + ".httpclient.socketTimeout";
- private static final String THREAD_POOL_MAX = GATEWAY_CONFIG_FILE_PREFIX + ".threadpool.max";
- public static final String HTTP_SERVER_REQUEST_BUFFER = GATEWAY_CONFIG_FILE_PREFIX + ".httpserver.requestBuffer";
- public static final String HTTP_SERVER_REQUEST_HEADER_BUFFER = GATEWAY_CONFIG_FILE_PREFIX + ".httpserver.requestHeaderBuffer";
- public static final String HTTP_SERVER_RESPONSE_BUFFER = GATEWAY_CONFIG_FILE_PREFIX + ".httpserver.responseBuffer";
- public static final String HTTP_SERVER_RESPONSE_HEADER_BUFFER = GATEWAY_CONFIG_FILE_PREFIX + ".httpserver.responseHeaderBuffer";
- public static final String DEPLOYMENTS_BACKUP_VERSION_LIMIT = GATEWAY_CONFIG_FILE_PREFIX + ".deployment.backup.versionLimit";
- public static final String DEPLOYMENTS_BACKUP_AGE_LIMIT = GATEWAY_CONFIG_FILE_PREFIX + ".deployment.backup.ageLimit";
- public static final String METRICS_ENABLED = GATEWAY_CONFIG_FILE_PREFIX + ".metrics.enabled";
- public static final String JMX_METRICS_REPORTING_ENABLED = GATEWAY_CONFIG_FILE_PREFIX + ".jmx.metrics.reporting.enabled";
- public static final String GRAPHITE_METRICS_REPORTING_ENABLED = GATEWAY_CONFIG_FILE_PREFIX + ".graphite.metrics.reporting.enabled";
- public static final String GRAPHITE_METRICS_REPORTING_HOST = GATEWAY_CONFIG_FILE_PREFIX + ".graphite.metrics.reporting.host";
- public static final String GRAPHITE_METRICS_REPORTING_PORT = GATEWAY_CONFIG_FILE_PREFIX + ".graphite.metrics.reporting.port";
- public static final String GRAPHITE_METRICS_REPORTING_FREQUENCY = GATEWAY_CONFIG_FILE_PREFIX + ".graphite.metrics.reporting.frequency";
- public static final String GATEWAY_IDLE_TIMEOUT = GATEWAY_CONFIG_FILE_PREFIX + ".idle.timeout";
- public static final String REMOTE_IP_HEADER_NAME = GATEWAY_CONFIG_FILE_PREFIX + ".remote.ip.header.name";
-
- /* @since 0.10 Websocket config variables */
- public static final String WEBSOCKET_FEATURE_ENABLED = GATEWAY_CONFIG_FILE_PREFIX + ".websocket.feature.enabled";
- public static final String WEBSOCKET_MAX_TEXT_MESSAGE_SIZE = GATEWAY_CONFIG_FILE_PREFIX + ".websocket.max.text.size";
- public static final String WEBSOCKET_MAX_BINARY_MESSAGE_SIZE = GATEWAY_CONFIG_FILE_PREFIX + ".websocket.max.binary.size";
- public static final String WEBSOCKET_MAX_TEXT_MESSAGE_BUFFER_SIZE = GATEWAY_CONFIG_FILE_PREFIX + ".websocket.max.text.buffer.size";
- public static final String WEBSOCKET_MAX_BINARY_MESSAGE_BUFFER_SIZE = GATEWAY_CONFIG_FILE_PREFIX + ".websocket.max.binary.buffer.size";
- public static final String WEBSOCKET_INPUT_BUFFER_SIZE = GATEWAY_CONFIG_FILE_PREFIX + ".websocket.input.buffer.size";
- public static final String WEBSOCKET_ASYNC_WRITE_TIMEOUT = GATEWAY_CONFIG_FILE_PREFIX + ".websocket.async.write.timeout";
- public static final String WEBSOCKET_IDLE_TIMEOUT = GATEWAY_CONFIG_FILE_PREFIX + ".websocket.idle.timeout";
-
- /**
- * Properties for for gateway port mapping feature
- */
- public static final String GATEWAY_PORT_MAPPING_PREFIX = GATEWAY_CONFIG_FILE_PREFIX + ".port.mapping.";
- public static final String GATEWAY_PORT_MAPPING_REGEX = GATEWAY_CONFIG_FILE_PREFIX + "\\.port\\.mapping\\..*";
- public static final String GATEWAY_PORT_MAPPING_ENABLED = GATEWAY_PORT_MAPPING_PREFIX + "enabled";
-
- /**
- * Comma seperated list of MIME Types to be compressed by Knox on the way out.
- *
- * @since 0.12
- */
- public static final String MIME_TYPES_TO_COMPRESS = GATEWAY_CONFIG_FILE_PREFIX
- + ".gzip.compress.mime.types";
-
- // These config property names are not inline with the convention of using the
- // GATEWAY_CONFIG_FILE_PREFIX as is done by those above. These are left for
- // backward compatibility.
- // LET'S NOT CONTINUE THIS PATTERN BUT LEAVE THEM FOR NOW.
- private static final String SSL_ENABLED = "ssl.enabled";
- private static final String SSL_EXCLUDE_PROTOCOLS = "ssl.exclude.protocols";
- private static final String SSL_INCLUDE_CIPHERS = "ssl.include.ciphers";
- private static final String SSL_EXCLUDE_CIPHERS = "ssl.exclude.ciphers";
- // END BACKWARD COMPATIBLE BLOCK
-
- public static final String DEFAULT_HTTP_PORT = "8888";
- public static final String DEFAULT_HTTP_PATH = "gateway";
- public static final String DEFAULT_DEPLOYMENT_DIR = "deployments";
- public static final String DEFAULT_SECURITY_DIR = "security";
- public static final String DEFAULT_DATA_DIR = "data";
-
- /* Websocket defaults */
- public static final boolean DEFAULT_WEBSOCKET_FEATURE_ENABLED = false;
- public static final int DEFAULT_WEBSOCKET_MAX_TEXT_MESSAGE_SIZE = Integer.MAX_VALUE;;
- public static final int DEFAULT_WEBSOCKET_MAX_BINARY_MESSAGE_SIZE = Integer.MAX_VALUE;;
- public static final int DEFAULT_WEBSOCKET_MAX_TEXT_MESSAGE_BUFFER_SIZE = 32768;
- public static final int DEFAULT_WEBSOCKET_MAX_BINARY_MESSAGE_BUFFER_SIZE = 32768;
- public static final int DEFAULT_WEBSOCKET_INPUT_BUFFER_SIZE = 4096;
- public static final int DEFAULT_WEBSOCKET_ASYNC_WRITE_TIMEOUT = 60000;
- public static final int DEFAULT_WEBSOCKET_IDLE_TIMEOUT = 300000;
-
- public static final boolean DEFAULT_GATEWAY_PORT_MAPPING_ENABLED = true;
-
- /**
- * Default list of MIME Type to be compressed.
- * @since 0.12
- */
- public static final String DEFAULT_MIME_TYPES_TO_COMPRESS = "text/html, text/plain, text/xml, text/css, "
- + "application/javascript, application/x-javascript, text/javascript";
-
- public static final String COOKIE_SCOPING_ENABLED = GATEWAY_CONFIG_FILE_PREFIX + ".scope.cookies.feature.enabled";
- public static final boolean DEFAULT_COOKIE_SCOPING_FEATURE_ENABLED = false;
- private static final String CRYPTO_ALGORITHM = GATEWAY_CONFIG_FILE_PREFIX + ".crypto.algorithm";
- private static final String CRYPTO_PBE_ALGORITHM = GATEWAY_CONFIG_FILE_PREFIX + ".crypto.pbe.algorithm";
- private static final String CRYPTO_TRANSFORMATION = GATEWAY_CONFIG_FILE_PREFIX + ".crypto.transformation";
- private static final String CRYPTO_SALTSIZE = GATEWAY_CONFIG_FILE_PREFIX + ".crypto.salt.size";
- private static final String CRYPTO_ITERATION_COUNT = GATEWAY_CONFIG_FILE_PREFIX + ".crypto.iteration.count";
- private static final String CRYPTO_KEY_LENGTH = GATEWAY_CONFIG_FILE_PREFIX + ".crypto.key.length";
- public static final String SERVER_HEADER_ENABLED = GATEWAY_CONFIG_FILE_PREFIX + ".server.header.enabled";
-
- private static List<String> DEFAULT_GLOBAL_RULES_SERVICES;
-
-
- public GatewayConfigImpl() {
- init();
- }
-
- private String getVar( String variableName, String defaultValue ) {
- String value = get( variableName );
- if( value == null ) {
- value = System.getProperty( variableName );
- }
- if( value == null ) {
- value = System.getenv( variableName );
- }
- if( value == null ) {
- value = defaultValue;
- }
- return value;
- }
-
- private String getGatewayHomeDir() {
- String home = get(
- GATEWAY_HOME_VAR,
- System.getProperty(
- GATEWAY_HOME_VAR,
- System.getenv( GATEWAY_HOME_VAR ) ) );
- return home;
- }
-
- private void setGatewayHomeDir( String dir ) {
- set( GATEWAY_HOME_VAR, dir );
- }
-
- @Override
- public String getGatewayConfDir() {
- String value = getVar( GATEWAY_CONF_HOME_VAR, getGatewayHomeDir() + File.separator + "conf" );
- return value;
- }
-
- @Override
- public String getGatewayDataDir() {
- String systemValue =
- System.getProperty(GATEWAY_DATA_HOME_VAR, System.getenv(GATEWAY_DATA_HOME_VAR));
- String dataDir = null;
- if (systemValue != null) {
- dataDir = systemValue;
- } else {
- dataDir = get(DATA_DIR, getGatewayHomeDir() + File.separator + DEFAULT_DATA_DIR);
- }
- return dataDir;
- }
-
- @Override
- public String getGatewayServicesDir() {
- return get(STACKS_SERVICES_DIR, getGatewayDataDir() + File.separator + DEFAULT_STACKS_SERVICES_DIR);
- }
-
- @Override
- public String getGatewayApplicationsDir() {
- return get(APPLICATIONS_DIR, getGatewayDataDir() + File.separator + DEFAULT_APPLICATIONS_DIR);
- }
-
- @Override
- public String getHadoopConfDir() {
- return get( HADOOP_CONF_DIR );
- }
-
- private void init() {
- // Load environment variables.
- for( Map.Entry<String, String> e : System.getenv().entrySet() ) {
- set( "env." + e.getKey(), e.getValue() );
- }
- // Load system properties.
- for( Map.Entry<Object, Object> p : System.getProperties().entrySet() ) {
- set( "sys." + p.getKey().toString(), p.getValue().toString() );
- }
-
- URL lastFileUrl = null;
- for( String fileName : GATEWAY_CONFIG_FILENAMES ) {
- lastFileUrl = loadConfig( fileName, lastFileUrl );
- }
- //set default services list
- setDefaultGlobalRulesServices();
-
- initGatewayHomeDir( lastFileUrl );
-
- // log whether the scoping cookies to the gateway.path feature is enabled
- log.cookieScopingFeatureEnabled(isCookieScopingToPathEnabled());
- }
-
- private void setDefaultGlobalRulesServices() {
- DEFAULT_GLOBAL_RULES_SERVICES = new ArrayList<>();
- DEFAULT_GLOBAL_RULES_SERVICES.add("NAMENODE");
- DEFAULT_GLOBAL_RULES_SERVICES.add("JOBTRACKER");
- DEFAULT_GLOBAL_RULES_SERVICES.add("WEBHDFS");
- DEFAULT_GLOBAL_RULES_SERVICES.add("WEBHCAT");
- DEFAULT_GLOBAL_RULES_SERVICES.add("OOZIE");
- DEFAULT_GLOBAL_RULES_SERVICES.add("WEBHBASE");
- DEFAULT_GLOBAL_RULES_SERVICES.add("HIVE");
- DEFAULT_GLOBAL_RULES_SERVICES.add("RESOURCEMANAGER");
- }
-
- private void initGatewayHomeDir( URL lastFileUrl ) {
- String home = System.getProperty( GATEWAY_HOME_VAR );
- if( home != null ) {
- set( GATEWAY_HOME_VAR, home );
- log.settingGatewayHomeDir( "system property", home );
- return;
- }
- home = System.getenv( GATEWAY_HOME_VAR );
- if( home != null ) {
- set( GATEWAY_HOME_VAR, home );
- log.settingGatewayHomeDir( "environment variable", home );
- return;
- }
- if( lastFileUrl != null ) {
- File file = new File( lastFileUrl.getFile() ).getAbsoluteFile();
- File dir = file.getParentFile().getParentFile(); // Move up two levels to get to parent of conf.
- if( dir.exists() && dir.canRead() )
- home = dir.getAbsolutePath();
- set( GATEWAY_HOME_VAR, home );
- log.settingGatewayHomeDir( "configuration file location", home );
- return;
- }
- home = System.getProperty( "user.dir" );
- if( home != null ) {
- set( GATEWAY_HOME_VAR, home );
- log.settingGatewayHomeDir( "user.dir system property", home );
- return;
- }
- }
-
- // 1. GATEWAY_HOME system property
- // 2. GATEWAY_HOME environment variable
- // 3. user.dir system property
- // 4. class path
- private URL loadConfig( String fileName, URL lastFileUrl ) {
- lastFileUrl = loadConfigFile( System.getProperty( GATEWAY_HOME_VAR ), fileName );
- if( lastFileUrl == null ) {
- lastFileUrl = loadConfigFile( System.getenv( GATEWAY_HOME_VAR ), fileName );
- }
- if( lastFileUrl == null ) {
- lastFileUrl = loadConfigFile( System.getProperty( "user.dir" ), fileName );
- }
- if( lastFileUrl == null ) {
- lastFileUrl = loadConfigResource( fileName );
- }
- if( lastFileUrl != null && !"file".equals( lastFileUrl.getProtocol() ) ) {
- lastFileUrl = null;
- }
- return lastFileUrl;
- }
-
- private URL loadConfigFile( String dir, String file ) {
- URL url = null;
- if( dir != null ) {
- File f = new File( dir, file );
- if( f.exists() ) {
- String path = f.getAbsolutePath();
- try {
- url = f.toURI().toURL();
- addResource( new Path( path ) );
- log.loadingConfigurationFile( path );
- } catch ( MalformedURLException e ) {
- log.failedToLoadConfig( path, e );
- }
- }
- }
- return url;
- }
-
- private URL loadConfigResource( String file ) {
- URL url = getResource( file );
- if( url != null ) {
- log.loadingConfigurationResource( url.toExternalForm() );
- addResource( url );
- }
- return url;
- }
-
- @Override
- public String getGatewayHost() {
- String host = get( HTTP_HOST, "0.0.0.0" );
- return host;
- }
-
- @Override
- public int getGatewayPort() {
- return Integer.parseInt( get( HTTP_PORT, DEFAULT_HTTP_PORT ) );
- }
-
- @Override
- public String getGatewayPath() {
- return get( HTTP_PATH, DEFAULT_HTTP_PATH );
- }
-
- @Override
- public String getGatewayTopologyDir() {
- return getGatewayConfDir() + File.separator + "topologies";
- }
-
- @Override
- public String getGatewayDeploymentDir() {
- return get(DEPLOYMENT_DIR, getGatewayDataDir() + File.separator + DEFAULT_DEPLOYMENT_DIR);
- }
-
- @Override
- public String getGatewaySecurityDir() {
- return get(SECURITY_DIR, getGatewayDataDir() + File.separator + DEFAULT_SECURITY_DIR);
- }
-
- @Override
- public InetSocketAddress getGatewayAddress() throws UnknownHostException {
- String host = getGatewayHost();
- int port = getGatewayPort();
- InetSocketAddress address = new InetSocketAddress( host, port );
- return address;
- }
-
- @Override
- public boolean isSSLEnabled() {
- String enabled = get( SSL_ENABLED, "true" );
-
- return "true".equals(enabled);
- }
-
- @Override
- public boolean isHadoopKerberosSecured() {
- String hadoopKerberosSecured = get( HADOOP_KERBEROS_SECURED, "false" );
- return "true".equals(hadoopKerberosSecured);
- }
-
- @Override
- public String getKerberosConfig() {
- return get( KRB5_CONFIG ) ;
- }
-
- @Override
- public boolean isKerberosDebugEnabled() {
- String kerberosDebugEnabled = get( KRB5_DEBUG, "false" );
- return "true".equals(kerberosDebugEnabled);
- }
-
- @Override
- public String getKerberosLoginConfig() {
- return get( KRB5_LOGIN_CONFIG );
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getDefaultTopologyName()
- */
- @Override
- public String getDefaultTopologyName() {
- String name = get(GATEWAY_DEFAULT_TOPOLOGY_NAME_PARAM);
- return name != null ? name : GATEWAY_DEFAULT_TOPOLOGY_NAME;
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getDefaultAppRedirectPath()
- */
- @Override
- public String getDefaultAppRedirectPath() {
- String defTopo = getDefaultTopologyName();
- if( defTopo == null ) {
- return null;
- } else {
- return "/" + getGatewayPath() + "/" + defTopo;
- }
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getFrontendUrl()
- */
- @Override
- public String getFrontendUrl() {
- String url = get( FRONTEND_URL, null );
- return url;
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getExcludedSSLProtocols()
- */
- @Override
- public List<String> getExcludedSSLProtocols() {
- List<String> protocols = null;
- String value = get(SSL_EXCLUDE_PROTOCOLS);
- if (!"none".equals(value)) {
- protocols = Arrays.asList(value.split("\\s*,\\s*"));
- }
- return protocols;
- }
-
- @Override
- public List<String> getIncludedSSLCiphers() {
- List<String> list = null;
- String value = get(SSL_INCLUDE_CIPHERS);
- if (value != null && !value.isEmpty() && !"none".equalsIgnoreCase(value.trim())) {
- list = Arrays.asList(value.trim().split("\\s*,\\s*"));
- }
- return list;
- }
-
- @Override
- public List<String> getExcludedSSLCiphers() {
- List<String> list = null;
- String value = get(SSL_EXCLUDE_CIPHERS);
- if (value != null && !value.isEmpty() && !"none".equalsIgnoreCase(value.trim())) {
- list = Arrays.asList(value.trim().split("\\s*,\\s*"));
- }
- return list;
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#isClientAuthNeeded()
- */
- @Override
- public boolean isClientAuthNeeded() {
- String clientAuthNeeded = get( CLIENT_AUTH_NEEDED, "false" );
- return "true".equals(clientAuthNeeded);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getTruststorePath()
- */
- @Override
- public String getTruststorePath() {
- return get( TRUSTSTORE_PATH, null);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getTrustAllCerts()
- */
- @Override
- public boolean getTrustAllCerts() {
- String trustAllCerts = get( TRUST_ALL_CERTS, "false" );
- return "true".equals(trustAllCerts);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getTruststorePath()
- */
- @Override
- public String getTruststoreType() {
- return get( TRUSTSTORE_TYPE, "JKS");
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getTruststorePath()
- */
- @Override
- public String getKeystoreType() {
- return get( KEYSTORE_TYPE, "JKS");
- }
-
- @Override
- public boolean isXForwardedEnabled() {
- String xForwardedEnabled = get( XFORWARDED_ENABLED, "true" );
- return "true".equals(xForwardedEnabled);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getEphemeralDHKeySize()
- */
- @Override
- public String getEphemeralDHKeySize() {
- return get( EPHEMERAL_DH_KEY_SIZE, "2048");
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getHttpClientMaxConnections()
- */
- @Override
- public int getHttpClientMaxConnections() {
- return getInt( HTTP_CLIENT_MAX_CONNECTION, 32 );
- }
-
- @Override
- public int getHttpClientConnectionTimeout() {
- int t = -1;
- String s = get( HTTP_CLIENT_CONNECTION_TIMEOUT, null );
- if ( s != null ) {
- try {
- t = (int)parseNetworkTimeout( s );
- } catch ( Exception e ) {
- // Ignore it and use the default.
- }
- }
- return t;
- }
-
- @Override
- public int getHttpClientSocketTimeout() {
- int t = -1;
- String s = get( HTTP_CLIENT_SOCKET_TIMEOUT, null );
- if ( s != null ) {
- try {
- t = (int)parseNetworkTimeout( s );
- } catch ( Exception e ) {
- // Ignore it and use the default.
- }
- }
- return t;
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#getThreadPoolMax()
- */
- @Override
- public int getThreadPoolMax() {
- int i = getInt( THREAD_POOL_MAX, 254 );
- // Testing has shown that a value lower than 5 prevents Jetty from servicing request.
- if( i < 5 ) {
- i = 5;
- }
- return i;
- }
-
- @Override
- public int getHttpServerRequestBuffer() {
- int i = getInt( HTTP_SERVER_REQUEST_BUFFER, 16 * 1024 );
- return i;
- }
-
- @Override
- public int getHttpServerRequestHeaderBuffer() {
- int i = getInt( HTTP_SERVER_REQUEST_HEADER_BUFFER, 8 * 1024 );
- return i;
- }
-
- @Override
- public int getHttpServerResponseBuffer() {
- int i = getInt( HTTP_SERVER_RESPONSE_BUFFER, 32 * 1024 );
- return i;
- }
-
- @Override
- public int getHttpServerResponseHeaderBuffer() {
- int i = getInt( HTTP_SERVER_RESPONSE_HEADER_BUFFER, 8 * 1024 );
- return i;
- }
-
- @Override
- public int getGatewayDeploymentsBackupVersionLimit() {
- int i = getInt( DEPLOYMENTS_BACKUP_VERSION_LIMIT, 5 );
- if( i < 0 ) {
- i = -1;
- }
- return i;
- }
-
- @Override
- public long getGatewayIdleTimeout() {
- return getLong(GATEWAY_IDLE_TIMEOUT, 300000l);
- }
-
- @Override
- public long getGatewayDeploymentsBackupAgeLimit() {
- PeriodFormatter f = new PeriodFormatterBuilder().appendDays().toFormatter();
- String s = get( DEPLOYMENTS_BACKUP_AGE_LIMIT, "-1" );
- long d;
- try {
- Period p = Period.parse( s, f );
- d = p.toStandardDuration().getMillis();
- if( d < 0 ) {
- d = -1;
- }
- } catch( Exception e ) {
- d = -1;
- }
- return d;
- }
-
- @Override
- public String getSigningKeystoreName() {
- return get(SIGNING_KEYSTORE_NAME);
- }
-
- @Override
- public String getSigningKeyAlias() {
- return get(SIGNING_KEY_ALIAS);
- }
-
- @Override
- public List<String> getGlobalRulesServices() {
- String value = get( GLOBAL_RULES_SERVICES );
- if ( value != null && !value.isEmpty() && !"none".equalsIgnoreCase(value.trim()) ) {
- return Arrays.asList( value.trim().split("\\s*,\\s*") );
- }
- return DEFAULT_GLOBAL_RULES_SERVICES;
- }
-
- @Override
- public boolean isMetricsEnabled() {
- String metricsEnabled = get( METRICS_ENABLED, "false" );
- return "true".equals(metricsEnabled);
- }
-
- @Override
- public boolean isJmxMetricsReportingEnabled() {
- String enabled = get( JMX_METRICS_REPORTING_ENABLED, "false" );
- return "true".equals(enabled);
- }
-
- @Override
- public boolean isGraphiteMetricsReportingEnabled() {
- String enabled = get( GRAPHITE_METRICS_REPORTING_ENABLED, "false" );
- return "true".equals(enabled);
- }
-
- @Override
- public String getGraphiteHost() {
- String host = get( GRAPHITE_METRICS_REPORTING_HOST, "localhost" );
- return host;
- }
-
- @Override
- public int getGraphitePort() {
- int i = getInt( GRAPHITE_METRICS_REPORTING_PORT, 32772 );
- return i;
- }
-
- @Override
- public int getGraphiteReportingFrequency() {
- int i = getInt( GRAPHITE_METRICS_REPORTING_FREQUENCY, 1 );
- return i;
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#isWebsocketEnabled()
- */
- @Override
- public boolean isWebsocketEnabled() {
- final String result = get( WEBSOCKET_FEATURE_ENABLED, Boolean.toString(DEFAULT_WEBSOCKET_FEATURE_ENABLED));
- return Boolean.parseBoolean(result);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#websocketMaxTextMessageSize()
- */
- @Override
- public int getWebsocketMaxTextMessageSize() {
- return getInt( WEBSOCKET_MAX_TEXT_MESSAGE_SIZE, DEFAULT_WEBSOCKET_MAX_TEXT_MESSAGE_SIZE);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#websocketMaxBinaryMessageSize()
- */
- @Override
- public int getWebsocketMaxBinaryMessageSize() {
- return getInt( WEBSOCKET_MAX_BINARY_MESSAGE_SIZE, DEFAULT_WEBSOCKET_MAX_BINARY_MESSAGE_SIZE);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#websocketMaxTextMessageBufferSize()
- */
- @Override
- public int getWebsocketMaxTextMessageBufferSize() {
- return getInt( WEBSOCKET_MAX_TEXT_MESSAGE_BUFFER_SIZE, DEFAULT_WEBSOCKET_MAX_TEXT_MESSAGE_BUFFER_SIZE);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#websocketMaxBinaryMessageBufferSize()
- */
- @Override
- public int getWebsocketMaxBinaryMessageBufferSize() {
- return getInt( WEBSOCKET_MAX_BINARY_MESSAGE_BUFFER_SIZE, DEFAULT_WEBSOCKET_MAX_BINARY_MESSAGE_BUFFER_SIZE);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#websocketInputBufferSize()
- */
- @Override
- public int getWebsocketInputBufferSize() {
- return getInt( WEBSOCKET_INPUT_BUFFER_SIZE, DEFAULT_WEBSOCKET_INPUT_BUFFER_SIZE);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#websocketAsyncWriteTimeout()
- */
- @Override
- public int getWebsocketAsyncWriteTimeout() {
- return getInt( WEBSOCKET_ASYNC_WRITE_TIMEOUT, DEFAULT_WEBSOCKET_ASYNC_WRITE_TIMEOUT);
- }
-
- /* (non-Javadoc)
- * @see org.apache.hadoop.gateway.config.GatewayConfig#websocketIdleTimeout()
- */
- @Override
- public int getWebsocketIdleTimeout() {
- return getInt( WEBSOCKET_IDLE_TIMEOUT, DEFAULT_WEBSOCKET_IDLE_TIMEOUT);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.apache.hadoop.gateway.config.GatewayConfig#getMimeTypesToCompress()
- */
- @Override
- public List<String> getMimeTypesToCompress() {
- List<String> mimeTypes = null;
- String value = get(MIME_TYPES_TO_COMPRESS, DEFAULT_MIME_TYPES_TO_COMPRESS);
- if (value != null && !value.isEmpty()) {
- mimeTypes = Arrays.asList(value.trim().split("\\s*,\\s*"));
- }
- return mimeTypes;
- }
-
- /**
- * Map of Topology names and their ports.
- *
- * @return
- */
- @Override
- public Map<String, Integer> getGatewayPortMappings() {
-
- final Map<String, Integer> result = new ConcurrentHashMap<String, Integer>();
- final Map<String, String> properties = getValByRegex(GATEWAY_PORT_MAPPING_REGEX);
-
- // Convert port no. from string to int
- for(final Map.Entry<String, String> e : properties.entrySet()) {
- // ignore the GATEWAY_PORT_MAPPING_ENABLED property
- if(!e.getKey().equalsIgnoreCase(GATEWAY_PORT_MAPPING_ENABLED)) {
- // extract the topology name and use it as a key
- result.put(StringUtils.substringAfter(e.getKey(), GATEWAY_PORT_MAPPING_PREFIX), Integer.parseInt(e.getValue()) );
- }
-
- }
-
- return Collections.unmodifiableMap(result);
- }
-
- /**
- * Is the Port Mapping feature on ?
- *
- * @return
- */
- @Override
- public boolean isGatewayPortMappingEnabled() {
- final String result = get( GATEWAY_PORT_MAPPING_ENABLED, Boolean.toString(DEFAULT_GATEWAY_PORT_MAPPING_ENABLED));
- return Boolean.parseBoolean(result);
- }
-
- private static long parseNetworkTimeout(String s ) {
- PeriodFormatter f = new PeriodFormatterBuilder()
- .appendMinutes().appendSuffix("m"," min")
- .appendSeconds().appendSuffix("s"," sec")
- .appendMillis().toFormatter();
- Period p = Period.parse( s, f );
- return p.toStandardDuration().getMillis();
- }
-
- @Override
- public boolean isCookieScopingToPathEnabled() {
- final boolean result = Boolean.parseBoolean(get(COOKIE_SCOPING_ENABLED,
- Boolean.toString(DEFAULT_COOKIE_SCOPING_FEATURE_ENABLED)));
- return result;
- }
-
- @Override
- public String getHeaderNameForRemoteAddress() {
- String value = getVar(REMOTE_IP_HEADER_NAME, "X-Forwarded-For");
- return value;
- }
-
- @Override
- public String getAlgorithm() {
- return getVar(CRYPTO_ALGORITHM, null);
- }
-
- @Override
- public String getPBEAlgorithm() {
- return getVar(CRYPTO_PBE_ALGORITHM, null);
- }
-
- @Override
- public String getTransformation() {
- return getVar(CRYPTO_TRANSFORMATION, null);
- }
-
- @Override
- public String getSaltSize() {
- return getVar(CRYPTO_SALTSIZE, null);
- }
-
- @Override
- public String getIterationCount() {
- return getVar(CRYPTO_ITERATION_COUNT, null);
- }
-
- @Override
- public String getKeyLength() {
- return getVar(CRYPTO_KEY_LENGTH, null);
- }
-
- @Override
- public boolean isGatewayServerHeaderEnabled() {
- return Boolean.parseBoolean(getVar(SERVER_HEADER_ENABLED, "true"));
- }
-}