001package org.javasimon.console;
002
003import java.io.IOException;
004import java.io.UnsupportedEncodingException;
005import java.net.URLDecoder;
006import javax.servlet.ServletException;
007
008import org.javasimon.Simon;
009
010/**
011 * Base class for actions (controller+model)
012 *
013 * @author gquintana
014 */
015public abstract class Action {
016
017        /**
018         * Action context (request, response & more)
019         */
020        private final ActionContext context;
021
022        public Action(ActionContext context) {
023                this.context = context;
024        }
025
026        public ActionContext getContext() {
027                return context;
028        }
029
030        /**
031         * Parse HTTP Request parameters and store them locally
032         */
033        public void readParameters() {
034        }
035
036        /**
037         * Execute action
038         */
039        public abstract void execute() throws ServletException, IOException, ActionException;
040
041        // mainly for IE8
042        protected void dontCache() {
043                getContext().getResponse().setHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
044                getContext().getResponse().setHeader("Pragma", "no-cache");
045        }
046
047        protected Simon findSimonByName(String name) {
048                Simon simon = context.getManager().getSimon(name);
049                if (simon == null) {
050                        try {
051                                final String nameDecoded = URLDecoder.decode(name, context.getCharacterEncoding());
052                                if (!name.equals(nameDecoded)) {
053                                        simon = context.getManager().getSimon(nameDecoded);
054                                }
055                        } catch (UnsupportedEncodingException e) {
056                                // pass
057                        }
058                }
059                return simon;
060        }
061}