View Javadoc

1   /*
2    * Copyright (c) 2007, Peter Mika All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * 
28   */
29  package org.openrdf.elmo.smusher.servlet;
30  
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  import java.io.Writer;
34  
35  import javax.servlet.ServletException;
36  import javax.servlet.http.HttpServlet;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  import org.openrdf.OpenRDFException;
41  import org.openrdf.elmo.sesame.SesameManager;
42  import org.openrdf.elmo.smusher.FastIterativeSmusher;
43  import org.openrdf.elmo.smusher.IFPSmusher;
44  import org.openrdf.elmo.smusher.NameSmusher;
45  import org.openrdf.elmo.smusher.Smusher;
46  import org.openrdf.elmo.smusher.listener.LogListener;
47  import org.openrdf.elmo.smusher.listener.PersonLogListener;
48  import org.openrdf.elmo.smusher.listener.SameAsListener;
49  import org.openrdf.elmo.smusher.Util;
50  import org.openrdf.repository.Repository;
51  
52  /** Web interface to the Elmo smusher. See the provided user manual for the
53   * documentation of initialization and request parameters.
54   * 
55   * @author Peter Mika (pmika@cs.vu.nl)
56   */
57  public class SmusherServlet extends HttpServlet {
58      /**
59       * 
60       */
61      private static final long serialVersionUID = -271211078620234650L;
62      private final static String CONTENT_TYPE = "text/html";
63  
64      /**
65       * 
66       */
67      public SmusherServlet() {
68          super();
69          // TODO Auto-generated constructor stub
70      }
71      
72      public static void smush(Repository repository, Writer log) throws OpenRDFException {
73          FastIterativeSmusher smusher = new FastIterativeSmusher();
74          SameAsListener listener = new SameAsListener(repository);
75          Smusher ifps = new IFPSmusher();
76          ifps.addListener(listener);
77          Smusher ns = new NameSmusher(); 
78          ns.addListener(listener);
79          smusher.addSmusher(ifps);
80          smusher.addSmusher(ns);
81         
82          if (log != null) {
83              smusher.addListener(new PersonLogListener(log, repository));
84          }
85          SesameManager manager= Util.initManager(repository);
86          smusher.smush(manager);
87      }
88      
89      public void doGet(HttpServletRequest request, HttpServletResponse response)
90      throws ServletException, IOException {
91          
92              PrintWriter out = response.getWriter();
93              response.setContentType(CONTENT_TYPE);
94              out.write("<html>");
95              out.write("<head><title>SmusherServlet</title></head>");
96              out.write("<body>");
97              Repository repository = Util.initRepository(getServletConfig(), request);
98              String verbose = getServletConfig().getInitParameter("verbose");
99              if (verbose == null) {
100                 verbose = request.getParameter("verbose");
101             }
102             try {
103 	            if (verbose == null || verbose.equalsIgnoreCase("true")) {
104 	            	smush(repository,out);
105 	                     
106 	            } else {
107 	                smush(repository, null);
108 	            }
109 		    } catch (OpenRDFException oe) {
110 		    	throw new ServletException(oe);
111 		    }
112            
113             out.write("</body>");
114             out.write("</html>");
115             out.close();
116         
117     }
118 
119     /** For debugging purposes only.
120      * 
121      * @param args
122      * @throws Exception
123      */
124     public static void main(String[] args) throws Exception {
125         Repository repository = Util.initRepository(args[0], args[1]);
126        
127         
128         //TODO: if we still need this 
129         //Turn off autocommit, otherwise EmailComparator slows down comparison
130         //session.setAutoCommit(false);
131         
132         smush(repository, new PrintWriter(System.out));
133         
134         
135     }
136 }