Search in sources :

Example 96 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class SelectMethodCandidatesTest method testDefaultMethod.

@Test
public void testDefaultMethod() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(DefaultMethodResource.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentType = "text/xml";
    String acceptContentTypes = "text/xml";
    Message m = new MessageImpl();
    m.put(Message.CONTENT_TYPE, contentType);
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(m);
    m.setExchange(ex);
    Endpoint e = mockEndpoint();
    ex.put(Endpoint.class, e);
    MetadataMap<String, String> values = new MetadataMap<>();
    OperationResourceInfo ori = findTargetResourceClass(resources, m, "/service/all", "PUT", values, contentType, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", "all", ori.getMethodToInvoke().getName());
    values.clear();
    ori = findTargetResourceClass(resources, m, "/service/all", "GET", values, contentType, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", "getAll", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, m, "/service", "GET", values, contentType, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", "get", ori.getMethodToInvoke().getName());
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Exchange(org.apache.cxf.message.Exchange) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Endpoint(org.apache.cxf.endpoint.Endpoint) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 97 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class SelectMethodCandidatesTest method testFindOverridesDifferentArguments.

@Test
public void testFindOverridesDifferentArguments() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(CustomizedApi.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    Message m = createMessage();
    m.put(Message.CONTENT_TYPE, "application/json");
    MetadataMap<String, String> values = new MetadataMap<>();
    OperationResourceInfo ori = findTargetResourceClass(resources, m, "/api", "GET", values, "application/json", sortMediaTypes("*/*"));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", "getApi", ori.getMethodToInvoke().getName());
    final Method expected = CustomizedApi.class.getMethod("getApi", new Class[] { ServletConfig.class, HttpHeaders.class, UriInfo.class, String.class });
    assertEquals(expected, ori.getMethodToInvoke());
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 98 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testFindTargetResourceClassWithTemplates.

@Test
public void testFindTargetResourceClassWithTemplates() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreTemplates.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    // If acceptContentTypes does not specify a specific Mime type, the
    // method is declared with a most specific ProduceMime type is selected.
    MetadataMap<String, String> values = new MetadataMap<>();
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/1/2/", "GET", values, contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("getBooks", ori.getMethodToInvoke().getName());
    assertEquals("Only id and final match groups should be there", 2, values.size());
    assertEquals("2 {id} values should've been picked up", 2, values.get("id").size());
    assertEquals("FINAL_MATCH_GROUP should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
    assertEquals("First {id} is 1", "1", values.getFirst("id"));
    assertEquals("Second id is 2", "2", values.get("id").get(1));
    values = new MetadataMap<>();
    ori = findTargetResourceClass(resources, createMessage(), "/2", "POST", values, contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("updateBookStoreInfo", ori.getMethodToInvoke().getName());
    assertEquals("Only id and final match groups should be there", 2, values.size());
    assertEquals("Only single {id} should've been picked up", 1, values.get("id").size());
    assertEquals("FINAL_MATCH_GROUP should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
    assertEquals("Only the first {id} should've been picked up", "2", values.getFirst("id"));
    values = new MetadataMap<>();
    ori = findTargetResourceClass(resources, createMessage(), "/3/4", "PUT", values, contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("updateBook", ori.getMethodToInvoke().getName());
    assertEquals("Only the first {id} should've been picked up", 3, values.size());
    assertEquals("Only the first {id} should've been picked up", 1, values.get("id").size());
    assertEquals("Only the first {id} should've been picked up", 1, values.get("bookId").size());
    assertEquals("Only the first {id} should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
    assertEquals("Only the first {id} should've been picked up", "3", values.getFirst("id"));
    assertEquals("Only the first {id} should've been picked up", "4", values.getFirst("bookId"));
}
Also used : JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 99 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testServletResourceFields.

@Test
public void testServletResourceFields() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    cri.setResourceProvider(new PerRequestResourceProvider(Customer.class));
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("postConstruct", new Class[] {}), cri);
    Customer c = new Customer();
    // Creating mocks for the servlet request, response and context
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    EasyMock.replay(request);
    EasyMock.replay(response);
    EasyMock.replay(context);
    Message m = createMessage();
    m.put(AbstractHTTPDestination.HTTP_REQUEST, request);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, context);
    InjectionUtils.injectContextFields(c, ori.getClassResourceInfo(), m);
    assertSame(request.getClass(), ((HttpServletRequestFilter) c.getServletRequestResource()).getRequest().getClass());
    HttpServletResponseFilter filter = (HttpServletResponseFilter) c.getServletResponseResource();
    assertSame(response.getClass(), filter.getResponse().getClass());
    assertSame(context.getClass(), c.getServletContextResource().getClass());
    assertNotNull(c.getServletRequest());
    assertNotNull(c.getServletResponse());
    assertNotNull(c.getServletContext());
    assertNotNull(c.getServletRequestResource());
    assertNotNull(c.getServletResponseResource());
    assertNotNull(c.getServletContextResource());
    assertSame(request.getClass(), ((HttpServletRequestFilter) c.getServletRequestResource()).getRequest().getClass());
    filter = (HttpServletResponseFilter) c.getServletResponse();
    assertSame(response.getClass(), filter.getResponse().getClass());
    assertSame(context.getClass(), c.getServletContext().getClass());
}
Also used : ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) HttpServletRequestFilter(org.apache.cxf.jaxrs.impl.HttpServletRequestFilter) Test(org.junit.Test)

Example 100 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testServletContextParameters.

@Test
public void testServletContextParameters() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("testServletParams", new Class[] { HttpServletRequest.class, HttpServletResponse.class, ServletContext.class, ServletConfig.class }), cri);
    ori.setHttpMethod("GET");
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpServletResponse response = new HttpServletResponseFilter(EasyMock.createMock(HttpServletResponse.class), null);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    ServletConfig config = EasyMock.createMock(ServletConfig.class);
    EasyMock.replay(request);
    EasyMock.replay(context);
    EasyMock.replay(config);
    Message m = createMessage();
    m.put(AbstractHTTPDestination.HTTP_REQUEST, request);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, context);
    m.put(AbstractHTTPDestination.HTTP_CONFIG, config);
    List<Object> params = JAXRSUtils.processParameters(ori, new MetadataMap<String, String>(), m);
    assertEquals("4 parameters expected", 4, params.size());
    assertSame(request.getClass(), ((HttpServletRequestFilter) params.get(0)).getRequest().getClass());
    assertSame(response.getClass(), params.get(1).getClass());
    assertSame(context.getClass(), params.get(2).getClass());
    assertSame(config.getClass(), params.get(3).getClass());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ServletConfig(javax.servlet.ServletConfig) ThreadLocalServletConfig(org.apache.cxf.jaxrs.impl.tl.ThreadLocalServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) HttpServletRequestFilter(org.apache.cxf.jaxrs.impl.HttpServletRequestFilter) Test(org.junit.Test)

Aggregations

OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)129 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)104 Message (org.apache.cxf.message.Message)72 Test (org.junit.Test)70 Method (java.lang.reflect.Method)52 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)40 Customer (org.apache.cxf.jaxrs.Customer)22 Endpoint (org.apache.cxf.endpoint.Endpoint)13 MessageImpl (org.apache.cxf.message.MessageImpl)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ArrayList (java.util.ArrayList)12 List (java.util.List)12 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)11 Response (javax.ws.rs.core.Response)11 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)11 Exchange (org.apache.cxf.message.Exchange)11 MediaType (javax.ws.rs.core.MediaType)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)7