在2个ProxyFactory(RESTEasy)之间共享cookie

时间:2013-05-15 07:51:04

标签: resteasy

我目前正在接受培训,我正在研究使用RESTEasy API的Android应用程序,我遇到了ProxyFactory.create方法(...,...)的一些问题。

让我解释一下:

我有两个REST服务。

AuthenticateService:

    @Path("/authent/tokens")
    public interface AuthenticateService {

    // This method add a data "token" in cookie    
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public PostCustomerOutput createToken(PostCustomerInput postCustomerInput) throws ConnectException;

    @Path("/{id}")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Void deleteToken(@PathParam("id") String token);
}

注册服务:

 @Path("/enrollment/otp")
    public interface UserEnrollmentService {

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public PostGenerateOTPOutput postGenerateOTP(PostGenerateOTPInput postGenerateOTPInput);

    @POST
    @Path("/check")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OutputImpl postCheckOTP(PostCheckOTPInput postCheckOTPInput);
}

在这两项服务上,我有一个处理Cookie中恢复数据的拦截器。

GrantAccessInterceptor:

public class GrantAccessInterceptor extends AbstractInDatabindingInterceptor {
    public GrantAccessInterceptor() {
        super(Phase.USER_STREAM);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
    HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);

    if (null != request) {
        // Read http header to get cookie/
        Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cook : cookies) {
                    if (cook.getName().equals("token")) {
                        log.info("Token find in cookies");
                        // TODO : do what I want with the cookie
                    }
                }
            } else {
                log.info("Cookies are empty !");
            }
        } 
    }
}

现在我写了以下测试:

@org.junit.Test
public void testCreateToken() {

    RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
    // Recover AuthenticateService 
    AuthenticateService authenticateService = ProxyFactory.create(AuthenticateService.class, urlLocal, executor);
    // Recover UserEnrollmentService 
    UserEnrollmentService userEnrollmentService = ProxyFactory.create(UserEnrollmentService.class, urlLocal, executor);

    PostCustomerInput in = new PostCustomerInput();
    // put data in PostCustomerInput
    PostCustomerOutput out = authenticateService.createToken(in);
    // authenticateService.deleteToken(out.getCustomerToken());
    PostGenerateOTPInput postGenerateOTPInput = new PostGenerateOTPInput();
    userEnrollmentService.postGenerateOTP(postGenerateOTPInput);
}

当我调用方法authenticateService.createToken时,我的GrantAccessInterceptor会向我显示正确的消息“Cookie已空!”这是正常的,因为cookie被添加到createToken方法中。 现在,如果我在同一服务(AuthenticateService)上调用deleteToken方法,我会收到消息“令牌中的令牌查找”,这是正常的。

在那之前一切都很顺利。

现在,如果在调用AuthenticateService的方法createToken后,我调用了UserEnrollmentService的方法,则GrantAccessInterceptor在cookie中找不到任何内容...... - > “饼干是空的!”

我认为问题来自ProxyFactory,它不在不同服务之间共享cookie。

1 个答案:

答案 0 :(得分:0)

处理Cookie不是ProxyFactory的工作,而是ClientExecutor

通过将相同的ClientExecutor传递给ProxyFactory,您应该可以共享Cookie:

ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor();
ProxyFactory.create(ServiceIntf1.class, "http://my-service-url", executor);
ProxyFactory.create(ServiceIntf1.class, "http://my-service-url", executor);