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

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