예제 #1
0
  public void getEntryById(DBBroker broker, String path, String id, OutgoingMessage response)
      throws EXistException, BadRequestException {
    XQuery xquery = broker.getXQueryService();
    CompiledXQuery feedQuery = xquery.getXQueryPool().borrowCompiledXQuery(broker, entryByIdSource);

    XQueryContext context;
    if (feedQuery == null) {
      context = xquery.newContext(AccessContext.REST);
      try {
        feedQuery = xquery.compile(context, entryByIdSource);
      } catch (XPathException ex) {
        throw new EXistException("Cannot compile xquery " + entryByIdSource.getURL(), ex);
      } catch (IOException ex) {
        throw new EXistException(
            "I/O exception while compiling xquery " + entryByIdSource.getURL(), ex);
      }
    } else {
      context = feedQuery.getContext();
    }
    context.setStaticallyKnownDocuments(
        new XmldbURI[] {XmldbURI.create(path).append(AtomProtocol.FEED_DOCUMENT_NAME)});

    try {
      context.declareVariable("id", id);
      Sequence resultSequence = xquery.execute(feedQuery, null);
      if (resultSequence.isEmpty()) {
        throw new BadRequestException("No topic was found.");
      }
      String charset = getContext().getDefaultCharset();
      response.setContentType("application/atom+xml; charset=" + charset);
      Serializer serializer = broker.getSerializer();
      serializer.reset();
      try {
        Writer w = new OutputStreamWriter(response.getOutputStream(), charset);
        SAXSerializer sax =
            (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        Properties outputProperties = new Properties();
        sax.setOutput(w, outputProperties);
        serializer.setProperties(outputProperties);
        serializer.setSAXHandlers(sax, sax);

        serializer.toSAX(resultSequence, 1, 1, false);

        SerializerPool.getInstance().returnObject(sax);
        w.flush();
        w.close();
      } catch (IOException ex) {
        LOG.fatal("Cannot read resource " + path, ex);
        throw new EXistException("I/O error on read of resource " + path, ex);
      } catch (SAXException saxe) {
        LOG.warn(saxe);
        throw new BadRequestException("Error while serializing XML: " + saxe.getMessage());
      }
      resultSequence.itemAt(0);
    } catch (XPathException ex) {
      throw new EXistException("Cannot execute xquery " + entryByIdSource.getURL(), ex);
    } finally {
      xquery.getXQueryPool().returnCompiledXQuery(entryByIdSource, feedQuery);
    }
  }
예제 #2
0
  private static String serialize(DBBroker broker, Item item) throws SAXException, XPathException {
    Serializer serializer = broker.getSerializer();

    serializer.reset();
    String value;
    if (Type.subTypeOf(item.getType(), Type.NODE)) {
      value = serializer.serialize((NodeValue) item);
    } else {
      value = item.getStringValue();
    }
    return value;
  }
  public void configure(DBBroker broker, Collection parent, Map parameters)
      throws CollectionConfigurationException {
    super.configure(broker, parent, parameters);
    String stylesheet = (String) parameters.get("src");
    if (stylesheet == null)
      throw new CollectionConfigurationException(
          "STXTransformerTrigger requires an " + "attribute 'src'");
    String origProperty = System.getProperty("javax.xml.transform.TransformerFactory");
    System.setProperty(
        "javax.xml.transform.TransformerFactory", "net.sf.joost.trax.TransformerFactoryImpl");
    factory = (SAXTransformerFactory) TransformerFactory.newInstance();
    // reset property to previous setting
    if (origProperty != null)
      System.setProperty("javax.xml.transform.TransformerFactory", origProperty);

    getLogger().debug("compiling stylesheet " + stylesheet);
    XmldbURI stylesheetUri = null;
    try {
      stylesheetUri = XmldbURI.xmldbUriFor(stylesheet);
    } catch (URISyntaxException e) {
    }
    // TODO: allow full XmldbURIs to be used as well.
    if (stylesheetUri == null || stylesheet.indexOf(':') == Constants.STRING_NOT_FOUND) {
      stylesheetUri = parent.getURI().resolveCollectionPath(stylesheetUri);
      DocumentImpl doc;
      try {
        doc = (DocumentImpl) broker.getXMLResource(stylesheetUri);
        if (doc == null)
          throw new CollectionConfigurationException(
              "stylesheet " + stylesheetUri + " not found in database");
        Serializer serializer = broker.getSerializer();
        TemplatesHandler thandler = factory.newTemplatesHandler();
        serializer.setSAXHandlers(thandler, null);
        serializer.toSAX(doc);
        template = thandler.getTemplates();
        handler = factory.newTransformerHandler(template);
      } catch (TransformerConfigurationException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      } catch (PermissionDeniedException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      } catch (SAXException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      }
    } else
      try {
        template = factory.newTemplates(new StreamSource(stylesheet));
        handler = factory.newTransformerHandler(template);
      } catch (TransformerConfigurationException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      }
  }
예제 #4
0
  protected void handleGet(
      boolean returnContent, DBBroker broker, IncomingMessage request, OutgoingMessage response)
      throws BadRequestException, PermissionDeniedException, NotFoundException, EXistException {
    DocumentImpl resource = null;
    XmldbURI pathUri = XmldbURI.create(request.getPath());
    try {

      resource = broker.getXMLResource(pathUri, Lock.READ_LOCK);

      if (resource == null) {

        String id = request.getParameter("id");
        if (id != null) {
          id = id.trim();
          if (id.length() == 0) {
            id = null;
          }
        }
        // Must be a collection
        Collection collection = broker.getCollection(pathUri);
        if (collection != null) {
          if (!collection.getPermissions().validate(broker.getUser(), Permission.READ)) {
            throw new PermissionDeniedException("Not allowed to read collection");
          }
          DocumentImpl feedDoc = collection.getDocument(broker, FEED_DOCUMENT_URI);
          if (feedDoc == null) {
            throw new BadRequestException(
                "Collection " + request.getPath() + " is not an Atom feed.");
          }

          // Return the collection feed
          String charset = getContext().getDefaultCharset();
          if (returnContent) {
            if (id == null) {
              response.setStatusCode(200);
              getFeed(broker, request.getPath(), response);
            } else {
              response.setStatusCode(200);
              getEntryById(broker, request.getPath(), id, response);
            }
          } else {
            response.setStatusCode(204);
          }
        } else {
          throw new NotFoundException("Resource " + request.getPath() + " not found");
        }
      } else {
        // Do we have permission to read the resource
        if (!resource.getPermissions().validate(broker.getUser(), Permission.READ)) {
          throw new PermissionDeniedException("Not allowed to read resource");
        }

        if (returnContent) {
          response.setStatusCode(200);
          if (resource.getResourceType() == DocumentImpl.BINARY_FILE) {
            response.setContentType(resource.getMetadata().getMimeType());
            try {
              OutputStream os = response.getOutputStream();
              broker.readBinaryResource((BinaryDocument) resource, os);
              os.flush();
            } catch (IOException ex) {
              LOG.fatal("Cannot read resource " + request.getPath(), ex);
              throw new EXistException("I/O error on read of resource " + request.getPath(), ex);
            }
          } else {
            // xml resource
            Serializer serializer = broker.getSerializer();
            serializer.reset();

            String charset = getContext().getDefaultCharset();
            // Serialize the document
            try {
              response.setContentType(
                  resource.getMetadata().getMimeType() + "; charset=" + charset);
              Writer w = new OutputStreamWriter(response.getOutputStream(), charset);
              serializer.serialize(resource, w);
              w.flush();
              w.close();
            } catch (IOException ex) {
              LOG.fatal("Cannot read resource " + request.getPath(), ex);
              throw new EXistException("I/O error on read of resource " + request.getPath(), ex);
            } catch (SAXException saxe) {
              LOG.warn(saxe);
              throw new BadRequestException("Error while serializing XML: " + saxe.getMessage());
            }
          }
        } else {
          response.setStatusCode(204);
        }
      }
    } finally {
      if (resource != null) {
        resource.getUpdateLock().release(Lock.READ_LOCK);
      }
    }
  }
예제 #5
0
  @Test
  public void testOptimizations() throws XPathException, SAXException, PermissionDeniedException {

    Serializer serializer = broker.getSerializer();
    serializer.reset();
    DocumentSet docs = root.allDocs(broker, new DefaultDocumentSet(), true);

    System.out.println("------------ Testing NativeElementIndex.findChildNodesByTagName ---------");
    // parent set: 1.1.1; child set: 1.1.1.1, 1.1.1.2, 1.1.1.3, 1.1.2.1, 1.2.1
    ExtNodeSet nestedSet = (ExtNodeSet) executeQuery(broker, "//section[@n = '1.1.1']", 1, null);
    NodeSet children =
        broker
            .getStructuralIndex()
            .findDescendantsByTagName(
                ElementValue.ELEMENT,
                new QName("para", ""),
                Constants.CHILD_AXIS,
                docs,
                nestedSet,
                -1);
    assertEquals(3, children.getLength());

    // parent set: 1.1; child set: 1.1.1, 1.1.2
    nestedSet = (ExtNodeSet) executeQuery(broker, "//section[@n = '1.1']", 1, null);
    children =
        broker
            .getStructuralIndex()
            .findDescendantsByTagName(
                ElementValue.ELEMENT,
                new QName("section", ""),
                Constants.CHILD_AXIS,
                docs,
                nestedSet,
                -1);
    assertEquals(2, children.getLength());

    // parent set: 1, 1.1, 1.1.1, 1.1.2 ; child set: 1.1.1.1, 1.1.1.2, 1.1.1.3, 1.1.2.1, 1.2.1
    // problem: ancestor set contains nested nodes
    nestedSet =
        (ExtNodeSet) executeQuery(broker, "//section[@n = ('1.1', '1.1.1', '1.1.2')]", 3, null);
    children =
        broker
            .getStructuralIndex()
            .findDescendantsByTagName(
                ElementValue.ELEMENT,
                new QName("para", ""),
                Constants.CHILD_AXIS,
                docs,
                nestedSet,
                -1);
    assertEquals(4, children.getLength());

    // parent set: 1.1, 1.1.2, 1.2 ; child set: 1.1.1.1, 1.1.1.2, 1.1.1.3, 1.1.2.1, 1.2.1
    // problem: ancestor set contains nested nodes
    nestedSet =
        (ExtNodeSet) executeQuery(broker, "//section[@n = ('1.1', '1.1.2', '1.2')]", 3, null);
    children =
        broker
            .getStructuralIndex()
            .findDescendantsByTagName(
                ElementValue.ELEMENT,
                new QName("para", ""),
                Constants.CHILD_AXIS,
                docs,
                nestedSet,
                -1);
    assertEquals(2, children.getLength());

    nestedSet = (ExtNodeSet) executeQuery(broker, "//section[@n = '1.1']", 1, null);
    children =
        broker
            .getStructuralIndex()
            .findDescendantsByTagName(
                ElementValue.ELEMENT,
                new QName("para", ""),
                Constants.DESCENDANT_AXIS,
                docs,
                nestedSet,
                -1);
    assertEquals(4, children.getLength());

    nestedSet = (ExtNodeSet) executeQuery(broker, "//section[@n = '1']", 1, null);
    children =
        broker
            .getStructuralIndex()
            .findDescendantsByTagName(
                ElementValue.ELEMENT,
                new QName("para", ""),
                Constants.DESCENDANT_AXIS,
                docs,
                nestedSet,
                -1);
    assertEquals(5, children.getLength());

    nestedSet = (ExtNodeSet) executeQuery(broker, "//section[@n = '1.1.2']", 1, null);
    children =
        broker
            .getStructuralIndex()
            .findDescendantsByTagName(
                ElementValue.ELEMENT,
                new QName("section", ""),
                Constants.DESCENDANT_SELF_AXIS,
                docs,
                nestedSet,
                -1);
    assertEquals(1, children.getLength());

    nestedSet = (ExtNodeSet) executeQuery(broker, "//section[@n = '1.1.2']", 1, null);
    children =
        broker
            .getStructuralIndex()
            .findDescendantsByTagName(
                ElementValue.ATTRIBUTE,
                new QName("n", ""),
                Constants.ATTRIBUTE_AXIS,
                docs,
                nestedSet,
                -1);
    assertEquals(1, children.getLength());

    nestedSet = (ExtNodeSet) executeQuery(broker, "//section[@n = '1.1']", 1, null);
    children =
        broker
            .getStructuralIndex()
            .findDescendantsByTagName(
                ElementValue.ATTRIBUTE,
                new QName("n", ""),
                Constants.DESCENDANT_ATTRIBUTE_AXIS,
                docs,
                nestedSet,
                -1);
    assertEquals(7, children.getLength());

    System.out.println("------------ PASSED: NativeElementIndex.findChildNodesByTagName ---------");
  }