Search in sources :

Example 41 with MetadataMap

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

the class JAXRSSamlTest method testGetBookPreviousSAMLTokenInForm.

@Test
public void testGetBookPreviousSAMLTokenInForm() throws Exception {
    String address = "https://localhost:" + PORT + "/samlform/bookstore/books";
    FormEncodingProvider<Form> formProvider = new FormEncodingProvider<Form>();
    formProvider.setExpectedEncoded(true);
    WebClient wc = createWebClientForExistingToken(address, new SamlFormOutInterceptor(), formProvider);
    wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML);
    try {
        Book book = wc.post(new Form(new MetadataMap<String, String>()).param("name", "CXF").param("id", "125"), Book.class);
        assertEquals(125L, book.getId());
    } catch (WebApplicationException ex) {
        fail(ex.getMessage());
    } catch (ProcessingException ex) {
        if (ex.getCause() != null && ex.getCause().getMessage() != null) {
            fail(ex.getCause().getMessage());
        } else {
            fail(ex.getMessage());
        }
    }
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) WebApplicationException(javax.ws.rs.WebApplicationException) Form(javax.ws.rs.core.Form) Book(org.apache.cxf.systest.jaxrs.security.Book) FormEncodingProvider(org.apache.cxf.jaxrs.provider.FormEncodingProvider) SamlFormOutInterceptor(org.apache.cxf.rs.security.saml.SamlFormOutInterceptor) WebClient(org.apache.cxf.jaxrs.client.WebClient) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 42 with MetadataMap

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

the class AbstractClient method getTemplateParametersMap.

protected MultivaluedMap<String, String> getTemplateParametersMap(URITemplate template, List<Object> values) {
    if (values != null && values.size() != 0) {
        List<String> vars = template.getVariables();
        MultivaluedMap<String, String> templatesMap = new MetadataMap<String, String>(vars.size());
        for (int i = 0; i < vars.size(); i++) {
            if (i < values.size()) {
                templatesMap.add(vars.get(i), values.get(i).toString());
            }
        }
        return templatesMap;
    }
    return null;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 43 with MetadataMap

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

the class JAXRSUtilsTest method testMultipleCookieParameters.

@Test
public void testMultipleCookieParameters() throws Exception {
    Class<?>[] argType = { String.class, String.class, Cookie.class };
    Method m = Customer.class.getMethod("testMultipleCookieParam", argType);
    Message messageImpl = createMessage();
    MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
    headers.add("Cookie", "c1=c1Value; c2=c2Value");
    headers.add("Cookie", "c3=c3Value");
    messageImpl.put(Message.PROTOCOL_HEADERS, headers);
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(params.size(), 3);
    assertEquals("c1Value", params.get(0));
    assertEquals("c2Value", params.get(1));
    assertEquals("c3Value", ((Cookie) params.get(2)).getValue());
}
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 44 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap 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<String, String>();
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage2(), "/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<String, String>();
    ori = findTargetResourceClass(resources, createMessage2(), "/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<String, String>();
    ori = findTargetResourceClass(resources, createMessage2(), "/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 45 with MetadataMap

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

the class JAXRSUtilsTest method testFormParametersBeanWithBoolean.

@Test
public void testFormParametersBeanWithBoolean() 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()));
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals("Bean should be created", 1, params.size());
    Customer.CustomerBean cb = (Customer.CustomerBean) params.get(0);
    assertNotNull(cb);
    assertEquals("aValue", cb.getA());
    assertEquals(new Long(123), cb.getB());
    assertTrue(cb.isCb());
}
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) 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