|
| 1 | +/* Copyright 2013 Pascal Christoph. |
| 2 | + * Licensed under the Eclipse Public License 1.0 */ |
| 3 | + |
| 4 | +package org.metafacture.biblio; |
| 5 | + |
| 6 | +import org.metafacture.framework.FluxCommand; |
| 7 | +import org.metafacture.framework.MetafactureException; |
| 8 | +import org.metafacture.framework.ObjectReceiver; |
| 9 | +import org.metafacture.framework.annotations.Description; |
| 10 | +import org.metafacture.framework.annotations.In; |
| 11 | +import org.metafacture.framework.annotations.Out; |
| 12 | +import org.metafacture.framework.helpers.DefaultObjectPipe; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStream; |
| 16 | +import java.io.InputStreamReader; |
| 17 | +import java.io.Reader; |
| 18 | +import java.net.HttpURLConnection; |
| 19 | +import java.net.URL; |
| 20 | + |
| 21 | +/** |
| 22 | + * Opens an SRU (Search Retrieval by URL) stream and passes a reader to the receiver. |
| 23 | + * The input should be the base URL of the SRU service to be retrieved from. |
| 24 | + * |
| 25 | + * @author Pascal Christoph (dr0i) |
| 26 | + */ |
| 27 | +@Description("Opens a SRU stream and passes a reader to the receiver. The input should be the base URL of the SRU service to be retrieved from. Mandatory argument is: QUERY.") |
| 28 | +@In(String.class) |
| 29 | +@Out(Reader.class) |
| 30 | +@FluxCommand("open-sru") |
| 31 | +public final class SruOpener extends DefaultObjectPipe<String, ObjectReceiver<Reader>> { |
| 32 | + |
| 33 | + private static final String OPERATION = "searchRetrieve"; |
| 34 | + private static final String RECORD_SCHEMA = "MARC21-xml"; |
| 35 | + private static final String USER_AGENT = ""; |
| 36 | + private static final String VERSION = "2.0"; |
| 37 | + |
| 38 | + private static final int CONNECTION_TIMEOUT = 11000; |
| 39 | + private static final int MAXIMUM_RECORDS = 10; |
| 40 | + private static final int START_RECORD = 1; |
| 41 | + private static final int TOTAL = 10; |
| 42 | + |
| 43 | + private String operation = OPERATION; |
| 44 | + private String query; |
| 45 | + private String recordSchema = RECORD_SCHEMA; |
| 46 | + private String userAgent = USER_AGENT; |
| 47 | + private String version = VERSION; |
| 48 | + |
| 49 | + private int maximumRecords = MAXIMUM_RECORDS; |
| 50 | + private int startRecord = START_RECORD; |
| 51 | + private int totalRecords = TOTAL; |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates an instance of {@link SruOpener} |
| 56 | + */ |
| 57 | + public SruOpener() { |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Sets the User Agent to use. <strong>Default value: {@value USER_AGENT}</strong>. |
| 62 | + * |
| 63 | + * @param userAgent a user agent to be used when opening a URL |
| 64 | + */ |
| 65 | + public void setUserAgent(final String userAgent) { |
| 66 | + this.userAgent = userAgent; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Sets the query of the search. |
| 71 | + * <strong>Setting a query is mandatory.</strong> |
| 72 | + * |
| 73 | + * @param query the query |
| 74 | + */ |
| 75 | + |
| 76 | + public void setQuery(final String query) { |
| 77 | + this.query = query; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets total number of records to be retrieved. <strong>Default value: {@value TOTAL}</strong>. |
| 82 | + * |
| 83 | + * @param totalRecords total number of records to be retrieved |
| 84 | + */ |
| 85 | + public void setTotal(final String totalRecords) { |
| 86 | + this.totalRecords = Integer.getInteger(totalRecords); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Sets the maximum of records returned in one lookup. <strong>Default value: {@value MAXIMUM_RECORDS}</strong>. |
| 91 | + * The lookup is repeated as long as {@link #maximumRecords} is lesser than {@link #totalRecords}. |
| 92 | + * |
| 93 | + * @param maximumRecords maximum of records returned in one lookup |
| 94 | + */ |
| 95 | + public void setMaximumRecords(final String maximumRecords) { |
| 96 | + this.maximumRecords = Integer.getInteger(maximumRecords); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Sets where to start when retrieving records. <strong>Default value: {@value START_RECORD}</strong>. |
| 101 | + * |
| 102 | + * @param startRecord where to start when retrieving records |
| 103 | + */ |
| 104 | + public void setStartRecord(final String startRecord) { |
| 105 | + this.startRecord = Integer.getInteger(startRecord); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the format of the retrieved record data. <strong>Default value: {@value RECORD_SCHEMA}</strong>. |
| 110 | + * |
| 111 | + * @param recordSchema the format of the data of the records |
| 112 | + */ |
| 113 | + public void setRecordSchema(final String recordSchema) { |
| 114 | + this.recordSchema = recordSchema; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Sets the kind of operation of the lookup. <strong>Default value: {@value OPERATION}</strong>. |
| 119 | + * |
| 120 | + * @param operation the kind of operation of the lookup |
| 121 | + */ |
| 122 | + public void setOperation(final String operation) { |
| 123 | + this.operation = operation; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Sets the version of the lookup. <strong>Default value: {@value VERSION}</strong>. |
| 128 | + * |
| 129 | + * @param version the version of the lookup |
| 130 | + */ |
| 131 | + public void setVersion(final String version) { |
| 132 | + this.version = version; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void process(final String baseUrl) { |
| 137 | + |
| 138 | + try { |
| 139 | + |
| 140 | + StringBuilder srUrl = new StringBuilder(baseUrl); |
| 141 | + if (query != null) { |
| 142 | + srUrl.append("?query=").append(query).append("&operation=").append(operation).append("&recordSchema=").append(recordSchema).append("&version=").append(version).append("&maximumRecords=").append(maximumRecords).append("&total=").append(totalRecords); |
| 143 | + } |
| 144 | + else { |
| 145 | + throw new IllegalArgumentException("Missing mandatory parameter 'query'"); |
| 146 | + } |
| 147 | + int retrievedRecords = 0; |
| 148 | + while (retrievedRecords < totalRecords) { |
| 149 | + startRecord = retrievedRecords + 1; |
| 150 | + retrieve(srUrl, startRecord); |
| 151 | + retrievedRecords = retrievedRecords + maximumRecords; |
| 152 | + } |
| 153 | + } |
| 154 | + catch (final IOException e) { |
| 155 | + throw new MetafactureException(e); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private void retrieve(StringBuilder srUrl, int startRecord) throws IOException { |
| 160 | + srUrl.append("&startRecord=").append(startRecord); |
| 161 | + final URL urlToOpen = new URL(srUrl.toString()); |
| 162 | + final HttpURLConnection connection = (HttpURLConnection) urlToOpen.openConnection(); |
| 163 | + |
| 164 | + connection.setConnectTimeout(CONNECTION_TIMEOUT); |
| 165 | + if (!userAgent.isEmpty()) { |
| 166 | + connection.setRequestProperty("User-Agent", userAgent); |
| 167 | + } |
| 168 | + |
| 169 | + try ( |
| 170 | + InputStreamReader inputStreamReader = new InputStreamReader(getInputStream(connection)) |
| 171 | + ) { |
| 172 | + getReceiver().process(inputStreamReader); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private InputStream getInputStream(final HttpURLConnection connection) { |
| 177 | + try { |
| 178 | + return connection.getInputStream(); |
| 179 | + } |
| 180 | + catch (final IOException e) { |
| 181 | + return connection.getErrorStream(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments