Changeset 1889
- Timestamp:
- 04/17/12 13:06:10 (12 years ago)
- Location:
- SRUServer/trunk
- Files:
-
- 4 edited
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
SRUServer/trunk/README.txt
r1881 r1889 1 1 ABSTRACT: 2 2 --------- 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). 3 This package implements the server-side part of the SRU/CQL protocol (SRU/S) 4 and conforms to SRU versin 1.1 and 1.2. The library will handle most of the 5 protocol related tasks for you and you'll only need to implement a few classes 6 to connect you search engine. However, the library will not save you from 7 doing your SRU/CQL homework (i.e. you'll need to have at least some 8 understanding of the protocol and adhere to the protocol sementics). 8 9 9 10 More Information about SRU/CQL: … … 15 16 HOW TO USE: 16 17 ----------- 17 You'll need to provide your own Servlet to initialize the SRUServ iceclass18 with a SRU EndpointConfig instance and dispatch the appropriate HTTP requets19 to it. The SRU EndpointConfig requires an XML document which contains the20 end-point configuration. It must conform to the " endpoint-config.xsd" schema18 You'll need to provide your own Servlet to initialize the SRUServer class 19 with a SRUServerConfig instance and dispatch the appropriate HTTP requets 20 to it. The SRUServerConfig requires an XML document which contains the 21 end-point configuration. It must conform to the "sru-server-config.xsd" schema 21 22 in the "src/main/resources/META-INF" directory. 22 23 Furthermore, you need to provide an implementation of the SRUSearchEngine -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRURequestImpl.java
r1881 r1889 194 194 195 195 196 boolean checkParameters(SRU EndpointConfig config) {196 boolean checkParameters(SRUServerConfig config) { 197 197 // parse mandatory operation parameter 198 198 final String op = getParameter(PARAM_OPERATION, false); -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUSearchEngine.java
r1881 r1889 21 21 * search engine. 22 22 * <p>Implementing the 23 * {@link #explain(SRU EndpointConfig, SRURequest, SRUDiagnosticList)} and24 * {@link #scan(SRU EndpointConfig, SRURequest, SRUDiagnosticList)} is optional,23 * {@link #explain(SRUServerConfig, SRURequest, SRUDiagnosticList)} and 24 * {@link #scan(SRUServerConfig, SRURequest, SRUDiagnosticList)} is optional, 25 25 * but implementing 26 * {@link #search(SRU EndpointConfig, SRURequest, SRUDiagnosticList)} is26 * {@link #search(SRUServerConfig, SRURequest, SRUDiagnosticList)} is 27 27 * mandatory.</p> 28 28 * <p>The implementation of these methods is required to be thread-safe.</p> … … 51 51 * @see SRUExplainResult 52 52 */ 53 public SRUExplainResult explain(SRU EndpointConfig config,53 public SRUExplainResult explain(SRUServerConfig config, 54 54 SRURequest request, SRUDiagnosticList diagnostics) 55 55 throws SRUException; … … 75 75 * @see SRUExplainResult 76 76 */ 77 public SRUSearchResultSet search(SRU EndpointConfig config,77 public SRUSearchResultSet search(SRUServerConfig config, 78 78 SRURequest request, SRUDiagnosticList diagnostics) 79 79 throws SRUException; … … 97 97 * @see SRUExplainResult 98 98 */ 99 public SRUScanResultSet scan(SRU EndpointConfig config, SRURequest request,99 public SRUScanResultSet scan(SRUServerConfig config, SRURequest request, 100 100 SRUDiagnosticList diagnostics) throws SRUException; 101 101 -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServer.java
r1882 r1889 33 33 import org.z3950.zing.cql.CQLNode; 34 34 35 import eu.clarin.sru.server.SRU EndpointConfig.DatabaseInfo;36 import eu.clarin.sru.server.SRU EndpointConfig.IndexInfo;37 import eu.clarin.sru.server.SRU EndpointConfig.LocalizedString;38 import eu.clarin.sru.server.SRU EndpointConfig.SchemaInfo;35 import eu.clarin.sru.server.SRUServerConfig.DatabaseInfo; 36 import eu.clarin.sru.server.SRUServerConfig.IndexInfo; 37 import eu.clarin.sru.server.SRUServerConfig.LocalizedString; 38 import eu.clarin.sru.server.SRUServerConfig.SchemaInfo; 39 39 40 40 41 41 /** 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> 45 47 * <pre> 46 48 * public class MySRUServlet extends HttpServlet { 47 * private transient SRUService service; 49 * private transient SRUServer sruServer; 50 * 48 51 * 49 52 * public void init() throws ServletException { 50 53 * final ServletContext ctx = getServletContext(); 51 54 * try { 52 * URL url = MySRUServlet.class.getClassLoader().getResource("META-INF/endpoint-config.xml"); 55 * URL url = MySRUServlet.class.getClassLoader().getResource( 56 * "META-INF/endpoint-config.xml"); 53 57 * if (url == null) { 54 * throw new ServletException( "not found, url == null");58 * throw new ServletException("not found, url == null"); 55 59 * } 56 60 * 57 * // get additional parametersfrom Servlet context61 * // get additional runtime configuration from Servlet context 58 62 * HashMap<String, String> params = new HashMap<String, String>(); 59 * for (Enumeration<?> i = ctx.getInitParameterNames(); 60 * i.hasMoreElements();) {61 * String key 63 * for (Enumeration<?> i = ctx.getInitParameterNames(); i 64 * .hasMoreElements();) { 65 * String key = (String) i.nextElement(); 62 66 * String value = ctx.getInitParameter(key); 63 * if ((value != null) & &!value.isEmpty()) {67 * if ((value != null) && !value.isEmpty()) { 64 68 * params.put(key, value); 65 69 * } 66 70 * } 67 71 * 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); 73 76 * } catch (Exception e) { 74 * throw new ServletException( "error initializing endpoint", e);77 * throw new ServletException("error initializing endpoint", e); 75 78 * } 76 79 * } 77 80 * 81 * 78 82 * protected void doGet(HttpServletRequest request, 79 83 * HttpServletResponse response) throws ServletException, IOException { 80 * s ervice.handleRequest(request, response);84 * sruServer.handleRequest(request, response); 81 85 * } 86 * 82 87 * 83 88 * protected void doPost(HttpServletRequest request, 84 89 * HttpServletResponse response) throws ServletException, IOException { 85 * s ervice.handleRequest(request, response);90 * sruServer.handleRequest(request, response); 86 91 * } 87 92 * } 88 93 * </pre> 89 94 * 90 * @see SRU EndpointConfig95 * @see SRUServerConfig 91 96 * @see SRUSearchEngine 92 97 * @see <a href="http://www.loc.gov/standards/sru/">SRU/CQL protocol 1.2</a> 93 98 */ 94 public class SRUServ ice{99 public class SRUServer { 95 100 private static final String SRU_NS = 96 101 "http://www.loc.gov/zing/srw/"; … … 110 115 private static final int RESPONSE_BUFFER_SIZE = 64 * 1024; 111 116 private static final Logger logger = 112 LoggerFactory.getLogger(SRUServ ice.class);113 private final SRU EndpointConfig config;117 LoggerFactory.getLogger(SRUServer.class); 118 private final SRUServerConfig config; 114 119 private final SRUSearchEngine searchEngine; 115 120 private final XMLOutputFactory writerFactory; … … 128 133 * if an error occurred 129 134 */ 130 public SRUServ ice(SRUEndpointConfig config, SRUSearchEngine searchEngine)135 public SRUServer(SRUServerConfig config, SRUSearchEngine searchEngine) 131 136 throws SRUException { 132 137 if (config == null) { … … 343 348 if (schemaInfo != null) { 344 349 out.writeStartElement(SRU_EXPLAIN_NS, "schemaInfo"); 345 for (SRU EndpointConfig.SchemaInfo schema : schemaInfo) {350 for (SRUServerConfig.SchemaInfo schema : schemaInfo) { 346 351 out.writeStartElement(SRU_EXPLAIN_NS, "schema"); 347 352 out.writeAttribute("identifier", schema.getIdentifier()); -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServerConfig.java
r1883 r1889 17 17 package eu.clarin.sru.server; 18 18 19 import java.io.I nputStream;19 import java.io.IOException; 20 20 import java.net.URL; 21 21 import java.util.ArrayList; … … 30 30 import javax.xml.parsers.DocumentBuilder; 31 31 import javax.xml.parsers.DocumentBuilderFactory; 32 import javax.xml.parsers.ParserConfigurationException; 32 33 import javax.xml.transform.Source; 33 34 import javax.xml.transform.dom.DOMSource; … … 37 38 import javax.xml.xpath.XPath; 38 39 import javax.xml.xpath.XPathConstants; 40 import javax.xml.xpath.XPathException; 39 41 import javax.xml.xpath.XPathExpression; 40 42 import javax.xml.xpath.XPathExpressionException; … … 52 54 53 55 /** 54 * Endpoint configuration. Most of the endpoint configuration is created from 55 * the XML file. 56 * SRU server configuration. 56 57 * 57 * <p>Example:</p> 58 * <p> 59 * Example: 60 * </p> 58 61 * <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"); 60 64 * if (url == null) { 61 * throw new ServletException( "not found, url == null");65 * throw new ServletException("not found, url == null"); 62 66 * } 63 67 * 68 * // other runtime configuration, usually obtained from servlet context 64 69 * HashMap<String, String> params = new HashMap<String, String>(); 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); 66 76 * </pre> 67 77 * 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 70 81 * <code>http://www.clarin.eu/sru-server/1.0/</code> XML namespace. 71 * </p> 82 * </p> 72 83 */ 73 public final class SRU EndpointConfig {84 public final class SRUServerConfig { 74 85 public static final String SRU_TRANSPORT = "sru.transport"; 75 86 public static final String SRU_HOST = "sru.host"; … … 79 90 private static final String CONFIG_FILE_NAMESPACE_URI = 80 91 "http://www.clarin.eu/sru-server/1.0/"; 92 private static final String CONFIG_FILE_SCHEMA_URL = 93 "META-INF/sru-server-config.xsd"; 81 94 82 95 public static final class LocalizedString { … … 413 426 414 427 415 private SRU EndpointConfig(String transport, String host, String port,428 private SRUServerConfig(String transport, String host, String port, 416 429 String database, boolean echoRequests, DatabaseInfo databaseinfo, 417 430 IndexInfo indexInfo, List<SchemaInfo> schemaInfo) { … … 524 537 525 538 /** 526 * Parse the XML endpoint configuration. 539 * Parse a SRU server XML configuration file and create an configuration 540 * object from it. 527 541 * 528 542 * @param params 529 543 * additional settings 530 * @param in531 * an {@link InputSource}to the XML configuration file544 * @param configFile 545 * an {@link URL} pointing to the XML configuration file 532 546 * @return a initialized <code>SRUEndpointConfig</code> instance 533 547 * @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> 535 550 * @throws SRUConfigException 536 * if an error occur ed551 * if an error occurred 537 552 */ 538 public static SRU EndpointConfig parse(Map<String, String> params,539 InputStream in) throws SRUConfigException {553 public static SRUServerConfig parse(Map<String, String> params, 554 URL configFile) throws SRUConfigException { 540 555 if (params == null) { 541 556 throw new NullPointerException("params == null"); 542 557 } 543 if ( in== null) {558 if (configFile == null) { 544 559 throw new NullPointerException("in == null"); 545 560 } 546 561 try { 547 URL url = SRU EndpointConfig.class.getClassLoader()548 .getResource( "META-INF/endpoint-config.xsd");562 URL url = SRUServerConfig.class.getClassLoader() 563 .getResource(CONFIG_FILE_SCHEMA_URL); 549 564 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 + "\""); 552 567 } 553 568 SchemaFactory sfactory = … … 564 579 // parse input 565 580 DocumentBuilder builder = factory.newDocumentBuilder(); 566 InputSource input = new InputSource( in);581 InputSource input = new InputSource(configFile.openStream()); 567 582 input.setPublicId(CONFIG_FILE_NAMESPACE_URI); 568 583 input.setSystemId(CONFIG_FILE_NAMESPACE_URI); … … 654 669 "\" is mandatory"); 655 670 } 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 } 656 682 657 683 String database = params.get(SRU_DATABASE); … … 661 687 } 662 688 689 // cleanup: remove leading slashed 690 while (database.startsWith("/")) { 691 database = database.substring(1); 692 } 693 663 694 String s; 664 695 boolean echoRequests = false; … … 667 698 } 668 699 669 return new SRU EndpointConfig(transport, host, port, database,700 return new SRUServerConfig(transport, host, port, database, 670 701 echoRequests, databaseInfo, indexInfo, 671 702 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); 672 711 } catch (SRUConfigException e) { 673 712 throw e; 674 } catch (Exception e) {675 throw new SRUConfigException("error parsing config", e);676 713 } 677 714 } -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUXMLStreamWriter.java
r1881 r1889 46 46 XMLStreamException { 47 47 this.writer = new OutputStreamWriter(stream, 48 SRUServ ice.RESPONSE_ENCODING) {48 SRUServer.RESPONSE_ENCODING) { 49 49 @Override 50 50 public void write(int c) throws IOException {
Note: See TracChangeset
for help on using the changeset viewer.