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

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

/*
 * 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.HashMap;
import java.util.List;
import java.util.Map;

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

import com.google.common.collect.Maps;
import com.google.inject.Module;

public class BaseServerModuleBuilder {

    public enum JaxrsImplementation {
        NONE,
        JERSEY
    }

    // By default, proxy all requests to the Guice/Jax-RS servlet
    private String jaxrsUriPattern = "/.*";
    private final List jaxrsResources = new ArrayList();
    private final List modules = new ArrayList();
    private final Map, Map>>> filters = new HashMap, Map>>>();
    private final Map, Map>>> filtersRegex = new HashMap, Map>>>();
    private final Map> jaxrsServlets = new HashMap>();
    private final Map> jaxrsServletsRegex = new HashMap>();
    private final Map> servlets = new HashMap>();
    private final Map> servletsRegex = new HashMap>();

    // Specific filters called by jersey
    private final List jerseyFilters = new ArrayList();
    private final Map jerseyParams = new HashMap();

    private JaxrsImplementation jaxrsImplementation = JaxrsImplementation.JERSEY;

    public BaseServerModuleBuilder() {
    }

    public BaseServerModuleBuilder addFilter(final String urlPattern, final Class filterKey) {
        return addFilter(urlPattern, filterKey, new HashMap());
    }

    public BaseServerModuleBuilder addFilter(final String urlPattern, final Class filterKey, final Map initParams) {
        if (this.filters.get(urlPattern) == null) {
            this.filters.put(urlPattern, new ArrayList, Map>>());
        }

        this.filters.get(urlPattern).add(Maps., Map>immutableEntry(filterKey, initParams));
        return this;
    }

    public BaseServerModuleBuilder addFilterRegex(final String urlPattern, final Class filterKey) {
        return addFilterRegex(urlPattern, filterKey, new HashMap());
    }

    public BaseServerModuleBuilder addFilterRegex(final String urlPattern, final Class filterKey, final Map initParams) {
        if (this.filtersRegex.get(urlPattern) == null) {
            this.filtersRegex.put(urlPattern, new ArrayList, Map>>());
        }

        this.filtersRegex.get(urlPattern).add(Maps., Map>immutableEntry(filterKey, initParams));
        return this;
    }

    public BaseServerModuleBuilder addServlet(final String urlPattern, final Class filterKey) {
        this.servlets.put(urlPattern, filterKey);
        return this;
    }

    public BaseServerModuleBuilder addServletRegex(final String urlPattern, final Class filterKey) {
        this.servletsRegex.put(urlPattern, filterKey);
        return this;
    }

    public BaseServerModuleBuilder addJaxrsServlet(final String urlPattern, final Class filterKey) {
        this.jaxrsServlets.put(urlPattern, filterKey);
        return this;
    }

    public BaseServerModuleBuilder addJaxrsServletRegex(final String urlPattern, final Class filterKey) {
        this.jaxrsServletsRegex.put(urlPattern, filterKey);
        return this;
    }

    public BaseServerModuleBuilder addJerseyFilter(final String filterClassName) {
        this.jerseyFilters.add(filterClassName);
        return this;
    }

    public BaseServerModuleBuilder addJerseyParam(final String key, final String value) {
        this.jerseyParams.put(key, value);
        return this;
    }

    /**
     * Specify the Uri pattern to use for the Guice/Jersey servlet
     *
     * @param jaxrsUriPattern Any Java-style regular expression
     * @return the current module builder
     * @see BaseServerModuleBuilder#addJaxrsResource
     */
    public BaseServerModuleBuilder setJaxrsUriPattern(final String jaxrsUriPattern) {
        this.jaxrsUriPattern = jaxrsUriPattern;
        return this;
    }

    /**
     * Add a package to be scanned for the Guice/Jersey servlet
     *
     * @param resource package to scan
     * @return the current module builder
     */
    public BaseServerModuleBuilder addJaxrsResource(final String resource) {
        this.jaxrsResources.add(resource);
        return this;
    }

    public BaseServerModuleBuilder setJaxrsImplementation(final JaxrsImplementation jaxrsImplementation) {
        this.jaxrsImplementation = jaxrsImplementation;
        return this;
    }

    public BaseServerModule build() {
        switch (jaxrsImplementation) {
            case NONE:
                return new BaseServerModule(filters,
                                            filtersRegex,
                                            servlets,
                                            servletsRegex,
                                            jaxrsServlets,
                                            jaxrsServletsRegex,
                                            jaxrsUriPattern,
                                            jaxrsResources);
            case JERSEY:
                return new JerseyBaseServerModule(filters,
                                                  filtersRegex,
                                                  servlets,
                                                  servletsRegex,
                                                  jaxrsServlets,
                                                  jaxrsServletsRegex,
                                                  jaxrsUriPattern,
                                                  jaxrsResources,
                                                  jerseyFilters,
                                                  jerseyParams);
            default:
                throw new IllegalArgumentException();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy