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    import ch.uzh.ifi.attempto.echocomp.Logger;
028    
029    /**
030     * This servlet class is used by the web server to start AceWiki.
031     * In order to run the AceWiki servlet, a web application archive (WAR) file has to be created.
032     * See the <a href="{@docRoot}/README.txt">README file of the Attempto Java Packages</a> and the
033     * <a href="{@docRoot}/web.xml">web.xml example file</a>.
034     *<p>
035     * SWI Prolog needs to be installed on the server and you need to have a compiled version of the Attempto APE
036     * distribution. See the documentation of {@link APELocal} for more information.
037     * 
038     * @author Tobias Kuhn
039     */
040    public class AceWikiServlet extends WebContainerServlet {
041    
042            private static final long serialVersionUID = -7342857942059126499L;
043    
044            /**
045             * Creates a new AceWiki servlet object.
046             */
047            public AceWikiServlet() {
048            }
049    
050            public ApplicationInstance newApplicationInstance() {
051                    String ontologyName = getInitParameter("ontology");
052                    String baseURI = getInitParameter("baseuri");
053                    String title = getInitParameter("title");
054                    String description = getInitParameter("description");
055                    String loginString = getInitParameter("login");
056                    int login;
057    
058                    if (loginString == null) loginString = "no";
059                    if (loginString.equals("yes") || loginString.equals("pw")) {
060                            login = AceWikiApp.PW_LOGIN;
061                    } else if (loginString.equals("nonpw")) {
062                            login = AceWikiApp.NONPW_LOGIN;
063                    } else {
064                            login = AceWikiApp.NO_LOGIN;
065                    }
066    
067                    if (!APELocal.isInitialized()) {
068                            String apeCommand = getServletContext().getInitParameter("apecommand");
069                            if (apeCommand == null) apeCommand = "ape.exe";
070                            APELocal.init(apeCommand);
071                    }
072    
073                    Ontology ontology = Ontology.loadOntology(ontologyName, baseURI);
074    
075                    Logger.log("syst", "syst", 0, "appl", "new application instance: " + ontologyName);
076                    return new AceWikiApp(ontology, title, description, login);
077            }
078    
079            protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
080                    String showpageParam = request.getParameter("showpage");
081                    if ("".equals(showpageParam)) showpageParam = null;
082                    String pageParam = request.getParameter("page");
083                    if ("".equals(pageParam)) showpageParam = null;
084                    String serviceidParam = request.getParameter("serviceId");
085                    if ("".equals(serviceidParam)) showpageParam = null;
086    
087                    if (!request.getSession().isNew() && showpageParam != null) {
088                            response.sendRedirect(response.encodeRedirectURL("?serviceId=ExternalEvent&page=" + showpageParam));
089                    }
090                    if (showpageParam == null && pageParam != null && serviceidParam == null) {
091                            response.sendRedirect(response.encodeRedirectURL("."));
092                    }
093    
094                    try {
095                            super.process(request, response);
096                    } catch (RuntimeException ex) {
097                            Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
098                            ex.printStackTrace();
099                            throw ex;
100                    } catch (IOException ex) {
101                            Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
102                            ex.printStackTrace();
103                            throw ex;
104                    } catch (ServletException ex) {
105                            Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
106                            ex.printStackTrace();
107                            throw ex;
108                    }
109            }
110    
111    }