Search in sources :

Example 61 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap 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 = EasyMock.createMock(Endpoint.class);
    e.isEmpty();
    EasyMock.expectLastCall().andReturn(true).anyTimes();
    e.size();
    EasyMock.expectLastCall().andReturn(0).anyTimes();
    e.getEndpointInfo();
    EasyMock.expectLastCall().andReturn(null).anyTimes();
    e.get(ServerProviderFactory.class.getName());
    EasyMock.expectLastCall().andReturn(ServerProviderFactory.getInstance()).times(3);
    e.get("org.apache.cxf.jaxrs.comparator");
    EasyMock.expectLastCall().andReturn(null);
    EasyMock.replay(e);
    ex.put(Endpoint.class, e);
    MetadataMap<String, String> values = new MetadataMap<String, String>();
    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 : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) 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 62 with MetadataMap

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

the class SelectMethodCandidatesTest method testFindFromAbstractGenericImpl4.

@Test
public void testFindFromAbstractGenericImpl4() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(GenericEntityImpl4.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "text/xml";
    String acceptContentTypes = "text/xml";
    Message m = new MessageImpl();
    m.put(Message.CONTENT_TYPE, "text/xml");
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(m);
    m.setExchange(ex);
    Endpoint e = EasyMock.createMock(Endpoint.class);
    e.isEmpty();
    EasyMock.expectLastCall().andReturn(true).anyTimes();
    e.size();
    EasyMock.expectLastCall().andReturn(0).anyTimes();
    e.getEndpointInfo();
    EasyMock.expectLastCall().andReturn(null).anyTimes();
    e.get(ServerProviderFactory.class.getName());
    EasyMock.expectLastCall().andReturn(ServerProviderFactory.getInstance()).times(2);
    e.get("org.apache.cxf.jaxrs.comparator");
    EasyMock.expectLastCall().andReturn(null);
    EasyMock.replay(e);
    ex.put(Endpoint.class, e);
    MetadataMap<String, String> values = new MetadataMap<String, String>();
    OperationResourceInfo ori = findTargetResourceClass(resources, m, "/books", "POST", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", "postEntity", ori.getMethodToInvoke().getName());
    String value = "<Books><Book><name>The Book</name><id>2</id></Book></Books>";
    m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
    List<Object> params = JAXRSUtils.processParameters(ori, values, m);
    assertEquals(1, params.size());
    List<?> books = (List<?>) params.get(0);
    assertEquals(1, books.size());
    Book book = (Book) books.get(0);
    assertNotNull(book);
    assertEquals(2L, book.getId());
    assertEquals("The Book", book.getName());
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) 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) ByteArrayInputStream(java.io.ByteArrayInputStream) Book(org.apache.cxf.jaxrs.resources.Book) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 63 with MetadataMap

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

the class SelectMethodCandidatesTest method doTestFindTargetSubResource.

public void doTestFindTargetSubResource(String path, String method, boolean setKeepSubProp) 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 = "text/xml,*/*";
    MetadataMap<String, String> values = new MetadataMap<String, String>();
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), path, "GET", values, contentTypes, sortMediaTypes(acceptContentTypes), setKeepSubProp);
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", method, 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)

Example 64 with MetadataMap

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

the class SelectMethodCandidatesTest method testFindFromAbstractGenericClass3.

@Test
public void testFindFromAbstractGenericClass3() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(BookEntity.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "text/xml";
    String acceptContentTypes = "text/xml";
    Message m = new MessageImpl();
    m.put(Message.CONTENT_TYPE, "text/xml");
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(m);
    m.setExchange(ex);
    Endpoint e = EasyMock.createMock(Endpoint.class);
    e.isEmpty();
    EasyMock.expectLastCall().andReturn(true).anyTimes();
    e.size();
    EasyMock.expectLastCall().andReturn(0).anyTimes();
    e.getEndpointInfo();
    EasyMock.expectLastCall().andReturn(null).anyTimes();
    e.get(ServerProviderFactory.class.getName());
    EasyMock.expectLastCall().andReturn(ServerProviderFactory.getInstance()).times(3);
    e.get("org.apache.cxf.jaxrs.comparator");
    EasyMock.expectLastCall().andReturn(null);
    EasyMock.replay(e);
    ex.put(Endpoint.class, e);
    MetadataMap<String, String> values = new MetadataMap<String, String>();
    OperationResourceInfo ori = findTargetResourceClass(resources, m, "/books", "PUT", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", "putEntity", ori.getMethodToInvoke().getName());
    String value = "<Chapter><title>The Book</title><id>2</id></Chapter>";
    m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
    List<Object> params = JAXRSUtils.processParameters(ori, values, m);
    assertEquals(1, params.size());
    Chapter c = (Chapter) params.get(0);
    assertNotNull(c);
    assertEquals(2L, c.getId());
    assertEquals("The Book", c.getTitle());
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Chapter(org.apache.cxf.jaxrs.resources.Chapter) Exchange(org.apache.cxf.message.Exchange) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Endpoint(org.apache.cxf.endpoint.Endpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 65 with MetadataMap

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

the class BinaryDataProviderTest method testReadBytesFromUtf8.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testReadBytesFromUtf8() throws Exception {
    MessageBodyReader p = new BinaryDataProvider();
    byte[] utf8Bytes = "世界ーファイル".getBytes("UTF-16");
    byte[] readBytes = (byte[]) p.readFrom(byte[].class, byte[].class, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), new ByteArrayInputStream(utf8Bytes));
    assertTrue(Arrays.equals(utf8Bytes, readBytes));
}
Also used : MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Annotation(java.lang.annotation.Annotation) 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