source: SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestSimpleClient.java @ 5748

Last change on this file since 5748 was 5748, checked in by Oliver Schonefeld, 10 years ago
  • more work-in-progress
  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1/**
2 * This software is copyright (c) 2011-2013 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.util.List;
20
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import eu.clarin.sru.client.fcs.ClarinFCSClientBuilder;
25import eu.clarin.sru.client.fcs.ClarinFCSRecordData;
26
27
28public class TestSimpleClient {
29    private static final Logger logger =
30            LoggerFactory.getLogger(TestSimpleClient.class);
31
32    public static void main(String[] args) {
33        if (args.length > 0) {
34            logger.info("initializing client ...");
35
36            SRUSimpleClient client = new ClarinFCSClientBuilder()
37                        .addDefaultDataViewParsers()
38                        .unknownDataViewAsString()
39                        .buildSimpleClient();
40
41            /*
42             * just use one dump handler for each request.
43             * A real application should be smarter here and use the
44             * appropriate handler
45             */
46            SRUDefaultHandlerAdapter handler = new SRUDefaultHandlerAdapter() {
47                @Override
48                public void onDiagnostics(List<SRUDiagnostic> diagnostics)
49                        throws SRUClientException {
50                    for (SRUDiagnostic diagnostic : diagnostics) {
51                        logger.info("onDiagnostics: uri={}, detail={}, message={}",
52                                diagnostic.getURI(), diagnostic.getDetails(),
53                                diagnostic.getMessage());
54                    }
55                }
56
57                @Override
58                public void onRequestStatistics(int bytes, long millisTotal,
59                        long millisNetwork, long millisParsing) {
60                    logger.info("onRequestStatistics(): {} bytes in {} millis",
61                            bytes, millisTotal);
62                }
63
64                @Override
65                public void onStartTerms() throws SRUClientException {
66                    logger.info("onStartTerms()");
67                }
68
69                @Override
70                public void onFinishTerms() throws SRUClientException {
71                    logger.info("onFinishTerms()");
72                }
73
74                @Override
75                public void onTerm(String value, int numberOfRecords,
76                        String displayTerm, SRUWhereInList whereInList)
77                        throws SRUClientException {
78                    logger.info("onTerm(): value = {}, numberOfRecords = {}, " +
79                            "displayTerm = {}, whereInList = {}",
80                            value, numberOfRecords, displayTerm, whereInList);
81                }
82
83                @Override
84                public void onStartRecords(int numberOfRecords,
85                        String resultSetId, int resultSetIdleTime)
86                        throws SRUClientException {
87                    logger.info("onStartRecords(): numberOfRecords = {}",
88                            numberOfRecords);
89                }
90
91                @Override
92                public void onFinishRecords(int nextRecordPosition)
93                        throws SRUClientException {
94                    logger.info("onFinishRecords(): nextRecordPosition = {}",
95                            nextRecordPosition);
96                }
97
98                @Override
99                public void onRecord(String identifier, int position,
100                        SRURecordData data) throws SRUClientException {
101                    logger.info("onRecord(): identifier = {}, position = {}, schema = {}",
102                                identifier, position, data.getRecordSchema());
103                    if (ClarinFCSRecordData.RECORD_SCHEMA.equals(data.getRecordSchema())) {
104                        ClarinFCSRecordData record =
105                                (ClarinFCSRecordData) data;
106                        TestUtils.dumpResource(record.getResource());
107                    } else if (SRUExplainRecordData.RECORD_SCHEMA.equals(data.getRecordSchema())) {
108                        TestUtils.dumpExplainRecordData(data);
109                    }
110                }
111
112                @Override
113                public void onSurrogateRecord(String identifier, int position,
114                        SRUDiagnostic data) throws SRUClientException {
115                    logger.info("onSurrogateRecord: identifier = {}, position = {}, uri={}, detail={}, message={}",
116                            identifier, position, data.getURI(), data.getDetails(), data.getMessage());
117                }
118            };
119
120            try {
121                logger.info("performing 'explain' request ...");
122                SRUExplainRequest request = TestUtils.makeExplainRequest(args[0]);
123                client.explain(request, handler);
124            } catch (SRUClientException e) {
125                logger.error("a fatal error occured while performing 'explain' request", e);
126            }
127
128            try {
129                logger.info("performing 'scan' request ...");
130                SRUScanRequest request = TestUtils.makeScanRequest(args[0]);
131                client.scan(request, handler);
132            } catch (SRUClientException e) {
133                logger.error("a fatal error occured while performing 'scan' request", e);
134            }
135
136            try {
137                logger.info("performing 'searchRetrieve' request ...");
138                SRUSearchRetrieveRequest request = TestUtils.makeSearchRequest(args[0], null);
139                client.searchRetrieve(request, handler);
140            } catch (SRUClientException e) {
141                logger.error("a fatal error occured while performing 'searchRetrieve' request", e);
142            }
143
144            logger.info("done");
145        } else {
146            System.err.println("missing args");
147            System.exit(64);
148        }
149    }
150
151
152    static {
153        org.apache.log4j.BasicConfigurator.configure(
154                new org.apache.log4j.ConsoleAppender(
155                        new org.apache.log4j.PatternLayout("%-5p [%t] %m%n"),
156                        org.apache.log4j.ConsoleAppender.SYSTEM_ERR));
157        org.apache.log4j.Logger logger =
158                org.apache.log4j.Logger.getRootLogger();
159        logger.setLevel(org.apache.log4j.Level.INFO);
160        logger.getLoggerRepository().getLogger("eu.clarin").setLevel(
161                org.apache.log4j.Level.DEBUG);
162    }
163
164} // class TestSimpleClient
Note: See TracBrowser for help on using the repository browser.