Add simple HTTP to z39.50 example
[yaz4j-moved-to-github.git] / examples / zgate / src / main / java / com / indexdata / zgate / ZgateServlet.java
1 /*
2  * Copyright (c) 1995-2010, Index Data
3  * All rights reserved.
4  * See the file LICENSE for details.
5  */
6 package com.indexdata.zgate;
7
8 import java.io.IOException;
9 import java.io.PrintWriter;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import org.yaz4j.Connection;
16 import org.yaz4j.Record;
17 import org.yaz4j.ResultSet;
18 import org.yaz4j.exception.ZoomException;
19
20 /**
21  *
22  * @author jakub
23  */
24 public class ZgateServlet extends HttpServlet {
25
26   @Override
27   public void init() throws ServletException {
28     System.out.println("Zeta: java.library.path=" + System.getProperty("java.library.path"));
29     System.out.println("Zeta: LD_LIBRARY_PATH=" + System.getenv("LD_LIBRARY_PATH"));
30   }
31
32   @Override
33   /*
34    * For dinosaur search use: ?zurl=z3950.loc.gov:7090/voyager&query=@attr 1=7 0253333490&syntax=usmarc
35    **/
36   protected void doGet(HttpServletRequest request, HttpServletResponse response)
37     throws ServletException, IOException {
38     String zurl = request.getParameter("zurl");
39     if (zurl == null || zurl.isEmpty()) { response.sendError(400, "Missing parameter 'zurl'"); return; }
40
41     String query = request.getParameter("query");
42     if (query == null || query.isEmpty()) { response.sendError(400, "Missing parameter 'query'"); return; }
43
44     String syntax = request.getParameter("syntax");
45     if (syntax == null || syntax.isEmpty()) { response.sendError(400, "Missing parameter 'syntax'"); return; }
46
47     int maxrecs=10;
48     if (request.getParameter("maxrecs") != null && !request.getParameter("maxrecs").isEmpty()) {
49       try {
50         maxrecs = Integer.parseInt(request.getParameter("maxrecs"));
51       } catch (NumberFormatException nfe) {
52         response.sendError(400, "Malformed parameter 'maxrecs'");
53         return;
54       }
55     }
56
57     response.getWriter().println("SEARCH PARAMETERS");
58     response.getWriter().println("zurl: " + zurl);
59     response.getWriter().println("query: " + query);
60     response.getWriter().println("syntax: " + syntax);
61     response.getWriter().println("maxrecs: " + maxrecs);
62     response.getWriter().println();
63
64     Connection con = new Connection(zurl, 0);
65     con.setSyntax(syntax);
66     try {
67       con.connect();
68       ResultSet set = con.search(query, Connection.QueryType.PrefixQuery);
69       response.getWriter().println("Showing " + maxrecs + " of " +set.getSize());
70       response.getWriter().println();
71       for(int i=0; i<set.getSize() && i<maxrecs; i++) {
72         Record rec = set.getRecord(i);
73         response.getWriter().print(rec.render());
74       }
75     } catch (ZoomException ze) {
76       throw new ServletException(ze);
77     } finally {
78       con.close();
79     }
80   }
81
82   @Override
83   protected void doPost(HttpServletRequest request, HttpServletResponse response)
84     throws ServletException, IOException {
85   }
86
87   @Override
88   public String getServletInfo() {
89     return "Zeta search engine servlet";
90   }
91 }