Changeset 7263 for FCSEndpointTester


Ignore:
Timestamp:
12/21/21 11:35:46 (2 years ago)
Author:
Oliver Schonefeld
Message:
  • use auto-detector from fcs-simple-endpoint library
File:
1 edited

Legend:

Unmodified
Added
Removed
  • FCSEndpointTester/trunk/src/main/java/eu/clarin/fcs/tester/FCSEndpointTester.java

    r7197 r7263  
    5555import eu.clarin.sru.client.SRUClient;
    5656import eu.clarin.sru.client.SRUClientException;
    57 import eu.clarin.sru.client.SRUExplainRequest;
    58 import eu.clarin.sru.client.SRUExplainResponse;
    59 import eu.clarin.sru.client.SRUVersion;
    60 import eu.clarin.sru.client.fcs.ClarinFCSClientBuilder;
    61 import eu.clarin.sru.client.fcs.ClarinFCSConstants;
    62 import eu.clarin.sru.client.fcs.ClarinFCSEndpointDescription;
    63 import eu.clarin.sru.client.fcs.ClarinFCSEndpointDescriptionParser;
     57import eu.clarin.sru.client.fcs.utils.ClarinFCSEndpointVersionAutodetector;
     58import eu.clarin.sru.client.fcs.utils.ClarinFCSEndpointVersionAutodetector.AutodetectedFCSVersion;
    6459
    6560
     
    8580            new FCSLoggingHandler();
    8681    private final ExecutorService executor = Executors.newCachedThreadPool();
     82    private final ClarinFCSEndpointVersionAutodetector versionAutodetector =
     83            new ClarinFCSEndpointVersionAutodetector();
    8784    private static final FCSEndpointTester INSTANCE = new FCSEndpointTester();
    8885
     
    199196        listener.updateProgress("Detecting CLARIN-FCS profile ...");
    200197
    201         FCSTestProfile profile = null;
    202 
    203         SRUClient client = new ClarinFCSClientBuilder()
    204                 .addDefaultDataViewParsers()
    205                 .setDefaultSRUVersion(SRUVersion.VERSION_2_0)
    206                 .unknownDataViewAsString()
    207                 .enableLegacySupport()
    208                 .registerExtraResponseDataParser(
    209                         new ClarinFCSEndpointDescriptionParser())
    210                 .buildClient();
    211 
     198        AutodetectedFCSVersion version = null;
    212199        try {
    213             SRUExplainRequest request = new SRUExplainRequest(endpointURI);
    214             request.setStrictMode(false);
    215             request.setVersion(SRUVersion.VERSION_1_2);
    216             request.setExtraRequestData(ClarinFCSConstants.X_FCS_ENDPOINT_DESCRIPTION,
    217                     ClarinFCSConstants.TRUE);
    218             request.setParseRecordDataEnabled(true);
    219             SRUExplainResponse response = client.explain(request);
    220 
    221             ClarinFCSEndpointDescription ed =
    222                     response.getFirstExtraResponseData(ClarinFCSEndpointDescription.class);
    223             if (ed != null) {
    224                 if (ed.getVersion() == 1) {
    225                     profile = FCSTestProfile.CLARIN_FCS_1_0;
    226                 }
    227             } else {
    228                 logger.debug("assume legacy");
    229                 profile = FCSTestProfile.CLARIN_FCS_LEGACY;
    230             }
    231 
    232             if (profile == null) {
    233                 request = new SRUExplainRequest(endpointURI);
    234                 request.setStrictMode(false);
    235                 request.setVersion(SRUVersion.VERSION_2_0);
    236                 request.setExtraRequestData(
    237                         ClarinFCSConstants.X_FCS_ENDPOINT_DESCRIPTION,
    238                         ClarinFCSConstants.TRUE);
    239                 request.setParseRecordDataEnabled(true);
    240                 try {
    241                     response = client.explain(request);
    242 
    243                     ed = response.getFirstExtraResponseData(
    244                             ClarinFCSEndpointDescription.class);
    245                     if (ed != null) {
    246                         if (ed.getVersion() == 2) {
    247                             profile = FCSTestProfile.CLARIN_FCS_2_0;
    248                         }
    249                     }
    250                 } catch (SRUClientException e) {
    251                     if ((e.getMessage() != null) && (e.getMessage()
    252                             .contains("responded with different version"))) {
    253                         throw new SRUClientException(
    254                                 "Seriously broken Endpoint: when trying to " +
    255                                 "detect FCS 2.0 the Endpoint illegally " +
    256                                 "responded with a SRU 1.2 reponse to a " +
    257                                 "SRU 2.0 request!");
    258                     } else {
    259                         throw e;
    260                     }
    261                 }
    262             }
    263             if (profile != null) {
    264                 final FCSTestContext context =
    265                         new FCSTestContext(profile,
    266                                 endpointURI,
    267                                 searchTerm,
    268                                 strictMode,
    269                                 connectTimeout,
    270                                 socketTimeout);
    271                 return context;
    272             }
     200            version = versionAutodetector.autodetectVersion(endpointURI);
    273201        } catch (SRUClientException e) {
    274202            logger.error("error", e);
     
    276204                    "auto-detecting CLARIN-FCS version", e);
    277205        }
    278         throw new SRUClientException("Unable to auto-detect CLARIN-FCS version!");
     206
     207        logger.debug("auto-detected endpoint version = {}", version);
     208
     209        FCSTestProfile profile = null;
     210        switch (version) {
     211        case FCS_LEGACY:
     212            profile = FCSTestProfile.CLARIN_FCS_LEGACY;
     213            break;
     214        case FCS_1_0:
     215            profile = FCSTestProfile.CLARIN_FCS_1_0;
     216            break;
     217        case FCS_2_0:
     218            profile = FCSTestProfile.CLARIN_FCS_2_0;
     219            break;
     220        case UNKNOWN:
     221            /* $FALL-THROUGH$ */
     222        default:
     223            throw new SRUClientException("Unable to auto-detect CLARIN-FCS version!");
     224        }
     225        return new FCSTestContext(profile,
     226                endpointURI,
     227                searchTerm,
     228                strictMode,
     229                connectTimeout,
     230                socketTimeout);
    279231    }
    280232
     
    301253
    302254
    303         List<FCSTest> tests = new ArrayList<FCSTest>();
     255        List<FCSTest> tests = new ArrayList<>();
    304256        for (FCSTest test : TESTS) {
    305257            final FCSTestCase tc =
     
    318270        for (FCSTest test : tests) {
    319271            if (results == null) {
    320                 results = new LinkedList<FCSTestResult>();
     272                results = new LinkedList<>();
    321273            }
    322274            logger.debug("running test {}:{}", num, test.getName());
     
    414366                    reflections.getTypesAnnotatedWith(FCSTestCase.class);
    415367            if ((annotations != null) && !annotations.isEmpty()) {
    416                 List<Class<?>> classes = new ArrayList<Class<?>>(
     368                List<Class<?>> classes = new ArrayList<>(
    417369                        annotations.size());
    418370                for (Class<?> clazz : annotations) {
     
    432384                for (Class<?> clazz : classes) {
    433385                    if (tests == null) {
    434                         tests = new ArrayList<FCSTest>(classes.size());
     386                        tests = new ArrayList<>(classes.size());
    435387                    }
    436388                    tests.add((FCSTest) clazz.newInstance());
Note: See TracChangeset for help on using the changeset viewer.