/**
 *  Copyright 2014-2016 CyberVision, Inc.
 *
 *  Licensed 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.kaaproject.kaa.sandbox.service.initialization;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.rewrite.handler.RewriteHandler;
import org.eclipse.jetty.rewrite.handler.RewritePatternRule;
import org.eclipse.jetty.webapp.WebAppContext;
import org.kaaproject.kaa.server.common.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class KaaSandboxInitializationService implements InitializationService {
	
	private static final Logger LOG = LoggerFactory.getLogger(KaaSandboxInitializationService.class);
	
    private int webPort;

    private Server server;
    private WebAppContext sandboxWebAppContext;
    private WebAppContext avroUiSandboxWebAppContext;
    
    public void setWebPort(int webPort) {
    	this.webPort = webPort;
    }
    
	@Override
	public void start() {
        LOG.info("Starting Kaa Sandbox Web Server...");
        String webappsDir = Environment.getServerHomeDir() + "/webapps";
        LOG.info("Webapps dir [{}]", webappsDir);
        
        server = new Server(webPort);
        HandlerCollection handlers = new HandlerCollection();
        
        sandboxWebAppContext = new WebAppContext();
        sandboxWebAppContext.setContextPath("/sandbox");
        sandboxWebAppContext.setDescriptor(webappsDir + "/sandbox/WEB-INF/web.xml");
        sandboxWebAppContext.setResourceBase(webappsDir + "/sandbox");
        
        avroUiSandboxWebAppContext = new WebAppContext();
        avroUiSandboxWebAppContext.setContextPath("/avroUiSandbox");
        avroUiSandboxWebAppContext.setDescriptor(webappsDir + "/avroUiSandbox/WEB-INF/web.xml");        
        avroUiSandboxWebAppContext.setResourceBase(webappsDir + "/avroUiSandbox");
        
        RewriteHandler rewriteRoot2Sandbox = new RewriteHandler();
        rewriteRoot2Sandbox.setHandler(sandboxWebAppContext);
        
        RewritePatternRule rootRule = new RewritePatternRule();
        rootRule.setPattern("/");
        rootRule.setReplacement("/sandbox");
        rootRule.setTerminating(true);
        rewriteRoot2Sandbox.addRule(rootRule);
        
        handlers.addHandler(sandboxWebAppContext);
        handlers.addHandler(avroUiSandboxWebAppContext);
        handlers.addHandler(rewriteRoot2Sandbox);
        
        server.setHandler(handlers);
        
        try {
            server.start();
            LOG.info("Kaa Sandbox Web Server started.");
        } catch (Exception e) {
            LOG.error("Error starting Kaa Sandbox Web Server!", e);
        }
        try {
			server.join();
		} catch (InterruptedException e) {
			LOG.error("Sandbox Web Server Interrupted!", e);
		}
	}

	@Override
	public void stop() {
        try {
            LOG.info("Stopping Kaa Sandbox Web Server...");
            server.stop();
            sandboxWebAppContext.destroy();
            avroUiSandboxWebAppContext.destroy();
            LOG.info("Kaa Sandbox Web Server stopped.");
        } catch (Exception e) {
            LOG.error("Error stopping Kaa Sandbox Web Server!", e);
        }
	}
	
}

@@ -708,24 +708,65 @@
 ver.Server;%0A
+import org.eclipse.jetty.server.Handler;%0A
 import org.e
@@ -912,21 +912,22 @@
 ndler.Re
-write
+direct
 PatternR
@@ -1484,24 +1484,596 @@
 ntext;%0A    %0A
+    private Handler getRedirectRoot2SandboxHandler() %7B%0A        RewriteHandler rewrite = new RewriteHandler();%0A        rewrite.setRewriteRequestURI(true);%0A        rewrite.setRewritePathInfo(true);%0A        %0A        String%5B%5D redirectArray = %7B%22%22, %22/%22%7D;%0A        for (String redirect : redirectArray) %7B%0A            RedirectPatternRule rule = new RedirectPatternRule();%0A            rule.setTerminating(true);%0A            rule.setPattern(redirect);%0A            rule.setLocation(%22/sandbox%22);%0A            rewrite.addRule(rule);%0A        %7D%0A        %0A        return rewrite;%0A    %7D%0A    %0A
     public v
@@ -3075,384 +3075,8 @@
    %0A
-        RewriteHandler rewriteRoot2Sandbox = new RewriteHandler();%0A        rewriteRoot2Sandbox.setHandler(sandboxWebAppContext);%0A        %0A        RewritePatternRule rootRule = new RewritePatternRule();%0A        rootRule.setPattern(%22/%22);%0A        rootRule.setReplacement(%22/sandbox%22);%0A        rootRule.setTerminating(true);%0A        rewriteRoot2Sandbox.addRule(rootRule);%0A        %0A
     
@@ -3207,23 +3207,27 @@
 Handler(
-rewrite
+getRedirect
 Root2San
@@ -3230,16 +3230,25 @@
 2Sandbox
+Handler()
 );%0A     

Move http://127.0.0.1:9080/sandbox to http://127.0.0.1:9080 Fix forwarding of "/" to "/sandbox";