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