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

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