Search in sources :

Example 21 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class FormEncodingProviderTest method testWriteMultipleValues.

@Test
public void testWriteMultipleValues() throws Exception {
    MultivaluedMap<String, String> mvMap = new MetadataMap<String, String>();
    mvMap.add("a", "a1");
    mvMap.add("a", "a2");
    FormEncodingProvider<MultivaluedMap<?, ?>> ferp = new FormEncodingProvider<MultivaluedMap<?, ?>>();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ferp.writeTo(mvMap, MultivaluedMap.class, MultivaluedMap.class, new Annotation[0], MediaType.APPLICATION_FORM_URLENCODED_TYPE, new MetadataMap<String, Object>(), bos);
    String result = bos.toString();
    assertEquals("Wrong value", "a=a1&a=a2", result);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 22 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class JAXRSServiceFactoryBeanTest method testNoSubResources.

@Test
public void testNoSubResources() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setEnableStaticResolution(true);
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    assertEquals(1, resources.size());
    // Verify root ClassResourceInfo: BookStoreNoSubResource
    ClassResourceInfo rootCri = resources.get(0);
    assertNotNull(rootCri.getURITemplate());
    URITemplate template = rootCri.getURITemplate();
    MultivaluedMap<String, String> values = new MetadataMap<String, String>();
    assertTrue(template.match("/bookstore/books/123", values));
    assertFalse(rootCri.hasSubResources());
    MethodDispatcher md = rootCri.getMethodDispatcher();
    assertEquals(7, md.getOperationResourceInfos().size());
    Set<OperationResourceInfo> ops = md.getOperationResourceInfos();
    assertTrue("No operation found", verifyOp(ops, "getBook", "GET", false));
    assertTrue("No operation found", verifyOp(ops, "getBookStoreInfo", "GET", false));
    assertTrue("No operation found", verifyOp(ops, "getBooks", "GET", false));
    assertTrue("No operation found", verifyOp(ops, "getBookJSON", "GET", false));
    assertTrue("No operation found", verifyOp(ops, "addBook", "POST", false));
    assertTrue("No operation found", verifyOp(ops, "updateBook", "PUT", false));
    assertTrue("No operation found", verifyOp(ops, "deleteBook", "DELETE", false));
}
Also used : ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Test(org.junit.Test)

Example 23 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class SelectMethodCandidatesTest method testSelectBar.

@Test
public void testSelectBar() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResource.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    String acceptContentTypes = "application/bar,application/foo;q=0.8";
    MetadataMap<String, String> values = new MetadataMap<String, String>();
    OperationResourceInfo ori = findTargetResourceClass(resources, null, "/1/2/3/d/custom", "GET", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("readBar", ori.getMethodToInvoke().getName());
    acceptContentTypes = "application/foo,application/bar;q=0.8";
    ori = findTargetResourceClass(resources, null, "/1/2/3/d/custom", "GET", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("readFoo", ori.getMethodToInvoke().getName());
    acceptContentTypes = "application/foo;q=0.5,application/bar";
    ori = findTargetResourceClass(resources, null, "/1/2/3/d/custom", "GET", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("readBar", ori.getMethodToInvoke().getName());
    acceptContentTypes = "application/foo,application/bar;q=0.5";
    ori = findTargetResourceClass(resources, null, "/1/2/3/d/custom", "GET", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("readFoo", ori.getMethodToInvoke().getName());
}
Also used : 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 24 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class SelectMethodCandidatesTest method doTestRootResourcesWithSameName.

private void doTestRootResourcesWithSameName(String path, String methodName, Class<?> expectedRoot) throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(RootResource.class, RootResource2.class, RootResource3.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    assertEquals(3, resources.size());
    String contentTypes = "text/xml";
    String acceptContentTypes = "text/xml";
    Message m = prepareMessage();
    MetadataMap<String, String> values = new MetadataMap<String, String>();
    OperationResourceInfo ori = findTargetResourceClass(resources, m, path, "PUT", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", methodName, ori.getMethodToInvoke().getName());
    assertSame(expectedRoot, ori.getClassResourceInfo().getServiceClass());
}
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)

Example 25 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class SelectMethodCandidatesTest method testFindTargetResourceClassWithTemplates.

@Test
public void testFindTargetResourceClassWithTemplates() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResource.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    String acceptContentTypes = "application/xml";
    // 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<String, String>();
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/1/2/3/d", "GET", values, contentTypes, Collections.singletonList(MediaType.valueOf(acceptContentTypes)));
    assertNotNull(ori);
    assertEquals("listMethod needs to be selected", "listMethod", ori.getMethodToInvoke().getName());
    acceptContentTypes = "application/xml,application/json;q=0.8";
    ori = findTargetResourceClass(resources, null, "/1/2/3/d/1", "GET", values, contentTypes, JAXRSUtils.parseMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("readMethod needs to be selected", "readMethod", ori.getMethodToInvoke().getName());
    contentTypes = "application/xml";
    acceptContentTypes = "application/xml";
    ori = findTargetResourceClass(resources, null, "/1/2/3/d/1", "GET", values, contentTypes, Collections.singletonList(MediaType.valueOf(acceptContentTypes)));
    assertNotNull(ori);
    assertEquals("readMethod needs to be selected", "readMethod", ori.getMethodToInvoke().getName());
    contentTypes = "application/json";
    acceptContentTypes = "application/json";
    ori = findTargetResourceClass(resources, null, "/1/2/3/d/1/bar/baz/baz", "GET", values, contentTypes, Collections.singletonList(MediaType.valueOf(acceptContentTypes)));
    assertNotNull(ori);
    assertEquals("readMethod2 needs to be selected", "readMethod2", ori.getMethodToInvoke().getName());
    contentTypes = "application/json";
    acceptContentTypes = "application/json";
    ori = findTargetResourceClass(resources, null, "/1/2/3/d/1", "GET", values, contentTypes, Collections.singletonList(MediaType.valueOf(acceptContentTypes)));
    assertNotNull(ori);
    assertEquals("unlimitedPath needs to be selected", "unlimitedPath", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, null, "/1/2/3/d/1/2", "GET", values, contentTypes, Collections.singletonList(MediaType.valueOf(acceptContentTypes)));
    assertNotNull(ori);
    assertEquals("limitedPath needs to be selected", "limitedPath", ori.getMethodToInvoke().getName());
}
Also used : 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)

Aggregations

MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)80 Test (org.junit.Test)43 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)36 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)34 ByteArrayInputStream (java.io.ByteArrayInputStream)25 Message (org.apache.cxf.message.Message)25 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)15 List (java.util.List)13 Method (java.lang.reflect.Method)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Endpoint (org.apache.cxf.endpoint.Endpoint)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 LinkedHashMap (java.util.LinkedHashMap)9 Customer (org.apache.cxf.jaxrs.Customer)9 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Annotation (java.lang.annotation.Annotation)8 HashMap (java.util.HashMap)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)7