Search in sources :

Example 1 with MessageContextImpl

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

the class OAuthInvoker method performInvocation.

@Override
protected Object performInvocation(Exchange exchange, final Object serviceObject, Method m, Object[] paramArray) throws Exception {
    Message inMessage = exchange.getInMessage();
    ClientTokenContext tokenContext = inMessage.getContent(ClientTokenContext.class);
    try {
        if (tokenContext != null) {
            StaticClientTokenContext.setClientTokenContext(tokenContext);
        }
        return super.performInvocation(exchange, serviceObject, m, paramArray);
    } catch (InvocationTargetException ex) {
        if (tokenContext != null && ex.getCause() instanceof NotAuthorizedException && !inMessage.containsKey(OAUTH2_CALL_RETRIED)) {
            ClientAccessToken accessToken = tokenContext.getToken();
            String refreshToken = accessToken.getRefreshToken();
            if (refreshToken != null) {
                accessToken = OAuthClientUtils.refreshAccessToken(accessTokenServiceClient, consumer, accessToken);
                validateRefreshedToken(tokenContext, accessToken);
                MessageContext mc = new MessageContextImpl(inMessage);
                ((ClientTokenContextImpl) tokenContext).setToken(accessToken);
                clientTokenContextManager.setClientTokenContext(mc, tokenContext);
                // retry
                inMessage.put(OAUTH2_CALL_RETRIED, true);
                return super.performInvocation(exchange, serviceObject, m, paramArray);
            }
        }
        throw ex;
    } finally {
        if (tokenContext != null) {
            StaticClientTokenContext.removeClientTokenContext();
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) InvocationTargetException(java.lang.reflect.InvocationTargetException) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Example 2 with MessageContextImpl

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

the class JAXRSUtils method createContextValue.

public static <T> T createContextValue(Message m, Type genericType, Class<T> clazz) {
    Message contextMessage = getContextMessage(m);
    Object o = null;
    if (UriInfo.class.isAssignableFrom(clazz)) {
        o = createUriInfo(contextMessage);
    } else if (HttpHeaders.class.isAssignableFrom(clazz) || ProtocolHeaders.class.isAssignableFrom(clazz)) {
        o = createHttpHeaders(contextMessage, clazz);
    } else if (SecurityContext.class.isAssignableFrom(clazz)) {
        SecurityContext customContext = contextMessage.get(SecurityContext.class);
        o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
    } else if (MessageContext.class.isAssignableFrom(clazz)) {
        o = new MessageContextImpl(m);
    } else if (ResourceInfo.class.isAssignableFrom(clazz)) {
        o = new ResourceInfoImpl(contextMessage);
    } else if (ResourceContext.class.isAssignableFrom(clazz)) {
        o = new ResourceContextImpl(contextMessage, contextMessage.getExchange().get(OperationResourceInfo.class));
    } else if (Request.class.isAssignableFrom(clazz)) {
        o = new RequestImpl(contextMessage);
    } else if (Providers.class.isAssignableFrom(clazz)) {
        o = new ProvidersImpl(contextMessage);
    } else if (ContextResolver.class.isAssignableFrom(clazz)) {
        o = createContextResolver(genericType, contextMessage);
    } else if (Configuration.class.isAssignableFrom(clazz)) {
        o = ProviderFactory.getInstance(contextMessage).getConfiguration(contextMessage);
    } else if (Application.class.isAssignableFrom(clazz)) {
        ProviderInfo<?> providerInfo = (ProviderInfo<?>) contextMessage.getExchange().getEndpoint().get(Application.class.getName());
        o = providerInfo == null ? null : providerInfo.getProvider();
    } else if (contextMessage != null) {
        ContextProvider<?> provider = ProviderFactory.getInstance(contextMessage).createContextProvider(clazz, contextMessage);
        if (provider != null) {
            o = provider.createContext(contextMessage);
        }
    }
    if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage)) {
        o = HttpUtils.createServletResourceValue(contextMessage, clazz);
    }
    return clazz.cast(o);
}
Also used : SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) ResourceContext(javax.ws.rs.container.ResourceContext) Message(org.apache.cxf.message.Message) Configuration(javax.ws.rs.core.Configuration) ResourceInfoImpl(org.apache.cxf.jaxrs.impl.ResourceInfoImpl) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) SecurityContext(javax.ws.rs.core.SecurityContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) RequestImpl(org.apache.cxf.jaxrs.impl.RequestImpl) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Example 3 with MessageContextImpl

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

the class XSLTJaxbProviderTest method testWriteWithAnnotation.

@Test
public void testWriteWithAnnotation() throws Exception {
    XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>();
    provider.setMessageContext(new MessageContextImpl(createMessage()));
    Book b = new Book();
    b.setId(123L);
    b.setName("TheBook");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Annotation[] anns = Root.class.getMethod("getBook").getAnnotations();
    assertTrue(provider.isWriteable(Book.class, Book.class, anns, MediaType.TEXT_XML_TYPE));
    provider.writeTo(b, Book.class, Book.class, anns, 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 : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl) Test(org.junit.Test)

Example 4 with MessageContextImpl

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

the class SpringViewResolverProviderTest method setUp.

@Before
public void setUp() {
    this.viewResolver = new SpringViewResolverProvider(viewResolverMock, localeResolverMock);
    ExchangeImpl exchange = new ExchangeImpl();
    Endpoint endpoint = new MockEndpoint();
    endpoint.put(ServerProviderFactory.class.getName(), ServerProviderFactory.getInstance());
    exchange.put(Endpoint.class, endpoint);
    exchange.put(ServerProviderFactory.class.getName(), ServerProviderFactory.getInstance());
    MessageImpl message = new MessageImpl();
    message.setExchange(exchange);
    message.put(AbstractHTTPDestination.HTTP_REQUEST, requestMock);
    message.put(AbstractHTTPDestination.HTTP_RESPONSE, responseMock);
    message.put(AbstractHTTPDestination.HTTP_CONTEXT, servletContextMock);
    viewResolver.setMessageContext(new MessageContextImpl(message));
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Endpoint(org.apache.cxf.endpoint.Endpoint) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl) Before(org.junit.Before)

Example 5 with MessageContextImpl

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

the class JAXBElementProviderTest method testOutAttributesAsElementsForList.

@Test
public void testOutAttributesAsElementsForList() throws Exception {
    // Provider
    JAXBElementProvider<List<?>> provider = new JAXBElementProvider<List<?>>();
    provider.setMessageContext(new MessageContextImpl(createMessage()));
    provider.setCollectionWrapperName("tagholders");
    Map<String, String> map = new HashMap<>();
    map.put("{http://tags}*", "*");
    provider.setOutTransformElements(map);
    provider.setAttributesToElements(true);
    // data setup
    TagVO2 tag = new TagVO2("A", "B");
    TagVO2Holder holder = new TagVO2Holder();
    holder.setTag(tag);
    List<TagVO2Holder> list = new ArrayList<JAXBElementProviderTest.TagVO2Holder>();
    list.add(holder);
    // ParameterizedType required for Lists of Objects
    ParameterizedType type = new ParameterizedType() {

        public Type getRawType() {
            return List.class;
        }

        public Type getOwnerType() {
            return null;
        }

        public Type[] getActualTypeArguments() {
            return new Type[] { TagVO2Holder.class };
        }
    };
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(list, ArrayList.class, type, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<tagholders><tagholder><attr>attribute</attr>" + "<thetag><group>B</group><name>A</name></thetag></tagholder></tagholders>";
    assertEquals(expected, bos.toString());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ParamType(org.apache.cxf.jaxrs.fortest.jaxb.jaxbelement.ParamType) ParameterizedCollectionType(org.apache.cxf.jaxrs.utils.ParameterizedCollectionType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) List(java.util.List) ArrayList(java.util.ArrayList) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl) Test(org.junit.Test)

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