Changeset 5753 for FCSSimpleEndpoint
- Timestamp:
- 10/28/14 14:24:38 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
FCSSimpleEndpoint/trunk/src/main/java/eu/clarin/sru/server/fcs/SimpleEndpointSearchEngineBase.java
r5670 r5753 2 2 3 3 import java.net.URI; 4 import java.util.Collections;5 4 import java.util.List; 6 5 import java.util.Map; … … 14 13 import org.slf4j.LoggerFactory; 15 14 import org.z3950.zing.cql.CQLNode; 16 import org.z3950.zing.cql.CQLRelation;17 15 import org.z3950.zing.cql.CQLTermNode; 18 import org.z3950.zing.cql.Modifier;19 20 16 import eu.clarin.sru.server.SRUConfigException; 21 17 import eu.clarin.sru.server.SRUConstants; … … 37 33 public abstract class SimpleEndpointSearchEngineBase extends 38 34 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"; 44 39 private static final String ED_PREFIX = "ed"; 45 40 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";52 41 private static final Logger logger = 53 42 LoggerFactory.getLogger(SimpleEndpointSearchEngineBase.class); 54 43 protected EndpointDescription endpointDescription; 55 private boolean disableLegecyMode;56 44 57 45 … … 81 69 "returned no valid implementation of an EndpointDescription"); 82 70 } 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 }90 71 } 91 72 … … 148 129 SRURequest request, SRUDiagnosticList diagnostics) 149 130 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); 227 132 } 228 133 … … 261 166 262 167 /** 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 269 176 protected SRUScanResultSet doScan(SRUServerConfig config, 270 177 SRURequest request, SRUDiagnosticList diagnostics) … … 300 207 } 301 208 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 modifiers330 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, so358 * 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 }375 209 } 376 210 … … 507 341 } 508 342 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 // title517 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" element525 }526 527 // description528 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" element536 }537 }538 539 // landing page540 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" element545 }546 547 // languages548 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" element554 555 }556 writer.writeEndElement(); // "Languages" element557 writer.writeEndElement(); // "ResourceInfo" element558 }559 560 561 /**562 * Check, if running in legacy mode. Only intended for comparability with563 * legacy clients until the new CLARIN-FCS specification is deployed on a564 * larger scale.565 *566 * @return <code>true</code> if running in legacy mode, <code>false</code>567 * otherwise568 */569 protected boolean isLegacyMode() {570 return !disableLegecyMode;571 }572 573 343 } // class SimpleEndpointSearchEngineBase
Note: See TracChangeset
for help on using the changeset viewer.