001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008, 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 ch.uzh.ifi.attempto.acewiki.core.ontology.Ontology;
024    import ch.uzh.ifi.attempto.ape.APELocal;
025    
026    import nextapp.echo2.app.ApplicationInstance;
027    import nextapp.echo2.webcontainer.WebContainerServlet;
028    
029    /**
030     * This servlet class should be launched by the web server in order to start an AceWiki application. If you are using a
031     * web application archive (WAR) then the web.xml file should contain something like this:
032     * 
033     * <pre>
034     * &lt;servlet&gt;
035     *   &lt;servlet-name&gt;GeoWiki&lt;/servlet-name&gt;
036     *   &lt;servlet-class&gt;ch.uzh.ifi.attempto.acewiki.AceWikiServlet&lt;/servlet-class&gt;
037     *   &lt;init-param&gt;
038     *     &lt;param-name&gt;ontology&lt;/param-name&gt;
039     *     &lt;param-value&gt;geo&lt;/param-value&gt;
040     *   &lt;/init-param&gt;
041     *   &lt;init-param&gt;
042     *     &lt;param-name&gt;baseuri&lt;/param-name&gt;
043     *     &lt;param-value&gt;http://attempto.ifi.uzh.ch/acewiki/&lt;/param-value&gt;
044     *   &lt;/init-param&gt;
045     *   &lt;init-param&gt;
046     *     &lt;param-name&gt;title&lt;/param-name&gt;
047     *     &lt;param-value&gt;Geography Wiki&lt;/param-value&gt;
048     *   &lt;/init-param&gt;
049     *   &lt;init-param&gt;
050     *     &lt;param-name&gt;description&lt;/param-name&gt;
051     *     &lt;param-value&gt;This wiki contains geographical knowledge.&lt;/param-value&gt;
052     *   &lt;/init-param&gt;
053     *   &lt;init-param&gt;
054     *     &lt;param-name&gt;login&lt;/param-name&gt;
055     *     &lt;param-value&gt;no&lt;/param-value&gt;
056     *   &lt;/init-param&gt;
057     * &lt;/servlet&gt;
058     * </pre>
059     * 
060     * The parameter 'ontology' is mandatory and specifies the name of the ontology. This name is used in the URIs of the
061     * OWL statements and for the file names on the server. The 'baseuri' parameter defines the base URI for the
062     * OWL statements. The values of the parameters 'title' and 'description' are shown on the start page of the wiki. Finally,
063     * the 'login' parameter defines whether users have to login before they can use the wiki. Note that the login feature
064     * is implemented only very rudimentary.
065     *<p>
066     * Furthermore, SWI Prolog needs to be installed on the server and you need to have a compiled version of the Attempto APE
067     * distribution. See the documentation of {@link APELocal} for more information. The server has to know the command of your
068     * SWI Prolog installation and the location of the ape.exe file. This is done by context parameters in the web.xml file:
069     * 
070     * <pre>
071     * &lt;context-param&gt;
072     *   &lt;param-name&gt;prologcommand&lt;/param-name&gt;
073     *   &lt;param-value&gt;swipl&lt;/param-value&gt;
074     * &lt;/context-param&gt;
075     *  
076     * &lt;context-param&gt;
077     *   &lt;param-name&gt;apecommand&lt;/param-name&gt;
078     *   &lt;param-value&gt;/path/to/file/ape.exe&lt;/param-value&gt;
079     * &lt;/context-param&gt;
080     * </pre>
081     * 
082     * One last tiny thing: The server delay window (that is shown on the client when the server is busy) looks for the
083     * wait icon "../wait.gif". You should copy the file "src/ch/uzh/ifi/attempto/acewiki/core/gui/img/wait.gif" to the
084     * respective folder on the server. (This should be improved, I admit.)
085     * 
086     * @author Tobias Kuhn
087     */
088    public class AceWikiServlet extends WebContainerServlet {
089            
090            private static final long serialVersionUID = -7342857942059126499L;
091            
092            private Ontology ontology;
093            
094            /**
095             * Creates a new AceWiki servlet object.
096             */
097            public AceWikiServlet() {
098            }
099            
100            public ApplicationInstance newApplicationInstance() {
101                    String ontologyName = getInitParameter("ontology");
102                    String baseURI = getInitParameter("baseuri");
103                    String title = getInitParameter("title");
104                    String description = getInitParameter("description");
105                    boolean login = "yes".equals(getInitParameter("login"));
106                    
107                    if (!APELocal.isInitialized()) {
108                            String prologCommand = getServletContext().getInitParameter("prologcommand");
109                            String apeCommand = getServletContext().getInitParameter("apecommand");
110                            if (prologCommand == null) prologCommand = "swipl";
111                            if (apeCommand == null) apeCommand = "ape.exe";
112                            APELocal.init(prologCommand, apeCommand);
113                    }
114                    
115                    if (ontology == null) {
116                            ontology = Ontology.loadOntology(ontologyName, baseURI);
117                    }
118                    
119                    Logger.log("syst", "syst", 0, "appl", "new application instance: " + ontologyName);
120                    return new AceWikiApp(ontology, title, description, login);
121            }
122            
123            protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
124                    try {
125                            super.process(request, response);
126                    } catch (RuntimeException ex) {
127                            Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
128                            ex.printStackTrace();
129                            throw ex;
130                    } catch (IOException ex) {
131                            Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
132                            ex.printStackTrace();
133                            throw ex;
134                    } catch (ServletException ex) {
135                            Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
136                            ex.printStackTrace();
137                            throw ex;
138                    }
139            }
140            
141            // This was a try to support specific URLs for each wiki articles:
142            /*
143            protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
144                    String pageParameter = request.getParameter("page");
145                    if (app != null && pageParameter != null) {
146                            final OntologyElement oe = app.getWiki().getOntology().get(request.getParameter("page"));
147                            if (taskQueue == null) {
148                                    taskQueue = app.createTaskQueue();
149                            }
150                            if (oe != null) {
151                                    app.enqueueTask(taskQueue, new Runnable() {
152                                            public void run() {
153                                                    app.getWiki().showPage(oe);
154                                            }
155                                    });
156                                    try {wait(500);} catch (Exception ex) {}
157                            }
158                    }
159                    
160                    super.process(request, response);
161            }
162            */
163    
164    }