Return attributes of HttpServletRequest with RequestContextHolder

0

I am working with JWT and it is necessary to handle the claims from a class, for this create the following AuthContextHolder.java :

package com.payulatam.ppp4.secure.api.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.payulatam.ppp4.secure.api.model.commons.SecureApiResources;

import io.jsonwebtoken.Claims;

public class AuthContextHolder {

    public Integer getWebUserId(){
        return getIntegerClaims(SecureApiResources.WEB_USER_ID_PARAM);
    }

    public Integer getMerchantId(){
        return getIntegerClaims(SecureApiResources.MERCHANT_ID_PARAM);
    }

    public Integer getDefaultAccountId(){
        return getIntegerClaims(SecureApiResources.DEFAULT_ACCOUNT_ID_PARAM);
    }

    public Integer getIntegerClaims(String claimParameter){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        final Claims claims = (Claims) request.getAttribute(SecureApiResources.CLAIMS_ATTR);
        return (Integer) claims.get(claimParameter);
    }

}

Perso, I wonder if this class can be improved through the use of a different pattern or approach.

Here is an example of the implementation of this class:

private AuthContextHolder authContextHolder = new AuthContextHolder();

@RequestMapping(value = SecureApiResources.Asobancaria.URL_ASOBANCARIA_MERCHANT_ACCOUNTS, method = RequestMethod.GET, produces = "application/json")

public @ResponseBody List<AccountAsobancariaBank> getAccountsAsobancaria() throws Exception {

    List<AccountAsobancariaBank> asobancariaData = asobancariaService.getAccounts(authContextHolder.getMerchantId());

    if ((asobancariaData.get(0) == null)) {
        HttpErrors httpErrors = new HttpErrors(
        SecureApiResources.ERROR_ACCOUNTS_NOT_FOUND);
        throw new EntityNotFoundException(httpErrors);
    }

    return asobancariaData;
}

I appreciate your advice.

    
asked by AndreFontaine 04.03.2016 в 17:15
source

1 answer

2

As you say, I do not see anything wrong with your code, but if you want to see another approach, I'll attach my version of your class, reduce the number of methods and reduce the visibility of the getIntegerClaims method, and create an enum type for the names of the Parameters, in the following way:

public class AutoContextHolder {
  public static Integer get(Params paramName) {
    return getIntegerClaims(paramName.getValue());
  }

  private static Integer getIntegerClaims(String claimParameter) {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    final Claims claims = (Claims) request.getAttribute(SecureApiResources.CLAIMS_ATTR);
    return (Integer) claims.get(claimParameter);
  }

  public enum Params {
    WEB_USER_ID(SecureApiResources.WEB_USER_ID_PARAM),
    MERCHANT_ID(SecureApiResources.MERCHANT_ID_PARAM),
    DEFAULT_ACCOUNT_ID(SecureApiResources.DEFAULT_ACCOUNT_ID_PARAM);

    Params(String paramName) {
      this.paramName=paramName;
    }

    public String getValue() {
      return paramName;
    }

    private String paramName;
  }

}

By using the code it would look like this:

List<AccountAsobancariaBank> asobancariaData =asobancariaService.getAccounts(AutoContextHolder.get(Params.MERCHANT_ID));

As I told you, it's another point of view.

    
answered by 04.04.2016 / 13:28
source