Add simple HTTP to z39.50 example
authorJakub Skoczen <jakub@indexdata.dk>
Thu, 18 Feb 2010 13:21:28 +0000 (14:21 +0100)
committerJakub Skoczen <jakub@indexdata.dk>
Thu, 18 Feb 2010 13:21:28 +0000 (14:21 +0100)
examples/zgate/pom.xml [new file with mode: 0644]
examples/zgate/src/main/java/com/indexdata/zgate/ZgateServlet.java [new file with mode: 0644]
examples/zgate/src/main/webapp/META-INF/context.xml [new file with mode: 0644]
examples/zgate/src/main/webapp/WEB-INF/web.xml [new file with mode: 0644]
examples/zgate/src/main/webapp/index.jsp [new file with mode: 0644]

diff --git a/examples/zgate/pom.xml b/examples/zgate/pom.xml
new file mode 100644 (file)
index 0000000..190c6d7
--- /dev/null
@@ -0,0 +1,55 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.indexdata</groupId>
+  <artifactId>zgate</artifactId>
+  <packaging>war</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>HTTP-to-Z3950 gateway</name>
+  <url>http://maven.apache.org</url>
+  <dependencies>
+
+    <dependency>
+      <groupId>org.yaz4j</groupId>
+      <artifactId>yaz4j-any</artifactId>
+      <version>1.0-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>2.5</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>javax.servlet.jsp</groupId>
+      <artifactId>jsp-api</artifactId>
+      <version>2.1</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.0.2</version>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>
+    </plugins>
+    <finalName>zgate</finalName>
+  </build>
+</project>
diff --git a/examples/zgate/src/main/java/com/indexdata/zgate/ZgateServlet.java b/examples/zgate/src/main/java/com/indexdata/zgate/ZgateServlet.java
new file mode 100644 (file)
index 0000000..13b1a07
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 1995-2010, Index Data
+ * All rights reserved.
+ * See the file LICENSE for details.
+ */
+package com.indexdata.zgate;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.yaz4j.Connection;
+import org.yaz4j.Record;
+import org.yaz4j.ResultSet;
+import org.yaz4j.exception.ZoomException;
+
+/**
+ *
+ * @author jakub
+ */
+public class ZgateServlet extends HttpServlet {
+
+  @Override
+  public void init() throws ServletException {
+    System.out.println("Zeta: java.library.path=" + System.getProperty("java.library.path"));
+    System.out.println("Zeta: LD_LIBRARY_PATH=" + System.getenv("LD_LIBRARY_PATH"));
+  }
+
+  @Override
+  /*
+   * For dinosaur search use: ?zurl=z3950.loc.gov:7090/voyager&query=@attr 1=7 0253333490&syntax=usmarc
+   **/
+  protected void doGet(HttpServletRequest request, HttpServletResponse response)
+    throws ServletException, IOException {
+    String zurl = request.getParameter("zurl");
+    if (zurl == null || zurl.isEmpty()) { response.sendError(400, "Missing parameter 'zurl'"); return; }
+
+    String query = request.getParameter("query");
+    if (query == null || query.isEmpty()) { response.sendError(400, "Missing parameter 'query'"); return; }
+
+    String syntax = request.getParameter("syntax");
+    if (syntax == null || syntax.isEmpty()) { response.sendError(400, "Missing parameter 'syntax'"); return; }
+
+    int maxrecs=10;
+    if (request.getParameter("maxrecs") != null && !request.getParameter("maxrecs").isEmpty()) {
+      try {
+        maxrecs = Integer.parseInt(request.getParameter("maxrecs"));
+      } catch (NumberFormatException nfe) {
+        response.sendError(400, "Malformed parameter 'maxrecs'");
+        return;
+      }
+    }
+
+    response.getWriter().println("SEARCH PARAMETERS");
+    response.getWriter().println("zurl: " + zurl);
+    response.getWriter().println("query: " + query);
+    response.getWriter().println("syntax: " + syntax);
+    response.getWriter().println("maxrecs: " + maxrecs);
+    response.getWriter().println();
+
+    Connection con = new Connection(zurl, 0);
+    con.setSyntax(syntax);
+    try {
+      con.connect();
+      ResultSet set = con.search(query, Connection.QueryType.PrefixQuery);
+      response.getWriter().println("Showing " + maxrecs + " of " +set.getSize());
+      response.getWriter().println();
+      for(int i=0; i<set.getSize() && i<maxrecs; i++) {
+        Record rec = set.getRecord(i);
+        response.getWriter().print(rec.render());
+      }
+    } catch (ZoomException ze) {
+      throw new ServletException(ze);
+    } finally {
+      con.close();
+    }
+  }
+
+  @Override
+  protected void doPost(HttpServletRequest request, HttpServletResponse response)
+    throws ServletException, IOException {
+  }
+
+  @Override
+  public String getServletInfo() {
+    return "Zeta search engine servlet";
+  }
+}
diff --git a/examples/zgate/src/main/webapp/META-INF/context.xml b/examples/zgate/src/main/webapp/META-INF/context.xml
new file mode 100644 (file)
index 0000000..1a672a4
--- /dev/null
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Context antiJARLocking="true" path="/zgate"/>
diff --git a/examples/zgate/src/main/webapp/WEB-INF/web.xml b/examples/zgate/src/main/webapp/WEB-INF/web.xml
new file mode 100644 (file)
index 0000000..7a5a3db
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
+    <display-name>zgate</display-name>
+    <servlet>
+        <servlet-name>ZgateServlet</servlet-name>
+        <servlet-class>com.indexdata.zgate.ZgateServlet</servlet-class>
+    </servlet>
+    <servlet-mapping>
+        <servlet-name>ZgateServlet</servlet-name>
+        <url-pattern>/zgate</url-pattern>
+    </servlet-mapping>
+    <session-config>
+        <session-timeout>
+            30
+        </session-timeout>
+    </session-config>
+    <welcome-file-list>
+      <welcome-file>zgate</welcome-file>
+      <welcome-file>index.jsp</welcome-file>
+    </welcome-file-list>
+</web-app>
diff --git a/examples/zgate/src/main/webapp/index.jsp b/examples/zgate/src/main/webapp/index.jsp
new file mode 100644 (file)
index 0000000..ab292c3
--- /dev/null
@@ -0,0 +1,13 @@
+<%@page contentType="text/html" pageEncoding="UTF-8"%>
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+   "http://www.w3.org/TR/html4/loose.dtd">
+
+<html>
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+        <title>JSP Page</title>
+    </head>
+    <body>
+        <h1>Hello World!</h1>
+    </body>
+</html>