Example usage for com.liferay.portal.kernel.servlet PortalSessionThreadLocal setHttpSession

List of usage examples for com.liferay.portal.kernel.servlet PortalSessionThreadLocal setHttpSession

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.servlet PortalSessionThreadLocal setHttpSession.

Prototype

public static void setHttpSession(HttpSession session) 

Source Link

Usage

From source file:com.liferay.documentlibrary.hook.filter.DLRecordFilter.java

License:Open Source License

/**
 * Get logged in user . //from w w  w.ja  v a2  s . c  om
 * If user is not logged in then it will return default user.
 * @param request
 * @return
 * @throws Exception
 */
private static User _getUser(HttpServletRequest request) throws Exception {
    HttpSession session = request.getSession();

    if (PortalSessionThreadLocal.getHttpSession() == null) {
        PortalSessionThreadLocal.setHttpSession(session);
    }

    User user = PortalUtil.getUser(request);

    if (user != null) {
        return user;
    }

    String userIdString = (String) session.getAttribute("j_username");
    String password = (String) session.getAttribute("j_password");

    if ((userIdString != null) && (password != null)) {
        long userId = GetterUtil.getLong(userIdString);

        user = UserLocalServiceUtil.getUser(userId);
    } else {
        long companyId = PortalUtil.getCompanyId(request);

        Company company = CompanyLocalServiceUtil.getCompany(companyId);

        user = company.getDefaultUser();
    }

    return user;
}

From source file:com.liferay.sync.internal.servlet.SyncDownloadServlet.java

License:Open Source License

@Override
public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    try {/*from w  w  w.ja  v  a 2s. c  o m*/
        HttpSession session = request.getSession();

        if (PortalSessionThreadLocal.getHttpSession() == null) {
            PortalSessionThreadLocal.setHttpSession(session);
        }

        User user = _portal.getUser(request);

        String syncUuid = request.getHeader("Sync-UUID");

        if (syncUuid != null) {
            SyncDevice syncDevice = SyncDeviceLocalServiceUtil.fetchSyncDeviceByUuidAndCompanyId(syncUuid,
                    user.getCompanyId());

            if (syncDevice != null) {
                syncDevice.checkStatus();
            }
        }

        PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user);

        PermissionThreadLocal.setPermissionChecker(permissionChecker);

        String path = _http.fixPath(request.getPathInfo());

        String[] pathArray = StringUtil.split(path, CharPool.SLASH);

        if (pathArray[0].equals("image")) {
            long imageId = GetterUtil.getLong(pathArray[1]);

            sendImage(request, response, imageId);
        } else if (pathArray[0].equals("zip")) {
            String zipFileIds = ParamUtil.get(request, "zipFileIds", StringPool.BLANK);

            if (Validator.isNull(zipFileIds)) {
                throw new IllegalArgumentException("Missing parameter zipFileIds");
            }

            JSONArray zipFileIdsJSONArray = JSONFactoryUtil.createJSONArray(zipFileIds);

            sendZipFile(response, user.getUserId(), zipFileIdsJSONArray);
        } else if (pathArray[0].equals("zipfolder")) {
            long repositoryId = ParamUtil.getLong(request, "repositoryId");
            long folderId = ParamUtil.getLong(request, "folderId");

            if (repositoryId == 0) {
                throw new IllegalArgumentException("Missing parameter repositoryId");
            } else if (folderId == 0) {
                throw new IllegalArgumentException("Missing parameter folderId");
            }

            sendZipFolder(response, user.getUserId(), repositoryId, folderId);
        } else {
            long groupId = GetterUtil.getLong(pathArray[0]);
            String fileUuid = pathArray[1];

            Group group = _groupLocalService.fetchGroup(groupId);

            if ((group == null) || !_syncUtil.isSyncEnabled(group)) {
                response.setHeader(_ERROR_HEADER, SyncSiteUnavailableException.class.getName());

                ServletResponseUtil.write(response, new byte[0]);

                return;
            }

            boolean patch = ParamUtil.getBoolean(request, "patch");

            if (patch) {
                sendPatch(request, response, user.getUserId(), groupId, fileUuid);
            } else {
                sendFile(request, response, user.getUserId(), groupId, fileUuid);
            }
        }
    } catch (NoSuchFileEntryException nsfee) {
        _portal.sendError(HttpServletResponse.SC_NOT_FOUND, nsfee, request, response);
    } catch (NoSuchFileVersionException nsfve) {
        _portal.sendError(HttpServletResponse.SC_NOT_FOUND, nsfve, request, response);
    } catch (Exception e) {
        _portal.sendError(e, request, response);
    }
}

From source file:com.liferay.sync.servlet.DownloadServlet.java

License:Open Source License

@Override
public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    try {//from  w w w.j  av  a  2s  .c  o m
        HttpSession session = request.getSession();

        if (PortalSessionThreadLocal.getHttpSession() == null) {
            PortalSessionThreadLocal.setHttpSession(session);
        }

        User user = PortalUtil.getUser(request);

        PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user);

        PermissionThreadLocal.setPermissionChecker(permissionChecker);

        String path = HttpUtil.fixPath(request.getPathInfo());
        String[] pathArray = StringUtil.split(path, CharPool.SLASH);

        if (pathArray[0].equals("image")) {
            long imageId = GetterUtil.getLong(pathArray[1]);

            sendImage(response, imageId);
        } else {
            long groupId = GetterUtil.getLong(pathArray[0]);
            String uuid = pathArray[1];

            boolean patch = ParamUtil.getBoolean(request, "patch");

            if (patch) {
                sendPatch(request, response, user, groupId, uuid);
            } else {
                sendFile(request, response, groupId, uuid);
            }
        }
    } catch (NoSuchFileEntryException nsfee) {
        PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND, nsfee, request, response);
    } catch (Exception e) {
        PortalUtil.sendError(e, request, response);
    }
}

From source file:com.liferay.tool.datamanipulator.thread.HandlerThread.java

License:Open Source License

@Override
public void run() {
    HttpSession httpSession = _requestContext.getSession();
    httpSession.setMaxInactiveInterval(-1);

    //PermissionThreadLocal.setIndexEnabled(false);
    PermissionThreadLocal.setPermissionChecker(_requestContext.getPermissionChecker());

    PrincipalThreadLocal.setName(_requestContext.getUserId());

    PortalSessionThreadLocal.setHttpSession(httpSession);

    String threadName = this.getName();

    long startTime = System.currentTimeMillis();

    StringBuilder startSB = new StringBuilder(5);
    startSB.append(threadName);/*  w  w w .  j a  va 2 s .  co  m*/
    startSB.append(": Started generating '");
    startSB.append(HandlerUtil.getHandlerDisplayName(_handler.getClass()));
    startSB.append("' entries at ");
    startSB.append(new java.util.Date(startTime));

    _log.info(startSB.toString());

    try {
        _handler.proceed(_requestContext);
    } catch (Exception e) {
        _log.error(e, e);
    }

    long endTime = System.currentTimeMillis();

    StringBuilder finishSB = new StringBuilder(5);
    finishSB.append(threadName);
    finishSB.append(": Finished generating '");
    finishSB.append(HandlerUtil.getHandlerDisplayName(_handler.getClass()));
    finishSB.append("' entries at ");
    finishSB.append(new java.util.Date(endTime));

    _log.info(finishSB.toString());

    StringBuilder tookSB = new StringBuilder(4);
    tookSB.append(threadName);
    tookSB.append(": The generation process took ");
    tookSB.append(DurationFormatUtils.formatDurationWords(endTime - startTime, true, true));

    _log.info(tookSB.toString());
}

From source file:yg0r2.datamanipulator.thread.HandlerThread.java

License:Open Source License

@Override
public void run() {
    HttpSession httpSession = _requestContext.getSession();
    httpSession.setMaxInactiveInterval(-1);

    PermissionThreadLocal.setIndexEnabled(false);
    PermissionThreadLocal.setPermissionChecker(_requestContext.getPermissionChecker());

    PrincipalThreadLocal.setName(_requestContext.getUserId());

    PortalSessionThreadLocal.setHttpSession(httpSession);

    String threadName = this.getName();

    long startTime = System.currentTimeMillis();

    StringBuilder startSB = new StringBuilder(5);
    startSB.append(threadName);// ww w  .j a  v  a  2 s  .  co m
    startSB.append(": Started add '");
    startSB.append(HandlerUtil.getHandlerDisplayName(_handler.getClass()));
    startSB.append("' entries at ");
    startSB.append(String.valueOf(startTime));

    _log.info(startSB.toString());

    try {
        _handler.proceed(_requestContext);
    } catch (Exception e) {
        _log.error(e, e);
    }

    long endTime = System.currentTimeMillis();

    StringBuilder finishSB = new StringBuilder(5);
    finishSB.append(threadName);
    finishSB.append(": Finished add '");
    finishSB.append(HandlerUtil.getHandlerDisplayName(_handler.getClass()));
    finishSB.append("' entries at ");
    finishSB.append(String.valueOf(endTime));

    _log.info(finishSB.toString());

    StringBuilder tookSB = new StringBuilder(4);
    tookSB.append(threadName);
    tookSB.append(": The whole process took ");
    tookSB.append(String.valueOf((endTime - startTime) / 1000D));
    tookSB.append("s.");

    _log.info(tookSB.toString());
}