<commit_before>package com.blstream.myhoard.authorization;

import com.blstream.myhoard.biz.model.TokenDTO;
import com.blstream.myhoard.biz.service.TokenService;
import com.blstream.myhoard.constants.Constants;
import static com.blstream.myhoard.constants.Constants.USER;
import com.blstream.myhoard.exception.AuthorizationException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyHoardInterceptor implements HandlerInterceptor {

    private static final Logger logger = Logger.getLogger(MyHoardInterceptor.class.getCanonicalName());

    @Autowired
    private TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("MyHoardInterceptor - preHandle");

        if (isAllowed(request)) {
            return true;
        }

        final String AccessToken = request.getHeader("Authorization");

        if (AccessToken == null) {
            logger.error("AccessToken is null");
            throw new AuthorizationException(Constants.ERROR_CODE_AUTH_TOKEN_INVALID);
        }

        TokenDTO tokenDTO = tokenService.getByAccessToken(AccessToken);

        request.setAttribute(USER, tokenDTO.getUser());

        SecurityContextHolder.getContext().setAuthentication(new UserAuthentication(tokenDTO.getUser()));

        // TODO RT remove
        logger.info(String.format("\nAccess Token: %s\nToken ID: %s\nUser ID: %s\nUser email: %s",
                AccessToken, tokenDTO.getId(), tokenDTO.getUser().getId(), tokenDTO.getUser().getEmail()));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        logger.info("MyHoardInterceptor - postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        logger.info("MyHoardInterceptor - afterCompletion");
    }

    private boolean isAllowed(HttpServletRequest request) {
        return request.getRequestURI().toUpperCase().equals("/USERS") && request.getMethod().toUpperCase().equals("POST");
    }

}<commit_msg>test commit<commit_after>package com.blstream.myhoard.authorization;

import com.blstream.myhoard.biz.model.TokenDTO;
import com.blstream.myhoard.biz.service.TokenService;
import com.blstream.myhoard.constants.Constants;
import static com.blstream.myhoard.constants.Constants.USER;
import com.blstream.myhoard.exception.AuthorizationException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyHoardInterceptor implements HandlerInterceptor {

    private static final Logger logger = Logger.getLogger(MyHoardInterceptor.class.getCanonicalName());

    @Autowired
    private TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("MyHoardInterceptor - preHandle");

        if (isAllowed(request)) {
            return true;
        }

        final String AccessToken = request.getHeader("Authorization");

        if (AccessToken == null) {
            logger.error("AccessToken is null");
            throw new AuthorizationException(Constants.ERROR_CODE_AUTH_TOKEN_INVALID);
        }

        TokenDTO tokenDTO = tokenService.getByAccessToken(AccessToken);

        request.setAttribute(USER, tokenDTO.getUser());

        SecurityContextHolder.getContext().setAuthentication(new UserAuthentication(tokenDTO.getUser()));

        // TODO RT remove
        logger.info(String.format("\nAccess Token: %s\nToken ID: %s\nUser ID: %s\nUser email: %s",
                AccessToken, tokenDTO.getId(), tokenDTO.getUser().getId(), tokenDTO.getUser().getEmail()));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        logger.info("MyHoardInterceptor - postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        logger.info("MyHoardInterceptor - afterCompletion");
    }

    private boolean isAllowed(HttpServletRequest request) {
        logger.info(String.format("URI: %s METHOD: %s", request.getRequestURI().toUpperCase(), request.getMethod().toUpperCase()));

        return (request.getRequestURI().toUpperCase().equals("/USERS")
                || request.getRequestURI().toUpperCase().equals("/USERS/")
                || request.getRequestURI().toUpperCase().equals("//USERS/")
                || request.getRequestURI().toUpperCase().equals("//USERS"))
                && request.getMethod().toUpperCase().equals("POST");
    }

}
{'fraction_non_alphanumeric': 0.10472426800790371, 'fraction_numerical': 0.00035925992455541585, 'mean_word_length': 4.0849315068493155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6962728', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1453, 'n_words': 278}