Search in sources :

Example 1 with NSData

use of com.webobjects.foundation.NSData in project wonder-slim by undur.

the class AjaxFileUpload method uploadSucceeded.

public WOActionResults uploadSucceeded() {
    AjaxUploadProgress progress = uploadProgress();
    try {
        boolean deleteFile = true;
        if (hasBinding("filePath")) {
            setValueForBinding(progress.fileName(), "filePath");
        }
        if (hasBinding("data")) {
            NSData data = new NSData(progress.tempFile().toURI().toURL());
            setValueForBinding(data, "data");
        }
        if (hasBinding("mimeType")) {
            String contentType = progress.contentType();
            if (contentType != null) {
                setValueForBinding(contentType, "mimeType");
            }
        }
        if (hasBinding("inputStream")) {
            setValueForBinding(new FileInputStream(progress.tempFile()), "inputStream");
            deleteFile = false;
        }
        if (hasBinding("outputStream")) {
            try (OutputStream outputStream = (OutputStream) valueForBinding("outputStream")) {
                if (outputStream != null) {
                    try (InputStream inputStream = new FileInputStream(progress.tempFile())) {
                        inputStream.transferTo(outputStream);
                    }
                }
            }
        }
        String finalFilePath = progress.tempFile().getAbsolutePath();
        if (hasBinding("streamToFilePath")) {
            File streamToFile = new File((String) valueForBinding("streamToFilePath"));
            boolean renamedFile;
            boolean renameFile;
            if (streamToFile.exists()) {
                boolean overwrite = ERXComponentUtilities.booleanValueForBinding(this, "overwrite");
                if (streamToFile.isDirectory()) {
                    File parentDir = streamToFile;
                    String fileName = fileNameFromBrowserSubmittedPath(progress.fileName());
                    streamToFile = reserveUniqueFile(new File(parentDir, fileName), overwrite);
                    renameFile = true;
                } else {
                    renameFile = overwrite;
                }
            } else {
                renameFile = true;
            }
            if (renameFile && !streamToFile.isDirectory()) {
                renameTo(progress.tempFile(), streamToFile);
                renamedFile = true;
            } else {
                renamedFile = false;
                progress.setFailure(new Exception("Could not rename file."));
                return uploadFailed();
            }
            if (renamedFile) {
                finalFilePath = streamToFile.getAbsolutePath();
            }
            deleteFile = false;
        } else if (hasBinding("keepTempFile") && deleteFile) {
            deleteFile = !ERXComponentUtilities.booleanValueForBinding(this, "keepTempFile");
        }
        if (deleteFile) {
            progress.dispose();
        } else if (hasBinding("finalFilePath")) {
            setValueForBinding(finalFilePath, "finalFilePath");
        }
    } catch (Throwable t) {
        t.printStackTrace();
        progress.setFailure(t);
        return uploadFailed();
    } finally {
        uploadFinished();
    }
    WOActionResults results = (WOActionResults) valueForBinding("succeededAction");
    return results;
}
Also used : NSData(com.webobjects.foundation.NSData) WOActionResults(com.webobjects.appserver.WOActionResults) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 2 with NSData

use of com.webobjects.foundation.NSData in project wonder-slim by undur.

the class NSDataSerializer method unmarshall.

public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    try {
        JSONObject jso = (JSONObject) o;
        String java_class = jso.getString("javaClass");
        if (java_class == null) {
            throw new UnmarshallException("no type hint");
        }
        String string = jso.getString("bytes");
        NSData data = (NSData) NSPropertyListSerialization.propertyListFromString(string);
        if (NSMutableData.class.equals(clazz)) {
            NSMutableData mutableData = new NSMutableData(data);
            state.setSerialized(o, mutableData);
            return mutableData;
        } else if (NSData.class.equals(clazz)) {
            state.setSerialized(o, data);
            return data;
        }
        throw new UnmarshallException("invalid class " + clazz);
    } catch (JSONException e) {
        throw new UnmarshallException("Failed to unmarshall NSData.", e);
    }
}
Also used : NSData(com.webobjects.foundation.NSData) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) NSMutableData(com.webobjects.foundation.NSMutableData) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 3 with NSData

use of com.webobjects.foundation.NSData in project wonder-slim by undur.

the class NSDataSerializer method tryUnmarshall.

public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    try {
        JSONObject jso = (JSONObject) o;
        String java_class = jso.getString("javaClass");
        if (java_class == null) {
            throw new UnmarshallException("no type hint");
        }
        String string = jso.getString("bytes");
        NSData data = (NSData) NSPropertyListSerialization.propertyListFromString(string);
        if (NSData.class.isAssignableFrom(clazz)) {
            return ObjectMatch.OKAY;
        }
        throw new UnmarshallException("invalid class " + clazz);
    } catch (JSONException e) {
        throw new UnmarshallException("Failed to unmarshall NSData.", e);
    }
}
Also used : NSData(com.webobjects.foundation.NSData) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 4 with NSData

use of com.webobjects.foundation.NSData in project wonder-slim by undur.

the class NSDataSerializer method marshall.

public Object marshall(SerializerState state, Object p, Object o) throws MarshallException {
    try {
        String bytes;
        if (o instanceof NSData) {
            bytes = NSPropertyListSerialization.stringFromPropertyList(o);
        } else {
            throw new MarshallException("cannot marshall date using class " + o.getClass());
        }
        JSONObject obj = new JSONObject();
        if (ser.getMarshallClassHints()) {
            obj.put("javaClass", o.getClass().getName());
        }
        obj.put("bytes", bytes);
        return obj;
    } catch (JSONException e) {
        throw new MarshallException("Failed to marshall NSData.", e);
    }
}
Also used : NSData(com.webobjects.foundation.NSData) JSONObject(org.json.JSONObject) MarshallException(org.jabsorb.serializer.MarshallException) JSONException(org.json.JSONException)

Example 5 with NSData

use of com.webobjects.foundation.NSData in project wonder-slim by undur.

the class ERXResponseCompression method compressResponse.

public static WOResponse compressResponse(final WOResponse response) {
    long start = System.currentTimeMillis();
    long inputBytesLength;
    InputStream contentInputStream = response.contentInputStream();
    NSData compressedData;
    if (contentInputStream != null) {
        inputBytesLength = response.contentInputStreamLength();
        compressedData = ERXCompressionUtilities.gzipInputStreamAsNSData(contentInputStream, (int) inputBytesLength);
        response.setContentStream(null, 0, 0);
    } else {
        NSData input = response.content();
        inputBytesLength = input.length();
        if (inputBytesLength > 0) {
            compressedData = ERXCompressionUtilities.gzipByteArrayAsNSData(input._bytesNoCopy(), 0, (int) inputBytesLength);
        } else {
            compressedData = NSData.EmptyData;
        }
    }
    if (inputBytesLength > 0) {
        if (compressedData == null) {
        // something went wrong
        } else {
            response.setContent(compressedData);
            response.setHeader(String.valueOf(compressedData.length()), "content-length");
            response.setHeader("gzip", "content-encoding");
            if (log.isDebugEnabled()) {
                log.debug("before: " + inputBytesLength + ", after " + compressedData.length() + ", time: " + (System.currentTimeMillis() - start));
            }
        }
    }
    return response;
}
Also used : NSData(com.webobjects.foundation.NSData) InputStream(java.io.InputStream)

Aggregations

NSData (com.webobjects.foundation.NSData)6 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 NSMutableData (com.webobjects.foundation.NSMutableData)2 InputStream (java.io.InputStream)2 UnmarshallException (org.jabsorb.serializer.UnmarshallException)2 WOActionResults (com.webobjects.appserver.WOActionResults)1 NSRange (com.webobjects.foundation.NSRange)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 MarshallException (org.jabsorb.serializer.MarshallException)1