Changeset 6838 for FCSSimpleEndpoint


Ignore:
Timestamp:
11/23/15 16:49:02 (8 years ago)
Author:
Oliver Schonefeld
Message:
  • some more FCS 2.0 stuff (wip!)
Location:
FCSSimpleEndpoint/trunk/src/main/java/eu/clarin/sru/server/fcs
Files:
3 edited

Legend:

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

    r6830 r6838  
    2727    private final String qualifier;
    2828    private final String altValueInfo;
    29     private final String altValueInfoURI;
     29    private final URI altValueInfoURI;
    3030
    3131
     
    5151    public Layer(String id, URI resultId, String type,
    5252            ContentEncoding encoding, String qualifier, String altValueInfo,
    53             String altValueInfoURI) {
     53            URI altValueInfoURI) {
    5454        if (id == null) {
    5555            throw new NullPointerException("id == null");
     
    161161     *         layer or <code>null</code>
    162162     */
    163     public String getAltValueInfoURI() {
     163    public URI getAltValueInfoURI() {
    164164        return altValueInfoURI;
    165165    }
  • FCSSimpleEndpoint/trunk/src/main/java/eu/clarin/sru/server/fcs/SimpleEndpointSearchEngineBase.java

    r6830 r6838  
    292292                    if (layer.getAltValueInfoURI() != null) {
    293293                        writer.writeAttribute("alt-value-info-uri",
    294                                 layer.getAltValueInfoURI());
     294                                layer.getAltValueInfoURI().toString());
    295295                    }
    296296                }
  • FCSSimpleEndpoint/trunk/src/main/java/eu/clarin/sru/server/fcs/utils/SimpleEndpointDescriptionParser.java

    r6830 r6838  
    5959    private static final URI CAP_ADVANCED_SEARCH =
    6060            URI.create("http://clarin.eu/fcs/capability/advanced-search");
     61    private static final String MINETYPE_HITS = "application/x-clarin-fcs-hits+xml";
     62    private static final String MINETYPE_ADV = "application/x-clarin-fcs-adv+xml";
    6163    private static final String LANG_EN = "en";
    6264    private static final String POLICY_SEND_DEFAULT = "send-by-default";
    6365    private static final String POLICY_NEED_REQUEST = "need-to-request";
     66    private static final String LAYER_ENCODING_VALUE = "value";
     67    private static final String LAYER_ENCODING_EMPTY = "empty";
    6468    private static final Logger logger =
    6569            LoggerFactory.getLogger(SimpleEndpointDescriptionParser.class);
     
    153157                try {
    154158                    URI uri = new URI(s);
    155                     if (capabilities.contains(uri)) {
     159                    if (!capabilities.contains(uri)) {
     160                        capabilities.add(uri);
     161                    } else {
    156162                        logger.warn("ignoring duplicate capability " +
    157163                                "entry for '{}'", uri);
    158164                    }
    159                     capabilities.add(uri);
    160165                } catch (URISyntaxException e) {
    161166                    throw new SRUConfigException("capability is not encoded " +
     
    174179        }
    175180        logger.debug("CAPS:'{}'", capabilities);
     181
     182        // used to check for id attribute uniqueness
     183        final Set<String> xml_ids = new HashSet<String>();
    176184
    177185        // supported data views
     
    188196                            + "must carry a proper 'id' attribute");
    189197                }
     198
     199                if (xml_ids.contains(id)) {
     200                    throw new SRUConfigException("The value of attribute " +
     201                            "'id' of element <SupportedDataView> must be unique: " + id);
     202                }
     203                xml_ids.add(id);
     204
    190205                String p = getAttribute(item, "delivery-policy");
    191206                if (p == null) {
     
    239254        logger.debug("DV: {}", supportedDataViews);
    240255
     256        // sanity check on data views
     257        boolean hasHitsView = false;
     258        boolean hasAdvView = false;
     259
     260        for (DataView dataView : supportedDataViews) {
     261            if (dataView.getMimeType().equals(MINETYPE_HITS)) {
     262                hasHitsView = true;
     263            } else if (dataView.getMimeType().equals(MINETYPE_ADV)) {
     264                hasAdvView = true;
     265            }
     266        }
     267        if (!hasHitsView) {
     268            throw new SRUConfigException("Generic Hits Data View (" +
     269                    MINETYPE_HITS + ") was not declared in <SupportedDataViews>");
     270        }
     271        if (capabilities.contains(CAP_ADVANCED_SEARCH) && !hasAdvView) {
     272            throw new SRUConfigException("Endpoint claimes to support " +
     273                    "Advanced FCS but does not declare Advanced Data View (" +
     274                    MINETYPE_ADV + ") in <SupportedDataViews>");
     275
     276        }
     277
    241278
    242279        // supported layers
     
    250287                String id = getAttribute(item, "id");
    251288                if (id == null) {
    252                     throw new SRUConfigException("Element <ed:SupportedLayer> "
     289                    throw new SRUConfigException("Element <SupportedLayer> "
    253290                            + "must carry a proper 'id' attribute");
    254291                }
     292
     293                if (xml_ids.contains(id)) {
     294                    throw new SRUConfigException("The value of attribute " +
     295                            "'id' of element <SupportedLayer> must be unique: " + id);
     296                }
     297                xml_ids.add(id);
     298
    255299                String s = getAttribute(item, "result-id");
    256300                if (s == null) {
    257                     throw new SRUConfigException("Element <ed:SupportedLayer> "
     301                    throw new SRUConfigException("Element <SupportedLayer> "
    258302                            + "must carry a proper 'result-id' attribute");
    259303                }
     
    263307                } catch (URISyntaxException e) {
    264308                    throw new SRUConfigException("Attribute 'result-id' on " +
    265                             "Element <ed:SupportedLayer> is not encoded " +
     309                            "Element <SupportedLayer> is not encoded " +
    266310                            "as proper URI: " + s);
    267311                }
     
    269313                String type = cleanString(item.getTextContent());
    270314                if ((type != null) && !type.isEmpty()) {
    271 
     315                    // sanity check on layer types
     316                    if (!(type.equals("text") ||
     317                            type.equals("lemma") ||
     318                            type.equals("pos") ||
     319                            type.equals("orth") ||
     320                            type.equals("norm") ||
     321                            type.equals("phonetic") ||
     322                            type.startsWith("x-"))) {
     323                        logger.warn("layer type '{}' is not defined by specification", type);
     324                    }
    272325                } else {
    273                     throw new SRUConfigException("Element <ed:SupportedLayer> "
    274                             + "as no proper type");
     326                    throw new SRUConfigException("Element <SupportedLayer> " +
     327                            "does not define a proper layer type");
    275328                }
    276329
    277330                String qualifier = getAttribute(item, "qualifier");
    278331
    279                 // FIXME: implement parsing of content encoding!
    280332                Layer.ContentEncoding encoding =
    281333                        Layer.ContentEncoding.VALUE;
     334                s = getAttribute(item, "type");
     335                if (s != null) {
     336                    if (LAYER_ENCODING_VALUE.equals(s)) {
     337                        encoding = Layer.ContentEncoding.VALUE;
     338                    } else if (LAYER_ENCODING_EMPTY.equals(s)) {
     339                        encoding = Layer.ContentEncoding.EMPTY;
     340                    } else {
     341                        throw new SRUConfigException("invalid layer encoding: " + s);
     342                    }
     343                }
     344
    282345
    283346                String altValueInfo = getAttribute(item, "alt-value-info");
    284                 String altValueInfoURI = null;
     347                URI altValueInfoURI = null;
    285348                if (altValueInfo != null) {
    286                     altValueInfoURI = getAttribute(item, "alt-value-info-uri");
     349                    s = getAttribute(item, "alt-value-info-uri");
     350                    if (s != null) {
     351                        try {
     352                          altValueInfoURI = new URI(s);
     353                        } catch (URISyntaxException e) {
     354                            throw new SRUConfigException("Attribute 'alt-value-info-uri' on " +
     355                                    "Element <SupportedLayer> is not encoded " +
     356                                    "as proper URI: " + s);
     357                        }
     358                    } else {
     359
     360                    }
    287361                }
    288362
     
    309383        exp = xpath.compile("/ed:EndpointDescription/ed:Resources/ed:Resource");
    310384        list = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
    311         final Set<String> ids = new HashSet<String>();
    312         List<ResourceInfo> resources = parseRessources(xpath, list, ids,
    313                 supportedDataViews, supportedLayers);
     385        final Set<String> pids = new HashSet<String>();
     386        List<ResourceInfo> resources = parseRessources(xpath, list, pids,
     387                supportedDataViews, supportedLayers, hasAdvView);
    314388        if ((resources == null) || resources.isEmpty()) {
    315389            throw new SRUConfigException("No resources where " +
     
    326400
    327401    private static List<ResourceInfo> parseRessources(XPath xpath,
    328             NodeList nodes, Set<String> ids, List<DataView> supportedDataViews,
    329             List<Layer> supportedLayers)
     402            NodeList nodes, Set<String> pids, List<DataView> supportedDataViews,
     403            List<Layer> supportedLayers, boolean hasAdv)
    330404                    throws SRUConfigException, XPathExpressionException {
    331405        List<ResourceInfo> ris = null;
     
    346420                      "must carry a proper 'pid' attribute");
    347421          }
    348           if (ids.contains(pid)) {
     422          if (pids.contains(pid)) {
    349423              throw new SRUConfigException("Another element <Resource> " +
    350424                      "with pid '" + pid + "' already exists");
    351425          }
    352           ids.add(pid);
    353 
    354           XPathExpression x1 = xpath.compile("ed:Title");
    355           NodeList l1 = (NodeList) x1.evaluate(node, XPathConstants.NODESET);
    356           if ((l1 != null) && (l1.getLength() > 0)) {
    357               for (int i = 0; i < l1.getLength(); i++) {
    358                   final Element n = (Element) l1.item(i);
     426          pids.add(pid);
     427
     428          XPathExpression exp = xpath.compile("ed:Title");
     429          NodeList list = (NodeList) exp.evaluate(node, XPathConstants.NODESET);
     430          if ((list != null) && (list.getLength() > 0)) {
     431              for (int i = 0; i < list.getLength(); i++) {
     432                  final Element n = (Element) list.item(i);
    359433
    360434                  final String lang = getLangAttribute(n);
     
    387461          }
    388462
    389           XPathExpression x2 = xpath.compile("ed:Description");
    390           NodeList l2 = (NodeList) x2.evaluate(node, XPathConstants.NODESET);
    391           if ((l2 != null) && (l2.getLength() > 0)) {
    392               for (int i = 0; i < l2.getLength(); i++) {
    393                   Element n = (Element) l2.item(i);
     463          exp = xpath.compile("ed:Description");
     464          list = (NodeList) exp.evaluate(node, XPathConstants.NODESET);
     465          if ((list != null) && (list.getLength() > 0)) {
     466              for (int i = 0; i < list.getLength(); i++) {
     467                  Element n = (Element) list.item(i);
    394468
    395469                  String lang = getLangAttribute(n);
     
    419493          }
    420494
    421           XPathExpression x3 = xpath.compile("ed:LandingPageURI");
    422           NodeList l3 = (NodeList) x3.evaluate(node, XPathConstants.NODESET);
    423           if ((l3 != null) && (l3.getLength() > 0)) {
    424               for (int i = 0; i < l3.getLength(); i++) {
    425                   Element n = (Element) l3.item(i);
     495          exp = xpath.compile("ed:LandingPageURI");
     496          list = (NodeList) exp.evaluate(node, XPathConstants.NODESET);
     497          if ((list != null) && (list.getLength() > 0)) {
     498              for (int i = 0; i < list.getLength(); i++) {
     499                  Element n = (Element) list.item(i);
    426500                  link = cleanString(n.getTextContent());
    427501              }
    428502          }
    429503
    430           XPathExpression x4 = xpath.compile("ed:Languages/ed:Language");
    431           NodeList l4 = (NodeList) x4.evaluate(node, XPathConstants.NODESET);
    432           if ((l4 != null) && (l4.getLength() > 0)) {
    433               for (int i = 0; i < l4.getLength(); i++) {
    434                   Element n = (Element) l4.item(i);
     504          exp = xpath.compile("ed:Languages/ed:Language");
     505          list = (NodeList) exp.evaluate(node, XPathConstants.NODESET);
     506          if ((list != null) && (list.getLength() > 0)) {
     507              for (int i = 0; i < list.getLength(); i++) {
     508                  Element n = (Element) list.item(i);
    435509
    436510                  String s = n.getTextContent();
     
    458532          }
    459533
    460           XPathExpression x5 = xpath.compile("ed:AvailableDataViews");
    461           Node n = (Node) x5.evaluate(node, XPathConstants.NODE);
     534          exp = xpath.compile("ed:AvailableDataViews");
     535          Node n = (Node) exp.evaluate(node, XPathConstants.NODE);
    462536          if ((n != null) && (n instanceof Element)) {
    463537              String ref = getAttribute((Element) n, "ref");
     
    495569          } else {
    496570              throw new SRUConfigException(
    497                       "missing element <ed:AvailableDataViews>");
     571                      "missing element <AvailableDataViews>");
    498572          }
    499573          if (availableDataViews == null) {
     
    502576          }
    503577
    504           XPathExpression x6 = xpath.compile("ed:Resources/ed:Resource");
    505           NodeList l6 = (NodeList) x6.evaluate(node, XPathConstants.NODESET);
    506           if ((l6 != null) && (l6.getLength() > 0)) {
    507               sub = parseRessources(xpath, l6, ids,
    508                       supportedDataViews, supportedLayers);
     578          exp = xpath.compile("ed:AvailableLayers");
     579          n = (Node) exp.evaluate(node, XPathConstants.NODE);
     580          if ((n != null) && (n instanceof Element)) {
     581              String ref = getAttribute((Element) n, "ref");
     582              if (ref == null) {
     583                  throw new SRUConfigException("Element <AvailableLayers> " +
     584                          "must carry a 'ref' attribute");
     585              }
     586              String[] refs = ref.split("\\s+");
     587              if ((refs == null) || (refs.length < 1)) {
     588                  throw new SRUConfigException("Attribute 'ref' on element " +
     589                          "<AvailableLayers> must contain a whitespace " +
     590                          "seperated list of data view references");
     591              }
     592
     593
     594              for (int i = 0; i < refs.length; i++) {
     595                  Layer layer = null;
     596                  for (Layer l : supportedLayers) {
     597                      if (refs[i].equals(l.getId())) {
     598                          layer = l;
     599                          break;
     600                      }
     601                  }
     602                  if (layer != null) {
     603                      if (availableLayers == null) {
     604                          availableLayers = new ArrayList<Layer>();
     605                      }
     606                      availableLayers.add(layer);
     607                  } else {
     608                      throw new SRUConfigException("A layer with " +
     609                              "identifier '" + refs[i] + "' was not defined " +
     610                              "in <SupportedLayers>");
     611                  }
     612              }
     613          } else {
     614              if (hasAdv) {
     615                  logger.debug("no <SupportedLayers> for ressource '{}'", pid);
     616              }
     617          }
     618
     619          exp = xpath.compile("ed:Resources/ed:Resource");
     620          list = (NodeList) exp.evaluate(node, XPathConstants.NODESET);
     621          if ((list != null) && (list.getLength() > 0)) {
     622              sub = parseRessources(xpath, list, pids,
     623                      supportedDataViews, supportedLayers, hasAdv);
    509624          }
    510625
     
    526641
    527642    private static String getAttribute(Element el, String localName) {
    528         String lang = el.getAttribute(localName);
    529         if (lang != null) {
    530             lang = lang.trim();
    531             if (!lang.isEmpty()) {
    532                 return lang;
     643        String value = el.getAttribute(localName);
     644        if (value != null) {
     645            value = value.trim();
     646            if (!value.isEmpty()) {
     647                return value;
    533648            }
    534649        }
Note: See TracChangeset for help on using the changeset viewer.