Пример #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
  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);
      }
    }
  }