2013-05-15 80 views
0

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

让我解释一下吧:

我有两个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); 
} 

EnrollmentService:

@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 !"); 
      } 
     } 
    } 
} 

现在,我写了下面的测试: “Cookie是空的”

@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被添加到createToken方法中。 现在,如果我在相同服务(AuthenticateService)上调用deleteToken方法,我会收到消息“令牌在Cookie中查找”,这是正常的。

在此之前一切都很好。

现在,如果在调用方法createToken的AuthenticateService后,我调用UserEnrollmentService的方法,GrantAccessInterceptor在cookies中找不到任何东西...... - >“Cookies为空!

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

回答

0

这不是ProxyFactory的工作来处理饼干,这是由ClientExecutor

通过传球同样ClientExecutorProxyFactory,你应该能够分享饼干:

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

感谢EIDEN!这正是我需要的 – Dish

相关问题