Search in sources :

Example 51 with OperationResourceInfo

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

the class JAXRSUtilsTest method testBeanParamsWithBooleanConverter.

@Test
public void testBeanParamsWithBooleanConverter() throws Exception {
    Class<?>[] argType = { Customer.CustomerBean.class };
    Method m = Customer.class.getMethod("testBeanParam", argType);
    Message messageImpl = createMessage();
    messageImpl.put(Message.REQUEST_URI, "/bar");
    // The converter converts any Boolean to null
    ProviderFactory.getInstance(messageImpl).registerUserProvider(new MyBoolParamConverterProvider());
    MultivaluedMap<String, String> headers = new MetadataMap<>();
    headers.putSingle("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    messageImpl.put(Message.PROTOCOL_HEADERS, headers);
    String body = "value=true";
    messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
    final ClassResourceInfo cri = new ClassResourceInfo(Customer.class);
    final MethodDispatcher md = new MethodDispatcher();
    final OperationResourceInfo ori = new OperationResourceInfo(m, cri);
    md.bind(ori, m);
    cri.setMethodDispatcher(md);
    cri.initBeanParamInfo(ServerProviderFactory.getInstance(messageImpl));
    List<Object> params = JAXRSUtils.processParameters(ori, null, messageImpl);
    assertEquals("Bean should be created", 1, params.size());
    Customer.CustomerBean cb = (Customer.CustomerBean) params.get(0);
    assertNotNull(cb);
    assertNull(cb.getBool());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) 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) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Test(org.junit.Test)

Example 52 with OperationResourceInfo

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

the class JAXRSUtilsTest method testLocaleParameter.

@Test
public void testLocaleParameter() throws Exception {
    Message messageImpl = createMessage();
    ProviderFactory.getInstance(messageImpl).registerUserProvider(new LocaleParameterHandler());
    Class<?>[] argType = { Locale.class };
    Method m = Customer.class.getMethod("testLocaleParam", argType);
    messageImpl.put(Message.QUERY_STRING, "p1=en_us");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(1, params.size());
    Locale l = (Locale) params.get(0);
    assertEquals("en", l.getLanguage());
    assertEquals("US", l.getCountry());
}
Also used : Locale(java.util.Locale) 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 53 with OperationResourceInfo

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

the class JAXRSUtilsTest method testMatrixParameters.

@SuppressWarnings("unchecked")
@Test
public void testMatrixParameters() throws Exception {
    Class<?>[] argType = { String.class, String.class, String.class, String.class, List.class, String.class };
    Method m = Customer.class.getMethod("testMatrixParam", argType);
    Message messageImpl = createMessage();
    messageImpl.put(Message.REQUEST_URI, "/foo;p4=0;p3=3/bar;p1=1;p2=/baz;p4=4;p4=5;p5");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals("5 Matrix params should've been identified", 6, params.size());
    assertEquals("First Matrix Parameter not matched correctly", "1", params.get(0));
    assertEquals("Second Matrix Parameter was not matched correctly", "", params.get(1));
    assertEquals("Third Matrix Parameter was not matched correctly", "3", params.get(2));
    assertEquals("Fourth Matrix Parameter was not matched correctly", "0", params.get(3));
    List<String> list = (List<String>) params.get(4);
    assertEquals(3, list.size());
    assertEquals("0", list.get(0));
    assertEquals("4", list.get(1));
    assertEquals("5", list.get(2));
    assertNull("Sixth Matrix Parameter was not matched correctly", params.get(5));
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 54 with OperationResourceInfo

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

the class JAXRSUtilsTest method testConversion.

@Test
public void testConversion() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("testConversion", new Class[] { PathSegmentImpl.class, SimpleFactory.class }), cri);
    ori.setHttpMethod("GET");
    ori.setURITemplate(new URITemplate("{id1}/{id2}"));
    MultivaluedMap<String, String> values = new MetadataMap<>();
    values.putSingle("id1", "1");
    values.putSingle("id2", "2");
    Message m = createMessage();
    List<Object> params = JAXRSUtils.processParameters(ori, values, m);
    PathSegment ps = (PathSegment) params.get(0);
    assertEquals("1", ps.getPath());
    SimpleFactory sf = (SimpleFactory) params.get(1);
    assertEquals(2, sf.getId());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) SimpleFactory(org.apache.cxf.jaxrs.SimpleFactory) PathSegment(javax.ws.rs.core.PathSegment) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) PathSegmentImpl(org.apache.cxf.jaxrs.impl.PathSegmentImpl) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 55 with OperationResourceInfo

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

the class JAXRSUtilsTest method testFormParametersAndMap.

@SuppressWarnings("unchecked")
@Test
public void testFormParametersAndMap() throws Exception {
    Class<?>[] argType = { MultivaluedMap.class, String.class, List.class };
    Method m = Customer.class.getMethod("testMultivaluedMapAndFormParam", argType);
    final Message messageImpl = createMessage();
    String body = "p1=1&p2=2&p2=3";
    messageImpl.put(Message.REQUEST_URI, "/foo");
    messageImpl.put("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
    ProviderFactory.getInstance(messageImpl).registerUserProvider(new FormEncodingProvider<Object>() {

        @Override
        protected void persistParamsOnMessage(MultivaluedMap<String, String> params) {
            messageImpl.put(FormUtils.FORM_PARAM_MAP, params);
        }
    });
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), new MetadataMap<String, String>(), messageImpl);
    assertEquals("3 params should've been identified", 3, params.size());
    MultivaluedMap<String, String> map = (MultivaluedMap<String, String>) params.get(0);
    assertEquals(2, map.size());
    assertEquals(1, map.get("p1").size());
    assertEquals("First map parameter not matched correctly", "1", map.getFirst("p1"));
    assertEquals(2, map.get("p2").size());
    assertEquals("2", map.get("p2").get(0));
    assertEquals("3", map.get("p2").get(1));
    assertEquals("First Form Parameter not matched correctly", "1", params.get(1));
    List<String> list = (List<String>) params.get(2);
    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) ByteArrayInputStream(java.io.ByteArrayInputStream) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) ArrayList(java.util.ArrayList) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) 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