/jaxrs/arquillian/resteasy-cdi-ejb-test/src/main/java/org/jboss/resteasy/cdi/interceptors/InterceptorResource.java

https://bitbucket.org/cprenzberg/resteasy · Java · 188 lines · 169 code · 12 blank · 7 comment · 9 complexity · 01b70b89b972b3a9577868f330ac322c MD5 · raw file

  1. package org.jboss.resteasy.cdi.interceptors;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.concurrent.atomic.AtomicInteger;
  6. import java.util.logging.Logger;
  7. import javax.annotation.PostConstruct;
  8. import javax.enterprise.context.RequestScoped;
  9. import javax.inject.Inject;
  10. import javax.interceptor.Interceptors;
  11. import javax.ws.rs.Consumes;
  12. import javax.ws.rs.GET;
  13. import javax.ws.rs.POST;
  14. import javax.ws.rs.Path;
  15. import javax.ws.rs.PathParam;
  16. import javax.ws.rs.Produces;
  17. import javax.ws.rs.WebApplicationException;
  18. import javax.ws.rs.core.MediaType;
  19. import javax.ws.rs.core.Response;
  20. import org.jboss.resteasy.cdi.util.Constants;
  21. /**
  22. *
  23. * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
  24. * @version $Revision: 1.1 $
  25. *
  26. * Copyright May 7, 2012
  27. */
  28. @Path("/")
  29. @RequestScoped
  30. @Interceptors ({Interceptor0.class})
  31. @ClassBinding
  32. @LifecycleBinding
  33. public class InterceptorResource
  34. {
  35. static private Map<Integer, Book> collection = new HashMap<Integer, Book>();
  36. static private AtomicInteger counter = new AtomicInteger();
  37. @Inject private Logger log;
  38. @Inject private Stereotyped stereotyped;
  39. @PostConstruct
  40. public void postConstruct()
  41. {
  42. log.info("executing InterceptorResource.postConstruct()");
  43. }
  44. @javax.annotation.PreDestroy
  45. public void PreDestroy()
  46. {
  47. log.info("executing InterceptorResource.PreDestroy()");
  48. }
  49. @POST
  50. @Path("create")
  51. @Consumes(Constants.MEDIA_TYPE_TEST_XML)
  52. @Produces(MediaType.TEXT_PLAIN)
  53. @Interceptors ({Interceptor1.class})
  54. @MethodBinding
  55. @FilterBinding
  56. public Response createBook(Book book)
  57. {
  58. log.info("entering InterceptorResource.createBook()");
  59. int id = counter.getAndIncrement();
  60. book.setId(id);
  61. collection.put(id, book);
  62. log.info("stored: " + id + "->" + book);
  63. log.info("leaving InterceptorResource.createBook()");
  64. return Response.ok(id).build();
  65. }
  66. @GET
  67. @Path("book/{id:[0-9][0-9]*}")
  68. @Produces(Constants.MEDIA_TYPE_TEST_XML)
  69. @Interceptors ({Interceptor1.class})
  70. @MethodBinding
  71. @FilterBinding
  72. public Book lookupBookById(@PathParam("id") int id)
  73. {
  74. log.info("entering InterceptorResource.lookupBookById(" + id + ")");
  75. log.info("books: " + collection);
  76. Book book = collection.get(id);
  77. if (book == null)
  78. {
  79. throw new WebApplicationException(Response.Status.NOT_FOUND);
  80. }
  81. log.info("leaving InterceptorResource.lookupBookById(" + id + ")");
  82. return book;
  83. }
  84. @POST
  85. @Path("test")
  86. @Produces(MediaType.TEXT_PLAIN)
  87. @Interceptors ({Interceptor1.class})
  88. @MethodBinding
  89. public Response test()
  90. {
  91. log.info("entering InterceptorResource.test()");
  92. stereotyped.test();
  93. ArrayList<Class<?>> expectedList = new ArrayList<Class<?>>();
  94. expectedList.add(RequestFilterInterceptor.class); // TestRequestFilter.filter()
  95. expectedList.add(Interceptor0.class); // BookReader.isReadable()
  96. expectedList.add(Interceptor2.class); // BookReader.isReadable()
  97. expectedList.add(BookReaderInterceptorInterceptor.class); // BookReaderInterceptor.aroundReadFrom()
  98. expectedList.add(BookReaderInterceptor.class); // BookReader.readFrom()
  99. expectedList.add(Interceptor0.class); // BookReader.readFrom()
  100. expectedList.add(Interceptor1.class); // BookReader.readFrom()
  101. expectedList.add(Interceptor2.class); // BookReader.readFrom()
  102. expectedList.add(Interceptor3.class); // BookReader.readFrom()
  103. expectedList.add(PostConstructInterceptor.class); // InterceptorResource.postConstruct()
  104. expectedList.add(Interceptor0.class); // InterceptorResource.createBook()
  105. expectedList.add(Interceptor1.class); // InterceptorResource.createBook()
  106. expectedList.add(Interceptor2.class); // InterceptorResource.createBook()
  107. expectedList.add(Interceptor3.class); // InterceptorResource.createBook()
  108. expectedList.add(ResponseFilterInterceptor.class); // TestResponseFilter.filter()
  109. expectedList.add(BookWriterInterceptorInterceptor.class); // BookWriterInterceptor.aroundWriteTo()
  110. expectedList.add(BookWriterInterceptor.class); // BookWriter.writeTo()
  111. expectedList.add(PreDestroyInterceptor.class); // InterceptorResource.preDestroy()
  112. expectedList.add(RequestFilterInterceptor.class); // TestRequestFilter.filter()
  113. expectedList.add(PostConstructInterceptor.class); // InterceptorResource.postConstruct()
  114. expectedList.add(Interceptor0.class); // InterceptorResource.lookBookById()
  115. expectedList.add(Interceptor1.class); // InterceptorResource.lookBookById()
  116. expectedList.add(Interceptor2.class); // InterceptorResource.lookBookById()
  117. expectedList.add(Interceptor3.class); // InterceptorResource.lookBookById()
  118. expectedList.add(ResponseFilterInterceptor.class); // TestResponseFilter.filter()
  119. expectedList.add(Interceptor0.class); // BookWriter.isWriteable()
  120. expectedList.add(Interceptor2.class); // BookWriter.isWriteable()
  121. expectedList.add(Interceptor0.class); // BookWriter.getSize()
  122. expectedList.add(Interceptor2.class); // BookWriter.getSize()
  123. expectedList.add(BookWriterInterceptorInterceptor.class); // BookWriterInterceptor.aroundWriteTo()
  124. expectedList.add(BookWriterInterceptor.class); // BookWriter.writeTo()
  125. expectedList.add(Interceptor0.class); // BookWriter.writeTo()
  126. expectedList.add(Interceptor1.class); // BookWriter.writeTo()
  127. expectedList.add(Interceptor2.class); // BookWriter.writeTo()
  128. expectedList.add(Interceptor3.class); // BookWriter.writeTo()
  129. expectedList.add(PreDestroyInterceptor.class); // InterceptorResource.preDestroy()
  130. expectedList.add(Interceptor0.class); // BookReader.isReadable()
  131. expectedList.add(Interceptor2.class); // BookReader.isReadable()
  132. expectedList.add(BookReaderInterceptorInterceptor.class); // BookReaderInterceptor.aroundReadFrom()
  133. expectedList.add(BookReaderInterceptor.class); // BookReader.readFrom()
  134. expectedList.add(Interceptor0.class); // BookReader.readFrom()
  135. expectedList.add(Interceptor1.class); // BookReader.readFrom()
  136. expectedList.add(Interceptor2.class); // BookReader.readFrom()
  137. expectedList.add(Interceptor3.class); // BookReader.readFrom()
  138. expectedList.add(PostConstructInterceptor.class); // InterceptorResource.postConstruct()
  139. expectedList.add(Interceptor0.class); // InterceptorResource.test()
  140. expectedList.add(Interceptor1.class); // InterceptorResource.test()
  141. expectedList.add(Interceptor2.class); // InterceptorResource.test()
  142. expectedList.add(Interceptor3.class); // InterceptorResource.test()
  143. expectedList.add(Interceptor2.class); // Stereotyped.test()
  144. expectedList.add(Interceptor3.class); // Stereotyped.test()
  145. ArrayList<Object> visitList = VisitList.getList();
  146. boolean status = expectedList.size() == visitList.size();
  147. if (!status)
  148. {
  149. log.info("expectedList.size() [" + expectedList.size() + "] != visitList.size() [" + visitList.size() + "]");
  150. }
  151. for (int i = 0; i < expectedList.size(); i++)
  152. {
  153. if (!expectedList.get(i).isAssignableFrom(visitList.get(i).getClass()))
  154. {
  155. status = false;
  156. log.info("visitList.get(" + i + ") incorrect: should be an instance of: " + expectedList.get(i) + ", is: " + visitList.get(i));
  157. break;
  158. }
  159. }
  160. if (!status)
  161. {
  162. log.info("\rexpected list:");
  163. for (int i = 0; i < expectedList.size(); i++)
  164. {
  165. log.info(i + ": " + expectedList.get(i).toString());
  166. }
  167. log.info("\rvisited list:");
  168. for (int i = 0; i < visitList.size(); i++)
  169. {
  170. log.info(i + ": " + visitList.get(i).toString());
  171. }
  172. }
  173. log.info("leaving InterceptorResource.test()");
  174. return status ? Response.ok().build() : Response.serverError().build();
  175. }
  176. }