Search in sources :

Example 1 with WOApplication

use of com.webobjects.appserver.WOApplication 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 WOApplication

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

the class ERXAjaxSession method _saveCurrentPage.

/**
 * Semi-private method that saves the current page. Overridden to put the page in the
 * permanent page cache if it's already in there.
 */
@Override
public void _saveCurrentPage() {
    if (overridePrivateCache) {
        WOContext _currentContext = context();
        if (_currentContext != null) {
            String contextID = context().contextID();
            log.debug("Saving page for contextID: {}", contextID);
            WOComponent currentPage = _currentContext._pageComponent();
            if (currentPage != null && currentPage._isPage()) {
                WOComponent permanentSenderPage = _permanentPageWithContextID(_currentContext._requestContextID());
                WOComponent permanentCurrentPage = _permanentPageWithContextID(contextID);
                if (permanentCurrentPage == null && _permanentPageCache().containsValue(currentPage)) {
                    // AK: note that we put it directly in the cache, not bothering with
                    // savePageInPermanentCache() as this one would clear out the old IDs
                    _permanentPageCache.setObjectForKey(currentPage, contextID);
                } else if (permanentCurrentPage != currentPage) {
                    WOApplication woapplication = WOApplication.application();
                    if (permanentSenderPage == currentPage && woapplication.permanentPageCacheSize() != 0) {
                        if (_shouldPutInPermanentCache(currentPage))
                            savePageInPermanentCache(currentPage);
                    } else if (woapplication.pageCacheSize() != 0)
                        savePage(currentPage);
                }
            }
        }
    } else {
        super._saveCurrentPage();
    }
}
Also used : WOComponent(com.webobjects.appserver.WOComponent) WOContext(com.webobjects.appserver.WOContext) WOApplication(com.webobjects.appserver.WOApplication)

Example 3 with WOApplication

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

the class ERXComponentRequestHandler method _dispatchWithPreparedSession.

private WOResponse _dispatchWithPreparedSession(WOSession aSession, WOContext aContext, NSDictionary someElements) {
    WOComponent aPage = null;
    WOResponse aResponse = null;
    String aPageName = (String) someElements.objectForKey("wopage");
    String oldContextID = aContext._requestContextID();
    String oldSessionID = (String) someElements.objectForKey(WOApplication.application().sessionIdKey());
    WOApplication anApplication = WOApplication.application();
    boolean clearIDsInCookies = false;
    if ((oldSessionID == null) || (oldContextID == null)) {
        if ((aPageName == null) && (!aSession.storesIDsInCookies())) {
            WORequest request = aContext.request();
            String cookieHeader = request.headerForKey("cookie");
            if ((cookieHeader != null) && (cookieHeader.length() > 0)) {
                NSDictionary cookieDict = request.cookieValues();
                if ((cookieDict.objectForKey(WOApplication.application().sessionIdKey()) != null) || (cookieDict.objectForKey(WOApplication.application().instanceIdKey()) != null)) {
                    clearIDsInCookies = true;
                }
            }
        }
        aPage = anApplication.pageWithName(aPageName, aContext);
    } else {
        aPage = _restorePageForContextID(oldContextID, aSession);
        if (aPage == null) {
            if (anApplication._isPageRecreationEnabled())
                aPage = anApplication.pageWithName(aPageName, aContext);
            else {
                return anApplication.handlePageRestorationErrorInContext(aContext);
            }
        }
    }
    aContext._setPageElement(aPage);
    aResponse = _dispatchWithPreparedPage(aPage, aSession, aContext, someElements);
    if (anApplication.isPageRefreshOnBacktrackEnabled()) {
        aResponse.disableClientCaching();
    }
    aSession._saveCurrentPage();
    if ((clearIDsInCookies) && (!aSession.storesIDsInCookies())) {
        aSession._clearCookieFromResponse(aResponse);
    }
    return aResponse;
}
Also used : WORequest(com.webobjects.appserver.WORequest) WOComponent(com.webobjects.appserver.WOComponent) NSDictionary(com.webobjects.foundation.NSDictionary) WOResponse(com.webobjects.appserver.WOResponse) WOApplication(com.webobjects.appserver.WOApplication)

Example 4 with WOApplication

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

the class ERXComponentRequestHandler method _handleRequest.

WOResponse _handleRequest(WORequest aRequest) {
    WOContext aContext = null;
    WOResponse aResponse;
    NSDictionary requestHandlerValues = requestHandlerValuesForRequest(aRequest);
    WOApplication anApplication = WOApplication.application();
    String aSessionID = (String) requestHandlerValues.objectForKey(WOApplication.application().sessionIdKey());
    if ((!anApplication.isRefusingNewSessions()) || (aSessionID != null)) {
        String aSenderID = (String) requestHandlerValues.objectForKey("woeid");
        String oldContextID = (String) requestHandlerValues.objectForKey("wocid");
        WOStatisticsStore aStatisticsStore = anApplication.statisticsStore();
        if (aStatisticsStore != null)
            aStatisticsStore.applicationWillHandleComponentActionRequest();
        try {
            aContext = anApplication.createContextForRequest(aRequest);
            aContext._setRequestContextID(oldContextID);
            aContext._setSenderID(aSenderID);
            anApplication.awake();
            aResponse = _dispatchWithPreparedApplication(anApplication, aContext, requestHandlerValues);
            NSNotificationCenter.defaultCenter().postNotification(WORequestHandler.DidHandleRequestNotification, aContext);
            anApplication.sleep();
        } catch (Exception exception) {
            try {
                NSLog.err.appendln("<" + getClass().getName() + ">: Exception occurred while handling request:\n" + exception.toString());
                if (NSLog.debugLoggingAllowedForLevelAndGroups(1, 4L)) {
                    NSLog.debug.appendln(exception);
                }
                if (aContext == null)
                    aContext = anApplication.createContextForRequest(aRequest);
                else {
                    aContext._putAwakeComponentsToSleep();
                }
                WOSession aSession = aContext._session();
                aResponse = anApplication.handleException(exception, aContext);
                if (aSession != null) {
                    try {
                        anApplication.saveSessionForContext(aContext);
                        anApplication.sleep();
                    } catch (Exception eAgain) {
                        NSLog.err.appendln("<WOApplication '" + anApplication.name() + "'>: Another Exception  occurred while trying to clean the application:\n" + eAgain.toString());
                        if (NSLog.debugLoggingAllowedForLevelAndGroups(1, 4L))
                            NSLog.debug.appendln(eAgain);
                    }
                }
            } finally {
                if ((aContext != null) && (aContext._session() != null))
                    anApplication.saveSessionForContext(aContext);
            }
        }
        if (aResponse != null) {
            aResponse._finalizeInContext(aContext);
        }
        if (aStatisticsStore != null) {
            WOComponent aPage = aContext.page();
            String aName = null;
            if (aPage != null) {
                aName = aPage.name();
            }
            aStatisticsStore.applicationDidHandleComponentActionRequestWithPageNamed(aName);
        }
    } else {
        String newLocationURL = anApplication._newLocationForRequest(aRequest);
        String contentString = "Sorry, your request could not immediately be processed. Please try this URL: <a href=\"" + newLocationURL + "\">" + newLocationURL + "</a>";
        aResponse = anApplication.createResponseInContext(null);
        WOResponse._redirectResponse(aResponse, newLocationURL, contentString);
        aResponse._finalizeInContext(null);
    }
    return aResponse;
}
Also used : NSDictionary(com.webobjects.foundation.NSDictionary) WOComponent(com.webobjects.appserver.WOComponent) WOContext(com.webobjects.appserver.WOContext) WOSession(com.webobjects.appserver.WOSession) WOStatisticsStore(com.webobjects.appserver.WOStatisticsStore) WOResponse(com.webobjects.appserver.WOResponse) WOApplication(com.webobjects.appserver.WOApplication)

Example 5 with WOApplication

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

the class ERXProperties method applicationMachinePropertiesPath.

/**
 * Returns the path to the application-specific system-wide file "fileName".  By default this path is /etc/WebObjects,
 * and the application name will be appended.  For instance, if you are asking for the MyApp Properties file for the
 * system, it would go in /etc/WebObjects/MyApp/Properties.
 *
 * @param fileName the Filename
 * @return the path, or null if the path does not exist
 */
private static String applicationMachinePropertiesPath(String fileName) {
    String applicationMachinePropertiesPath = null;
    String machinePropertiesPath = NSProperties.getProperty("er.extensions.ERXProperties.machinePropertiesPath", "/etc/WebObjects");
    WOApplication application = WOApplication.application();
    String applicationName;
    if (application != null) {
        applicationName = application.name();
    } else {
        applicationName = NSProperties.getProperty("WOApplicationName");
        if (applicationName == null) {
            NSBundle mainBundle = NSBundle.mainBundle();
            if (mainBundle != null) {
                applicationName = mainBundle.name();
            }
            if (applicationName == null) {
                applicationName = "Unknown";
            }
        }
    }
    File applicationPropertiesFile = new File(machinePropertiesPath + File.separator + fileName);
    if (!applicationPropertiesFile.exists()) {
        applicationPropertiesFile = new File(machinePropertiesPath + File.separator + applicationName + File.separator + fileName);
    }
    if (applicationPropertiesFile.exists()) {
        try {
            applicationMachinePropertiesPath = applicationPropertiesFile.getCanonicalPath();
        } catch (IOException e) {
            log.error("Failed to load machine Properties file '{}'.", fileName, e);
        }
    }
    return applicationMachinePropertiesPath;
}
Also used : NSBundle(com.webobjects.foundation.NSBundle) IOException(java.io.IOException) File(java.io.File) WOApplication(com.webobjects.appserver.WOApplication)

Aggregations

WOApplication (com.webobjects.appserver.WOApplication)11 WOResponse (com.webobjects.appserver.WOResponse)7 WOComponent (com.webobjects.appserver.WOComponent)4 WOContext (com.webobjects.appserver.WOContext)4 WOSession (com.webobjects.appserver.WOSession)3 WORequest (com.webobjects.appserver.WORequest)2 NSBundle (com.webobjects.foundation.NSBundle)2 NSDictionary (com.webobjects.foundation.NSDictionary)2 File (java.io.File)2 IOException (java.io.IOException)2 WOActionResults (com.webobjects.appserver.WOActionResults)1 WODynamicURL (com.webobjects.appserver.WODynamicURL)1 WOMultipartIterator (com.webobjects.appserver.WOMultipartIterator)1 WOStatisticsStore (com.webobjects.appserver.WOStatisticsStore)1 NSArray (com.webobjects.foundation.NSArray)1 ERXAjaxApplication (er.extensions.appserver.ajax.ERXAjaxApplication)1 ERXUnitAwareDecimalFormat (er.extensions.formatters.ERXUnitAwareDecimalFormat)1 ERXMutableURL (er.extensions.foundation.ERXMutableURL)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1