我目前正在进行培训,我正在开发使用RESTEasy API的ProxyFactory.create应用程序,我遇到了一些ProxyFactory.create方法的问题(.,.)。
让我解释一下:
我有两次休息服务。
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);
}在这两个服务上,我有一个拦截器来处理Cookies中恢复的数据。
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向我显示了正确的消息:"Cookies是空的!“这是正常的,因为cookie是添加到createToken方法中的。现在,如果我在同一个服务(AuthenticateService)上调用AuthenticateService方法,就会得到消息“令牌在cookie中查找”,这是可以的。
在那之前一切都很好。
现在,如果在调用createToken of AuthenticateService方法之后,我调用了一个UserEnrollmentService方法,GrantAccessInterceptor就不会在Cookie中找到任何东西. -> "Cookies是空的!“
我认为问题来自于ProxyFactory,它没有在不同的服务之间共享cookie。
发布于 2013-05-15 13:12:37
处理饼干不是ProxyFactory的工作,这取决于ClientExecutor。
通过将相同的ClientExecutor传递给ProxyFactory,您应该能够共享cookies:
ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor();
ProxyFactory.create(ServiceIntf1.class, "http://my-service-url", executor);
ProxyFactory.create(ServiceIntf1.class, "http://my-service-url", executor);https://stackoverflow.com/questions/16559587
复制相似问题