Changeset 1889


Ignore:
Timestamp:
04/17/12 13:06:10 (12 years ago)
Author:
oschonef
Message:
  • re-name some classes to better match to SRU specification components
    • SRUService -> SRUServer
    • SRUEndpointConfig -> SRUServerConfig
  • some more sanity checking in config parsing
  • some updates to java docs
Location:
SRUServer/trunk
Files:
4 edited
3 moved

Legend:

Unmodified
Added
Removed
  • SRUServer/trunk/README.txt

    r1881 r1889  
    11ABSTRACT:
    22---------
    3 This package implements a SRU/CQL end-point conforming to the SRU protocol
    4 version 1.1 and 1.2. The library will handle most of the protocol related tasks
    5 for you and you'll only need to implement a few classes. The library will not
    6 save you from dowing your SR/CQL homework (i.e. you'll need to have at least
    7 a basic understanding of the protocol).
     3This package implements the server-side part of the SRU/CQL protocol (SRU/S)
     4and conforms to SRU versin 1.1 and 1.2. The library will handle most of the
     5protocol related tasks for you and you'll only need to implement a few classes
     6to connect you search engine. However, the library will not save you from
     7doing your SRU/CQL homework (i.e. you'll need to have at least some
     8understanding of the protocol and adhere to the protocol sementics).
    89
    910More Information about SRU/CQL:
     
    1516HOW TO USE:
    1617-----------
    17 You'll need to provide your own Servlet to initialize the SRUService class
    18 with a SRUEndpointConfig instance and dispatch the appropriate HTTP requets
    19 to it. The SRUEndpointConfig requires an XML document which contains the
    20 end-point configuration. It must conform to the "endpoint-config.xsd" schema
     18You'll need to provide your own Servlet to initialize the SRUServer class
     19with a SRUServerConfig instance and dispatch the appropriate HTTP requets
     20to it. The SRUServerConfig requires an XML document which contains the
     21end-point configuration. It must conform to the "sru-server-config.xsd" schema
    2122in the "src/main/resources/META-INF" directory.
    2223Furthermore, you need to provide an implementation of the SRUSearchEngine
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRURequestImpl.java

    r1881 r1889  
    194194
    195195
    196     boolean checkParameters(SRUEndpointConfig config) {
     196    boolean checkParameters(SRUServerConfig config) {
    197197        // parse mandatory operation parameter
    198198        final String op = getParameter(PARAM_OPERATION, false);
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUSearchEngine.java

    r1881 r1889  
    2121 * search engine.
    2222 * <p>Implementing the
    23  * {@link #explain(SRUEndpointConfig, SRURequest, SRUDiagnosticList)} and
    24  * {@link #scan(SRUEndpointConfig, SRURequest, SRUDiagnosticList)} is optional,
     23 * {@link #explain(SRUServerConfig, SRURequest, SRUDiagnosticList)} and
     24 * {@link #scan(SRUServerConfig, SRURequest, SRUDiagnosticList)} is optional,
    2525 * but implementing
    26  * {@link #search(SRUEndpointConfig, SRURequest, SRUDiagnosticList)} is
     26 * {@link #search(SRUServerConfig, SRURequest, SRUDiagnosticList)} is
    2727 * mandatory.</p>
    2828 * <p>The implementation of these methods is required to be thread-safe.</p>
     
    5151     * @see SRUExplainResult
    5252     */
    53     public SRUExplainResult explain(SRUEndpointConfig config,
     53    public SRUExplainResult explain(SRUServerConfig config,
    5454            SRURequest request, SRUDiagnosticList diagnostics)
    5555            throws SRUException;
     
    7575     * @see SRUExplainResult
    7676     */
    77     public SRUSearchResultSet search(SRUEndpointConfig config,
     77    public SRUSearchResultSet search(SRUServerConfig config,
    7878            SRURequest request, SRUDiagnosticList diagnostics)
    7979            throws SRUException;
     
    9797     * @see SRUExplainResult
    9898     */
    99     public SRUScanResultSet scan(SRUEndpointConfig config, SRURequest request,
     99    public SRUScanResultSet scan(SRUServerConfig config, SRURequest request,
    100100            SRUDiagnosticList diagnostics) throws SRUException;
    101101
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServer.java

    r1882 r1889  
    3333import org.z3950.zing.cql.CQLNode;
    3434
    35 import eu.clarin.sru.server.SRUEndpointConfig.DatabaseInfo;
    36 import eu.clarin.sru.server.SRUEndpointConfig.IndexInfo;
    37 import eu.clarin.sru.server.SRUEndpointConfig.LocalizedString;
    38 import eu.clarin.sru.server.SRUEndpointConfig.SchemaInfo;
     35import eu.clarin.sru.server.SRUServerConfig.DatabaseInfo;
     36import eu.clarin.sru.server.SRUServerConfig.IndexInfo;
     37import eu.clarin.sru.server.SRUServerConfig.LocalizedString;
     38import eu.clarin.sru.server.SRUServerConfig.SchemaInfo;
    3939
    4040
    4141/**
    42  * SRU/CQL server implementation. This class implements SRU/CQL version 1.1
    43  * and 1.2.
    44  * <p>An example servlet implementing an endpoint:</p>
     42 * SRU/CQL protocol implementation for the server-side (SRU/S). This class
     43 * implements SRU/CQL version 1.1 and and 1.2.
     44 * <p>
     45 * An example servlet using this class:
     46 * </p>
    4547 * <pre>
    4648 * public class MySRUServlet extends HttpServlet {
    47  *     private transient SRUService service;
     49 *     private transient SRUServer sruServer;
     50 *
    4851 *
    4952 *     public void init() throws ServletException {
    5053 *         final ServletContext ctx = getServletContext();
    5154 *         try {
    52  *             URL url = MySRUServlet.class.getClassLoader().getResource("META-INF/endpoint-config.xml");
     55 *             URL url = MySRUServlet.class.getClassLoader().getResource(
     56 *                     &quot;META-INF/endpoint-config.xml&quot;);
    5357 *             if (url == null) {
    54  *                 throw new ServletException("not found, url == null");
     58 *                 throw new ServletException(&quot;not found, url == null&quot;);
    5559 *             }
    5660 *
    57  *             // get additional parameters from Servlet context
     61 *             // get additional runtime configuration from Servlet context
    5862 *             HashMap&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
    59  *             for (Enumeration&lt;?&gt; i = ctx.getInitParameterNames();
    60  *                 i.hasMoreElements();) {
    61  *                 String key   = (String) i.nextElement();
     63 *             for (Enumeration&lt;?&gt; i = ctx.getInitParameterNames(); i
     64 *                     .hasMoreElements();) {
     65 *                 String key = (String) i.nextElement();
    6266 *                 String value = ctx.getInitParameter(key);
    63  *                 if ((value != null) && !value.isEmpty()) {
     67 *                 if ((value != null) &amp;&amp; !value.isEmpty()) {
    6468 *                     params.put(key, value);
    6569 *                 }
    6670 *             }
    6771 *
    68  *             SRUEndpointConfig config =
    69  *                 SRUEndpointConfig.parse(params, url.openStream());
    70  *             SRUSearchEngine searchEngine =
    71  *                 new MySRUSearchEngine(config, params);
    72  *             service = new SRUService(config, searchEngine);
     72 *             SRUServerConfig config = SRUServerConfig.parse(params,
     73 *                     url.openStream());
     74 *             SRUSearchEngine searchEngine = new MySRUSearchEngine(config, params);
     75 *             sruServer = new SRUServer(config, searchEngine);
    7376 *         } catch (Exception e) {
    74  *             throw new ServletException("error initializing endpoint", e);
     77 *             throw new ServletException(&quot;error initializing endpoint&quot;, e);
    7578 *         }
    7679 *     }
    7780 *
     81 *
    7882 *     protected void doGet(HttpServletRequest request,
    7983 *             HttpServletResponse response) throws ServletException, IOException {
    80  *         service.handleRequest(request, response);
     84 *         sruServer.handleRequest(request, response);
    8185 *     }
     86 *
    8287 *
    8388 *     protected void doPost(HttpServletRequest request,
    8489 *             HttpServletResponse response) throws ServletException, IOException {
    85  *         service.handleRequest(request, response);
     90 *         sruServer.handleRequest(request, response);
    8691 *     }
    8792 * }
    8893 * </pre>
    8994 *
    90  * @see SRUEndpointConfig
     95 * @see SRUServerConfig
    9196 * @see SRUSearchEngine
    9297 * @see <a href="http://www.loc.gov/standards/sru/">SRU/CQL protocol 1.2</a>
    9398 */
    94 public class SRUService {
     99public class SRUServer {
    95100    private static final String SRU_NS =
    96101            "http://www.loc.gov/zing/srw/";
     
    110115    private static final int RESPONSE_BUFFER_SIZE = 64 * 1024;
    111116    private static final Logger logger =
    112             LoggerFactory.getLogger(SRUService.class);
    113     private final SRUEndpointConfig config;
     117            LoggerFactory.getLogger(SRUServer.class);
     118    private final SRUServerConfig config;
    114119    private final SRUSearchEngine searchEngine;
    115120    private final XMLOutputFactory writerFactory;
     
    128133     *             if an error occurred
    129134     */
    130     public SRUService(SRUEndpointConfig config, SRUSearchEngine searchEngine)
     135    public SRUServer(SRUServerConfig config, SRUSearchEngine searchEngine)
    131136            throws SRUException {
    132137        if (config == null) {
     
    343348        if (schemaInfo != null) {
    344349            out.writeStartElement(SRU_EXPLAIN_NS, "schemaInfo");
    345             for (SRUEndpointConfig.SchemaInfo schema : schemaInfo) {
     350            for (SRUServerConfig.SchemaInfo schema : schemaInfo) {
    346351                out.writeStartElement(SRU_EXPLAIN_NS, "schema");
    347352                out.writeAttribute("identifier", schema.getIdentifier());
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServerConfig.java

    r1883 r1889  
    1717package eu.clarin.sru.server;
    1818
    19 import java.io.InputStream;
     19import java.io.IOException;
    2020import java.net.URL;
    2121import java.util.ArrayList;
     
    3030import javax.xml.parsers.DocumentBuilder;
    3131import javax.xml.parsers.DocumentBuilderFactory;
     32import javax.xml.parsers.ParserConfigurationException;
    3233import javax.xml.transform.Source;
    3334import javax.xml.transform.dom.DOMSource;
     
    3738import javax.xml.xpath.XPath;
    3839import javax.xml.xpath.XPathConstants;
     40import javax.xml.xpath.XPathException;
    3941import javax.xml.xpath.XPathExpression;
    4042import javax.xml.xpath.XPathExpressionException;
     
    5254
    5355/**
    54  * Endpoint configuration. Most of the endpoint configuration is created from
    55  * the XML file.
     56 * SRU server configuration.
    5657 *
    57  * <p>Example:</p>
     58 * <p>
     59 * Example:
     60 * </p>
    5861 * <pre>
    59  * URL url = MySRUServlet.class.getClassLoader().getResource("META-INF/endpoint-config.xml");
     62 * URL url = MySRUServlet.class.getClassLoader()
     63 *               .getResource("META-INF/sru-server-config.xml");
    6064 * if (url == null) {
    61  *     throw new ServletException("not found, url == null");
     65 *     throw new ServletException(&quot;not found, url == null&quot;);
    6266 * }
    6367 *
     68 * // other runtime configuration, usually obtained from servlet context
    6469 * HashMap&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
    65  * SRUEndpointConfig config = SRUEndpointConfig.parse(params, url.openStream());
     70 * params.put(SRUServerConfig.SRU_TRANSPORT, "http");
     71 * params.put(SRUServerConfig.SRU_HOST, "127.0.0.1");
     72 * params.put(SRUServerConfig.SRU_PORT, "80");
     73 * params.put(SRUServerConfig.SRU_DATABASE, "sru-server");
     74 *
     75 * SRUServerConfig config = SRUServerConfig.parse(params, url);
    6676 * </pre>
    6777 *
    68  * <p>The XML configuration file must validate against the "endpoint-config.xsd"
    69  * schema bundled with the package and need to have the
     78 * <p>
     79 * The XML configuration file must validate against the "sru-server-config.xsd"
     80 * W3C schema bundled with the package and need to have the
    7081 * <code>http://www.clarin.eu/sru-server/1.0/</code> XML namespace.
    71  * </p> 
     82 * </p>
    7283 */
    73 public final class SRUEndpointConfig {
     84public final class SRUServerConfig {
    7485    public static final String SRU_TRANSPORT     = "sru.transport";
    7586    public static final String SRU_HOST          = "sru.host";
     
    7990    private static final String CONFIG_FILE_NAMESPACE_URI =
    8091            "http://www.clarin.eu/sru-server/1.0/";
     92    private static final String CONFIG_FILE_SCHEMA_URL =
     93            "META-INF/sru-server-config.xsd";
    8194
    8295    public static final class LocalizedString {
     
    413426
    414427
    415     private SRUEndpointConfig(String transport, String host, String port,
     428    private SRUServerConfig(String transport, String host, String port,
    416429            String database, boolean echoRequests, DatabaseInfo databaseinfo,
    417430            IndexInfo indexInfo, List<SchemaInfo> schemaInfo) {
     
    524537
    525538    /**
    526      * Parse the XML endpoint configuration.
     539     * Parse a SRU server XML configuration file and create an configuration
     540     * object from it.
    527541     *
    528542     * @param params
    529543     *            additional settings
    530      * @param in
    531      *            an {@link InputSource} to the XML configuration file
     544     * @param configFile
     545     *            an {@link URL} pointing to the XML configuration file
    532546     * @return a initialized <code>SRUEndpointConfig</code> instance
    533547     * @throws NullPointerException
    534      *             if <em>params</em> or <em>in</em> is <code>null</code>
     548     *             if <em>params</em> or <em>configFile</em> is
     549     *             <code>null</code>
    535550     * @throws SRUConfigException
    536      *             if an error occured
     551     *             if an error occurred
    537552     */
    538     public static SRUEndpointConfig parse(Map<String, String> params,
    539             InputStream in) throws SRUConfigException {
     553    public static SRUServerConfig parse(Map<String, String> params,
     554            URL configFile) throws SRUConfigException {
    540555        if (params == null) {
    541556            throw new NullPointerException("params == null");
    542557        }
    543         if (in == null) {
     558        if (configFile == null) {
    544559            throw new NullPointerException("in == null");
    545560        }
    546561        try {
    547             URL url = SRUEndpointConfig.class.getClassLoader()
    548                     .getResource("META-INF/endpoint-config.xsd");
     562            URL url = SRUServerConfig.class.getClassLoader()
     563                    .getResource(CONFIG_FILE_SCHEMA_URL);
    549564            if (url == null) {
    550                 throw new SRUConfigException("cannot open " +
    551                         "\"META-INF/endpoint-config.xsd\"");
     565                throw new SRUConfigException("cannot open \"" +
     566                        CONFIG_FILE_SCHEMA_URL + "\"");
    552567            }
    553568            SchemaFactory sfactory =
     
    564579            // parse input
    565580            DocumentBuilder builder = factory.newDocumentBuilder();
    566             InputSource input = new InputSource(in);
     581            InputSource input = new InputSource(configFile.openStream());
    567582            input.setPublicId(CONFIG_FILE_NAMESPACE_URI);
    568583            input.setSystemId(CONFIG_FILE_NAMESPACE_URI);
     
    654669                        "\" is mandatory");
    655670            }
     671            // sanity check
     672            try {
     673                int num = Integer.parseInt(port);
     674                if ((num < 1) && (num > 65535)) {
     675                    throw new SRUConfigException("parameter \"" + SRU_PORT +
     676                            "\" must be between 1 and 65535");
     677                }
     678            } catch (NumberFormatException e) {
     679                throw new SRUConfigException("parameter \"" + SRU_PORT +
     680                        "\" must be nummerical");
     681            }
    656682
    657683            String database = params.get(SRU_DATABASE);
     
    661687            }
    662688
     689            // cleanup: remove leading slashed
     690            while (database.startsWith("/")) {
     691                database = database.substring(1);
     692            }
     693
    663694            String s;
    664695            boolean echoRequests = false;
     
    667698            }
    668699
    669             return new SRUEndpointConfig(transport, host, port, database,
     700            return new SRUServerConfig(transport, host, port, database,
    670701                    echoRequests, databaseInfo, indexInfo,
    671702                    schemaInfo);
     703        } catch (IOException e) {
     704            throw new SRUConfigException("error reading configuration file", e);
     705        } catch (XPathException e) {
     706            throw new SRUConfigException("error parsing configuration file", e);
     707        } catch (ParserConfigurationException e) {
     708            throw new SRUConfigException("error parsing configuration file", e);
     709        } catch (SAXException e) {
     710            throw new SRUConfigException("error parsing configuration file", e);
    672711        } catch (SRUConfigException e) {
    673712            throw e;
    674         } catch (Exception e) {
    675             throw new SRUConfigException("error parsing config", e);
    676713        }
    677714    }
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUXMLStreamWriter.java

    r1881 r1889  
    4646            XMLStreamException {
    4747        this.writer = new OutputStreamWriter(stream,
    48                 SRUService.RESPONSE_ENCODING) {
     48                SRUServer.RESPONSE_ENCODING) {
    4949            @Override
    5050            public void write(int c) throws IOException {
Note: See TracChangeset for help on using the changeset viewer.