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

org.wisdom.api.interception.RequestContext Maven / Gradle / Ivy

There is a newer version: 0.10.0
Show newest version
/*
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * 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.
 * #L%
 */
package org.wisdom.api.interception;

import org.wisdom.api.http.Context;
import org.wisdom.api.http.Request;
import org.wisdom.api.http.Result;
import org.wisdom.api.http.Results;
import org.wisdom.api.router.Route;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.regex.Pattern;

/**
 * The request context let access interceptor to the current HTTP context, filters and interceptors.
 * This class is responsible of the request resolution, and so hold the filter chain.
 */
public class RequestContext {

    /**
     * The invoked route.
     */
    private final Route route;

    /**
     * The filter chain.
     */
    private final List chain;

    /**
     * The map storing the configuration for interceptors.
     */
    private final Map, Object> interceptors;

    /**
     * The action parameters.
     */
    private final Object[] parameters;

    private ListIterator iterator;

    /**
     * Creates a new Interception Context. Instances should only be created by the router.
     *
     * @param route        the intercepted route
     * @param chain        the ordered interception chain containing filters and interceptors.
     * @param interceptors the set of interceptors and their configuration
     * @param parameters   the parameters
     */
    public RequestContext(Route route, List chain, Map, Object> interceptors,
                          Object[] parameters) {
        this.route = route;
        this.interceptors = interceptors;

        this.chain = new LinkedList<>(chain);
        this.parameters = Arrays.copyOf(parameters, parameters.length);

        // Add the action invocation
        final ActionInvoker invoker = new ActionInvoker();
        this.chain.add(invoker);
    }

    /**
     * Retrieves the configuration annotation for the given interceptor.
     *
     * @param interceptor the interceptor
     * @return the configuration, {@code null} if not found
     */
     A getConfigurationForInterceptor(Interceptor interceptor) {
        return (A) interceptors.get(interceptor);
    }

    /**
     * Calls the next interceptors.
     *
     * @return the result from the next interceptor
     * @throws java.lang.Exception if the invocation fails.
     */
    public Result proceed() throws Exception {
        if (iterator == null) {
            iterator = chain.listIterator();
        }
        if (!iterator.hasNext()) {
            throw new IllegalStateException("Reached the end of the chain without result.");
        }
        Filter filter = iterator.next();
        return filter.call(route, this);
    }

    /**
     * @return the HTTP context.
     */
    public Context context() {
        return Context.CONTEXT.get();
    }

    /**
     * @return the incoming request.
     */
    public Request request() {
        return context().request();
    }

    /**
     * @return the invoked route.
     */
    public Route route() {
        return route;
    }

    /**
     * Access to the data stored in the request's scope.
     * It lets filters and interceptors sharing data. All modifications will be seen by the other participants of the
     * chain, action methods and templates.
     *
     * @return the data
     */
    public Map data() {
        return request().data();
    }

    /**
     * The end (actually middle) of the chain. This interceptor is a fake calling the action method.
     * It does not call {@link RequestContext#proceed()}.
     */
    private class ActionInvoker implements Filter {

        /**
         * We are the end of the chain, so we call the action method.
         * If the route is unbound, there are no action method, a {@literal 404 - NOT FOUND} result is returned.
         *
         * @param route   the intercepted route
         * @param context the filter context
         * @return the result of the action method, {@literal 404 - NOT FOUND} for unbound routes.
         * @throws java.lang.reflect.InvocationTargetException if the action method throws an exception
         * @throws java.lang.IllegalAccessException            if the action method cannot be called
         */
        @Override
        public Result call(Route route, RequestContext context) throws InvocationTargetException, IllegalAccessException {
            if (RequestContext.this.route.isUnbound()) {
                return Results.notFound();
            } else {
                return (Result) RequestContext.this.route.getControllerMethod().invoke(
                        RequestContext.this.route.getControllerObject(), parameters);
            }
        }

        /**
         * @return {@literal null} as it's meaningless here.
         */
        @Override
        public Pattern uri() {
            // Not meaningful here.
            return null;
        }

        /**
         * @return {@literal -1} as it's meaningless here.
         */
        @Override
        public int priority() {
            // Anyway, we're the last.
            return -1;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy