001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * JDMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with JDMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.jdmp.jetty.coreobject;
025    
026    import java.io.IOException;
027    import java.io.PrintWriter;
028    
029    import javax.servlet.ServletException;
030    import javax.servlet.http.HttpServlet;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    import org.jdmp.core.module.Module;
035    import org.jdmp.core.script.Result;
036    import org.jdmp.jetty.html.HtmlUtils;
037    import org.jdmp.jetty.html.Page;
038    import org.jdmp.jetty.html.tags.BRTag;
039    import org.jdmp.jetty.html.tags.FormTag;
040    import org.jdmp.jetty.html.tags.H1Tag;
041    import org.jdmp.jetty.html.tags.InputSubmitTag;
042    import org.jdmp.jetty.html.tags.InputTextTag;
043    
044    public class JettyConsoleServlet extends HttpServlet {
045            private static final long serialVersionUID = 1786601018402743358L;
046    
047            private Module module = null;
048    
049            public JettyConsoleServlet(Module o) {
050                    this.module = o;
051            }
052    
053            @Override
054            protected void doGet(HttpServletRequest request,
055                            HttpServletResponse response) throws ServletException, IOException {
056                    doPost(request, response);
057            }
058    
059            @Override
060            protected void doPost(HttpServletRequest request,
061                            HttpServletResponse response) throws ServletException, IOException {
062                    response.setContentType("text/html");
063                    response.setStatus(HttpServletResponse.SC_OK);
064    
065                    String command = request.getParameter("command");
066    
067                    Result result = null;
068                    try {
069                            result = module.execute(command);
070                    } catch (Exception e) {
071                    }
072    
073                    PrintWriter out = response.getWriter();
074    
075                    String path = request.getPathInfo().substring(1);
076                    String action = null;
077    
078                    Page page = new Page("Welcome to JDMP");
079                    page.add(new H1Tag("Welcome to JDMP"));
080                    page.add("This is a console for ");
081                    page.add(module.getClass().getSimpleName());
082                    page.add(" [" + module.getLabel() + "]. ");
083                    page.add(new BRTag());
084                    page.add(new BRTag());
085                    page.add("Command to execute:");
086                    page.add(new BRTag());
087                    FormTag form = new FormTag("/console");
088                    form.add(new InputTextTag("command"));
089                    form.add(new InputSubmitTag("submit", "submit"));
090                    page.add(form);
091                    page.add(new BRTag());
092    
093                    if (result != null) {
094                            page.add(new BRTag());
095                            page.add(HtmlUtils.format(result.toString()));
096                    }
097    
098                    out.append(page.toString());
099    
100                    out.close();
101            }
102    }