首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在2 ProxyFactory (RESTEasy)之间共享cookies

在2 ProxyFactory (RESTEasy)之间共享cookies
EN

Stack Overflow用户
提问于 2013-05-15 07:51:04
回答 1查看 386关注 0票数 0

我目前正在进行培训,我正在开发使用RESTEasy API的ProxyFactory.create应用程序,我遇到了一些ProxyFactory.create方法的问题(.,.)。

让我解释一下:

我有两次休息服务。

AuthenticateService :

代码语言:javascript
复制
    @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);
}

EnrollmentService :

代码语言:javascript
复制
 @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);
}

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

GrantAccessInterceptor :

代码语言:javascript
复制
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 !");
            }
        } 
    }
}

现在,我编写了以下测试:

代码语言:javascript
复制
@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向我显示了正确的消息:"Cookies是空的!“这是正常的,因为cookie是添加到createToken方法中的。现在,如果我在同一个服务(AuthenticateService)上调用AuthenticateService方法,就会得到消息“令牌在cookie中查找”,这是可以的。

在那之前一切都很好。

现在,如果在调用createToken of AuthenticateService方法之后,我调用了一个UserEnrollmentService方法,GrantAccessInterceptor就不会在Cookie中找到任何东西. -> "Cookies是空的!“

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-15 13:12:37

处理饼干不是ProxyFactory的工作,这取决于ClientExecutor

通过将相同的ClientExecutor传递给ProxyFactory,您应该能够共享cookies:

代码语言:javascript
复制
ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor();
ProxyFactory.create(ServiceIntf1.class, "http://my-service-url", executor);
ProxyFactory.create(ServiceIntf1.class, "http://my-service-url", executor);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16559587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档