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