001package org.javasimon.console.action;
002
003import org.javasimon.Simon;
004import org.javasimon.console.ActionContext;
005import org.javasimon.console.ActionException;
006import org.javasimon.console.json.ArrayJS;
007import org.javasimon.console.json.ObjectJS;
008
009import java.io.IOException;
010
011import javax.servlet.ServletException;
012
013/**
014 * Export one Simons as a JSON object for display in detail view.
015 * All attributes of simons are exported.
016 * Path: http://.../data/detail.html?name=o.j...SimonName&timeFormat=MILLISECOND
017 *
018 * @author gquintana
019 */
020public class DetailJsonAction extends AbstractJsonAction {
021
022        public static final String PATH = "/data/detail.json";
023
024        /**
025         * Name of the simon from where to start.
026         * {@code null} means root.
027         */
028        private String name;
029
030        public DetailJsonAction(ActionContext context) {
031                super(context);
032        }
033
034        @Override
035        public void readParameters() {
036                super.readParameters();
037                name = getContext().getParameterAsString("name", null);
038        }
039
040        @Override
041        public void execute() throws ServletException, IOException, ActionException {
042                if (name == null) {
043                        throw new ActionException("Null name");
044                }
045                Simon simon = getContext().getManager().getSimon(name);
046                if (simon == null) {
047                        throw new ActionException("Simon \"" + name + "\" not found");
048                }
049                getContext().setContentType("application/json");
050                ObjectJS simonJS = createObjectJS(simon);
051                // Plugins
052                ArrayJS pluginsJS = new ArrayJS();
053                for (DetailPlugin plugin : getContext().getPluginManager().getPluginsByType(DetailPlugin.class)) {
054                        if (plugin.supports(simon)) {
055                                ObjectJS pluginJS = plugin.toJson(jsonStringifierFactory);
056                                final ObjectJS pluginDataJS = plugin.executeJson(getContext(), jsonStringifierFactory, simon);
057                                if (pluginDataJS != null) {
058                                        pluginJS.setAttribute("data", pluginDataJS);
059                                }
060                                pluginsJS.addElement(pluginJS);
061                        }
062                }
063                simonJS.setAttribute("plugins", pluginsJS);
064                simonJS.write(getContext().getWriter());
065        }
066}