Example usage for javax.servlet.http HttpSession isNew

List of usage examples for javax.servlet.http HttpSession isNew

Introduction

In this page you can find the example usage for javax.servlet.http HttpSession isNew.

Prototype

public boolean isNew();

Source Link

Document

Returns true if the client does not yet know about the session or if the client chooses not to join the session.

Usage

From source file:de.innovationgate.wgpublisher.WGPDispatcher.java

public boolean dispatchErrorTmlRequest(WGAError error, javax.servlet.http.HttpServletRequest request,
        javax.servlet.http.HttpServletResponse response) {

    try {/*from w  ww.j  a  va  2 s .  co m*/

        if (error.getCausingDatabase() == null) {
            return false;
        }

        HttpSession session = request.getSession();

        // Parse request
        WGPRequestPath path = (WGPRequestPath) request.getAttribute(WGACore.ATTRIB_REQUESTPATH);
        if (path == null) {
            path = WGPRequestPath.parseRequest(this, request, response);
            if (path == null)
                return false;
            request.setAttribute(WGACore.ATTRIB_REQUESTPATH, path);
        }

        // Again fetch the wrapped request and response created by the filter, as we are outside it here and we have again the raw request/response object
        HttpServletRequest wrappedRequest = (HttpServletRequest) request
                .getAttribute(WGAFilter.REQATTRIB_REQUEST_WRAPPER);
        if (wrappedRequest != null) {
            request = wrappedRequest;
        }
        HttpServletResponse wrappedResponse = (HttpServletResponse) request
                .getAttribute(WGAFilter.REQATTRIB_RESPONSE_WRAPPER);
        if (wrappedResponse != null) {
            response = wrappedResponse;
        }

        // Determine tml design for this request
        WGA wga = WGA.get(request, response, getCore());
        WGDatabase errorDatabase = getCore().getContentdbs().get(error.getCausingDatabase());
        if (!errorDatabase.isSessionOpen() && !wga.openDatabase(errorDatabase)) {
            return false;
        }

        WGTMLModule tmlLib = determineErrorModule(errorDatabase, wga, error);
        if (tmlLib == null) {
            return false;
        }

        WGContent content = errorDatabase.getDummyContent(path == null ? null : path.getRequestLanguage());

        // Personalize
        TMLUserProfile tmlUserProfile = null;
        WGTransientPortletRegistry portletRegistry = null;
        TMLPortletStateStorage portletStateStorage = null;
        try {
            tmlUserProfile = getCore().getPersManager().prepareUserProfileForRequest(request, response, content,
                    errorDatabase, null, false);
        } catch (Exception e) {
            _log.error("Unable to personalize WebTML request " + path.getCompleteURL(), e);
        }

        // Set context attributes for tml

        request.setAttribute(WGACore.ATTRIB_WGPPATH, path.getPublisherURL());
        request.setAttribute(WGACore.ATTRIB_TAGIDS, new ConcurrentHashMap<String, BaseTagStatus>());
        request.setAttribute(WGACore.ATTRIB_REQUESTURL, path.getCompleteURL());
        request.setAttribute(WGACore.ATTRIB_MIMETYPE, "text/html");
        request.setAttribute(WGACore.ATTRIB_MEDIAKEY, "html");
        request.setAttribute(WGACore.ATTRIB_REQUESTTYPE, REQUESTTYPE_TML);
        request.setAttribute(WGACore.ATTRIB_URI_HASH,
                WGUtils.createMD5HEX(request.getRequestURI().getBytes("UTF-8")));
        request.setAttribute(WGACore.ATTRIB_FORMDATA, null);
        request.setAttribute(WGACore.ATTRIB_OUTER_DESIGN, tmlLib.getName());
        request.setAttribute(WGACore.ATTRIB_OUTER_DESIGN_DB, tmlLib.getDatabase().getDbReference());
        request.setAttribute(WGACore.ATTRIB_COOKIES, fetchHttpCookies(request));

        // TML Cache control
        response.setHeader("Pragma", "No-Cache");
        response.setHeader("Cache-Control", "No-Cache");

        // Prepare WebTML environment
        TMLContext mainContext = new WebTMLEnvironmentBuilder(getCore(), content, request, response,
                tmlUserProfile, null).prepareWebTmlEnvironment();

        // Change path type to TML, so the WebTML rendering works normally
        path.setPathType(WGPRequestPath.TYPE_TML);
        path.setMediaKey(_core.getMediaKey("html"));

        // Dispatch to jsp
        rootDispatch(wga, wga.design(tmlLib.getDatabase()).resolve(tmlLib.getName()), mainContext, "html");

        // Eventually do redirect
        if (request.getAttribute(WGACore.ATTRIB_REDIRECT) != null) {

            if (!response.isCommitted()) {
                if (!session.isNew()) { // on new sessions we must not reset
                                        // the response (#00000147)
                    response.reset();
                }
                response.sendRedirect(String.valueOf(request.getAttribute(WGACore.ATTRIB_REDIRECT)));
            }
        }

        return true;

    } catch (Exception t) {
        getCore().getLog().error("Exception handling error request", t);
        if (!response.isCommitted()) {
            response.reset();
        }
        return false;
    } finally {
        WGFactory.getInstance().closeSessions();
        TMLContext.clearThreadMainContext();
    }
}

From source file:de.innovationgate.wgpublisher.WGPDispatcher.java

private void dispatchTmlRequest(WGPRequestPath path, javax.servlet.http.HttpServletRequest request,
        javax.servlet.http.HttpServletResponse response, Date startDate) throws java.lang.Exception {

    WGContent content = path.getContent();
    WGDatabase database = path.getDatabase();
    if (database == null) {
        throw new HttpErrorException(404, "No database of key " + path.getDatabaseKey(), null);
    }/*from   w w w . ja  v  a2  s . co  m*/

    WGARequestInformation info = (WGARequestInformation) request
            .getAttribute(WGARequestInformation.REQUEST_ATTRIBUTENAME);
    if (info != null) {
        info.setDatabase(database);
        info.setContent(content);
        if (content != null) {
            info.setType(WGARequestInformation.TYPE_CONTENT);
        } else {
            info.setType(WGARequestInformation.TYPE_TML);
        }
    }

    boolean ajax = false;

    HttpSession session = request.getSession();

    // Determine requested mime type and type key
    String mediaKey = path.getMediaKey();
    if (mediaKey == null) {
        mediaKey = database.getAttribute(WGACore.DBATTRIB_DEFAULT_MEDIAKEY).toString();
    }

    // Ensure existing SO registry for this session/database
    SessionScopeResolver.getRegistry(request.getSession(), database.getDbReference(), true);

    // Look if a session cookie has to be set
    _core.setSessionCookie(request, response, database);

    // Context request, if content key filled or we have a title path
    if (content != null) {

        if (!content.mayBePublished(
                isBrowserInterface(session) || isAuthoringMode(database.getDbReference(), session),
                WGContent.DISPLAYTYPE_NONE)) {
            sendNoContentNotification(path, request, response, database);
            return;
        }

        if (content.isVirtual()) {
            if (isBrowserInterface(session)) {
                if (!content.getStatus().equals(WGContent.STATUS_DRAFT)
                        && request.getParameter("forceVLink") == null) {

                    String url = getVirtualContentURL(request, database, path, content);
                    sendRedirect(request, response, url);
                    return;
                }
            } else {
                String vLink = buildVirtualLink(WGA.get(request, response, getCore()), content,
                        path.getMediaKey(), path.getLayoutKey());
                if (vLink != null) {
                    sendRedirect(request, response, vLink);
                    return;
                } else {
                    throw new HttpErrorException(HttpServletResponse.SC_NOT_FOUND,
                            "Unresolveable virtual link on content " + content.getContentKey().toString(),
                            path.getDatabaseKey());
                }
            }
        }

    }

    // Contextless request. We use a dummy content
    else {
        content = database.getDummyContent(path.getRequestLanguage());
    }

    // Test browsability of content
    if (!content.isDummy() && getBrowsingSecurity(database) <= BrowsingSecurity.NO_BROWSING) {
        throw new HttpErrorException(java.net.HttpURLConnection.HTTP_FORBIDDEN,
                "Browsing not allowed in database '" + path.getDatabaseKey() + "'", path.getDatabaseKey());
    }

    // Drop cache if requested by url param
    if (request.getQueryString() != null && request.getQueryString().toLowerCase().indexOf("dropcache") != -1
            && isAdminLoggedIn(request)) {
        content.dropCache();
    }

    // Preparse TMLForm data into multipart form data
    TMLForm.MultipartFormData formData = null;
    String ajaxFormdataSessionKey = request.getParameter("$ajaxformkey");
    if (ajaxFormdataSessionKey != null && !ajaxFormdataSessionKey.trim().equals("")) {
        formData = (TMLForm.MultipartFormData) request.getSession().getAttribute(ajaxFormdataSessionKey);
        if (formData != null && !formData.isValid()) {
            formData = null;
        }
        request.getSession().removeAttribute(ajaxFormdataSessionKey);
    } else if (ServletFileUpload.isMultipartContent(request) && request.getContentLength() != -1) {
        try {
            formData = new TMLForm.MultipartFormData(request, getCore());
        } catch (IOException e) {
            getCore().getLog().warn(
                    "Could not parse multipart form data because of IO exception: " + WGUtils.getRootCause(e));
        } catch (Exception e) {
            getCore().getLog().error("Exception parsing multipart form data", e);
        }
    }

    // Read ajax information, to be able to determine AJAX requests
    String encAjaxInfo = request.getParameter("$ajaxInfo");
    boolean isAjax = (encAjaxInfo != null);

    TMLUserProfile tmlUserProfile = null;
    try {
        tmlUserProfile = getCore().getPersManager().prepareUserProfileForRequest(request, response, content,
                database, formData, isAjax);
        if (info != null && tmlUserProfile != null) {
            info.setProfile(tmlUserProfile);
        }
    } catch (Exception e) {
        _log.error("Unable to personalize WebTML request " + path.getCompleteURL(), e);
    }

    // Set some basic attributes for WebTML processing
    request.setAttribute(WGACore.ATTRIB_WGPPATH, path.getPublisherURL());
    request.setAttribute(WGACore.ATTRIB_TAGIDS, new ConcurrentHashMap<String, BaseTagStatus>());
    request.setAttribute(WGACore.ATTRIB_REQUESTURL, path.getCompleteURL());
    request.setAttribute(WGACore.ATTRIB_REQUESTTYPE, REQUESTTYPE_TML);
    request.setAttribute(WGACore.ATTRIB_URI_HASH,
            WGUtils.createMD5HEX(request.getRequestURI().getBytes("UTF-8")));
    request.setAttribute(WGACore.ATTRIB_FORMDATA, formData);
    request.setAttribute(WGACore.ATTRIB_COOKIES, fetchHttpCookies(request));

    // Determine tml design for this request
    WGA wga = WGA.get(request, response, getCore());
    Design outerLayout = null;
    if (path.getLayoutKey() != null) {
        outerLayout = wga.design(database).resolve(path.getLayoutKey());
        WGTMLModule tmlLib = outerLayout.getTMLModule(mediaKey);
        if (tmlLib != null && tmlLib.isDirectAccessAllowed() == false) {
            throw new HttpErrorException(java.net.HttpURLConnection.HTTP_FORBIDDEN,
                    "This design is not allowed for direct access: " + tmlLib.getName() + " ("
                            + tmlLib.getMediaKey() + ")",
                    path.getDatabaseKey());
        }
    } else {
        WGStructEntry entry = content.getStructEntry();
        if (entry == null) {
            throw new HttpErrorException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                    "Content " + content.getContentKey().toString() + " has no struct entry",
                    path.getDatabaseKey());
        }
        outerLayout = wga.design(database).resolve(entry.getContentType().getOuterLayoutName());
    }

    request.setAttribute(WGACore.ATTRIB_OUTER_DESIGN, outerLayout.getBaseReference().getResourceName());

    // If AJAX read the AJAX data and use WebTML module from there
    AjaxInfo ajaxInfo = null;
    if (encAjaxInfo != null) {
        ajaxInfo = readAjaxInformation(request, database, encAjaxInfo);
        ajax = true;
        WGTMLModule tmlLib = getAjaxTMLModule(request, database, ajaxInfo);
        outerLayout = wga.design(tmlLib.getDatabase()).resolve(tmlLib.getName());
        mediaKey = tmlLib.getMediaKey();
    }

    // Set these here for once, so the following scripts can fetch them. Might be changed by the renderer.
    MediaKey mediaKeyObj = _core.getMediaKey(mediaKey);
    request.setAttribute(WGACore.ATTRIB_MIMETYPE, mediaKeyObj.getMimeType());
    request.setAttribute(WGACore.ATTRIB_MEDIAKEY, mediaKeyObj.getKey());

    // Update usage statistics
    getCore().getUsageStatistics().addRequestStatistic(request, session, database, tmlUserProfile);

    // Prepare WebTML environment
    TMLContext mainContext = new WebTMLEnvironmentBuilder(getCore(), content, request, response, tmlUserProfile,
            ajaxInfo).prepareWebTmlEnvironment();

    // Dispatch
    try {
        rootDispatch(wga, outerLayout, mainContext, mediaKey);

        // Eventually do redirect
        if (request.getAttribute(WGACore.ATTRIB_REDIRECT) != null) {
            if (!ajax) { // On AJAX requests the redirect is performed by Root.tmlEndTag()
                if (!response.isCommitted()) {
                    if (!session.isNew()) { // on new sessions we must not reset the response (#00000147)
                        response.reset();
                    }
                    response.sendRedirect(String.valueOf(request.getAttribute(WGACore.ATTRIB_REDIRECT)));
                } else { // Out of luck for redirect. We only can log that we could not redirect
                    getCore().getLog()
                            .warn("Unable to perform redirect to '"
                                    + String.valueOf(request.getAttribute(WGACore.ATTRIB_REDIRECT))
                                    + "' because response was already committed");
                }
            }
        }

    } catch (Exception t) {
        throw t;
    } finally {
        if (info != null) {
            info.setMimeType((String) request.getAttribute(WGACore.ATTRIB_MIMETYPE));
            info.setPath(path);
            info.setDesign(outerLayout.getTMLModule((String) request.getAttribute(WGACore.ATTRIB_MEDIAKEY)));
            info.setAjax(ajax);
        }

        TMLContext.clearThreadMainContext();
    }
}