Search in sources :

Example 6 with JSONSerializer

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;
}
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 7 with JSONSerializer

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

Example 8 with JSONSerializer

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;
}
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 9 with JSONSerializer

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);
    }
}
Also used : StringReader(java.io.StringReader) Manifest(org.apache.pivot.wtk.Manifest) IOException(java.io.IOException) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 10 with JSONSerializer

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;
}
Also used : FileList(org.apache.pivot.io.FileList) DropAction(org.apache.pivot.wtk.DropAction) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Aggregations

JSONSerializer (org.apache.pivot.json.JSONSerializer)25 IOException (java.io.IOException)11 Test (org.junit.Test)11 SerializationException (org.apache.pivot.serialization.SerializationException)8 StringReader (java.io.StringReader)6 ArrayList (org.apache.pivot.collections.ArrayList)5 HashMap (org.apache.pivot.collections.HashMap)5 List (org.apache.pivot.collections.List)5 File (java.io.File)4 InputStream (java.io.InputStream)4 URL (java.net.URL)3 Map (org.apache.pivot.collections.Map)3 QueryException (org.apache.pivot.web.QueryException)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 Color (java.awt.Color)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 BeanAdapter (org.apache.pivot.beans.BeanAdapter)1