Example usage for org.springframework.web.method HandlerMethod getBean

List of usage examples for org.springframework.web.method HandlerMethod getBean

Introduction

In this page you can find the example usage for org.springframework.web.method HandlerMethod getBean.

Prototype

public Object getBean() 

Source Link

Document

Return the bean for this handler method.

Usage

From source file:org.springjutsu.validation.util.RequestUtils.java

/**
 * Given a handler object, return the base controller
 * class-level requestMapping paths. In case the controller
 * specifies one or more base path(s).//ww w .  ja v a  2 s.c  o  m
 * @param handler the handler object
 * @return the controller request paths.
 */
public static String[] getControllerRequestPaths(HandlerMethod handler) {
    RequestMapping requestMapping = AnnotationUtils.findAnnotation(AopUtils.getTargetClass(handler.getBean()),
            RequestMapping.class);
    return requestMapping == null ? null : requestMapping.value();
}

From source file:com.baomidou.framework.spring.DataLoadingInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        if (handlerMethod.getBean() instanceof HandlerInterceptor) {
            ((HandlerInterceptor) handlerMethod.getBean()).postHandle(request, response, handler, modelAndView);
        }/* w  w w  .ja  v a  2  s . c om*/
    }
}

From source file:com.baomidou.framework.spring.DataLoadingInterceptor.java

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception ex) throws Exception {
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        if (handlerMethod.getBean() instanceof HandlerInterceptor) {
            ((HandlerInterceptor) handlerMethod.getBean()).afterCompletion(request, response, handler, ex);
        }/*from w w  w. j a  v a  2s  .com*/
    }
}

From source file:com.baomidou.framework.spring.DataLoadingInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        if (handlerMethod.getBean() instanceof HandlerInterceptor) {
            return ((HandlerInterceptor) handlerMethod.getBean()).preHandle(request, response, handler);
        }//from  w  w w. ja  v a2s .  com
    }
    return true;
}

From source file:com.nec.harvest.servlet.interceptor.TitleInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    final HandlerMethod handlerMethod = (org.springframework.web.method.HandlerMethod) handler;
    final Object controller = handlerMethod.getBean();
    if (controller instanceof TitleRenderer) {
        request.setAttribute(BaseController.TITLE, ((TitleRenderer) controller).getTitleName());
    }/*from  w ww  .ja  v  a2s . c  o m*/
    super.postHandle(request, response, handler, modelAndView);
}

From source file:net.mamian.mySpringboot.interceptor.AuthInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
        ////from ww w .j a  va2  s  . c o m
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        //
        Object target = handlerMethod.getBean();

        //FreeAccess
        if (target.getClass().isAnnotationPresent(FreeAccess.class)) {
            return true;
        }

        //LoginRequired
        boolean loginRequired = target.getClass().isAnnotationPresent(LoginRequired.class)
                || null != handlerMethod.getMethodAnnotation(LoginRequired.class);
        if (loginRequired && !checkLogin(request)) {
            //                response.setStatus(HttpStatus.UNAUTHORIZED.value());
            response.sendRedirect(request.getContextPath() + "/login");
            return false;
        }

        //EmployeeLoginRequired
        boolean employeeLoginRequired = target.getClass().isAnnotationPresent(EmployeeLoginRequired.class)
                || null != handlerMethod.getMethodAnnotation(EmployeeLoginRequired.class);
        Employee employee = null;
        if (employeeLoginRequired) {
            employee = ContextUtils.getEmployee(request);
            if (employee == null) {
                //                    response.setStatus(HttpStatus.PROXY_AUTHENTICATION_REQUIRED.value());
                response.sendRedirect(request.getContextPath() + "/login");
                return false;
            }
        }

        Set<Privilege> priv = new HashSet<>();

        //PrivilegeRequired
        PrivilegeRequired pr = target.getClass().getAnnotation(PrivilegeRequired.class);
        if (pr != null && pr.value() != null && pr.value().length > 0) {
            priv.addAll(Arrays.asList(pr.value()));
        }

        //PrivilegeRequired
        pr = handlerMethod.getMethodAnnotation(PrivilegeRequired.class);
        if (pr != null && pr.value() != null && pr.value().length > 0) {
            priv.addAll(Arrays.asList(pr.value()));
        }

        Privilege[] privileges = priv.toArray(new Privilege[priv.size()]);
        if (privileges.length > 0 && !checkPrivileges(employee, privileges)) {
            request.setAttribute("insufficientPrivilege", privileges[0].getMsg());
            response.setStatus(HttpStatus.FORBIDDEN.value());
            return false;
        }
    }
    return true;
}

From source file:com.hypersocket.json.ControllerInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    if (handler instanceof HandlerMethod) {

        HandlerMethod method = (HandlerMethod) handler;

        if (method.getMethodAnnotation(AuthenticationRequired.class) != null
                || method.getMethodAnnotation(AuthenticationRequiredButDontTouchSession.class) != null) {

            if (!(method.getBean() instanceof AuthenticatedController)) {
                if (log.isErrorEnabled()) {
                    log.error(//from w w  w. ja va2s .c  o m
                            "Use of @AuthenticationRequired annotation is restricted to subclass of AuthenticatedController");
                }
                throw new IllegalArgumentException(
                        "Use of @AuthenticationRequired annotation is restricted to subclass of AuthenticatedController");
            }

            AuthenticatedController contrl = (AuthenticatedController) method.getBean();

            if (method.getMethodAnnotation(AuthenticationRequiredButDontTouchSession.class) != null) {
                contrl.getSessionUtils().getSession(request);
            } else {
                contrl.getSessionUtils().touchSession(request, response);
            }
        }
    }

    return true;

}

From source file:com.nec.harvest.servlet.interceptor.BackOriginGroupInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    final HandlerMethod handlerMethod = (org.springframework.web.method.HandlerMethod) handler;
    final Object controller = handlerMethod.getBean();
    if (controller instanceof AbstractRenderer || controller instanceof PageRenderer) {
        final HttpSession session = request.getSession();
        final Object userOrgCode = (String) session.getAttribute(Constants.SESS_ORGANIZATION_CODE);

        String viewName = null;//from   w ww  . ja  va2 s  . c om
        if (controller instanceof AbstractRenderer) {
            viewName = ((AbstractRenderer) controller).getViewName();
        } else if (controller instanceof PageRenderer) {
            viewName = ((PageRenderer) controller).getViewName();
        }

        if (userOrgCode == null) {
            logger.warn("?????????????");

            // ?????????????
            modelAndView.setViewName(viewName);
            modelAndView.addObject(BaseController.ERROR, Boolean.TRUE);
            modelAndView.addObject(BaseController.ERROR_MESSAGE,
                    MessageHelper.get(MsgConstants.AF001_ENT_CHK_M02));
        }

        final Object businessDay = session.getAttribute(Constants.SESS_BUSINESS_DAY);
        if (businessDay == null) {
            logger.warn("?????????????");

            // ?????????????
            modelAndView.setViewName(viewName);
            modelAndView.addObject(BaseController.ERROR, Boolean.TRUE);
            modelAndView.addObject(BaseController.ERROR_MESSAGE,
                    MessageHelper.get(MsgConstants.AF001_ENT_CHK_M03));
        }
    }
    super.postHandle(request, response, handler, modelAndView);
}

From source file:com.google.code.rees.scope.spring.SpringConversationAdapter.java

public SpringConversationAdapter(HttpServletRequest request, HandlerMethod handler,
        ConversationContextManager conversationContextManager) {
    this.sessionContext = SessionContextUtil.getSessionContext(request);
    this.requestContext = RequestContextUtil.getRequestContext(request);
    this.action = handler.getBean();
    this.actionId = handler.getMethod().getName();
    this.conversationContextManager = conversationContextManager;
}

From source file:com.nec.harvest.servlet.interceptor.BackOriginGroupInterceptor.java

@Override
@SuppressWarnings("unchecked")
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    final User userPricipal = AuthenticatedUserDetails.getUserPrincipal();
    if (userPricipal == null || userPricipal.getOrganization() == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Please login again with right permission");
        }/*w ww  .j a  v  a 2 s .  c  o m*/
        logger.info("Sorry, you don't have permission to access this url");

        // Sorry, you don't have permission to access this url. Please login again with right permission
        response.setContentType(HttpServletContentType.PLAN_TEXT);
        response.sendRedirect(request.getContextPath() + "/logout");
        response.flushBuffer();
        return false;
    }

    final HandlerMethod handlerMethod = (org.springframework.web.method.HandlerMethod) handler;
    final Object controller = handlerMethod.getBean();
    if (controller instanceof MenuController) {
        return super.preHandle(request, response, handler);
    }

    final Map<String, Object> pathVariables = (Map<String, Object>) request
            .getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);

    final String PRO_GROUP_NO = "proGNo";
    final String proGroupNo = (String) pathVariables.get(PRO_GROUP_NO);
    final boolean hasMenuGroups = menuGroupService
            .hasMenuGroupByUserRoleAndSpecificGroup(userPricipal.getUsrKbn(), proGroupNo);
    if (!hasMenuGroups) {
        logger.info("Sorry, you don't have permission to access this url");

        // Sorry, you don't have permission to access this url. Please login again with right permission
        response.setContentType(HttpServletContentType.PLAN_TEXT);
        response.sendRedirect(request.getContextPath() + "/logout");
        response.flushBuffer();
        return false;
    }

    final String ORG_CODE = "orgCode";
    final HttpSession session = request.getSession();

    // Get active original code
    String orgCode = (String) pathVariables.get(ORG_CODE);
    if (StringUtils.isNotEmpty(orgCode)) {
        final String userOrgCode = (String) session.getAttribute(Constants.SESS_ORGANIZATION_CODE);
        if (!userOrgCode.equals(orgCode)) {
            logger.info("Sorry, you don't have permission to access this url");

            // Sorry, you don't have permission to access this url. Please login again with right permission
            response.setContentType(HttpServletContentType.PLAN_TEXT);
            response.sendRedirect(request.getContextPath() + "/logout");
            response.flushBuffer();
            return false;
        }
    }

    // All of original groups
    String[] processGroupNumbers = null;
    if (controller instanceof DailyReportingProGroup) {
        processGroupNumbers = ((DailyReportingProGroup) controller).getProcessGroupNumber();
    } else if (controller instanceof MasterManagementProGroup) {
        processGroupNumbers = ((MasterManagementProGroup) controller).getProcessGroupNumber();
    } else if (controller instanceof ProfitAndLossManagementProGroup) {
        processGroupNumbers = ((ProfitAndLossManagementProGroup) controller).getProcessGroupNumber();
    }

    // If the end-user already logged in into Harvest system, but have an error occurred 
    // when trying to set some information into SESSION then we can reset again that 
    // information into SESSION
    orgCode = (String) session.getAttribute(Constants.SESS_ORGANIZATION_CODE);
    if (orgCode == null) {
        session.setAttribute(Constants.SESS_ORGANIZATION_CODE, userPricipal.getOrganization().getStrCode());
    }

    final Object businessDay = session.getAttribute(Constants.SESS_BUSINESS_DAY);
    if (businessDay == null) {
        BusinessDayService businessDayService = ContextAwareContainer.getInstance()
                .getComponent(BusinessDayService.class);
        final BusinessDay businessDate = businessDayService.findLatest();

        // 
        session.setAttribute(Constants.SESS_BUSINESS_DAY, businessDate.getEigDate());
    }

    // Granted authority of user logged-in
    final String grantedAuthority = userPricipal.getUsrKbn();
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    for (GrantedAuthority authority : authentication.getAuthorities()) {
        logger.info("User {} was logged-in with granted role {}", authentication.getName(),
                authority.getAuthority());
    }

    /**
     * ?
     * 
     * 1?2?3??4
     */
    logger.info(
            "Granted authority of logged user: {}, NOTE: 1?2?3??4",
            grantedAuthority);

    // 
    if (StringUtils.isNotEmpty(grantedAuthority)) {
        if (ArrayUtils.isNotEmpty(processGroupNumbers)) {
            // Trying to store the original group menu into the REQUEST
            final String processGroupNumber = processGroupNumbers[Integer.valueOf(grantedAuthority) - 1];
            request.setAttribute(Constants.SESS_ORIGINAL_GROUP, processGroupNumber);

            // 
            logger.info("Were are trying to handle the sub-menu of group {}", processGroupNumber);
        }
    }

    return super.preHandle(request, response, handler);
}