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 | */ |
---|
17 | package eu.clarin.sru.client; |
---|
18 | |
---|
19 | import java.util.List; |
---|
20 | |
---|
21 | import org.slf4j.Logger; |
---|
22 | import org.slf4j.LoggerFactory; |
---|
23 | |
---|
24 | import eu.clarin.sru.fcs.ClarinFederatedContentSearchRecordData; |
---|
25 | import eu.clarin.sru.fcs.ClarinFederatedContentSearchRecordParser; |
---|
26 | |
---|
27 | |
---|
28 | public class TestClient { |
---|
29 | private static final Logger logger = |
---|
30 | LoggerFactory.getLogger(TestClient.class); |
---|
31 | |
---|
32 | public static void main(String[] args) { |
---|
33 | if (args.length > 0) { |
---|
34 | logger.info("initializing client ..."); |
---|
35 | SRUClient client = new SRUSimpleClient(SRUVersion.VERSION_1_2); |
---|
36 | try { |
---|
37 | client.registerRecordParser(new ClarinFederatedContentSearchRecordParser()); |
---|
38 | } catch (SRUClientException e) { |
---|
39 | logger.error("error adding record parser", e); |
---|
40 | System.exit(1); |
---|
41 | } |
---|
42 | |
---|
43 | /* |
---|
44 | * just use one dump handler for each request. |
---|
45 | * A real application should be smarter here and use the |
---|
46 | * appropriate handler |
---|
47 | */ |
---|
48 | SRUDefaultHandlerAdapter handler = new SRUDefaultHandlerAdapter() { |
---|
49 | @Override |
---|
50 | public void onDiagnostics(List<SRUDiagnostic> diagnostics) |
---|
51 | throws SRUClientException { |
---|
52 | for (SRUDiagnostic diagnostic : diagnostics) { |
---|
53 | logger.info("onDiagnostics: uri={}, detail={}, message={}", |
---|
54 | new Object[] { diagnostic.getURI(), |
---|
55 | diagnostic.getDetails(), |
---|
56 | diagnostic.getMessage() }); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public void onRequestStatistics(int bytes, long millisTotal, |
---|
62 | long millisNetwork, long millisParsing) { |
---|
63 | logger.info("onRequestStatistics(): {} bytes in {} millis", |
---|
64 | bytes, millisTotal); |
---|
65 | } |
---|
66 | |
---|
67 | @Override |
---|
68 | public void onStartTerms() throws SRUClientException { |
---|
69 | logger.info("onStartTerms()"); |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | public void onFinishTerms() throws SRUClientException { |
---|
74 | logger.info("onFinishTerms()"); |
---|
75 | } |
---|
76 | |
---|
77 | @Override |
---|
78 | public void onTerm(String value, int numberOfRecords, |
---|
79 | String displayTerm, WhereInList whereInList) |
---|
80 | throws SRUClientException { |
---|
81 | logger.info("onTerm(): value = {}, numberOfRecords = {}, displayTerm = {}, whereInList = {}", |
---|
82 | new Object[] { value, numberOfRecords, displayTerm, |
---|
83 | whereInList }); |
---|
84 | } |
---|
85 | |
---|
86 | @Override |
---|
87 | public void onStartRecords(int numberOfRecords, |
---|
88 | int resultSetId, int resultSetIdleTime) |
---|
89 | throws SRUClientException { |
---|
90 | logger.info("onStartRecords(): numberOfRecords = {}", |
---|
91 | numberOfRecords); |
---|
92 | } |
---|
93 | |
---|
94 | @Override |
---|
95 | public void onFinishRecords(int nextRecordPosition) |
---|
96 | throws SRUClientException { |
---|
97 | logger.info("onFinishRecords(): nextRecordPosition = {}", |
---|
98 | nextRecordPosition); |
---|
99 | } |
---|
100 | |
---|
101 | @Override |
---|
102 | public void onRecord(String identifier, int position, |
---|
103 | SRURecordData data) throws SRUClientException { |
---|
104 | logger.info("onRecord(): identifier = {}, position = {}, schema = {}", |
---|
105 | new Object[] { identifier, position, |
---|
106 | data.getRecordSchema() }); |
---|
107 | if (ClarinFederatedContentSearchRecordParser.FCS_NS |
---|
108 | .equals(data.getRecordSchema())) { |
---|
109 | ClarinFederatedContentSearchRecordData record = (ClarinFederatedContentSearchRecordData) data; |
---|
110 | logger.info("CLARIN-FCS: \"{}\"/\"{}\"/\"{}\"", |
---|
111 | new Object[] { record.getLeft(), |
---|
112 | record.getKeyword(), record.getRight() }); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | @Override |
---|
117 | public void onSurrogateRecord(String identifier, int position, |
---|
118 | SRUDiagnostic data) throws SRUClientException { |
---|
119 | logger.info("onSurrogateRecord: identifier = {}, position = {}, uri={}, detail={}, message={}", |
---|
120 | new Object[] { identifier, position, data.getURI(), |
---|
121 | data.getDetails(), data.getMessage() }); |
---|
122 | } |
---|
123 | }; |
---|
124 | |
---|
125 | try { |
---|
126 | logger.info("performing 'explain' request ..."); |
---|
127 | SRUExplainRequest request = new SRUExplainRequest(args[0]); |
---|
128 | client.explain(request, handler); |
---|
129 | } catch (SRUClientException e) { |
---|
130 | logger.error("a fatal error occured while performing 'explain' request", e); |
---|
131 | } |
---|
132 | |
---|
133 | try { |
---|
134 | logger.info("performing 'scan' request ..."); |
---|
135 | SRUScanRequest request = new SRUScanRequest(args[0]); |
---|
136 | request.setScanClause("cmd.collections"); |
---|
137 | request.setMaximumTerms(2); |
---|
138 | // request.setExtraRequestData( |
---|
139 | // SRUAbstractRequest.X_MALFORMED_OPERATION, |
---|
140 | // SRUAbstractRequest.MALFORMED_OMIT); |
---|
141 | // request.setExtraRequestData( |
---|
142 | // SRUAbstractRequest.X_MALFORMED_VERSION, |
---|
143 | // SRUAbstractRequest.MALFORMED_OMIT); |
---|
144 | client.scan(request, handler); |
---|
145 | } catch (SRUClientException e) { |
---|
146 | logger.error("a fatal error occured while performing 'scan' request", e); |
---|
147 | } |
---|
148 | |
---|
149 | try { |
---|
150 | logger.info("performing 'searchRetrieve' request ..."); |
---|
151 | SRUSearchRetrieveRequest request = |
---|
152 | new SRUSearchRetrieveRequest(args[0]); |
---|
153 | request.setQuery("Faustus"); |
---|
154 | request.setRecordSchema(ClarinFederatedContentSearchRecordParser.FCS_RECORD_SCHEMA); |
---|
155 | request.setMaximumRecords(5); |
---|
156 | request.setRecordPacking(SRURecordPacking.XML); |
---|
157 | request.setExtraRequestData("x-indent-response", "4"); |
---|
158 | // request.setExtraRequestData( |
---|
159 | // SRUAbstractRequest.X_MALFORMED_OPERATION, |
---|
160 | // "invalid"); |
---|
161 | // request.setExtraRequestData( |
---|
162 | // SRUAbstractRequest.X_MALFORMED_VERSION, |
---|
163 | // SRUAbstractRequest.MALFORMED_OMIT); |
---|
164 | client.searchRetrieve(request, handler); |
---|
165 | } catch (SRUClientException e) { |
---|
166 | logger.error("a fatal error occured while performing 'searchRetrieve' request", e); |
---|
167 | } |
---|
168 | } else { |
---|
169 | System.err.println("missing args"); |
---|
170 | System.exit(64); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | static { |
---|
175 | org.apache.log4j.BasicConfigurator.configure( |
---|
176 | new org.apache.log4j.ConsoleAppender( |
---|
177 | new org.apache.log4j.PatternLayout("%-5p [%t] %m%n"), |
---|
178 | org.apache.log4j.ConsoleAppender.SYSTEM_ERR)); |
---|
179 | org.apache.log4j.Logger logger = |
---|
180 | org.apache.log4j.Logger.getRootLogger(); |
---|
181 | logger.setLevel(org.apache.log4j.Level.INFO); |
---|
182 | logger.getLoggerRepository() |
---|
183 | .getLogger("eu.clarin").setLevel(org.apache.log4j.Level.DEBUG); |
---|
184 | } |
---|
185 | } // class TestClient |
---|