2013-05-15 82 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

相關問題