Search in sources :

Example 1 with IUiSession

use of org.eclipse.scout.rt.ui.html.IUiSession in project scout.rt by eclipse.

the class DynamicResourceInfo method fromPath.

public static DynamicResourceInfo fromPath(HttpServletRequest req, String path) {
    if (path == null) {
        return null;
    }
    Matcher m = PATTERN_DYNAMIC_ADAPTER_RESOURCE_PATH.matcher(path);
    if (!m.matches()) {
        return null;
    }
    String uiSessionId = m.group(1);
    String adapterId = m.group(2);
    String filename = m.group(3);
    // lookup the UiSession on the current HttpSession to ensure the requested dynamic resource
    // is from one of the UiSessions of the currently authenticated user!
    IUiSession uiSession = UiSession.get(req, uiSessionId);
    if (uiSession == null) {
        return null;
    }
    return new DynamicResourceInfo(uiSession, adapterId, filename);
}
Also used : Matcher(java.util.regex.Matcher) IUiSession(org.eclipse.scout.rt.ui.html.IUiSession)

Example 2 with IUiSession

use of org.eclipse.scout.rt.ui.html.IUiSession in project scout.rt by eclipse.

the class BinaryResourceUrlUtilityTest method testCreateDynamicAdapterResourceUrl_BinaryResource.

@Test
public void testCreateDynamicAdapterResourceUrl_BinaryResource() {
    IJsonAdapter<Long> jsonAdapter = Mockito.mock(IMockJsonAdapter.class);
    IUiSession uiSession = Mockito.mock(IUiSession.class);
    Mockito.when(jsonAdapter.getId()).thenReturn("123");
    Mockito.when(jsonAdapter.getUiSession()).thenReturn(uiSession);
    Mockito.when(uiSession.getUiSessionId()).thenReturn("abc");
    String expectedUrl = "dynamic/abc/123/" + m_fingerprint + "/foo.txt";
    assertEquals(expectedUrl, BinaryResourceUrlUtility.createDynamicAdapterResourceUrl(jsonAdapter, m_binaryResource));
}
Also used : IUiSession(org.eclipse.scout.rt.ui.html.IUiSession) Test(org.junit.Test)

Example 3 with IUiSession

use of org.eclipse.scout.rt.ui.html.IUiSession in project scout.rt by eclipse.

the class JsonMessageRequestHandler method createUiSession.

protected IUiSession createUiSession(HttpServletRequest req, HttpServletResponse resp, JsonStartupRequest jsonStartupReq) throws ServletException, IOException {
    HttpSession httpSession = req.getSession();
    ISessionStore sessionStore = m_httpSessionHelper.getSessionStore(httpSession);
    final long startNanos = System.nanoTime();
    if (LOG.isDebugEnabled()) {
        LOG.debug("JSON request started");
    }
    LOG.debug("Creating new UI session....");
    IUiSession uiSession = BEANS.get(IUiSession.class);
    uiSession.init(req, resp, jsonStartupReq);
    sessionStore.registerUiSession(uiSession);
    LOG.info("Created new UI session with ID {} in {} ms [maxIdleTime={}s, httpSession.maxInactiveInterval={}s]", uiSession.getUiSessionId(), StringUtility.formatNanos(System.nanoTime() - startNanos), m_maxUserIdleTime, req.getSession().getMaxInactiveInterval());
    return uiSession;
}
Also used : ISessionStore(org.eclipse.scout.rt.ui.html.ISessionStore) HttpSession(javax.servlet.http.HttpSession) IUiSession(org.eclipse.scout.rt.ui.html.IUiSession)

Example 4 with IUiSession

use of org.eclipse.scout.rt.ui.html.IUiSession in project scout.rt by eclipse.

the class UploadRequestHandler method handlePost.

@Override
public boolean handlePost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    // serve only /upload
    String pathInfo = req.getPathInfo();
    Matcher matcher = PATTERN_UPLOAD_ADAPTER_RESOURCE_PATH.matcher(pathInfo);
    if (!matcher.matches()) {
        return false;
    }
    final String uiSessionId = matcher.group(1);
    final String targetAdapterId = matcher.group(2);
    // Check if is really a file upload
    if (!ServletFileUpload.isMultipartContent(req)) {
        return false;
    }
    final long startNanos = System.nanoTime();
    if (LOG.isDebugEnabled()) {
        LOG.debug("File upload started");
    }
    // disable caching
    m_httpCacheControl.checkAndSetCacheHeaders(req, resp, null);
    try {
        // Get and validate existing UI session
        final IUiSession uiSession = UiSession.get(req, uiSessionId);
        if (uiSession == null) {
            throw new IllegalStateException("Could not resolve UI session with ID " + uiSessionId);
        }
        // Touch the session
        uiSession.touch();
        // Associate subsequent processing with the uiSession.
        RunContexts.copyCurrent().withThreadLocal(IUiSession.CURRENT, uiSession).withDiagnostics(BEANS.all(IUiRunContextDiagnostics.class)).run(new IRunnable() {

            @Override
            public void run() throws Exception {
                handleUploadFileRequest(IUiSession.CURRENT.get(), req, resp, targetAdapterId);
            }
        }, DefaultExceptionTranslator.class);
    } catch (Exception e) {
        LOG.error("Unexpected error while handling multipart upload request", e);
        writeJsonResponse(resp, m_jsonRequestHelper.createUnrecoverableFailureResponse());
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("File upload completed in {} ms", StringUtility.formatNanos(System.nanoTime() - startNanos));
        }
    }
    return true;
}
Also used : Matcher(java.util.regex.Matcher) IUiSession(org.eclipse.scout.rt.ui.html.IUiSession) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ServletException(javax.servlet.ServletException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) IOException(java.io.IOException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 5 with IUiSession

use of org.eclipse.scout.rt.ui.html.IUiSession in project scout.rt by eclipse.

the class JsonTestUtility method createAndInitializeUiSession.

public static IUiSession createAndInitializeUiSession() {
    String clientSessionId = "testClientSession123";
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    final HttpSession httpSession = Mockito.mock(HttpSession.class);
    final Object sessionMutex = new Object();
    Mockito.when(request.getLocale()).thenReturn(new Locale("de_CH"));
    Mockito.when(request.getHeader("User-Agent")).thenReturn("dummy");
    Mockito.when(request.getSession()).thenReturn(httpSession);
    Mockito.when(request.getSession(false)).thenReturn(httpSession);
    Mockito.when(httpSession.getAttribute(HttpSessionMutex.SESSION_MUTEX_ATTRIBUTE_NAME)).thenReturn(sessionMutex);
    final ISessionStore sessionStore = BEANS.get(HttpSessionHelper.class).getSessionStore(httpSession);
    Mockito.when(httpSession.getAttribute(HttpSessionHelper.SESSION_STORE_ATTRIBUTE_NAME)).thenReturn(sessionStore);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            ((HttpSessionBindingListener) sessionStore).valueUnbound(null);
            return null;
        }
    }).when(httpSession).invalidate();
    JSONObject jsonReqObj = new JSONObject();
    jsonReqObj.put(JsonStartupRequest.PROP_CLIENT_SESSION_ID, clientSessionId);
    jsonReqObj.put("startup", true);
    JsonStartupRequest jsonStartupRequest = new JsonStartupRequest(new JsonRequest(jsonReqObj));
    IUiSession uiSession = new TestEnvironmentUiSession();
    uiSession.init(request, response, jsonStartupRequest);
    return uiSession;
}
Also used : Locale(java.util.Locale) ISessionStore(org.eclipse.scout.rt.ui.html.ISessionStore) HttpSessionHelper(org.eclipse.scout.rt.ui.html.HttpSessionHelper) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonRequest(org.eclipse.scout.rt.ui.html.json.JsonRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) JsonStartupRequest(org.eclipse.scout.rt.ui.html.json.JsonStartupRequest) JSONObject(org.json.JSONObject) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IUiSession(org.eclipse.scout.rt.ui.html.IUiSession) IJsonObject(org.eclipse.scout.rt.ui.html.json.IJsonObject) JSONObject(org.json.JSONObject)

Aggregations

IUiSession (org.eclipse.scout.rt.ui.html.IUiSession)7 HttpSession (javax.servlet.http.HttpSession)3 ISessionStore (org.eclipse.scout.rt.ui.html.ISessionStore)3 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 ServletException (javax.servlet.ServletException)2 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)2 HttpSessionHelper (org.eclipse.scout.rt.ui.html.HttpSessionHelper)2 JSONObject (org.json.JSONObject)2 Locale (java.util.Locale)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 PlatformError (org.eclipse.scout.rt.platform.exception.PlatformError)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 IJsonObject (org.eclipse.scout.rt.ui.html.json.IJsonObject)1 JsonRequest (org.eclipse.scout.rt.ui.html.json.JsonRequest)1 JsonStartupRequest (org.eclipse.scout.rt.ui.html.json.JsonStartupRequest)1 Test (org.junit.Test)1