/*
 * 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 com.zxy.commons.jetty.server;

import com.zxy.commons.jetty.exception.ServerErrorException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import javax.servlet.http.HttpServlet;
import java.net.InetSocketAddress;
import java.util.Map;

/**
 * Abstract jetty server
 *
 * <p>
 * <a href="AbstractWebServer.java"><i>View Source</i></a>
 *
 * @author zhaoxunyong@qq.com
 * @version 1.0
 * @since 1.0
 */
public abstract class AbstractWebServer {

    protected abstract String getContextPath();

    protected abstract Map<String, HttpServlet> getServlets();

    protected Server startServer(String host, int port) {
        InetSocketAddress address = null;
        if (host == null || "".equals(host)) {
            address = new InetSocketAddress(port);
        } else {
            address = new InetSocketAddress(host, port);
        }
        Server server = new Server(address);
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath(getContextPath());
        server.setHandler(context);

        Map<String, HttpServlet> servlets = getServlets();
        if(servlets != null && !servlets.isEmpty()) {
            for(Map.Entry<String, HttpServlet> entry : servlets.entrySet()) {
                context.addServlet(new ServletHolder(entry.getValue()),entry.getKey());
            }
        }
        try {
            server.start();
        } catch (Exception e) {
            throw new ServerErrorException(e);
        }
        //  server.join();
        return server;
    }
}

@@ -933,24 +933,71 @@
 ver.Server;%0A
+import org.eclipse.jetty.servlet.FilterHolder;%0A
 import org.e
@@ -1085,24 +1085,90 @@
 letHolder;%0A%0A
+import javax.servlet.DispatcherType;%0Aimport javax.servlet.Filter;%0A
 import javax
@@ -1246,19 +1246,151 @@
 va.util.
-Map
+EnumSet;%0Aimport java.util.EventListener;%0Aimport java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.Map;%0Aimport java.util.Set
 ;%0A%0A/**%0A 
@@ -1719,16 +1719,534 @@
 otected 
+Map%3CString, Filter%3E getFilters() %7B%0A        return new HashMap%3C%3E();%0A    %7D%0A    %0A    protected Set%3CEventListener%3E getEventListeners() %7B%0A        return new HashSet%3C%3E();%0A    %7D%0A%0A    /**%0A     * startServer%0A     * @param port port%0A     * @return Server%0A    */%0A    public Server startServer(int port) %7B%0A        return startServer(null, port);%0A    %7D%0A%0A    /**%0A     * startServer%0A     * @param host host%0A     * @param port port%0A     * @return Server%0A    */%0A    @SuppressWarnings(%22PMD.AvoidInstantiatingObjectsInLoops%22)%0A    public 
 Server s
@@ -2732,24 +2732,209 @@
 (context);%0A%0A
+//        handler.addServletWithMapping(HelloServlet.class, %22/*%22);%0A//        handler.addFilterWithMapping(HelloPrintingFilter.class, %22/*%22, EnumSet.of(DispatcherType.REQUEST));%0A        %0A
         Map%3C
@@ -3183,16 +3183,17 @@
 alue()),
+ 
 entry.ge
@@ -3217,32 +3217,658 @@
     %7D%0A        %7D%0A
+        %0A        Map%3CString, Filter%3E filters = getFilters();%0A        if(filters != null && !filters.isEmpty()) %7B%0A            for(Map.Entry%3CString, Filter%3E entry : filters.entrySet()) %7B%0A                context.addFilter(new FilterHolder(entry.getValue()), entry.getKey(), EnumSet.of(DispatcherType.REQUEST));%0A            %7D%0A        %7D%0A        %0A        Set%3CEventListener%3E eventListeners = getEventListeners();%0A        if(eventListeners != null && !eventListeners.isEmpty()) %7B%0A            for(EventListener eventListener : eventListeners) %7B%0A                context.addEventListener(eventListener);%0A            %7D%0A        %7D%0A        %0A
         try %7B%0A  
@@ -4039,9 +4039,8 @@
 %0A    %7D%0A%7D
-%0A

Add jetty filter.