Search in sources :

Example 6 with NSArray

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

the class AjaxFileUploadRequestHandler method handleRequest.

@Override
public WOResponse handleRequest(WORequest request) {
    WOApplication application = WOApplication.application();
    application.awake();
    try {
        WOContext context = application.createContextForRequest(request);
        WOResponse response = application.createResponseInContext(context);
        String uploadIdentifier = null;
        String uploadFileName = null;
        InputStream uploadInputStream = null;
        long streamLength = -1L;
        try {
            String sessionIdKey = WOApplication.application().sessionIdKey();
            String sessionId = request.cookieValueForKey(sessionIdKey);
            WOMultipartIterator multipartIterator = request.multipartIterator();
            if (multipartIterator == null) {
                response.appendContentString("Already Consumed!");
            } else {
                WOMultipartIterator.WOFormData formData = null;
                while ((formData = multipartIterator.nextFormData()) != null) {
                    String name = formData.name();
                    if (sessionIdKey.equals(name)) {
                        sessionId = formData.formValue();
                    } else if ("id".equals(name)) {
                        uploadIdentifier = formData.formValue();
                    } else if (formData.isFileUpload()) {
                        uploadFileName = request.stringFormValueForKey(name + ".filename");
                        streamLength = multipartIterator.contentLengthRemaining();
                        uploadInputStream = formData.formDataInputStream();
                        break;
                    }
                }
                context._setRequestSessionID(sessionId);
                WOSession session = null;
                if (context._requestSessionID() != null) {
                    session = WOApplication.application().restoreSessionWithID(sessionId, context);
                }
                if (session == null) {
                    throw new Exception("No valid session!");
                }
                File tempFile = File.createTempFile("AjaxFileUpload", ".tmp", _tempFileFolder);
                tempFile.deleteOnExit();
                AjaxUploadProgress progress = new AjaxUploadProgress(uploadIdentifier, tempFile, uploadFileName, streamLength);
                try {
                    AjaxProgressBar.registerProgress(session, progress);
                } finally {
                    if (context._requestSessionID() != null) {
                        WOApplication.application().saveSessionForContext(context);
                    }
                }
                if (formData != null) {
                    NSArray<String> contentType = (NSArray<String>) formData.headers().valueForKey("content-type");
                    if (contentType != null) {
                        progress.setContentType(contentType.objectAtIndex(0));
                    }
                }
                try {
                    if (_maxUploadSize >= 0L && streamLength > _maxUploadSize) {
                        IOException e = new IOException("You attempted to upload a file larger than the maximum allowed size of " + new ERXUnitAwareDecimalFormat(ERXUnitAwareDecimalFormat.BYTE).format(_maxUploadSize) + ".");
                        progress.setFailure(e);
                        progress.dispose();
                        throw e;
                    }
                    try (FileOutputStream fos = new FileOutputStream(progress.tempFile())) {
                        progress.copyAndTrack(uploadInputStream, fos, _maxUploadSize);
                    }
                    if (!progress.isCanceled() && !progress.shouldReset()) {
                        downloadFinished(progress);
                    }
                } finally {
                    progress.setDone(true);
                }
            }
        } catch (Throwable t) {
            log.error("Upload failed", t);
            response.appendContentString("Failed: " + t.getMessage());
        } finally {
            if (uploadInputStream != null) {
                try {
                    uploadInputStream.close();
                } catch (IOException e) {
                // ignore
                }
            }
        }
        return response;
    } finally {
        application.sleep();
    }
}
Also used : NSArray(com.webobjects.foundation.NSArray) InputStream(java.io.InputStream) WOMultipartIterator(com.webobjects.appserver.WOMultipartIterator) IOException(java.io.IOException) ERXUnitAwareDecimalFormat(er.extensions.formatters.ERXUnitAwareDecimalFormat) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) WOContext(com.webobjects.appserver.WOContext) WOSession(com.webobjects.appserver.WOSession) WOResponse(com.webobjects.appserver.WOResponse) File(java.io.File) WOApplication(com.webobjects.appserver.WOApplication)

Example 7 with NSArray

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

the class AjaxTreeModel method isLeaf.

public boolean isLeaf(Object node) {
    boolean isLeaf;
    if (_isLeafKeyPath == null) {
        NSArray childrenTreeNodes = childrenTreeNodes(node);
        isLeaf = childrenTreeNodes == null || childrenTreeNodes.count() == 0;
    } else if (_delegate.respondsTo("isLeaf")) {
        isLeaf = _delegate.booleanPerform("isLeaf", node);
    } else {
        Boolean isLeafBoolean = (Boolean) NSKeyValueCodingAdditions.Utility.valueForKeyPath(node, _isLeafKeyPath);
        isLeaf = isLeafBoolean.booleanValue();
    }
    return isLeaf;
}
Also used : NSArray(com.webobjects.foundation.NSArray)

Example 8 with NSArray

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

the class AjaxSelectionList method selectedIndex.

public int selectedIndex() {
    NSArray list = list();
    int selectedIndex;
    Object selection = selection();
    if (selection == null) {
        selectedIndex = -1;
    } else {
        selectedIndex = list.indexOfObject(selection);
    }
    return selectedIndex;
}
Also used : NSArray(com.webobjects.foundation.NSArray)

Example 9 with NSArray

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

the class AjaxUtils method arrayValueForObject.

/**
 * Returns the array for the given object.  If the object is a string, it will be parsed as a
 * JSON value.
 *
 * @param <T> the array type
 * @param value the object value
 * @return an array (or null)
 */
@SuppressWarnings("unchecked")
public static <T> NSArray<T> arrayValueForObject(Object value) {
    NSArray arrayValue;
    if (value == null) {
        arrayValue = null;
    } else if (value instanceof NSArray) {
        arrayValue = (NSArray<T>) value;
    } else if (value instanceof String) {
        try {
            String strValue = ((String) value).trim();
            if (!strValue.startsWith("[")) {
                strValue = "[" + strValue + "]";
            }
            JSONSerializer serializer = new JSONSerializer();
            serializer.registerDefaultSerializers();
            Object objValue = serializer.fromJSON(strValue);
            if (objValue.getClass().isArray()) {
                arrayValue = new NSArray((Object[]) objValue);
            } else if (objValue instanceof Collection) {
                arrayValue = new NSArray((Collection) objValue);
            } else {
                arrayValue = new NSArray(objValue);
            }
        } catch (Throwable e) {
            throw new IllegalArgumentException("Failed to convert String to array.", e);
        }
    } else {
        throw new IllegalArgumentException("Unable to convert '" + value + "' to an array.");
    }
    return arrayValue;
}
Also used : NSArray(com.webobjects.foundation.NSArray) Collection(java.util.Collection) JSONSerializer(org.jabsorb.JSONSerializer)

Example 10 with NSArray

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

the class AjaxValue method javascriptValue.

/**
 * @return a String representing this AjaxValue in a form suitable for use in JavaScript
 */
public String javascriptValue() {
    String strValue;
    AjaxOption.Type type = _type;
    if (type == AjaxOption.STRING_OR_ARRAY) {
        if (_value == null) {
            type = AjaxOption.STRING;
        } else if (_value instanceof NSArray) {
            type = AjaxOption.ARRAY;
        } else if (_value instanceof String) {
            strValue = (String) _value;
            if (strValue.startsWith("[")) {
                type = AjaxOption.ARRAY;
            } else {
                type = AjaxOption.STRING;
            }
        }
    }
    if (_value == null || _value == NSKeyValueCoding.NullValue) {
        strValue = null;
    } else if (type == AjaxOption.STRING) {
        strValue = javaScriptEscaped(_value);
    } else if (type == AjaxOption.NUMBER) {
        strValue = _value.toString();
    } else if (type == AjaxOption.ARRAY) {
        if (_value instanceof NSArray) {
            NSArray arrayValue = (NSArray) _value;
            StringBuilder sb = new StringBuilder();
            sb.append('[');
            Enumeration objEnum = arrayValue.objectEnumerator();
            while (objEnum.hasMoreElements()) {
                Object o = objEnum.nextElement();
                sb.append(new AjaxValue(o).javascriptValue());
                if (objEnum.hasMoreElements()) {
                    sb.append(',');
                }
            }
            sb.append(']');
            strValue = sb.toString();
        } else {
            strValue = _value.toString();
        }
    } else if (type == AjaxOption.DICTIONARY) {
        if (_value instanceof NSDictionary) {
            NSDictionary dictValue = (NSDictionary) _value;
            StringBuilder sb = new StringBuilder();
            sb.append('{');
            Enumeration keyEnum = dictValue.keyEnumerator();
            while (keyEnum.hasMoreElements()) {
                Object key = keyEnum.nextElement();
                Object value = dictValue.objectForKey(key);
                sb.append(new AjaxValue(key).javascriptValue());
                sb.append(':');
                sb.append(new AjaxValue(value).javascriptValue());
                if (keyEnum.hasMoreElements()) {
                    sb.append(',');
                }
            }
            sb.append('}');
            strValue = sb.toString();
        } else {
            strValue = _value.toString();
        }
    } else if (type == AjaxOption.STRING_ARRAY) {
        if (_value instanceof NSArray) {
            NSArray arrayValue = (NSArray) _value;
            int count = arrayValue.count();
            if (count == 1) {
                strValue = new AjaxValue(AjaxOption.STRING, arrayValue.objectAtIndex(0)).javascriptValue();
            } else if (count > 0) {
                StringBuilder sb = new StringBuilder();
                sb.append('[');
                Enumeration objEnum = arrayValue.objectEnumerator();
                while (objEnum.hasMoreElements()) {
                    Object o = objEnum.nextElement();
                    sb.append(new AjaxValue(AjaxOption.STRING, o).javascriptValue());
                    if (objEnum.hasMoreElements()) {
                        sb.append(',');
                    }
                }
                sb.append(']');
                strValue = sb.toString();
            } else {
                strValue = "[]";
            }
        } else {
            strValue = _value.toString();
        }
    } else if (type == AjaxOption.SCRIPT) {
        strValue = _value.toString();
    } else if (type == AjaxOption.FUNCTION) {
        strValue = "function() {" + _value.toString() + "}";
    } else if (type == AjaxOption.FUNCTION_1) {
        strValue = "function(v) {" + _value.toString() + "}";
    } else if (type == AjaxOption.FUNCTION_2) {
        strValue = "function(v1, v2) {" + _value.toString() + "}";
    } else {
        strValue = _value.toString();
    }
    return strValue;
}
Also used : Enumeration(java.util.Enumeration) NSArray(com.webobjects.foundation.NSArray) NSDictionary(com.webobjects.foundation.NSDictionary)

Aggregations

NSArray (com.webobjects.foundation.NSArray)53 Enumeration (java.util.Enumeration)17 NSMutableArray (com.webobjects.foundation.NSMutableArray)13 NSBundle (com.webobjects.foundation.NSBundle)5 NSMutableDictionary (com.webobjects.foundation.NSMutableDictionary)5 File (java.io.File)4 List (java.util.List)4 WOAssociation (com.webobjects.appserver.WOAssociation)3 WOComponent (com.webobjects.appserver.WOComponent)3 NSDictionary (com.webobjects.foundation.NSDictionary)3 IOException (java.io.IOException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 WOElement (com.webobjects.appserver.WOElement)2 WOResponse (com.webobjects.appserver.WOResponse)2 WOConstantValueAssociation (com.webobjects.appserver._private.WOConstantValueAssociation)2 EOEvent (com.webobjects.eocontrol.EOEvent)2 EOSortOrdering (com.webobjects.eocontrol.EOSortOrdering)2 NSForwardException (com.webobjects.foundation.NSForwardException)2 NSKeyValueCoding (com.webobjects.foundation.NSKeyValueCoding)2