Example usage for org.springframework.web.context.request RequestContextHolder getRequestAttributes

List of usage examples for org.springframework.web.context.request RequestContextHolder getRequestAttributes

Introduction

In this page you can find the example usage for org.springframework.web.context.request RequestContextHolder getRequestAttributes.

Prototype

@Nullable
public static RequestAttributes getRequestAttributes() 

Source Link

Document

Return the RequestAttributes currently bound to the thread.

Usage

From source file:aode.lx.service.impl.BaseServiceImpl.java

@Override
public List<T> search(Map<String, SearchFilter> additionalFilters, Pageable pageable) {
    RequestAttributes ar = RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = ((ServletRequestAttributes) ar).getRequest();
    Map<String, SearchFilter> filters = SearchFilter.parse(request);
    addAdditionalFilters(filters, additionalFilters);
    Specification<T> spec = DynamicSpecifications.bySearchFilter(filters.values(), this.entityClass);
    List<T> list = this.getRepository().findAll(spec, pageable).getContent();
    return list;//from w  w  w.  j  a va 2s . c  o  m
}

From source file:tv.arte.resteventapi.core.presentation.decoration.RestEventApiControllerLinkBuilder.java

/**
 * Copy of {@link ServletUriComponentsBuilder#getCurrentRequest()} until SPR-10110 gets fixed.
 * /*from  w  w w  .  j  a  va  2  s.  co  m*/
 * @return
 */
private static HttpServletRequest getCurrentRequest() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    Assert.state(requestAttributes != null, "Could not find current request via RequestContextHolder");
    Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
    HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
    Assert.state(servletRequest != null, "Could not find current HttpServletRequest");
    return servletRequest;
}

From source file:de.blizzy.documentr.web.Functions.java

public static String getPageHeaderHtml(String projectName, String branchName, String path) throws IOException {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();/*from   ww  w .  j  a v  a2s.  c om*/
    String contextPath = request.getContextPath();
    String html = pageRenderer.getHeaderHtml(projectName, branchName, path, authentication, contextPath);
    return markdownProcessor.processNonCacheableMacros(html, projectName, branchName, path, authentication,
            contextPath);
}

From source file:org.gooru.insights.api.security.MethodAuthorizationAspect.java

private Map<String, Boolean> hasRedisOperations(AuthorizeOperations authorizeOperations,
        ProceedingJoinPoint pjp) {//from  ww  w .  ja v a  2  s  .c  o  m

    HttpServletRequest request = null;
    HttpSession session = null;
    String sessionToken = null;
    Map<String, Boolean> validStatus = new HashMap<String, Boolean>();
    validStatus.put(PROCEED, false);
    validStatus.put(DO_API, false);
    if (RequestContextHolder.getRequestAttributes() != null) {
        request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        session = request.getSession(true);
        sessionToken = RequestUtils.getSessionToken(request);
        RequestUtils.logRequest(request);
    }
    if (sessionToken != null) {
        String result;
        try {
            result = redisService.getDirectValue(GOORU_PREFIX + sessionToken);
            if (result == null || result.isEmpty()) {
                InsightsLogger.error("null value in redis data for " + GOORU_PREFIX + sessionToken);
                validStatus.put(DO_API, true);
                return validStatus;
            }
            JSONObject jsonObject = new JSONObject(result);
            jsonObject = new JSONObject(jsonObject.getString(ApiConstants.USER_TOKEN));
            jsonObject = new JSONObject(jsonObject.getString(ApiConstants.USER));
            User user = new User();
            user.setFirstName(jsonObject.getString(ApiConstants.FIRST_NAME));
            user.setLastName(jsonObject.getString(ApiConstants.LAST_NAME));
            user.setEmailId(jsonObject.getString(ApiConstants.EMAIL_ID));
            user.setGooruUId(jsonObject.getString(ApiConstants.PARTY_UId));
            if (hasGooruAdminAuthority(authorizeOperations, jsonObject)) {
                session.setAttribute(ApiConstants.SESSION_TOKEN, sessionToken);
                validStatus.put(PROCEED, true);
                return validStatus;
            }
            if (hasAuthority(authorizeOperations, jsonObject, request)) {
                session.setAttribute(ApiConstants.SESSION_TOKEN, sessionToken);
                validStatus.put(PROCEED, true);
                return validStatus;
            }
        } catch (Exception e) {
            InsightsLogger.error("Exception from redis:" + GOORU_PREFIX + sessionToken, e);
            validStatus.put(DO_API, true);
            return validStatus;
        }
    } else {
        throw new AccessDeniedException("sessionToken can not be NULL!");
    }
    return validStatus;
}

From source file:fr.mcc.ginco.audit.tracking.GincoRevListener.java

@Override
public void newRevision(Object revisionEntity) {
    GincoRevEntity gincoRevEntity = (GincoRevEntity) revisionEntity;
    if (RequestContextHolder.getRequestAttributes() == null) {
        logger.error("The RequestContext is empty!!!!!");
    } else {/*w  w w.  ja v a 2  s. c o m*/
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        gincoRevEntity.setUsername(name);
    }
}

From source file:org.carewebframework.ui.thread.ZKThread.java

/**
 * Creates a thread for executing a background operation.
 * /*from   w ww .j a  va 2  s  .c  o  m*/
 * @param target Target operation.
 * @param requester ZK component requesting the operation.
 * @param eventName Name of the event used to notify requester of completion. When fired, the
 *            data associated with the event will be a reference to this instance and may be
 *            interrogated to determine the outcome of the operation.
 */
public ZKThread(final ZKRunnable target, final Component requester, final String eventName) {
    super();
    this.target = target;
    this.event = new Event(eventName, requester, this);
    this.desktop = requester.getDesktop();
    this.requestAttributes = RequestContextHolder.getRequestAttributes();
    this.thread = new ThreadEx();
}

From source file:org.coursera.cmbrehm.kewlvideo.server.VideoController.java

private String getUrlBaseForLocalServer() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();//from   ww  w  .j av  a  2s.  c  om
    String base = "http://" + request.getServerName()
            + ((request.getServerPort() != 80) ? ":" + request.getServerPort() : "");
    return base;
}

From source file:org.gooru.insights.security.MethodAuthorizationAspect.java

private boolean hasApiOperationsAuthority(AuthorizeOperations authorizeOperations, ProceedingJoinPoint pjp) {

    HttpServletRequest request = null;//from  ww w .  j av  a 2s  .  c o  m
    HttpSession session = null;
    String sessionToken = null;
    Map<String, Boolean> validStatus = new HashMap<String, Boolean>();
    validStatus.put(APIConstants.PROCEED, false);
    if (RequestContextHolder.getRequestAttributes() != null) {
        request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        session = request.getSession(true);
    }
    sessionToken = getSessionToken(request);
    if (sessionToken != null && !sessionToken.isEmpty()) {
        String address = endPoint.getColumnByName(APIConstants.CONSTANT_VALUE).getStringValue()
                + APIConstants.GOORU_URI + sessionToken;
        ClientResource client = new ClientResource(address);
        Form headers = (Form) client.getRequestAttributes().get(REST_HEADER);
        if (headers == null) {
            headers = new Form();
        }
        headers.add(APIConstants.GOORU_SESSION_TOKEN, sessionToken);
        client.getRequestAttributes().put(REST_HEADER, headers);
        if (client.getStatus().isSuccess()) {
            try {
                Representation representation = client.get();
                JsonRepresentation jsonRepresentation = new JsonRepresentation(representation);
                JSONObject jsonObj = jsonRepresentation.getJsonObject();
                User user = new User();
                user.setFirstName(jsonObj.getString(APIConstants.FIRST_NAME));
                user.setLastName(jsonObj.getString(APIConstants.LAST_NAME));
                user.setEmailId(jsonObj.getString(APIConstants.EMAIL_ID));
                user.setGooruUId(jsonObj.getString(APIConstants.GOORUUID));
                if (hasGooruAdminAuthority(authorizeOperations, jsonObj)) {
                    session.setAttribute(APIConstants.TOKEN, sessionToken);
                    return true;
                }
                if (hasAuthority(authorizeOperations, jsonObj)) {
                    session.setAttribute(APIConstants.TOKEN, sessionToken);
                    return true;
                }
            } catch (Exception e) {
                throw new AccessDeniedException(MessageHandler.getMessage(ErrorConstants.E102,
                        new String[] { APIConstants.SESSION_TOKEN }));
            }
        } else {
            throw new AccessDeniedException(MessageHandler.getMessage(ErrorConstants.E102,
                    new String[] { APIConstants.SESSION_TOKEN }));
        }
    } else {
        InsightsLogger.debug(ASPECT,
                MessageHandler.getMessage(ErrorConstants.E100, new String[] { APIConstants.SESSION_TOKEN }));
        throw new AccessDeniedException(
                MessageHandler.getMessage(ErrorConstants.E100, new String[] { APIConstants.SESSION_TOKEN }));
    }
    return false;
}

From source file:your.microservice.core.util.YourServletUriComponentsBuilder.java

/**
 * Obtain the request through {@link RequestContextHolder}.
 * @return the active servlet request//from  ww w. j  a va  2s .  c  o m
 */
protected static HttpServletRequest getCurrentRequest() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    Assert.state(requestAttributes != null, "Could not find current request via RequestContextHolder");
    Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
    HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
    Assert.state(servletRequest != null, "Could not find current HttpServletRequest");
    return servletRequest;
}

From source file:aode.lx.service.impl.BaseServiceImpl.java

@Override
public List<T> search(Map<String, SearchFilter> additionalFilters) {
    RequestAttributes ar = RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = ((ServletRequestAttributes) ar).getRequest();
    Map<String, SearchFilter> filters = SearchFilter.parse(request);
    addAdditionalFilters(filters, additionalFilters);
    Specification<T> spec = DynamicSpecifications.bySearchFilter(filters.values(), this.entityClass);
    List<T> list = this.getRepository().findAll(spec);
    return list;/* w  ww. j a va 2  s  .c  o  m*/
}