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

ru.frostman.web.dispatch.ActionDefinition Maven / Gradle / Ivy

/******************************************************************************
 * WebJavin - Java Web Framework.                                             *
 *                                                                            *
 * Copyright (c) 2011 - Sergey "Frosman" Lukjanov, [email protected]             *
 *                                                                            *
 * 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 ru.frostman.web.dispatch;

import ru.frostman.web.dispatch.url.UrlPattern;
import ru.frostman.web.thr.ActionInitializationException;
import ru.frostman.web.util.HttpMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.Set;

/**
 * @author slukjanov aka Frostman
 */
public class ActionDefinition {
    private final List urlPatterns;
    private final Set methods;
    private final String invokerClassName;
    private Class invokerClass;
    private Constructor invokerClassConstructor;

    public ActionDefinition(List urlPatterns, Set methods, String invokerClassName) {
        this.urlPatterns = urlPatterns;
        this.methods = methods;
        this.invokerClassName = invokerClassName;
    }

    @SuppressWarnings({"unchecked"})
    public void init(ClassLoader classLoader) {
        try {
            invokerClass = (Class) classLoader.loadClass(invokerClassName);
            invokerClassConstructor = invokerClass.getConstructor(HttpServletRequest.class, HttpServletResponse.class);
        } catch (Exception e) {
            throw new ActionInitializationException("Can't initialize ActionDefinition for ActionInvoker: "
                    + invokerClass.getName());
        }
    }

    public boolean matches(String url, HttpMethod method) {
        if (!methods.contains(method)) {
            return false;
        }

        for (UrlPattern urlPattern : urlPatterns) {
            if (urlPattern.matches(url)) {
                return true;
            }
        }

        return false;
    }

    public ActionInvoker initInvoker(HttpServletRequest request, HttpServletResponse response) {
        try {
            return invokerClassConstructor.newInstance(request, response);
        } catch (Exception e) {
            throw new ActionInitializationException("Can't initialize ActionInvoker: " + invokerClass.getName(), e);
        }
    }

    public List getUrlPatterns() {
        return urlPatterns;
    }

    public Set getMethods() {
        return methods;
    }

    public String getInvokerClassName() {
        return invokerClassName;
    }

    public Class getInvokerClass() {
        return invokerClass;
    }

    public Constructor getInvokerClassConstructor() {
        return invokerClassConstructor;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy