Ignore:
Timestamp:
07/11/14 15:29:52 (10 years ago)
Author:
margaretha@ids-mannheim.de
Message:

Added endpoint description,
updated SimpleResourceInfoInventory? according to the new format.

File:
1 edited

Legend:

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

    r2801 r5477  
    11package eu.clarin.sru.server.fcs;
    22
     3import java.util.ArrayList;
    34import java.util.Collections;
    45import java.util.List;
     
    67
    78import javax.servlet.ServletContext;
    8 import javax.xml.XMLConstants;
    99import javax.xml.stream.XMLStreamException;
    1010import javax.xml.stream.XMLStreamWriter;
     
    3636public abstract class SimpleEndpointSearchEngineBase extends
    3737        SRUSearchEngineBase {
    38     private static final String FCS_RESOURCE_INFO_NS =
    39             "http://clarin.eu/fcs/1.0/resource-info";
    40     private static final String X_CMD_RESOURCE_INFO = "x-cmd-resource-info";
     38   
     39//    private static final String X_CMD_RESOURCE_INFO = "x-cmd-resource-info";
     40    private static final String X_FCS_ENDPOINT_DESCRIPTION = "x-fcs-endpoint-description";
     41   
    4142    private static final String FCS_SCAN_INDEX_FCS_RESOURCE = "fcs.resource";
    4243    private static final String FCS_SCAN_INDEX_CQL_SERVERCHOICE = "cql.serverChoice";
     
    4748            LoggerFactory.getLogger(SimpleEndpointSearchEngineBase.class);
    4849    protected ResourceInfoInventory resourceInfoInventory;
    49 
    50 
    51     /**
     50   
     51    public List<String> capabilities;
     52    public List<DataView> supportedDataViews;
     53   
     54    public void addEndpointCapability(String c){
     55        capabilities.add(c);
     56    }
     57   
     58    public void addDataView(DataView d){
     59        supportedDataViews.add(d);
     60    }
     61       
     62        /**
    5263     * This method should not be overridden. Perform your custom initialization
    5364     * in the {@link #doInit(ServletContext, SRUServerConfig, Map)} method
     
    6172        logger.debug("initializing");
    6273        super.init(context, config, params);
    63 
     74       
     75        logger.debug("Setting basic capability");
     76        capabilities = new ArrayList<String>();
     77        capabilities.add("http://clarin.eu/fcs/capability/basic-search");
     78       
     79        logger.debug("Setting Generic Hits dataview");
     80        supportedDataViews = new ArrayList<DataView>();
     81        supportedDataViews.add(
     82                        new DataView("The representation of the hit",
     83                                "application/x-clarin-fcs-hits+xml",
     84                                DataView.PayloadDisposition.INLINE,
     85                                DataView.PayloadDelivery.SEND_BY_DEFAULT,
     86                                "hits")
     87        );
     88       
    6489        logger.debug("initializing search engine implementation");
    6590        doInit(context, config, params);
    66 
     91               
    6792        logger.debug("initizalizing resource info inventory");
    6893        this.resourceInfoInventory = createResourceInfoInventory(context, config, params);
     
    97122            throws SRUException {
    98123        final boolean provideResourceInfo =
    99                 parseBoolean(request.getExtraRequestData(X_CMD_RESOURCE_INFO));
     124                parseBoolean(request.getExtraRequestData(X_FCS_ENDPOINT_DESCRIPTION));
    100125        if (provideResourceInfo) {
    101126            final List<ResourceInfo> resourceInfoList =
     
    112137                public void writeExtraResponseData(XMLStreamWriter writer)
    113138                        throws XMLStreamException {
    114                     writeFullResourceInfo(writer, null, resourceInfoList);
     139                        EndpointDescriptionWriter.writeEndpointDescription(writer,
     140                                        capabilities, supportedDataViews, resourceInfoList);
     141                    //ResourceInfoWriter.writeFullResourceInfo(writer, null, resourceInfoList);
    115142                }
    116143            };
     
    130157     */
    131158    @Override
     159    @Deprecated
    132160    public final SRUScanResultSet scan(SRUServerConfig config,
    133161            SRURequest request, SRUDiagnosticList diagnostics)
     
    153181             */
    154182            final boolean provideResourceInfo = parseBoolean(
    155                     request.getExtraRequestData(X_CMD_RESOURCE_INFO));
     183                    request.getExtraRequestData(X_FCS_ENDPOINT_DESCRIPTION));
    156184
    157185            return new SRUScanResultSet(diagnostics) {
     
    198226                        throws XMLStreamException {
    199227                    if (provideResourceInfo) {
    200                         writeResourceInfo(writer, null, result.get(idx));
     228                        ResourceInfoWriter.writeResourceInfo(writer, null, result.get(idx));
    201229                    }
    202230                }
     
    266294     *      SRUDiagnosticList)
    267295     */
     296    @Deprecated
    268297    protected SRUScanResultSet doScan(SRUServerConfig config,
    269298            SRURequest request, SRUDiagnosticList diagnostics)
     
    301330    }
    302331
    303 
     332    @Deprecated
    304333    private List<ResourceInfo> translateFcsScanResource(CQLNode scanClause)
    305334            throws SRUException {
     
    373402
    374403
    375     private static void writeFullResourceInfo(XMLStreamWriter writer,
    376             String prefix, List<ResourceInfo> resourceInfoList)
    377             throws XMLStreamException {
    378         if (resourceInfoList == null) {
    379             throw new NullPointerException("resourceInfoList == null");
    380         }
    381         if (!resourceInfoList.isEmpty()) {
    382             final boolean defaultNS = ((prefix == null) || prefix.isEmpty());
    383             if (defaultNS) {
    384                 writer.setDefaultNamespace(FCS_RESOURCE_INFO_NS);
    385             } else {
    386                 writer.setPrefix(prefix, FCS_RESOURCE_INFO_NS);
    387             }
    388             writer.writeStartElement(FCS_RESOURCE_INFO_NS, "ResourceCollection");
    389             if (defaultNS) {
    390                 writer.writeDefaultNamespace(FCS_RESOURCE_INFO_NS);
    391             } else {
    392                 writer.writeNamespace(prefix, FCS_RESOURCE_INFO_NS);
    393             }
    394             for (ResourceInfo resourceInfo : resourceInfoList) {
    395                 doWriteResourceInfo(writer, prefix, resourceInfo, false, true);
    396             }
    397             writer.writeEndElement(); // "ResourceCollection" element
    398         }
    399     }
    400 
    401 
    402     private static void writeResourceInfo(XMLStreamWriter writer, String prefix,
    403             ResourceInfo resourceInfo) throws XMLStreamException {
    404         if (resourceInfo == null) {
    405             throw new NullPointerException("resourceInfo == null");
    406         }
    407         doWriteResourceInfo(writer, prefix, resourceInfo, true, false);
    408     }
    409 
    410 
    411     private static void doWriteResourceInfo(XMLStreamWriter writer,
    412             String prefix, ResourceInfo resourceInfo, boolean writeNS,
    413             boolean recursive) throws XMLStreamException {
    414         final boolean defaultNS = ((prefix == null) || prefix.isEmpty());
    415         if (writeNS) {
    416             if (defaultNS) {
    417                 writer.setDefaultNamespace(FCS_RESOURCE_INFO_NS);
    418             } else {
    419                 writer.setPrefix(prefix, FCS_RESOURCE_INFO_NS);
    420             }
    421         }
    422         writer.writeStartElement(FCS_RESOURCE_INFO_NS, "ResourceInfo");
    423         if (writeNS) {
    424             if (defaultNS) {
    425                 writer.writeDefaultNamespace(FCS_RESOURCE_INFO_NS);
    426             } else {
    427                 writer.writeNamespace(prefix, FCS_RESOURCE_INFO_NS);
    428             }
    429         }
    430         if (recursive) {
    431             /*
    432              * HACK: only output @pid for recursive (= explain) requests.
    433              * This should be revisited, if we decide to go for the explain
    434              * style enumeration of resources.
    435              */
    436             writer.writeAttribute("pid", resourceInfo.getPid());
    437         }
    438         if (resourceInfo.hasSubResources()) {
    439             writer.writeAttribute("hasSubResources", "true");
    440         }
    441 
    442         final Map<String, String> title = resourceInfo.getTitle();
    443         for (Map.Entry<String, String> i : title.entrySet()) {
    444             writer.setPrefix(XMLConstants.XML_NS_PREFIX,
    445                     XMLConstants.XML_NS_URI);
    446             writer.writeStartElement(FCS_RESOURCE_INFO_NS, "Title");
    447             writer.writeAttribute(XMLConstants.XML_NS_URI, "lang", i.getKey());
    448             writer.writeCharacters(i.getValue());
    449             writer.writeEndElement(); // "title" element
    450         }
    451 
    452         final Map<String, String> description = resourceInfo.getDescription();
    453         if (description != null) {
    454             for (Map.Entry<String, String> i : description.entrySet()) {
    455                 writer.writeStartElement(FCS_RESOURCE_INFO_NS, "Description");
    456                 writer.writeAttribute(XMLConstants.XML_NS_URI, "lang",
    457                         i.getKey());
    458                 writer.writeCharacters(i.getValue());
    459                 writer.writeEndElement(); // "Description" element
    460             }
    461         }
    462 
    463         final String landingPageURI = resourceInfo.getLandingPageURI();
    464         if (landingPageURI != null) {
    465             writer.writeStartElement(FCS_RESOURCE_INFO_NS, "LandingPageURI");
    466             writer.writeCharacters(landingPageURI);
    467             writer.writeEndElement(); // "LandingPageURI" element
    468         }
    469 
    470         final List<String> languages = resourceInfo.getLanguages();
    471         writer.writeStartElement(FCS_RESOURCE_INFO_NS, "Languages");
    472         for (String i : languages) {
    473             writer.writeStartElement(FCS_RESOURCE_INFO_NS, "Language");
    474             writer.writeCharacters(i);
    475             writer.writeEndElement(); // "Language" element
    476 
    477         }
    478         writer.writeEndElement(); // "Languages" element
    479 
    480         if (recursive && resourceInfo.hasSubResources()) {
    481             writer.writeStartElement(FCS_RESOURCE_INFO_NS,
    482                     "ResourceInfoCollection");
    483             for (ResourceInfo r : resourceInfo.getSubResources()) {
    484                 doWriteResourceInfo(writer, prefix, r, writeNS, recursive);
    485             }
    486             writer.writeEndElement(); // "ResourceCollection" element
    487         }
    488         writer.writeEndElement(); // "ResourceInfo" element
    489     }
     404   
    490405
    491406} // class SimpleEndpointSearchEngineBase
Note: See TracChangeset for help on using the changeset viewer.