@Override
  public Response postDelegationToken(DelegationToken tokenData, HttpServletRequest hsr)
      throws AuthorizationException, IOException, InterruptedException, Exception {

    if (tokenData == null || hsr == null) {
      RouterAuditLogger.logFailure(getUser().getShortUserName(), POST_DELEGATION_TOKEN,
          UNKNOWN, TARGET_WEB_SERVICE, "Parameter error, the tokenData or hsr is null.");
      throw new IllegalArgumentException("Parameter error, the tokenData or hsr is null.");
    }

    try {
      // get Caller UserGroupInformation
      Configuration conf = federationFacade.getConf();
      UserGroupInformation callerUGI = getKerberosUserGroupInformation(conf, hsr);

      // create a delegation token
      return createDelegationToken(tokenData, callerUGI);
    } catch (YarnException e) {
      LOG.error("Create delegation token request failed.", e);
      RouterAuditLogger.logFailure(getUser().getShortUserName(), POST_DELEGATION_TOKEN,
          UNKNOWN, TARGET_WEB_SERVICE, e.getLocalizedMessage());
      return Response.status(Status.FORBIDDEN).entity(e.getMessage()).build();
    }
  }
@Test
  public void testPostDelegationToken() throws Exception {
    Long now = Time.now();

    DelegationToken token = new DelegationToken();
    token.setRenewer(TEST_RENEWER);

    Principal principal = mock(Principal.class);
    when(principal.getName()).thenReturn(TEST_RENEWER);

    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRemoteUser()).thenReturn(TEST_RENEWER);
    when(request.getUserPrincipal()).thenReturn(principal);
    when(request.getAuthType()).thenReturn("kerberos");

    Response response = interceptor.postDelegationToken(token, request);
    Assert.assertNotNull(response);

    Object entity = response.getEntity();
    Assert.assertNotNull(entity);
    Assert.assertTrue(entity instanceof DelegationToken);

    DelegationToken dtoken = (DelegationToken) entity;
    Assert.assertEquals(TEST_RENEWER, dtoken.getRenewer());
    Assert.assertEquals(TEST_RENEWER, dtoken.getOwner());
    Assert.assertEquals("RM_DELEGATION_TOKEN", dtoken.getKind());
    Assert.assertNotNull(dtoken.getToken());
    Assert.assertTrue(dtoken.getNextExpirationTime() > now);
  }