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 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 try {
082 super.process(request, response);
083 } catch (RuntimeException ex) {
084 Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
085 ex.printStackTrace();
086 throw ex;
087 } catch (IOException ex) {
088 Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
089 ex.printStackTrace();
090 throw ex;
091 } catch (ServletException ex) {
092 Logger.log("syst", "syst", 0, "fail", "fatal error: " + ex);
093 ex.printStackTrace();
094 throw ex;
095 }
096 }
097
098 // This was a try to support specific URLs for each wiki articles:
099 /*
100 protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
101 String pageParameter = request.getParameter("page");
102 if (app != null && pageParameter != null) {
103 final OntologyElement oe = app.getWiki().getOntology().get(request.getParameter("page"));
104 if (taskQueue == null) {
105 taskQueue = app.createTaskQueue();
106 }
107 if (oe != null) {
108 app.enqueueTask(taskQueue, new Runnable() {
109 public void run() {
110 app.getWiki().showPage(oe);
111 }
112 });
113 try {wait(500);} catch (Exception ex) {}
114 }
115 }
116
117 super.process(request, response);
118 }
119 */
120
121 }