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
RevLine 
[2088]1/**
[7274]2 * This software is copyright (c) 2012-2022 by
3 *  - Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
[2088]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 *
[7274]12 * @copyright Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
[2088]13 *
14 * @license http://www.gnu.org/licenses/gpl-3.0.txt
15 *  GNU General Public License v3
16 */
[6942]17package eu.clarin.sru.client.fcs;
[1963]18
[1969]19import java.util.List;
20
[1963]21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
[6942]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;
[1963]34
[1969]35
[2165]36public class TestSimpleClient {
[1963]37    private static final Logger logger =
[2165]38            LoggerFactory.getLogger(TestSimpleClient.class);
[1963]39
40    public static void main(String[] args) {
41        if (args.length > 0) {
[1981]42            logger.info("initializing client ...");
[1963]43
[5748]44            SRUSimpleClient client = new ClarinFCSClientBuilder()
45                        .addDefaultDataViewParsers()
46                        .unknownDataViewAsString()
[5743]47                        .buildSimpleClient();
[2936]48
[2088]49            /*
50             * just use one dump handler for each request.
51             * A real application should be smarter here and use the
[2196]52             * appropriate handler
[2088]53             */
[1981]54            SRUDefaultHandlerAdapter handler = new SRUDefaultHandlerAdapter() {
55                @Override
[2021]56                public void onDiagnostics(List<SRUDiagnostic> diagnostics)
[1981]57                        throws SRUClientException {
58                    for (SRUDiagnostic diagnostic : diagnostics) {
[2021]59                        logger.info("onDiagnostics: uri={}, detail={}, message={}",
[2938]60                                diagnostic.getURI(), diagnostic.getDetails(),
61                                diagnostic.getMessage());
[1969]62                    }
[1981]63                }
[1969]64
[1981]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                }
[1972]71
[1981]72                @Override
73                public void onStartTerms() throws SRUClientException {
74                    logger.info("onStartTerms()");
75                }
[1963]76
[1981]77                @Override
78                public void onFinishTerms() throws SRUClientException {
79                    logger.info("onFinishTerms()");
80                }
[1963]81
[1981]82                @Override
83                public void onTerm(String value, int numberOfRecords,
[2163]84                        String displayTerm, SRUWhereInList whereInList)
[1981]85                        throws SRUClientException {
[2938]86                    logger.info("onTerm(): value = {}, numberOfRecords = {}, " +
87                            "displayTerm = {}, whereInList = {}",
88                            value, numberOfRecords, displayTerm, whereInList);
[1981]89                }
[1963]90
[1981]91                @Override
92                public void onStartRecords(int numberOfRecords,
[2156]93                        String resultSetId, int resultSetIdleTime)
[1981]94                        throws SRUClientException {
95                    logger.info("onStartRecords(): numberOfRecords = {}",
96                            numberOfRecords);
97                }
[1963]98
[1981]99                @Override
100                public void onFinishRecords(int nextRecordPosition)
101                        throws SRUClientException {
102                    logger.info("onFinishRecords(): nextRecordPosition = {}",
103                            nextRecordPosition);
104                }
[1963]105
[1981]106                @Override
107                public void onRecord(String identifier, int position,
108                        SRURecordData data) throws SRUClientException {
109                    logger.info("onRecord(): identifier = {}, position = {}, schema = {}",
[2938]110                                identifier, position, data.getRecordSchema());
111                    if (ClarinFCSRecordData.RECORD_SCHEMA.equals(data.getRecordSchema())) {
[2305]112                        ClarinFCSRecordData record =
113                                (ClarinFCSRecordData) data;
[2394]114                        TestUtils.dumpResource(record.getResource());
[2938]115                    } else if (SRUExplainRecordData.RECORD_SCHEMA.equals(data.getRecordSchema())) {
[2936]116                        TestUtils.dumpExplainRecordData(data);
[1969]117                    }
[1981]118                }
[1971]119
[1981]120                @Override
121                public void onSurrogateRecord(String identifier, int position,
122                        SRUDiagnostic data) throws SRUClientException {
123                    logger.info("onSurrogateRecord: identifier = {}, position = {}, uri={}, detail={}, message={}",
[2938]124                            identifier, position, data.getURI(), data.getDetails(), data.getMessage());
[1981]125                }
126            };
[1963]127
[1981]128            try {
[1969]129                logger.info("performing 'explain' request ...");
[2388]130                SRUExplainRequest request = TestUtils.makeExplainRequest(args[0]);
[1981]131                client.explain(request, handler);
132            } catch (SRUClientException e) {
133                logger.error("a fatal error occured while performing 'explain' request", e);
134            }
[1963]135
[1981]136            try {
[1963]137                logger.info("performing 'scan' request ...");
[2388]138                SRUScanRequest request = TestUtils.makeScanRequest(args[0]);
[1981]139                client.scan(request, handler);
140            } catch (SRUClientException e) {
141                logger.error("a fatal error occured while performing 'scan' request", e);
142            }
[1963]143
[2102]144            try {
145                logger.info("performing 'searchRetrieve' request ...");
[2388]146                SRUSearchRetrieveRequest request = TestUtils.makeSearchRequest(args[0], null);
[2102]147                client.searchRetrieve(request, handler);
148            } catch (SRUClientException e) {
149                logger.error("a fatal error occured while performing 'searchRetrieve' request", e);
150            }
[2196]151
[2165]152            logger.info("done");
[1963]153        } else {
154            System.err.println("missing args");
155            System.exit(64);
156        }
157    }
158
[2304]159
[1963]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);
[2165]168        logger.getLoggerRepository().getLogger("eu.clarin").setLevel(
169                org.apache.log4j.Level.DEBUG);
[1963]170    }
[2165]171
172} // class TestSimpleClient
Note: See TracBrowser for help on using the repository browser.