All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.killbill.commons.skeleton.modules.BaseServerModule Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
/*
 * Copyright 2010-2014 Ning, Inc.
 *
 * Ning 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.killbill.commons.skeleton.modules;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.Filter;
import javax.servlet.http.HttpServlet;

import com.google.inject.servlet.ServletModule;

public class BaseServerModule extends ServletModule {

    private final Map, Map>>> filters;
    private final Map, Map>>> filtersRegex;
    private final Map> servlets;
    private final Map> servletsRegex;

    // JAX-RS resources
    final Map> jaxrsServlets;
    final Map> jaxrsServletsRegex;
    final String jaxrsUriPattern;
    final Collection jaxrsResources = new ArrayList();

    public BaseServerModule(final Map, Map>>> filters,
                            final Map, Map>>> filtersRegex,
                            final Map> servlets,
                            final Map> servletsRegex,
                            final Map> jaxrsServlets,
                            final Map> jaxrsServletsRegex,
                            final String jaxrsUriPattern,
                            final Collection jaxrsResources) {
        this.filters = filters;
        this.filtersRegex = filtersRegex;
        this.servlets = servlets;
        this.servletsRegex = servletsRegex;
        this.jaxrsServlets = jaxrsServlets;
        this.jaxrsServletsRegex = jaxrsServletsRegex;
        this.jaxrsUriPattern = jaxrsUriPattern;
        this.jaxrsResources.addAll(jaxrsResources);
    }

    @Override
    public void configureServlets() {
        super.configureServlets();

        configureFilters();
        configureFiltersRegex();
        configureRegularServlets();
        configureRegularServletsRegex();
        configureResources();
    }

    protected void configureFilters() {
        for (final String urlPattern : filters.keySet()) {
            for (final Map.Entry, Map> filter : filters.get(urlPattern)) {
                filter(urlPattern).through(filter.getKey(), filter.getValue());
            }
        }
    }

    protected void configureFiltersRegex() {
        for (final String urlPattern : filtersRegex.keySet()) {
            for (final Map.Entry, Map> filter : filtersRegex.get(urlPattern)) {
                filterRegex(urlPattern).through(filter.getKey(), filter.getValue());
            }
        }
    }

    protected void configureRegularServlets() {
        for (final String urlPattern : servlets.keySet()) {
            serve(urlPattern).with(servlets.get(urlPattern));
        }
    }

    protected void configureRegularServletsRegex() {
        for (final String urlPattern : servletsRegex.keySet()) {
            serveRegex(urlPattern).with(servletsRegex.get(urlPattern));
        }
    }

    protected void configureResources() {
        for (final String urlPattern : jaxrsServlets.keySet()) {
            serve(urlPattern).with(jaxrsServlets.get(urlPattern));
        }

        for (final String urlPattern : jaxrsServletsRegex.keySet()) {
            serveRegex(urlPattern).with(jaxrsServletsRegex.get(urlPattern));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy