開発メモ

開発用のメモです。

基底アクションクラス

import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public abstract class AbstractAction extends ActionSupport {

    /**
    * seasar 2 managed.
    */
    @Resource
    protected HttpServletRequest request;

    /**
    * seasar 2 managed.
    */
    @Resource
    protected HttpServletResponse response;

    /**
    * seasar 2 managed.
    */
    @Resource
    protected HttpSession session;

    /**
    * seasar 2 managed.
    */
    @Resource
    protected ServletContext application;

    /**
    * {@link Logger}.
    *
    * @return logger.
    */
    public Logger logger() {
        return Logger.getLogger(getClass());
    }

    /**
    * {@link HttpServletRequest#setAttribute(String, Object)}
    *
    * @param request
    *            {@link HttpServletRequest}
    * @param key
    *            {@link String}
    * @param value
    *            {@link Object}
    */
    protected void setAttribute(//
            HttpServletRequest request, String key, Object value) {

        request = Objects.requireNonNull(request);
        key = Objects.requireNonNull(key);

        if (Objects.isNull(value)) {
            request.removeAttribute(key);
        } else {
            request.setAttribute(key, value);
        }
    }

    /**
    * {@link HttpSession#setAttribute(String, Object)}
    *
    * @param session
    *            {@link HttpSession}
    * @param key
    *            {@link String}
    * @param value
    *            {@link Object}
    */
    protected void setAttribute(//
            HttpSession session, String key, Object value) {

        session = Objects.requireNonNull(session);
        key = Objects.requireNonNull(key);

        if (Objects.isNull(value)) {
            session.removeAttribute(key);
        } else {
            session.setAttribute(key, value);
        }
    }

    /**
    * {@link HttpServletRequest#setAttribute(String, Object)}
    *
    * @param key
    *            {@link String}
    * @param value
    *            {@link Object}
    */
    protected void request(String key, Object value) {
        setAttribute(this.request, key, value);
    }

    /**
    * {@link HttpSession#setAttribute(String, Object)}
    *
    * @param key
    *            {@link String}
    * @param value
    *            {@link Object}
    */
    protected void session(String key, Object value) {
        setAttribute(this.session, key, value);
    }

    public String cookie(String key) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (Objects.equals(cookie.getName(), key)) {
                return cookie.getValue();
            }
        }
        return "";
    }

    /**
    * {@link HttpServletResponse#addCookie(Cookie)}.
    *
    * @param key
    *            {@link Cookie#getName()}
    * @param value
    *            {@link Cookie#getValue()}
    * @return value toString
    */
    public String cookie(String key, Object value) {
        key = Objects.requireNonNull(key);
        Cookie cookie = new Cookie(key, String.valueOf(value));
        response.addCookie(cookie);
        return String.valueOf(value);
    }

    /**
    * {@link HttpServletResponse#addCookie(Cookie)}.
    *
    * @param key
    *            {@link Cookie#getName()}
    * @param value
    *            {@link Cookie#getValue()}
    * @param maxAge
    *            {@link Cookie#getMaxAge()}
    * @return value toString
    */
    public String cookie(String key, Object value, int maxAge) {
        key = Objects.requireNonNull(key);
        Cookie cookie = new Cookie(key, String.valueOf(value));
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
        return String.valueOf(value);
    }

}
Twitter: @asahina_alice