Search in sources :

Example 46 with MetadataMap

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

the class JAXRSUtilsTest method testFindTargetResourceClass.

@Test
public void testFindTargetResourceClass() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.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.
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books/123/", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/json,application/xml;q=0.9"));
    assertNotNull(ori);
    assertEquals("getBookJSON", ori.getMethodToInvoke().getName());
    // test
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/json"));
    assertNotNull(ori);
    assertEquals("getBookJSON", ori.getMethodToInvoke().getName());
    // test
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    assertNotNull(ori);
    assertEquals("getBook", ori.getMethodToInvoke().getName());
    // test
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    assertNotNull(ori);
    assertEquals("getBooks", ori.getMethodToInvoke().getName());
    // test find POST
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books", "POST", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    assertNotNull(ori);
    assertEquals("addBook", ori.getMethodToInvoke().getName());
    // test find PUT
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books", "PUT", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    assertEquals("updateBook", ori.getMethodToInvoke().getName());
    // test find DELETE
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books/123", "DELETE", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    assertNotNull(ori);
    assertEquals("deleteBook", ori.getMethodToInvoke().getName());
}
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 47 with MetadataMap

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

the class JAXRSUtilsTest method testMatrixAndPathSegmentParameters.

@Test
public void testMatrixAndPathSegmentParameters() throws Exception {
    Class<?>[] argType = { PathSegment.class, String.class };
    Method m = Customer.class.getMethod("testPathSegment", argType);
    Message messageImpl = createMessage();
    messageImpl.put(Message.REQUEST_URI, "/bar%20foo;p4=0%201");
    MultivaluedMap<String, String> values = new MetadataMap<String, String>();
    values.add("ps", "bar%20foo;p4=0%201");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), values, messageImpl);
    assertEquals("2 params should've been identified", 2, params.size());
    PathSegment ps = (PathSegment) params.get(0);
    assertEquals("bar foo", ps.getPath());
    assertEquals(1, ps.getMatrixParameters().size());
    assertEquals("0 1", ps.getMatrixParameters().getFirst("p4"));
    assertEquals("bar foo", params.get(1));
}
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) PathSegment(javax.ws.rs.core.PathSegment) Test(org.junit.Test)

Example 48 with MetadataMap

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

the class JAXRSUtilsTest method testHttpContextParameters.

@SuppressWarnings("unchecked")
@Test
public void testHttpContextParameters() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("testParams", new Class[] { UriInfo.class, HttpHeaders.class, Request.class, SecurityContext.class, Providers.class, String.class, List.class }), cri);
    ori.setHttpMethod("GET");
    MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
    headers.add("Foo", "bar, baz");
    Message m = createMessage();
    m.put("org.apache.cxf.http.header.split", "true");
    m.put(Message.PROTOCOL_HEADERS, headers);
    List<Object> params = JAXRSUtils.processParameters(ori, new MetadataMap<String, String>(), m);
    assertEquals("7 parameters expected", 7, params.size());
    assertSame(UriInfoImpl.class, params.get(0).getClass());
    assertSame(HttpHeadersImpl.class, params.get(1).getClass());
    assertSame(RequestImpl.class, params.get(2).getClass());
    assertSame(SecurityContextImpl.class, params.get(3).getClass());
    assertSame(ProvidersImpl.class, params.get(4).getClass());
    assertSame(String.class, params.get(5).getClass());
    assertEquals("Wrong header param", "bar", params.get(5));
    List<String> values = (List<String>) params.get(6);
    assertEquals("Wrong headers size", 2, values.size());
    assertEquals("Wrong 1st header param", "bar", values.get(0));
    assertEquals("Wrong 2nd header param", "baz", values.get(1));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Request(javax.ws.rs.core.Request) ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Providers(javax.ws.rs.ext.Providers) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) SecurityContext(javax.ws.rs.core.SecurityContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) ArrayList(java.util.ArrayList) UriInfo(javax.ws.rs.core.UriInfo) ThreadLocalUriInfo(org.apache.cxf.jaxrs.impl.tl.ThreadLocalUriInfo) Test(org.junit.Test)

Example 49 with MetadataMap

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

the class JAXRSUtilsTest method testFormParametersBean.

@Test
public void testFormParametersBean() throws Exception {
    Class<?>[] argType = { Customer.CustomerBean.class };
    Method m = Customer.class.getMethod("testFormBean", argType);
    Message messageImpl = createMessage();
    messageImpl.put(Message.REQUEST_URI, "/bar");
    MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
    headers.putSingle("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    messageImpl.put(Message.PROTOCOL_HEADERS, headers);
    String body = "a=aValue&b=123&cb=true";
    messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
    Message complexMessageImpl = createMessage();
    complexMessageImpl.put(Message.REQUEST_URI, "/bar");
    complexMessageImpl.put(Message.PROTOCOL_HEADERS, headers);
    body = "c=1&a=A&b=123&c=2&c=3&" + "d.c=4&d.a=B&d.b=456&d.c=5&d.c=6&" + "e.c=41&e.a=B1&e.b=457&e.c=51&e.c=61&" + "e.c=42&e.a=B2&e.b=458&e.c=52&e.c=62&" + "d.d.c=7&d.d.a=C&d.d.b=789&d.d.c=8&d.d.c=9&" + "d.e.c=71&d.e.a=C1&d.e.b=790&d.e.c=81&d.e.c=91&" + "d.e.c=72&d.e.a=C2&d.e.b=791&d.e.c=82&d.e.c=92";
    complexMessageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
    verifyParametersBean(m, null, messageImpl, null, complexMessageImpl);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ByteArrayInputStream(java.io.ByteArrayInputStream) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 50 with MetadataMap

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

the class JAXRSUtilsTest method doTestFormParamsWithEncoding.

private void doTestFormParamsWithEncoding(String enc, boolean setEnc) throws Exception {
    Class<?>[] argType = { String.class, List.class };
    Method m = Customer.class.getMethod("testFormParam", argType);
    Message messageImpl = createMessage();
    String body = "p1=" + URLEncoder.encode("\u00E4\u00F6\u00FC", enc) + "&p2=2&p2=3";
    messageImpl.put(Message.REQUEST_URI, "/foo");
    MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
    String ct = MediaType.APPLICATION_FORM_URLENCODED;
    if (setEnc) {
        ct += ";charset=" + enc;
    }
    headers.putSingle("Content-Type", ct);
    messageImpl.put(Message.PROTOCOL_HEADERS, headers);
    messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals("2 form params should've been identified", 2, params.size());
    assertEquals("First Form Parameter not matched correctly", "\u00E4\u00F6\u00FC", params.get(0));
    List<String> list = CastUtils.cast((List<?>) params.get(1));
    assertEquals(2, list.size());
    assertEquals("2", list.get(0));
    assertEquals("3", list.get(1));
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Method(java.lang.reflect.Method) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

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