Search in sources :

Example 6 with MessageContextImpl

use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.

the class XSLTJaxbProviderTest method testWrite.

@Test
public void testWrite() throws Exception {
    XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>();
    provider.setOutTemplate(TEMPLATE_LOCATION);
    provider.setMessageContext(new MessageContextImpl(createMessage()));
    Book b = new Book();
    b.setId(123L);
    b.setName("TheBook");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(b, Book.class, Book.class, b.getClass().getAnnotations(), MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    Unmarshaller um = provider.getClassContext(Book.class).createUnmarshaller();
    Book b2 = (Book) um.unmarshal(new StringReader(bos.toString()));
    b.setName("TheBook2");
    assertEquals("Transformation is bad", b, b2);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Unmarshaller(javax.xml.bind.Unmarshaller) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl) Test(org.junit.Test)

Example 7 with MessageContextImpl

use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.

the class XSLTJaxbProviderTest method testWriteToStreamWriter.

@Test
public void testWriteToStreamWriter() throws Exception {
    XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>() {

        @Override
        protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os, MediaType mt) {
            return StaxUtils.createXMLStreamWriter(os);
        }
    };
    provider.setOutTemplate(TEMPLATE_LOCATION);
    provider.setMessageContext(new MessageContextImpl(createMessage()));
    Book b = new Book();
    b.setId(123L);
    b.setName("TheBook");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(b, Book.class, Book.class, b.getClass().getAnnotations(), MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    Unmarshaller um = provider.getClassContext(Book.class).createUnmarshaller();
    Book b2 = (Book) um.unmarshal(new StringReader(bos.toString()));
    b.setName("TheBook2");
    assertEquals("Transformation is bad", b, b2);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) StringReader(java.io.StringReader) MediaType(javax.ws.rs.core.MediaType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Unmarshaller(javax.xml.bind.Unmarshaller) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl) Test(org.junit.Test)

Example 8 with MessageContextImpl

use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.

the class JAXRSUtils method processFormParam.

private static Object processFormParam(Message m, String key, Class<?> pClass, Type genericType, Annotation[] paramAnns, String defaultValue, boolean decode) {
    MessageContext mc = new MessageContextImpl(m);
    MediaType mt = mc.getHttpHeaders().getMediaType();
    @SuppressWarnings("unchecked") MultivaluedMap<String, String> params = (MultivaluedMap<String, String>) m.get(FormUtils.FORM_PARAM_MAP);
    if (params == null) {
        params = new MetadataMap<String, String>();
        m.put(FormUtils.FORM_PARAM_MAP, params);
        if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
            String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
            String body = FormUtils.readBody(m.getContent(InputStream.class), enc);
            FormUtils.populateMapFromStringOrHttpRequest(params, m, body, enc, decode);
        } else {
            if ("multipart".equalsIgnoreCase(mt.getType()) && MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(mt)) {
                MultipartBody body = AttachmentUtils.getMultipartBody(mc);
                FormUtils.populateMapFromMultipart(params, body, m, decode);
            } else {
                org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("WRONG_FORM_MEDIA_TYPE", BUNDLE, mt.toString());
                LOG.warning(errorMsg.toString());
                throw ExceptionUtils.toNotSupportedException(null, null);
            }
        }
    }
    if ("".equals(key)) {
        return InjectionUtils.handleBean(pClass, paramAnns, params, ParameterType.FORM, m, false);
    }
    List<String> results = params.get(key);
    return InjectionUtils.createParameterObject(results, pClass, genericType, paramAnns, defaultValue, false, ParameterType.FORM, m);
}
Also used : Message(org.apache.cxf.message.Message) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) InputStream(java.io.InputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) MediaType(javax.ws.rs.core.MediaType) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Example 9 with MessageContextImpl

use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.

the class OAuthRequestFilter method filter.

@Override
public void filter(ContainerRequestContext context) {
    try {
        Message m = JAXRSUtils.getCurrentMessage();
        MessageContext mc = new MessageContextImpl(m);
        OAuthInfo info = handleOAuthRequest(mc.getHttpServletRequest());
        setSecurityContext(mc, m, info);
    } catch (OAuthProblemException e) {
        context.abortWith(Response.status(401).header("WWW-Authenticate", "OAuth").build());
    } catch (Exception e) {
        context.abortWith(Response.status(401).header("WWW-Authenticate", "OAuth").build());
    }
}
Also used : OAuthProblemException(net.oauth.OAuthProblemException) Message(org.apache.cxf.message.Message) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) OAuthProblemException(net.oauth.OAuthProblemException) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Example 10 with MessageContextImpl

use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.

the class OidcRpAuthenticationFilter method checkSecurityContext.

protected boolean checkSecurityContext(ContainerRequestContext rc) {
    OidcClientTokenContext tokenContext = (OidcClientTokenContext) stateManager.getClientTokenContext(mc);
    if (tokenContext == null) {
        return false;
    }
    IdToken idToken = tokenContext.getIdToken();
    try {
        // If ID token has expired then the context is no longer valid
        JwtUtils.validateJwtExpiry(idToken, 0, idToken.getExpiryTime() != null);
    } catch (JwtException ex) {
        stateManager.removeClientTokenContext(new MessageContextImpl(JAXRSUtils.getCurrentMessage()));
        return false;
    }
    OidcClientTokenContextImpl newTokenContext = new OidcClientTokenContextImpl();
    newTokenContext.setToken(tokenContext.getToken());
    newTokenContext.setIdToken(idToken);
    newTokenContext.setUserInfo(tokenContext.getUserInfo());
    newTokenContext.setState(toRequestState(rc));
    JAXRSUtils.getCurrentMessage().setContent(ClientTokenContext.class, newTokenContext);
    OidcSecurityContext oidcSecCtx = new OidcSecurityContext(newTokenContext);
    oidcSecCtx.setRoleClaim(roleClaim);
    rc.setSecurityContext(oidcSecCtx);
    return true;
}
Also used : IdToken(org.apache.cxf.rs.security.oidc.common.IdToken) JwtException(org.apache.cxf.rs.security.jose.jwt.JwtException) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Aggregations

MessageContextImpl (org.apache.cxf.jaxrs.ext.MessageContextImpl)11 Message (org.apache.cxf.message.Message)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 MessageContext (org.apache.cxf.jaxrs.ext.MessageContext)4 Test (org.junit.Test)4 StringReader (java.io.StringReader)3 MediaType (javax.ws.rs.core.MediaType)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 Book (org.apache.cxf.jaxrs.resources.Book)3 SuperBook (org.apache.cxf.jaxrs.resources.SuperBook)3 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1