/*
 * 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.activemq.artemis.component;

import java.net.URI;

import org.apache.activemq.artemis.ActiveMQWebLogger;
import org.apache.activemq.artemis.components.ExternalComponent;
import org.apache.activemq.artemis.dto.AppDTO;
import org.apache.activemq.artemis.dto.ComponentDTO;
import org.apache.activemq.artemis.dto.WebServerDTO;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.webapp.WebAppContext;

public class WebServerComponent implements ExternalComponent {

   private Server server;
   private HandlerList handlers;
   private WebServerDTO webServerConfig;
   private URI uri;
   private String jolokiaUrl;

   @Override
   public void configure(ComponentDTO config, String artemisInstance, String artemisHome) throws Exception {
      webServerConfig = (WebServerDTO) config;
      String path = webServerConfig.path.startsWith("/") ? webServerConfig.path : "/" + webServerConfig.path;
      uri = new URI(webServerConfig.bind);
      server = new Server();
      ServerConnector connector = new ServerConnector(server);
      connector.setPort(uri.getPort());
      connector.setHost(uri.getHost());

      server.setConnectors(new Connector[]{connector});

      handlers = new HandlerList();

      if (webServerConfig.apps != null) {
         for (AppDTO app : webServerConfig.apps) {
            deployWar(app.url, app.war, artemisHome, path);
            if (app.war.startsWith("jolokia")) {
               jolokiaUrl = webServerConfig.bind + "/" + app.url;
            }
         }
      }

      WebAppContext handler = new WebAppContext();
      handler.setContextPath("/");
      handler.setResourceBase(artemisHome + path);
      handler.setLogUrlOnStart(true);

      ResourceHandler resourceHandler = new ResourceHandler();
      resourceHandler.setResourceBase(artemisHome + path);
      resourceHandler.setDirectoriesListed(true);
      resourceHandler.setWelcomeFiles(new String[]{"index.html"});

      DefaultHandler defaultHandler = new DefaultHandler();
      defaultHandler.setServeIcon(false);

      handlers.addHandler(resourceHandler);
      handlers.addHandler(defaultHandler);
      server.setHandler(handlers);
   }

   @Override
   public void start() throws Exception {
      server.start();
      ActiveMQWebLogger.LOGGER.webserverStarted(webServerConfig.bind);
      if (jolokiaUrl != null) {
         ActiveMQWebLogger.LOGGER.jolokiaAvailable(jolokiaUrl);
      }
   }

   @Override
   public void stop() throws Exception {
      server.stop();
   }

   @Override
   public boolean isStarted() {
      return server != null && server.isStarted();
   }

   private void deployWar(String url, String warURL, String activeMQHome, String path) {
      WebAppContext webapp = new WebAppContext();
      if (url.startsWith("/")) {
         webapp.setContextPath(url);
      }
      else {
         webapp.setContextPath("/" + url);
      }
      webapp.setWar(activeMQHome + path + "/" + warURL);
      handlers.addHandler(webapp);
   }
}

@@ -845,30 +845,8 @@
 t;%0A%0A
-import java.net.URI;%0A%0A
 impo
@@ -891,24 +891,24 @@
 QWebLogger;%0A
+
 import org.a
@@ -1459,16 +1459,121 @@
 ntext;%0A%0A
+import java.io.IOException;%0Aimport java.net.URI;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A%0A
 public c
@@ -1952,118 +1952,8 @@
 ig;%0A
-      String path = webServerConfig.path.startsWith(%22/%22) ? webServerConfig.path : %22/%22 + webServerConfig.path;%0A
     
@@ -2254,24 +2254,161 @@
 lerList();%0A%0A
+      Path warDir = Paths.get(artemisHome != null ? artemisHome : %22.%22)%0A              .resolve( webServerConfig.path ).toAbsolutePath();%0A%0A
       if (we
@@ -2532,25 +2532,14 @@
 ar, 
+w
 ar
-temisHome, path
+Dir
 );%0A 
@@ -2802,34 +2802,33 @@
 rceBase(
+w
 ar
-temisHome + path
+Dir.toString()
 );%0A     
@@ -2966,26 +2966,25 @@
 ase(
+w
 ar
-temisHome + path
+Dir.toString()
 );%0A 
@@ -3824,46 +3824,51 @@
  war
-URL, String activeMQHome, String path)
+File, Path warDirectory) throws IOException
  %7B%0A 
@@ -4054,24 +4054,25 @@
 l);%0A      %7D%0A
+%0A
       webapp
@@ -4083,42 +4083,48 @@
 War(
-activeMQHome + path + %22/%22 + warURL
+warDirectory.resolve(warFile).toString()
 );%0A 

Clean up path handling in in the WebServerComponent a bit.