Esempio n. 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);
    }
  }
Esempio n. 2
0
  public static void serialize(
      XQueryContext context, SequenceIterator siNode, Properties outputProperties, OutputStream os)
      throws IOException {

    LOG.debug("Serializing started.");
    SAXSerializer sax =
        (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
    try {
      String encoding = outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8");
      Writer writer = new OutputStreamWriter(os, encoding);
      sax.setOutput(writer, outputProperties);
      Serializer serializer = context.getBroker().getSerializer();
      serializer.reset();
      serializer.setProperties(outputProperties);
      serializer.setReceiver(sax);

      sax.startDocument();

      while (siNode.hasNext()) {
        NodeValue next = (NodeValue) siNode.nextItem();
        serializer.toSAX(next);
      }

      sax.endDocument();
      writer.close();

    } catch (Exception e) {
      String txt = "A problem ocurred while serializing the node set";
      LOG.debug(txt + ".", e);
      throw new ExistIOException(txt + ": " + e.getMessage(), e);

    } finally {
      LOG.debug("Serializing done.");
      SerializerPool.getInstance().returnObject(sax);
    }
  }