Search in sources :

Example 1 with WOResponse

use of com.webobjects.appserver.WOResponse 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 2 with WOResponse

use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.

the class AjaxHyperlink method handleRequest.

@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
    WOComponent component = context.component();
    WOActionResults results = (WOActionResults) valueForBinding("action", component);
    if (results == null) {
        String script = (String) valueForBinding("onClickServer", component);
        if (script != null) {
            WOResponse response = AjaxUtils.createResponse(request, context);
            AjaxUtils.appendScriptHeader(response);
            response.appendContentString(script);
            AjaxUtils.appendScriptFooter(response);
            results = response;
        }
    }
    return results;
}
Also used : WOActionResults(com.webobjects.appserver.WOActionResults) WOComponent(com.webobjects.appserver.WOComponent) WOResponse(com.webobjects.appserver.WOResponse)

Example 3 with WOResponse

use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.

the class AjaxInPlaceEditor method handleRequest.

// Formatting/Parsing method "inspired by" WOTextField
@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
    WOComponent component = context.component();
    String strValue = request.stringFormValueForKey("value");
    Object objValue = strValue;
    if (strValue != null) {
        Format format = null;
        if (strValue.length() != 0) {
            format = WOFormatterRepository.formatterForComponent(component, _dateFormat, _numberFormat, _formatter);
        }
        if (format != null) {
            try {
                Object parsedValue = format.parseObject(strValue);
                String formattedValue = format.format(parsedValue);
                objValue = format.parseObject(formattedValue);
            } catch (ParseException parseexception) {
                String valueKeyPath = _valueAssociation.keyPath();
                ValidationException validationexception = new ValidationException(parseexception.getMessage(), strValue, valueKeyPath);
                component.validationFailedWithException(validationexception, strValue, valueKeyPath);
                return null;
            }
            if (objValue != null && _useDecimalNumber != null && _useDecimalNumber.booleanValueInComponent(component)) {
                objValue = new BigDecimal(objValue.toString());
            }
        } else if (objValue.toString().length() == 0) {
            objValue = null;
        }
    }
    _valueAssociation.setValue(objValue, component);
    // just executing action, ignoring result
    valueForBinding("action", component);
    WOResponse response = AjaxUtils.createResponse(request, context);
    _appendValueAttributeToResponse(response, context);
    return response;
}
Also used : Format(java.text.Format) ValidationException(com.webobjects.foundation.NSValidation.ValidationException) WOComponent(com.webobjects.appserver.WOComponent) ParseException(java.text.ParseException) WOResponse(com.webobjects.appserver.WOResponse) BigDecimal(java.math.BigDecimal)

Example 4 with WOResponse

use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.

the class AjaxUpdateContainer method handleRequest.

@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
    WOComponent component = context.component();
    String id = _containerID(context);
    if (associations().objectForKey("action") != null) {
        @SuppressWarnings("unused") WOActionResults results = (WOActionResults) valueForBinding("action", component);
    // ignore results
    }
    WOResponse response = AjaxUtils.createResponse(request, context);
    AjaxUtils.setPageReplacementCacheKey(context, id);
    if (hasChildrenElements()) {
        appendChildrenToResponse(response, context);
    }
    String onRefreshComplete = (String) valueForBinding("onRefreshComplete", component);
    if (onRefreshComplete != null) {
        AjaxUtils.appendScriptHeader(response);
        response.appendContentString(onRefreshComplete);
        AjaxUtils.appendScriptFooter(response);
    }
    if (AjaxModalDialog.isInDialog(context)) {
        AjaxUtils.appendScriptHeader(response);
        response.appendContentString("AMD.contentUpdated();");
        AjaxUtils.appendScriptFooter(response);
    }
    return null;
}
Also used : WOActionResults(com.webobjects.appserver.WOActionResults) WOComponent(com.webobjects.appserver.WOComponent) WOResponse(com.webobjects.appserver.WOResponse)

Example 5 with WOResponse

use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.

the class AjaxSubmitButton method handleRequest.

@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
    WOComponent component = context.component();
    WOActionResults result = (WOActionResults) valueForBinding("action", component);
    if (ERXAjaxApplication.isAjaxReplacement(request)) {
        AjaxUtils.setPageReplacementCacheKey(context, (String) valueForBinding("replaceID", component));
    } else if (result == null || booleanValueForBinding("ignoreActionResponse", false, component)) {
        WOResponse response = AjaxUtils.createResponse(request, context);
        String onClickServer = (String) valueForBinding("onClickServer", component);
        if (onClickServer != null) {
            AjaxUtils.appendScriptHeaderIfNecessary(request, response);
            response.appendContentString(onClickServer);
            AjaxUtils.appendScriptFooterIfNecessary(request, response);
        }
        result = response;
    } else {
        String updateContainerID = AjaxUpdateContainer.updateContainerID(this, component);
        if (updateContainerID != null) {
            AjaxUtils.setPageReplacementCacheKey(context, updateContainerID);
        }
    }
    return result;
}
Also used : WOActionResults(com.webobjects.appserver.WOActionResults) WOComponent(com.webobjects.appserver.WOComponent) WOResponse(com.webobjects.appserver.WOResponse)

Aggregations

WOResponse (com.webobjects.appserver.WOResponse)33 WOComponent (com.webobjects.appserver.WOComponent)10 WOApplication (com.webobjects.appserver.WOApplication)7 WOActionResults (com.webobjects.appserver.WOActionResults)6 WOContext (com.webobjects.appserver.WOContext)5 WORequest (com.webobjects.appserver.WORequest)4 WOSession (com.webobjects.appserver.WOSession)4 NSDictionary (com.webobjects.foundation.NSDictionary)4 WODynamicURL (com.webobjects.appserver.WODynamicURL)2 NSArray (com.webobjects.foundation.NSArray)2 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 JSONRPCBridge (org.jabsorb.JSONRPCBridge)2 JSONObject (org.json.JSONObject)2 WOAssociation (com.webobjects.appserver.WOAssociation)1