source: SRUClient/tags/SRUClient-0.9.3/src/main/java/eu/clarin/sru/client/SRUSearchRetrieveRequest.java @ 5751

Last change on this file since 5751 was 5751, checked in by Oliver Schonefeld, 10 years ago
  • tag version 0.9.3
  • Property svn:eol-style set to native
File size: 11.3 KB
Line 
1/**
2 * This software is copyright (c) 2012-2014 by
3 *  - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
4 * This is free software. You can redistribute it
5 * and/or modify it under the terms described in
6 * the GNU General Public License v3 of which you
7 * should have received a copy. Otherwise you can download
8 * it from
9 *
10 *   http://www.gnu.org/licenses/gpl-3.0.txt
11 *
12 * @copyright Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
13 *
14 * @license http://www.gnu.org/licenses/gpl-3.0.txt
15 *  GNU General Public License v3
16 */
17package eu.clarin.sru.client;
18
19import java.net.URI;
20
21/**
22 * An object for performing a <em>explain</em> operation.
23 * <p>
24 * The following argument arguments are mandatory:
25 * </p>
26 * <ul>
27 * <li><em>query</em></li>
28 * </ul>
29 *
30 * @see SRUSearchRetrieveHandler
31 * @see <a href="http://www.loc.gov/standards/sru/specs/search-retrieve.html">
32 *      SRU SearchRetrieve Operation</a>
33 */
34public class SRUSearchRetrieveRequest extends SRUAbstractRequest {
35    /** for end-point conformance testing only. never use in production. */
36    public static final String X_MALFORMED_QUERY =
37            "x-malformed-query";
38    /** for end-point conformance testing only. never use in production. */
39    public static final String X_MALFORMED_START_RECORD =
40            "x-malformed-startRecord";
41    /** for end-point conformance testing only. never use in production. */
42    public static final String X_MALFORMED_MAXIMUM_RECORDS =
43            "x-malformed-maximumRecords";
44    /** for end-point conformance testing only. never use in production. */
45    public static final String X_MALFORMED_RECORD_PACKING =
46            "x-malformed-recordPacking";
47    private String query;
48    private int startRecord = -1;
49    private int maximumRecords = -1;
50    private SRURecordPacking recordPacking;
51    private String recordSchema;
52    private int resultSetTTL = -1;
53
54
55    /**
56     * Constructor.
57     *
58     * @param baseURI
59     *            the baseURI of the endpoint
60     */
61    public SRUSearchRetrieveRequest(URI baseURI) {
62        super(baseURI);
63    }
64
65
66    /**
67     * Constructor.
68     *
69     * @param baseURI
70     *            the baseURI of the endpoint
71     */
72    public SRUSearchRetrieveRequest(String baseURI) {
73        super(baseURI);
74    }
75
76
77    /**
78     * Get the value of the <em>query</em> argument for this request.
79     *
80     * @return the value for the <em>query</em> argument or <code>null</code> of
81     *         none was set
82     */
83    public String getQuery() {
84        return query;
85    }
86
87
88    /**
89     * Set the value of the <em>query</em> argument for this request.
90     *
91     * @param query
92     *            the value for the <em>query</em> argument
93     * @throws NullPointerException
94     *             if any required argument is <code>null</code>
95     * @throws IllegalArgumentException
96     *             if any argument is invalid
97     */
98    public void setQuery(String query) {
99        if (query == null) {
100            throw new NullPointerException("query == null");
101        }
102        if (query.isEmpty()) {
103            throw new IllegalArgumentException("query is an empty string");
104        }
105        this.query = query;
106    }
107
108
109    /**
110     * Get the value of the <em>startRecord</em> argument for this request.
111     *
112     * @return the value for the <em>startRecord</em> argument or
113     *         <code>-1</code> of none was set
114     */
115    public int getStartRecord() {
116        return startRecord;
117    }
118
119
120    /**
121     * Set the value of the <em>startRecord</em> argument for this request.
122     *
123     * @param startRecord
124     *            the value for the <em>startRecord</em> argument
125     * @throws IllegalArgumentException
126     *             if any argument is invalid
127     */
128    public void setStartRecord(int startRecord) {
129        if (startRecord < 1) {
130            throw new IllegalArgumentException("startRecord < 1");
131        }
132        this.startRecord = startRecord;
133    }
134
135
136    /**
137     * Get the value of the <em>maximumRecords</em> argument for this request.
138     *
139     * @return the value for the <em>maximumRecords</em> argument or
140     *         <code>-1</code> of none was set
141     */
142    public int getMaximumRecords() {
143        return maximumRecords;
144    }
145
146
147    /**
148     * Set the value of the <em>maximumRecords</em> argument for this request.
149     *
150     * @param maximumRecords
151     *            the value for the <em>maximumRecords</em> argument
152     * @throws IllegalArgumentException
153     *             if any argument is invalid
154     */
155    public void setMaximumRecords(int maximumRecords) {
156        if (maximumRecords < 0) {
157            throw new IllegalArgumentException("maximumRecords < 0");
158        }
159        this.maximumRecords = maximumRecords;
160    }
161
162
163    /**
164     * Get the value of the <em>recordSchema</em> argument for this request.
165     *
166     * @return the value for the <em>recordSchema</em> argument or
167     *         <code>null</code> of none was set
168     */
169    public String getRecordSchema() {
170        return recordSchema;
171    }
172
173
174    /**
175     * Set the value of the <em>recordSchema</em> argument for this request.
176     *
177     * @param recordSchema
178     *            the value for the <em>recordSchema</em> argument
179     * @throws NullPointerException
180     *             if any required argument is <code>null</code>
181     * @throws IllegalArgumentException
182     *             if any argument is invalid
183     */
184    public void setRecordSchema(String recordSchema) {
185        this.recordSchema = recordSchema;
186    }
187
188
189    /**
190     * Get the value of the <em>recordSchema</em> argument for this request.
191     *
192     * @return the value for the <em>recordSchema</em> argument or
193     *         <code>null</code> of none was set
194     */
195    public SRURecordPacking getRecordPacking() {
196        return recordPacking;
197    }
198
199
200    /**
201     * Set the value of the <em>recordPacking</em> argument for this request.
202     *
203     * @param recordPacking
204     *            the value for the <em>recordPacking</em> argument
205     * @throws NullPointerException
206     *             if any required argument is <code>null</code>
207     */
208    public void setRecordPacking(SRURecordPacking recordPacking) {
209        if (recordPacking == null) {
210            throw new NullPointerException("recordPacking == null");
211        }
212        this.recordPacking = recordPacking;
213    }
214
215
216    /**
217     * Get the value of the <em>resultSetTTL</em> argument for this request.
218     *
219     * @return the value for the <em>resultSetTTL</em> argument or
220     *         <code>-1</code> of none was set
221     */
222    public int getResultSetTTL() {
223        return resultSetTTL;
224    }
225
226
227    /**
228     * Set the value of the <em>resultSetTTL</em> argument for this request.
229     *
230     * @param resultSetTTL
231     *            the value for the <em>resultSetTTL</em> argument
232     * @throws IllegalArgumentException
233     *             if any argument is invalid
234     */
235    public void setResultSetTTL(int resultSetTTL) {
236        this.resultSetTTL = resultSetTTL;
237    }
238
239
240    @Override
241    SRUOperation getOperation() {
242        return SRUOperation.SEARCH_RETRIEVE;
243    }
244
245
246    @Override
247    void addParametersToURI(URIHelper uriHelper) throws SRUClientException {
248        /*
249         * append query argument (mandatory)
250         *
251         * NB: Setting "x-malformed-query" as an extra request parameter makes
252         * the client to send invalid requests. This is intended to use for
253         * testing endpoints for protocol conformance (i.e. provoke an error)
254         * and SHOULD NEVER be used in production!
255         */
256        final String malformedQuery = getExtraRequestData(X_MALFORMED_QUERY);
257        if (malformedQuery == null) {
258            if ((query == null) || query.isEmpty()) {
259                throw new SRUClientException(
260                        "mandatory argument 'query' not set or empty");
261            }
262            uriHelper.append(PARAM_QUERY, query);
263        } else {
264            if (!malformedQuery.equalsIgnoreCase(MALFORMED_OMIT)) {
265                uriHelper.append(PARAM_QUERY, malformedQuery);
266            }
267        }
268
269        /*
270         * append startRecord argument (optional)
271         *
272         * NB: Setting "x-malformed-startRecord" as an extra request parameter
273         * makes the client to send invalid requests. This is intended to use
274         * for testing endpoints for protocol conformance (i.e. provoke an
275         * error) and SHOULD NEVER be used in production!
276         */
277        final String malformedStartRecord =
278                getExtraRequestData(X_MALFORMED_START_RECORD);
279        if (malformedStartRecord == null) {
280            if (startRecord > 0) {
281                uriHelper.append(PARAM_START_RECORD, startRecord);
282            }
283        } else {
284            if (!malformedStartRecord.equalsIgnoreCase(MALFORMED_OMIT)) {
285                uriHelper.append(PARAM_START_RECORD, malformedStartRecord);
286            }
287        }
288
289        /*
290         * append maximumRecords argument (optional)
291         *
292         * NB: Setting "x-malformed-maximumRecords" as an extra request
293         * parameter makes the client to send invalid requests. This is
294         * intended to use for testing endpoints for protocol conformance
295         * (i.e. provoke an error) and SHOULD NEVER be used in production!
296         */
297        final String malformedMaxiumRecords =
298                getExtraRequestData(X_MALFORMED_MAXIMUM_RECORDS);
299        if (malformedMaxiumRecords == null) {
300            if (maximumRecords > -1) {
301                uriHelper.append(PARAM_MAXIMUM_RECORDS, maximumRecords);
302            }
303        } else {
304            if (!malformedMaxiumRecords.equalsIgnoreCase(MALFORMED_OMIT)) {
305                uriHelper.append(PARAM_MAXIMUM_RECORDS,
306                        malformedMaxiumRecords);
307            }
308        }
309
310        /*
311         * append recordPacking argument (optional)
312         *
313         * NB: Setting "x-malformed-recordPacking" as an extra request
314         * parameter makes the client to send invalid requests. This is
315         * intended to use for testing endpoints for protocol conformance
316         * (i.e. provoke an error) and SHOULD NEVER be used in production!
317         */
318        final String malformedRecordPacking =
319                getExtraRequestData(X_MALFORMED_RECORD_PACKING);
320        if (malformedRecordPacking == null) {
321            if (recordPacking != null) {
322                switch (recordPacking) {
323                case XML:
324                    uriHelper.append(PARAM_RECORD_PACKING, RECORD_PACKING_XML);
325                    break;
326                case STRING:
327                    uriHelper.append(PARAM_RECORD_PACKING,
328                            RECORD_PACKING_STRING);
329                    break;
330                default:
331                    throw new SRUClientException(
332                            "unsupported record packing: " + recordPacking);
333                } // switch
334            }
335        } else {
336            if (!malformedRecordPacking.equalsIgnoreCase(MALFORMED_OMIT)) {
337                uriHelper.append(PARAM_RECORD_PACKING, malformedRecordPacking);
338            }
339        }
340
341        /*
342         * append recordSchema argument (optional)
343         */
344        if (recordSchema != null) {
345            uriHelper.append(PARAM_RECORD_SCHEMA, recordSchema);
346        }
347
348        /*
349         * append resultSetTTL argument (optional)
350         */
351        if (resultSetTTL > -1) {
352            uriHelper.append(PARAM_RESULT_SET_TTL, resultSetTTL);
353        }
354    }
355
356} // class SRUSearchRetrieveRequest
Note: See TracBrowser for help on using the repository browser.