source: FCSSimpleClient/trunk/src/test/java/eu/clarin/sru/client/fcs/TestSimpleClient.java @ 7280

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