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;
}
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);
}
}
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();
}
}
Aggregations