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.index;
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.algorithm.indexer.Indexer;
035    import org.jdmp.core.dataset.DataSet;
036    import org.jdmp.jetty.html.Page;
037    import org.jdmp.jetty.html.elements.DataSetDiv;
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 JettyIndexServlet extends HttpServlet {
045            private static final long serialVersionUID = -529359384170033358L;
046    
047            private Indexer index = null;
048    
049            public JettyIndexServlet(Indexer index) {
050                    this.index = index;
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    
063                    response.setContentType("text/html");
064                    response.setStatus(HttpServletResponse.SC_OK);
065    
066                    String query = request.getParameter("q");
067    
068                    PrintWriter out = response.getWriter();
069    
070                    String path = request.getPathInfo().substring(1);
071                    String action = null;
072    
073                    Page page = new Page("JDMP Search");
074                    page.add(new H1Tag("JDMP Search"));
075                    page.add(index.getSize() + " items in index");
076                    page.add(new BRTag());
077                    FormTag form = new FormTag("/");
078                    form.setParameter("method", "get");
079                    form.add(new InputTextTag("q", query));
080                    form.add(new InputSubmitTag("submit", "submit"));
081                    page.add(form);
082                    page.add(new BRTag());
083    
084                    if (query != null) {
085                            DataSet result = null;
086                            try {
087                                    result = index.search(query);
088                            } catch (Exception e) {
089                                    e.printStackTrace();
090                            }
091                            page.add(new DataSetDiv(result));
092                    }
093    
094                    out.append(page.toString());
095    
096                    out.close();
097            }
098    
099    }