001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008-2009, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
003    //
004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
006    // either version 3 of the License, or (at your option) any later version.
007    //
008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
010    // PURPOSE. See the GNU Lesser General Public License for more details.
011    //
012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
013    // Java Packages. If not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki;
016    
017    import java.io.IOException;
018    
019    import javax.servlet.ServletException;
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import nextapp.echo2.app.ApplicationInstance;
024    import nextapp.echo2.webcontainer.WebContainerServlet;
025    import ch.uzh.ifi.attempto.acewiki.core.ontology.Ontology;
026    import ch.uzh.ifi.attempto.ape.APELocal;
027    
028    /**
029     * This servlet class is used by the web server to start AceWiki.
030     * In order to run the AceWiki servlet, a web application archive (WAR) file has to be created.
031     * See the <a href="{@docRoot}/README.txt">README file of the Attempto Java Packages</a> and the
032     * <a href="{@docRoot}/ch/uzh/ifi/attempto/acewiki/web.xml">web.xml example file</a>.
033     *<p>
034     * SWI Prolog needs to be installed on the server and you need to have a compiled version of the Attempto APE
035     * distribution. See the documentation of {@link APELocal} for more information.
036     * 
037     * @author Tobias Kuhn
038     */
039    public class AceWikiServlet extends WebContainerServlet {
040            
041            private static final long serialVersionUID = -7342857942059126499L;
042            
043            /**
044             * Creates a new AceWiki servlet object.
045             */
046            public AceWikiServlet() {
047            }
048            
049            public ApplicationInstance newApplicationInstance() {
050                    String ontologyName = getInitParameter("ontology");
051                    String baseURI = getInitParameter("baseuri");
052                    String title = getInitParameter("title");
053                    String description = getInitParameter("description");
054                    String loginString = getInitParameter("login");
055                    int login;
056                    
057                    if (loginString == null) loginString = "no";
058                    if (loginString.equals("yes") || loginString.equals("pw")) {
059                            login = AceWikiApp.PW_LOGIN;
060                    } else if (loginString.equals("nonpw")) {
061                            login = AceWikiApp.NONPW_LOGIN;
062                    } else {
063                            login = AceWikiApp.NO_LOGIN;
064                    }
065                    
066                    if (!APELocal.isInitialized()) {
067                            String prologCommand = getServletContext().getInitParameter("prologcommand");
068                            String apeCommand = getServletContext().getInitParameter("apecommand");
069                            if (prologCommand == null) prologCommand = "swipl";
070                            if (apeCommand == null) apeCommand = "ape.exe";
071                            APELocal.init(prologCommand, apeCommand);
072                    }
073                    
074                    Ontology ontology = Ontology.loadOntology(ontologyName, baseURI);
075                    
076                    Logger.log("syst", "syst", 0, "appl", "new application instance: " + ontologyName);
077                    return new AceWikiApp(ontology, title, description, login);
078            }
079            
080            protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
081                    String showpageParam = request.getParameter("showpage");
082                    if ("".equals(showpageParam)) showpageParam = null;
083                    String pageParam = request.getParameter("page");
084                    if ("".equals(pageParam)) showpageParam = null;
085                    String serviceidParam = request.getParameter("serviceId");
086                    if ("".equals(serviceidParam)) showpageParam = null;
087                    
088                    if (!request.getSession().isNew() && showpageParam != null) {
089                            response.sendRedirect(response.encodeRedirectURL("?serviceId=ExternalEvent&page=" + showpageParam));
090                    }
091                    if (showpageParam == null && pageParam != null && serviceidParam == null) {
092                            response.sendRedirect(response.encodeRedirectURL("."));
093                    }
094                    
095                    try {
096                            super.process(request, response);
097                    } catch (RuntimeException ex) {
098                            Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
099                            ex.printStackTrace();
100                            throw ex;
101                    } catch (IOException ex) {
102                            Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
103                            ex.printStackTrace();
104                            throw ex;
105                    } catch (ServletException ex) {
106                            Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
107                            ex.printStackTrace();
108                            throw ex;
109                    }
110            }
111    
112    }