Package org.exist.http

Examples of org.exist.http.BadRequestException


          }

          final 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.getSubject(),
            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 {
              final OutputStream os = response.getOutputStream();
              broker.readBinaryResource(
                  (BinaryDocument) resource, os);
              os.flush();
            } catch (final 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
            final Serializer serializer = broker.getSerializer();
            serializer.reset();

            final String charset = getContext().getDefaultCharset();
            // Serialize the document
            try {
              response.setContentType(resource.getMetadata()
                  .getMimeType() + "; charset=" + charset);
              final Writer w = new OutputStreamWriter(
                  response.getOutputStream(), charset);
              serializer.serialize(resource, w);
              w.flush();
              w.close();
            } catch (final IOException ex) {
              LOG.fatal(
                  "Cannot read resource " + request.getPath(),
                  ex);
              throw new EXistException(
                  "I/O error on read of resource "
                      + request.getPath(), ex);
            } catch (final SAXException saxe) {
              LOG.warn(saxe);
              throw new BadRequestException(
                  "Error while serializing XML: "
                      + saxe.getMessage());
            }
          }
View Full Code Here


    try {
      context.declareVariable("id", id);
      final Sequence resultSequence = xquery.execute(feedQuery, null);
      if (resultSequence.isEmpty()) {
        throw new BadRequestException("No topic was found.");
      }

      final String charset = getContext().getDefaultCharset();
      response.setContentType("application/atom+xml; charset=" + charset);
      final Serializer serializer = broker.getSerializer();
      serializer.reset();
      try {
        final Writer w = new OutputStreamWriter(response.getOutputStream(), charset);
        final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        final Properties outputProperties = new Properties();
        sax.setOutput(w, outputProperties);
        serializer.setProperties(outputProperties);
        serializer.setSAXHandlers(sax, sax);

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

        SerializerPool.getInstance().returnObject(sax);
        w.flush();
        w.close();
      } catch (final IOException ex) {
        LOG.fatal("Cannot read resource " + path, ex);
        throw new EXistException("I/O error on read of resource "
            + path, ex);
      } catch (final SAXException saxe) {
        LOG.warn(saxe);
        throw new BadRequestException("Error while serializing XML: "
            + saxe.getMessage());
      }
      resultSequence.itemAt(0);

    } catch (final XPathException ex) {
View Full Code Here

    );

    try {
      final Sequence resultSequence = xquery.execute(feedQuery, null);
      if (resultSequence.isEmpty()) {
        throw new BadRequestException("No feed was found.");
      }

      final String charset = getContext().getDefaultCharset();
      response.setContentType("application/atom+xml; charset=" + charset);
      final Serializer serializer = broker.getSerializer();
      serializer.reset();

      try {
        final Writer w = new OutputStreamWriter(response.getOutputStream(), charset);
        final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        final Properties outputProperties = new Properties();
        sax.setOutput(w, outputProperties);
        serializer.setProperties(outputProperties);
        serializer.setSAXHandlers(sax, sax);

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

        SerializerPool.getInstance().returnObject(sax);
        w.flush();
        w.close();

      } catch (final IOException ex) {
        LOG.fatal("Cannot read resource " + path, ex);
        throw new EXistException("I/O error on read of resource " + path, ex);
      } catch (final SAXException saxe) {
        LOG.warn(saxe);
        throw new BadRequestException("Error while serializing XML: " + saxe.getMessage());
      }
      resultSequence.itemAt(0);

    } catch (final XPathException ex) {
      throw new EXistException("Cannot execute xquery " + getFeedSource.getURL(), ex);
View Full Code Here

      // TODO: handle post body
      doQuery(broker, request, response, post);
    } else if (allowQueryPost) {
      final Collection collection = broker.getCollection(XmldbURI.create(request.getPath()));
      if (collection == null)
        {throw new BadRequestException("Collection " + request.getPath() + " does not exist.");}

      final XQuery xquery = broker.getXQueryService();

      final XQueryContext context = xquery.newContext(AccessContext.REST);
      context.setModuleLoadPath(getContext().getModuleLoadPath());

      String contentType = request.getHeader("Content-Type");
      String charset = getContext().getDefaultCharset();

      MimeType mime = MimeType.XML_TYPE;
      if (contentType != null) {
        final int semicolon = contentType.indexOf(';');
        if (semicolon > 0) {
          contentType = contentType.substring(0, semicolon).trim();
        }
        mime = MimeTable.getInstance().getContentType(contentType);
        final int equals = contentType.indexOf('=', semicolon);
        if (equals > 0) {
          final String param = contentType.substring(semicolon + 1, equals).trim();
          if (param.compareToIgnoreCase("charset=") == 0) {
            charset = param.substring(equals + 1).trim();
          }
        }
      }

      if (!mime.isXMLType() && !mime.equals(xqueryMimeType)) {
        throw new BadRequestException(
            "The xquery mime type is not an XML mime type nor application/xquery");
      }

      CompiledXQuery compiledQuery = null;
      try {
        final StringBuilder builder = new StringBuilder();
        final Reader r = new InputStreamReader(request.getInputStream(), charset);
        final char[] buffer = new char[4096];
        int len;
        long count = 0;
        final long contentLength = request.getContentLength();
        while ((len = r.read(buffer)) >= 0 && count < contentLength) {
          count += len;
          builder.append(buffer, 0, len);
        }
        compiledQuery = xquery.compile(context, new StringSource(builder.toString()));
     
      } catch (final XPathException ex) {
        throw new EXistException("Cannot compile xquery.", ex);
      } catch (final IOException ex) {
        throw new EXistException(
            "I/O exception while compiling xquery.", ex);
      }

      context.setStaticallyKnownDocuments(
        new XmldbURI[] {
          XmldbURI.create(request.getPath()).append(AtomProtocol.FEED_DOCUMENT_NAME)
        }
      );

      try {
        final Sequence resultSequence = xquery.execute(compiledQuery, null);
        if (resultSequence.isEmpty()) {
          throw new BadRequestException("No topic was found.");
        }
        response.setStatusCode(200);
        response.setContentType(Atom.MIME_TYPE + "; charset=" + charset);
        final Serializer serializer = broker.getSerializer();
        serializer.reset();
        try {
          final Writer w = new OutputStreamWriter(
              response.getOutputStream(), charset);
          final SAXSerializer sax = (SAXSerializer) SerializerPool
              .getInstance().borrowObject(SAXSerializer.class);
          final Properties outputProperties = new Properties();
          sax.setOutput(w, outputProperties);
          serializer.setProperties(outputProperties);
          serializer.setSAXHandlers(sax, sax);

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

          SerializerPool.getInstance().returnObject(sax);
          w.flush();
          w.close();
        } catch (final IOException ex) {
          LOG.fatal("Cannot read resource " + request.getPath(), ex);
          throw new EXistException("I/O error on read of resource "
              + request.getPath(), ex);
        } catch (final SAXException saxe) {
          LOG.warn(saxe);
          throw new BadRequestException(
              "Error while serializing XML: " + saxe.getMessage());
        }
        resultSequence.itemAt(0);
      } catch (final XPathException ex) {
        throw new EXistException("Cannot execute xquery.", ex);
View Full Code Here

      NotFoundException, EXistException {

    final Collection collection = broker.getCollection(XmldbURI.create(request.getPath()));
   
    if (collection == null)
      {throw new BadRequestException("Collection " + request.getPath() + " does not exist.");}

    final XQuery xquery = broker.getXQueryService();
    CompiledXQuery feedQuery = xquery.getXQueryPool().borrowCompiledXQuery(broker, config.querySource);

    XQueryContext context;
    if (feedQuery == null) {
      context = xquery.newContext(AccessContext.REST);
      context.setModuleLoadPath(getContext().getModuleLoadPath());
      try {
        feedQuery = xquery.compile(context, config.querySource);
      } catch (final XPathException ex) {
        throw new EXistException("Cannot compile xquery "
            + config.querySource.getURL(), ex);
      } catch (final IOException ex) {
        throw new EXistException(
            "I/O exception while compiling xquery "
                + config.querySource.getURL(), ex);
      }
    } else {
      context = feedQuery.getContext();
      context.setModuleLoadPath(getContext().getModuleLoadPath());
    }

    context.setStaticallyKnownDocuments(new XmldbURI[] { XmldbURI.create(
        request.getPath()).append(AtomProtocol.FEED_DOCUMENT_NAME) });

    try {
      declareVariables(context, request.getRequest(), response.getResponse());

      final Sequence resultSequence = xquery.execute(feedQuery, null);
      if (resultSequence.isEmpty())
        {throw new BadRequestException("No topic was found.");}

      final String charset = getContext().getDefaultCharset();
      response.setStatusCode(200);
      response.setContentType(config.contentType + "; charset=" + charset);
      final Serializer serializer = broker.getSerializer();
      serializer.reset();
      try {
        final Writer w = new OutputStreamWriter(response.getOutputStream(), charset);
        final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
       
        final Properties outputProperties = new Properties();
        sax.setOutput(w, outputProperties);
        serializer.setProperties(outputProperties);
        serializer.setSAXHandlers(sax, sax);

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

        SerializerPool.getInstance().returnObject(sax);
        w.flush();
        w.close();
      } catch (final IOException ex) {
        LOG.fatal("Cannot read resource " + request.getPath(), ex);
        throw new EXistException("I/O error on read of resource "
            + request.getPath(), ex);
      } catch (final SAXException saxe) {
        LOG.warn(saxe);
        throw new BadRequestException("Error while serializing XML: "
            + saxe.getMessage());
      }
      resultSequence.itemAt(0);
    } catch (final XPathException ex) {
      throw new EXistException("Cannot execute xquery "
View Full Code Here

    } else if ("DELETE".equals(method)) {
      doDelete(broker, request, response);

    } else {
      throw new BadRequestException("Method " + request.getMethod()
          + " is not supported by this module.");
    }
  }
View Full Code Here

  public void doGet(DBBroker broker, IncomingMessage request,
      OutgoingMessage response) throws BadRequestException,
      PermissionDeniedException, NotFoundException, EXistException {
   
    throw new BadRequestException("Method " + request.getMethod()
        + " is not supported by this module.");
  }
View Full Code Here

  public void doHead(DBBroker broker, IncomingMessage request,
      OutgoingMessage response) throws BadRequestException,
      PermissionDeniedException, NotFoundException, EXistException {
   
    throw new BadRequestException("Method " + request.getMethod()
        + " is not supported by this module.");
  }
View Full Code Here

  public void doPost(DBBroker broker, IncomingMessage request,
      OutgoingMessage response) throws BadRequestException,
      PermissionDeniedException, NotFoundException, EXistException {
   
    throw new BadRequestException("Method " + request.getMethod()
        + " is not supported by this module.");
  }
View Full Code Here

  public void doPut(DBBroker broker, IncomingMessage request,
      OutgoingMessage response) throws BadRequestException,
      PermissionDeniedException, NotFoundException, EXistException {
   
    throw new BadRequestException("Method " + request.getMethod()
        + " is not supported by this module.");
  }
View Full Code Here

TOP

Related Classes of org.exist.http.BadRequestException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.