Search in sources :

Example 6 with QueryException

use of org.apache.pivot.web.QueryException in project pivot by apache.

the class RESTDemoServlet method doPut.

@SuppressWarnings("resource")
@Override
protected boolean doPut(Path path, Object value) throws QueryException {
    if (path.getLength() != 1 || value == null) {
        throw new QueryException(Query.Status.BAD_REQUEST);
    }
    // Write the value to the temp file
    File directory = new File(System.getProperty("java.io.tmpdir"));
    File file = new File(directory, path.get(0));
    if (!file.exists()) {
        throw new QueryException(Query.Status.NOT_FOUND);
    }
    try {
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.writeObject(value, new FileOutputStream(file));
    } catch (IOException exception) {
        throw new QueryException(Query.Status.INTERNAL_SERVER_ERROR);
    } catch (SerializationException exception) {
        throw new QueryException(Query.Status.INTERNAL_SERVER_ERROR);
    }
    return false;
}
Also used : QueryException(org.apache.pivot.web.QueryException) SerializationException(org.apache.pivot.serialization.SerializationException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 7 with QueryException

use of org.apache.pivot.web.QueryException in project pivot by apache.

the class QueryServlet method doPost.

@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Path path = getPath(request);
    URL location = null;
    try {
        validate(Query.Method.POST, path);
        Object value = null;
        if (request.getContentLength() > 0) {
            Serializer<?> serializer = createSerializer(Query.Method.POST, path);
            value = serializer.readObject(request.getInputStream());
        }
        location = doPost(path, value);
    } catch (SerializationException exception) {
        throw new ServletException(exception);
    } catch (QueryException exception) {
        response.setStatus(exception.getStatus());
        response.flushBuffer();
    }
    if (!response.isCommitted()) {
        if (location == null) {
            response.setStatus(Query.Status.NO_CONTENT);
        } else {
            response.setStatus(Query.Status.CREATED);
            response.setHeader(LOCATION_HEADER, location.toString());
        }
        setResponseHeaders(response);
        response.setContentLength(0);
    }
}
Also used : ServletException(javax.servlet.ServletException) QueryException(org.apache.pivot.web.QueryException) SerializationException(org.apache.pivot.serialization.SerializationException) URL(java.net.URL)

Example 8 with QueryException

use of org.apache.pivot.web.QueryException in project pivot by apache.

the class QueryServlet method doGet.

@Override
@SuppressWarnings("unchecked")
protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Path path = getPath(request);
    Object result = null;
    Serializer<Object> serializer = null;
    try {
        validate(Query.Method.GET, path);
        result = doGet(path);
        serializer = (Serializer<Object>) createSerializer(Query.Method.GET, path);
    } catch (QueryException exception) {
        response.setStatus(exception.getStatus());
        response.flushBuffer();
    }
    if (!response.isCommitted() && serializer != null) {
        response.setStatus(Query.Status.OK);
        setResponseHeaders(response);
        response.setContentType(serializer.getMIMEType(result));
        OutputStream responseOutputStream = response.getOutputStream();
        if (determineContentLength) {
            File tempFile = File.createTempFile(getClass().getName(), null);
            // Serialize the result to an intermediary file
            try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) {
                serializer.writeObject(result, fileOutputStream);
            } catch (SerializationException exception) {
                throw new ServletException(exception);
            }
            // Set the content length header
            response.setHeader(CONTENT_LENGTH_HEADER, String.valueOf(tempFile.length()));
            // Write the contents of the file out to the response
            try (FileInputStream fileInputStream = new FileInputStream(tempFile)) {
                byte[] buffer = new byte[1024];
                int nBytes;
                do {
                    nBytes = fileInputStream.read(buffer);
                    if (nBytes > 0) {
                        responseOutputStream.write(buffer, 0, nBytes);
                    }
                } while (nBytes != -1);
            }
        } else {
            try {
                serializer.writeObject(result, responseOutputStream);
            } catch (SerializationException exception) {
                throw new ServletException(exception);
            }
        }
        response.flushBuffer();
    }
}
Also used : ServletException(javax.servlet.ServletException) QueryException(org.apache.pivot.web.QueryException) SerializationException(org.apache.pivot.serialization.SerializationException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

QueryException (org.apache.pivot.web.QueryException)8 SerializationException (org.apache.pivot.serialization.SerializationException)6 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 URL (java.net.URL)3 ServletException (javax.servlet.ServletException)3 JSONSerializer (org.apache.pivot.json.JSONSerializer)3 FileInputStream (java.io.FileInputStream)2 MalformedURLException (java.net.MalformedURLException)2 OutputStream (java.io.OutputStream)1