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();
}
}
}
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);
}
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);
}
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));
}
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());
}
Aggregations