package com.bardframework.bard.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * This class is used in GenericHandler, so that you could store information while run
 * the middleware and handlers.
 */
public class Context {
    /**
     * The raw HttpServletRequest
     */
    protected HttpServletRequest request;

    /**
     * The raw HttpServletResponse
     */
    protected HttpServletResponse response;

    /**
     * The result got by handler
     */
    protected Object result;

    /**
     * If there is any exception while running the chain, it is stored here.
     */
    protected Throwable exception;
    /**
     * Only used by {@link Injector}, store the current injectorVariable that will be injected.
     */
    protected Object injectorVariable;
    /**
     * Only used by {@link Injector}, the type of inject injectorVariable.
     */
    protected Class<?> injectorVariableType = Object.class;
    /**
     * The custom map so that you could put anything into the context.
     */
    protected Map<String, Object> custom = new HashMap<>();
    /**
     * Is the exception handled? If not, the framework will through it.
     */
    private boolean exceptionHandled = false;

    public Context(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    public <T> void putCustom(String key, T value) {
        custom.put(key, value);
    }

    @SuppressWarnings("unchecked")
    public <T> T getCustom(String key) {
        return (T) custom.get(key);
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public Throwable getException() {
        return exception;
    }

    public void setException(Throwable exception) {
        this.exception = exception;
    }

    public boolean isExceptionHandled() {
        return exceptionHandled;
    }

    public void setExceptionHandled(boolean exceptionHandled) {
        this.exceptionHandled = exceptionHandled;
    }

    public Object getInjectorVariable() {
        return injectorVariable;
    }

    public void setInjectorVariable(Object injectorVariable) {
        this.injectorVariable = injectorVariable;
    }

    public Class<?> getInjectorVariableType() {
        return injectorVariableType;
    }
}

@@ -1463,95 +1463,429 @@
     
-public %3CT%3E void putCustom(String key, T value) %7B%0A        custom.put(key, value);%0A    %7D%0A
+/**%0A     * Put a custom value into context.%0A     *%0A     * @param key   The key to get value.%0A     * @param value The value.%0A     * @param %3CT%3E   The type of value.%0A     */%0A    public %3CT%3E void putCustom(String key, T value) %7B%0A        custom.put(key, value);%0A    %7D%0A%0A    /**%0A     * Get the custom value in context.%0A     * @param key The key to get value.%0A     * @param %3CT%3E The type of value.%0A     * @return The value.%0A     */
 %0A   

add doc