source: SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSearchRetrieveRequest.java @ 2121

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