Changeset 5753 for FCSSimpleEndpoint


Ignore:
Timestamp:
10/28/14 14:24:38 (10 years ago)
Author:
Oliver Schonefeld
Message:
  • remove legacy mode
File:
1 edited

Legend:

Unmodified
Added
Removed
  • FCSSimpleEndpoint/trunk/src/main/java/eu/clarin/sru/server/fcs/SimpleEndpointSearchEngineBase.java

    r5670 r5753  
    22
    33import java.net.URI;
    4 import java.util.Collections;
    54import java.util.List;
    65import java.util.Map;
     
    1413import org.slf4j.LoggerFactory;
    1514import org.z3950.zing.cql.CQLNode;
    16 import org.z3950.zing.cql.CQLRelation;
    1715import org.z3950.zing.cql.CQLTermNode;
    18 import org.z3950.zing.cql.Modifier;
    19 
    2016import eu.clarin.sru.server.SRUConfigException;
    2117import eu.clarin.sru.server.SRUConstants;
     
    3733public abstract class SimpleEndpointSearchEngineBase extends
    3834        SRUSearchEngineBase {
    39     private static final String FCS_DISABLE_LEGECY_MODE =
    40             "eu.clarin.sru.server.fcs.disableLegacyMode";
    41     private static final String X_FCS_ENDPOINT_DESCRIPTION = "x-fcs-endpoint-description";
    42     private static final String X_CMD_RESOURCE_INFO = "x-cmd-resource-info";
    43     private static final String ED_NS = "http://clarin.eu/fcs/endpoint-description";
     35    private static final String X_FCS_ENDPOINT_DESCRIPTION =
     36            "x-fcs-endpoint-description";
     37    private static final String ED_NS =
     38            "http://clarin.eu/fcs/endpoint-description";
    4439    private static final String ED_PREFIX = "ed";
    4540    private static final int ED_VERSION = 1;
    46     private static final String FCS_RESOURCE_INFO_NS = "http://clarin.eu/fcs/1.0/resource-info";
    47     private static final String FCS_SCAN_INDEX_FCS_RESOURCE = "fcs.resource";
    48     private static final String FCS_SCAN_INDEX_CQL_SERVERCHOICE = "cql.serverChoice";
    49     private static final String FCS_SCAN_SUPPORTED_RELATION_CQL_1_1 = "scr";
    50     private static final String FCS_SCAN_SUPPORTED_RELATION_CQL_1_2 = "=";
    51     private static final String FCS_SUPPORTED_RELATION_EXACT = "exact";
    5241    private static final Logger logger =
    5342            LoggerFactory.getLogger(SimpleEndpointSearchEngineBase.class);
    5443    protected EndpointDescription endpointDescription;
    55     private boolean disableLegecyMode;
    5644
    5745
     
    8169                    "returned no valid implementation of an EndpointDescription");
    8270        }
    83 
    84         if (params.containsKey(FCS_DISABLE_LEGECY_MODE)) {
    85             logger.info("disabling FCS legacy mode");
    86             this.disableLegecyMode = false;
    87         } else {
    88             this.disableLegecyMode = true;
    89         }
    9071    }
    9172
     
    148129            SRURequest request, SRUDiagnosticList diagnostics)
    149130            throws SRUException {
    150 
    151         if (isLegacyMode()) {
    152             /*
    153              * Check if we got a scan on fcs.resource. If yes, handle it
    154              * accordingly, otherwise delegate to user-provided implementation.
    155              */
    156             final List<ResourceInfo> result = translateFcsScanResource(request
    157                     .getScanClause());
    158             if (result != null) {
    159                 /*
    160                  * Make sure, we honor the maximumTerms limit, of the client
    161                  * requests it ...
    162                  */
    163                 final int maxTerms = ((result.size() > 0) && (request
    164                         .getMaximumTerms() > 0)) ? Math.min(result.size(),
    165                         request.getMaximumTerms()) : result.size();
    166 
    167                 /*
    168                  * Shall we provide extended resource information ... ?
    169                  */
    170                 final boolean provideResourceInfo = parseBoolean(request
    171                         .getExtraRequestData(X_CMD_RESOURCE_INFO));
    172 
    173                 return new SRUScanResultSet(diagnostics) {
    174                     private int idx = -1;
    175 
    176 
    177                     @Override
    178                     public boolean nextTerm() {
    179                         return (result != null) && (++idx < maxTerms);
    180                     }
    181 
    182 
    183                     @Override
    184                     public String getValue() {
    185                         return result.get(idx).getPid();
    186                     }
    187 
    188 
    189                     @Override
    190                     public int getNumberOfRecords() {
    191                         return -1;
    192                     }
    193 
    194 
    195                     @Override
    196                     public String getDisplayTerm() {
    197                         return result.get(idx).getTitle("en");
    198                     }
    199 
    200 
    201                     @Override
    202                     public WhereInList getWhereInList() {
    203                         return null;
    204                     }
    205 
    206 
    207                     @Override
    208                     public boolean hasExtraTermData() {
    209                         return provideResourceInfo;
    210                     }
    211 
    212 
    213                     @Override
    214                     public void writeExtraTermData(XMLStreamWriter writer)
    215                             throws XMLStreamException {
    216                         if (provideResourceInfo) {
    217                             writeLegacyResourceInfo(writer, result.get(idx));
    218                         }
    219                     }
    220                 };
    221             } else {
    222                 return doScan(config, request, diagnostics);
    223             }
    224         } else {
    225             return doScan(config, request, diagnostics);
    226         }
     131        return doScan(config, request, diagnostics);
    227132    }
    228133
     
    261166
    262167    /**
    263      * Handle a <em>explain</em> operation. The default implementation is a
    264      * no-op. Override this method, if you want to provide a custom behavior.
    265      *
    266      * @see SRUSearchEngine#explain(SRUServerConfig, SRURequest,
    267      *      SRUDiagnosticList)
    268      */
     168     * Handle a <em>scan</em> operation. The default implementation is a no-op.
     169     * Override this method, if you want to provide a custom behavior.
     170     *
     171     * @see SRUSearchEngine#scan(SRUServerConfig, SRURequest, SRUDiagnosticList)
     172     * @deprecated override
     173     *             {@link #scan(SRUServerConfig, SRURequest, SRUDiagnosticList)}
     174     */
     175    @Deprecated
    269176    protected SRUScanResultSet doScan(SRUServerConfig config,
    270177            SRURequest request, SRUDiagnosticList diagnostics)
     
    300207        }
    301208        return false;
    302     }
    303 
    304 
    305     private List<ResourceInfo> translateFcsScanResource(CQLNode scanClause)
    306             throws SRUException {
    307         if (scanClause instanceof CQLTermNode) {
    308             final CQLTermNode root = (CQLTermNode) scanClause;
    309             logger.debug("index = '{}', relation = '{}', term = '{}'",
    310                     new Object[] { root.getIndex(),
    311                             root.getRelation().getBase(), root.getTerm() });
    312 
    313             String index = root.getIndex();
    314             if (FCS_SCAN_INDEX_CQL_SERVERCHOICE.equals(index) &&
    315                     FCS_SCAN_INDEX_FCS_RESOURCE.equals(root.getTerm())) {
    316                 throw new SRUException(SRUConstants.SRU_UNSUPPORTED_INDEX,
    317                         "scan operation with 'scanClause' with value " +
    318                         "'fcs.resource' is deprecated within CLARIN-FCS");
    319             }
    320             if (!(FCS_SCAN_INDEX_FCS_RESOURCE.equals(index))) {
    321                 logger.debug("got scan operation on index '{}', bailing ...",
    322                         index);
    323                 return null;
    324             }
    325 
    326             logger.warn("scan on 'fcs.resource' for endpoint resource " +
    327                     "enumeration is deprecated.");
    328 
    329             // only allow "=" relation without any modifiers
    330             final CQLRelation relationNode = root.getRelation();
    331             String relation = relationNode.getBase();
    332             if (!(FCS_SCAN_SUPPORTED_RELATION_CQL_1_1.equals(relation) ||
    333                     FCS_SCAN_SUPPORTED_RELATION_CQL_1_2.equals(relation) ||
    334                     FCS_SUPPORTED_RELATION_EXACT.equals(relation))) {
    335                 throw new SRUException(SRUConstants.SRU_UNSUPPORTED_RELATION,
    336                         relationNode.getBase(), "Relation \"" +
    337                                 relationNode.getBase() +
    338                                 "\" is not supported in scan operation.");
    339             }
    340             final List<Modifier> modifiers = relationNode.getModifiers();
    341             if ((modifiers != null) && !modifiers.isEmpty()) {
    342                 Modifier modifier = modifiers.get(0);
    343                 throw new SRUException(
    344                         SRUConstants.SRU_UNSUPPORTED_RELATION_MODIFIER,
    345                         modifier.getValue(), "Relation modifier \"" +
    346                                 modifier.getValue() +
    347                                 "\" is not supported in scan operation.");
    348             }
    349 
    350             final String term = root.getTerm();
    351             if ((term == null) || term.isEmpty()) {
    352                 throw new SRUException(SRUConstants.SRU_EMPTY_TERM_UNSUPPORTED,
    353                         "An empty term is not supported in scan operation.");
    354             }
    355 
    356             /*
    357              * generate result: currently we only have a flat hierarchy, so
    358              * return an empty result on any attempt to do a recursive scan ...
    359              */
    360             List<ResourceInfo> results = null;
    361             if ((FCS_SCAN_INDEX_CQL_SERVERCHOICE.equals(index) &&
    362                     FCS_SCAN_INDEX_FCS_RESOURCE.equals(term)) ||
    363                     (FCS_SCAN_INDEX_FCS_RESOURCE.equals(index))) {
    364                 results = endpointDescription.getResourceList(term);
    365             }
    366             if ((results == null) || results.isEmpty()) {
    367                 return Collections.emptyList();
    368             } else {
    369                 return results;
    370             }
    371         } else {
    372             throw new SRUException(SRUConstants.SRU_QUERY_FEATURE_UNSUPPORTED,
    373                     "Scan clause too complex.");
    374         }
    375209    }
    376210
     
    507341    }
    508342
    509 
    510     private void writeLegacyResourceInfo(XMLStreamWriter writer,
    511             ResourceInfo resourceInfo) throws XMLStreamException {
    512         writer.setDefaultNamespace(FCS_RESOURCE_INFO_NS);
    513         writer.writeStartElement(FCS_RESOURCE_INFO_NS, "ResourceInfo");
    514         writer.writeDefaultNamespace(FCS_RESOURCE_INFO_NS);
    515 
    516         // title
    517         final Map<String, String> title = resourceInfo.getTitle();
    518         for (Map.Entry<String, String> i : title.entrySet()) {
    519             writer.setPrefix(XMLConstants.XML_NS_PREFIX,
    520                     XMLConstants.XML_NS_URI);
    521             writer.writeStartElement(FCS_RESOURCE_INFO_NS, "Title");
    522             writer.writeAttribute(XMLConstants.XML_NS_URI, "lang", i.getKey());
    523             writer.writeCharacters(i.getValue());
    524             writer.writeEndElement(); // "title" element
    525         }
    526 
    527         // description
    528         final Map<String, String> description = resourceInfo.getDescription();
    529         if (description != null) {
    530             for (Map.Entry<String, String> i : description.entrySet()) {
    531                 writer.writeStartElement(FCS_RESOURCE_INFO_NS, "Description");
    532                 writer.writeAttribute(XMLConstants.XML_NS_URI, "lang",
    533                         i.getKey());
    534                 writer.writeCharacters(i.getValue());
    535                 writer.writeEndElement(); // "Description" element
    536             }
    537         }
    538 
    539         // landing page
    540         final String landingPageURI = resourceInfo.getLandingPageURI();
    541         if (landingPageURI != null) {
    542             writer.writeStartElement(FCS_RESOURCE_INFO_NS, "LandingPageURI");
    543             writer.writeCharacters(landingPageURI);
    544             writer.writeEndElement(); // "LandingPageURI" element
    545         }
    546 
    547         // languages
    548         final List<String> languages = resourceInfo.getLanguages();
    549         writer.writeStartElement(FCS_RESOURCE_INFO_NS, "Languages");
    550         for (String i : languages) {
    551             writer.writeStartElement(FCS_RESOURCE_INFO_NS, "Language");
    552             writer.writeCharacters(i);
    553             writer.writeEndElement(); // "Language" element
    554 
    555         }
    556         writer.writeEndElement(); // "Languages" element
    557         writer.writeEndElement(); // "ResourceInfo" element
    558     }
    559 
    560 
    561     /**
    562      * Check, if running in legacy mode. Only intended for comparability with
    563      * legacy clients until the new CLARIN-FCS specification is deployed on a
    564      * larger scale.
    565      *
    566      * @return <code>true</code> if running in legacy mode, <code>false</code>
    567      *         otherwise
    568      */
    569     protected boolean isLegacyMode() {
    570         return !disableLegecyMode;
    571     }
    572 
    573343} // class SimpleEndpointSearchEngineBase
Note: See TracChangeset for help on using the changeset viewer.