use of org.apache.pivot.json.JSONSerializer 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;
}
use of org.apache.pivot.json.JSONSerializer 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;
}
use of org.apache.pivot.json.JSONSerializer 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.json.JSONSerializer in project pivot by apache.
the class JSONViewer method paste.
public void paste() {
Manifest clipboardContent = Clipboard.getContent();
if (clipboardContent != null && clipboardContent.containsText()) {
String json = null;
JSONSerializer jsonSerializer = new JSONSerializer();
try {
json = clipboardContent.getText();
setValue(jsonSerializer.readObject(new StringReader(json)));
} catch (Exception exception) {
Prompt.prompt(exception.getMessage(), window);
}
window.setTitle(WINDOW_TITLE);
}
}
use of org.apache.pivot.json.JSONSerializer in project pivot by apache.
the class JSONViewer method drop.
public DropAction drop(Manifest dragContent) {
DropAction dropAction = null;
try {
FileList fileList = dragContent.getFileList();
if (fileList.getLength() == 1) {
File file = fileList.get(0);
JSONSerializer jsonSerializer = new JSONSerializer();
@SuppressWarnings("resource") FileInputStream fileInputStream = null;
try {
try {
fileInputStream = new FileInputStream(file);
setValue(jsonSerializer.readObject(fileInputStream));
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
}
} catch (Exception exception) {
Prompt.prompt(exception.getMessage(), window);
}
window.setTitle(WINDOW_TITLE + " - " + file.getName());
dropAction = DropAction.COPY;
} else {
Prompt.prompt("Multiple files not supported.", window);
}
} catch (IOException exception) {
Prompt.prompt(exception.getMessage(), window);
}
return dropAction;
}
Aggregations