Search in sources :

Example 1 with FormEncodingProvider

use of org.apache.cxf.jaxrs.provider.FormEncodingProvider in project cxf by apache.

the class OAuthRequestFilter method getTokenFromFormData.

protected String getTokenFromFormData(Message message) {
    String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
    String type = (String) message.get(Message.CONTENT_TYPE);
    if (type != null && MediaType.APPLICATION_FORM_URLENCODED.startsWith(type) && method != null && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT))) {
        try {
            FormEncodingProvider<Form> provider = new FormEncodingProvider<Form>(true);
            Form form = FormUtils.readForm(provider, message);
            MultivaluedMap<String, String> formData = form.asMap();
            String token = formData.getFirst(OAuthConstants.ACCESS_TOKEN);
            if (token != null) {
                FormUtils.restoreForm(provider, form, message);
                return token;
            }
        } catch (Exception ex) {
        // the exception will be thrown below
        }
    }
    AuthorizationUtils.throwAuthorizationFailure(supportedSchemes, realm);
    return null;
}
Also used : Form(javax.ws.rs.core.Form) FormEncodingProvider(org.apache.cxf.jaxrs.provider.FormEncodingProvider)

Example 2 with FormEncodingProvider

use of org.apache.cxf.jaxrs.provider.FormEncodingProvider 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 3 with FormEncodingProvider

use of org.apache.cxf.jaxrs.provider.FormEncodingProvider in project cxf by apache.

the class JAXRSSamlTest method testGetBookSAMLTokenInForm.

@Test
public void testGetBookSAMLTokenInForm() throws Exception {
    String address = "https://localhost:" + PORT + "/samlform/bookstore/books";
    FormEncodingProvider<Form> formProvider = new FormEncodingProvider<Form>();
    formProvider.setExpectedEncoded(true);
    WebClient wc = createWebClient(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)

Aggregations

Form (javax.ws.rs.core.Form)3 FormEncodingProvider (org.apache.cxf.jaxrs.provider.FormEncodingProvider)3 ProcessingException (javax.ws.rs.ProcessingException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)2 SamlFormOutInterceptor (org.apache.cxf.rs.security.saml.SamlFormOutInterceptor)2 Book (org.apache.cxf.systest.jaxrs.security.Book)2 Test (org.junit.Test)2