Search in sources :

Example 1 with QueryException

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

the class ExpenseServlet method doPost.

@Override
protected URL doPost(Path path, Object value) throws QueryException {
    if (value == null) {
        throw new QueryException(Query.Status.BAD_REQUEST);
    }
    Expense expense = (Expense) value;
    // Add the expense to the list/map
    int id;
    synchronized (this) {
        id = nextID++;
        expense.setID(id);
        expenses.add(expense);
        expenseMap.put(id, expense);
    }
    // Return the location of the newly-created resource
    URL location = getLocation();
    try {
        location = new URL(location, Integer.toString(id));
    } catch (MalformedURLException exception) {
        throw new QueryException(Query.Status.INTERNAL_SERVER_ERROR);
    }
    return location;
}
Also used : QueryException(org.apache.pivot.web.QueryException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Example 2 with QueryException

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

the class RESTDemoServlet method doDelete.

@Override
protected void doDelete(Path path) throws QueryException {
    if (path.getLength() != 1) {
        throw new QueryException(Query.Status.BAD_REQUEST);
    }
    // Delete the 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);
    }
    boolean deleted = file.delete();
    if (!deleted) {
        throw new QueryException(Query.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : QueryException(org.apache.pivot.web.QueryException) File(java.io.File)

Example 3 with QueryException

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

the class RESTDemoServlet method doGet.

@SuppressWarnings("resource")
@Override
protected Object doGet(Path path) throws QueryException {
    if (path.getLength() != 1) {
        throw new QueryException(Query.Status.BAD_REQUEST);
    }
    // Read the value from 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);
    }
    Object value;
    try {
        JSONSerializer jsonSerializer = new JSONSerializer();
        value = jsonSerializer.readObject(new FileInputStream(file));
    } catch (IOException exception) {
        throw new QueryException(Query.Status.INTERNAL_SERVER_ERROR);
    } catch (SerializationException exception) {
        throw new QueryException(Query.Status.INTERNAL_SERVER_ERROR);
    }
    return value;
}
Also used : QueryException(org.apache.pivot.web.QueryException) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 4 with QueryException

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

the class QueryServlet method doPut.

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

Example 5 with QueryException

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

the class RESTDemoServlet method doPost.

@SuppressWarnings("resource")
@Override
protected URL doPost(Path path, Object value) throws QueryException {
    if (path.getLength() > 0 || value == null) {
        throw new QueryException(Query.Status.BAD_REQUEST);
    }
    // Write the value to a temp file
    File directory = new File(System.getProperty("java.io.tmpdir"));
    File file;
    try {
        file = File.createTempFile(getClass().getName(), null, directory);
        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 the location of the resource
    URL location;
    try {
        location = new URL(getLocation(), file.getName());
    } catch (MalformedURLException exception) {
        throw new QueryException(Query.Status.INTERNAL_SERVER_ERROR);
    }
    return location;
}
Also used : QueryException(org.apache.pivot.web.QueryException) MalformedURLException(java.net.MalformedURLException) SerializationException(org.apache.pivot.serialization.SerializationException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) JSONSerializer(org.apache.pivot.json.JSONSerializer)

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