use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.
the class ProviderFactoryTest method testCreateMessageBodyReaderInterceptorWithReaderInterceptor.
@Test
public void testCreateMessageBodyReaderInterceptorWithReaderInterceptor() throws Exception {
ReaderInterceptor ri = readerInterceptorContext -> readerInterceptorContext.proceed();
ProviderInfo<ReaderInterceptor> pi = new ProviderInfo<>(ri, null, true);
ServerProviderFactory spf = ServerProviderFactory.getInstance();
spf.readerInterceptors.put(new ProviderFactory.NameKey("org.apache.cxf.filter.binding", 1, ri.getClass()), pi);
final Message message = prepareMessage(MediaType.APPLICATION_XML, MediaType.APPLICATION_XML);
List<ReaderInterceptor> interceptors = spf.createMessageBodyReaderInterceptor(Book.class, Book.class, new Annotation[0], MediaType.APPLICATION_XML_TYPE, message, true, null);
assertSame(2, interceptors.size());
}
use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.
the class ProviderFactory method createMessageBodyReaderInterceptor.
public <T> List<ReaderInterceptor> createMessageBodyReaderInterceptor(Class<T> bodyType, Type parameterType, Annotation[] parameterAnnotations, MediaType mediaType, Message m, boolean checkMbrNow, Set<String> names) {
MessageBodyReader<T> mr = !checkMbrNow ? null : createMessageBodyReader(bodyType, parameterType, parameterAnnotations, mediaType, m);
int size = readerInterceptors.size();
if (mr != null || size > 0) {
ReaderInterceptor mbrReader = new ReaderInterceptorMBR(mr, getResponseMessage(m));
final List<ReaderInterceptor> interceptors;
if (size > 0) {
interceptors = new ArrayList<>(size + 1);
List<ProviderInfo<ReaderInterceptor>> readers = getBoundFilters(readerInterceptors, names);
for (ProviderInfo<ReaderInterceptor> p : readers) {
injectContextValues(p, m);
interceptors.add(p.getProvider());
}
interceptors.add(mbrReader);
} else {
interceptors = Collections.singletonList(mbrReader);
}
return interceptors;
}
return null;
}
use of org.apache.cxf.jaxrs.model.ProviderInfo 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)) {
final OperationResourceInfo ori = contextMessage.getExchange().get(OperationResourceInfo.class);
if (ori != null) {
o = new ResourceContextImpl(contextMessage, ori);
}
} 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.model.ProviderInfo in project cxf by apache.
the class JAXRSUtils method runContainerRequestFilters.
public static boolean runContainerRequestFilters(ServerProviderFactory pf, Message m, boolean preMatch, Set<String> names) throws IOException {
List<ProviderInfo<ContainerRequestFilter>> containerFilters = preMatch ? pf.getPreMatchContainerRequestFilters() : pf.getPostMatchContainerRequestFilters(names);
if (!containerFilters.isEmpty()) {
ContainerRequestContext context = new ContainerRequestContextImpl(m, preMatch, false);
for (ProviderInfo<ContainerRequestFilter> filter : containerFilters) {
InjectionUtils.injectContexts(filter.getProvider(), filter, m);
filter.getProvider().filter(context);
Response response = m.getExchange().get(Response.class);
if (response != null) {
setMessageContentType(m, response);
return true;
}
}
}
return false;
}
use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.
the class ClientRequestFilterInterceptor method handleMessage.
public void handleMessage(Message outMessage) throws Fault {
ClientProviderFactory pf = ClientProviderFactory.getInstance(outMessage);
if (pf == null) {
return;
}
// create an empty proxy output stream that the filter can interact with
// and save a reference for later
ProxyOutputStream pos = new ProxyOutputStream();
outMessage.setContent(OutputStream.class, pos);
outMessage.setContent(ProxyOutputStream.class, pos);
List<ProviderInfo<ClientRequestFilter>> filters = pf.getClientRequestFilters();
if (!filters.isEmpty()) {
final Exchange exchange = outMessage.getExchange();
final ClientRequestContext context = new ClientRequestContextImpl(outMessage, false);
for (ProviderInfo<ClientRequestFilter> filter : filters) {
InjectionUtils.injectContexts(filter.getProvider(), filter, outMessage);
try {
filter.getProvider().filter(context);
@SuppressWarnings("unchecked") Map<String, List<Object>> headers = CastUtils.cast((Map<String, List<Object>>) outMessage.get(Message.PROTOCOL_HEADERS));
HttpUtils.convertHeaderValuesToString(headers, false);
Response response = outMessage.getExchange().get(Response.class);
if (response != null) {
outMessage.getInterceptorChain().abort();
Message inMessage = new MessageImpl();
inMessage.setExchange(exchange);
inMessage.put(Message.RESPONSE_CODE, response.getStatus());
inMessage.put(Message.PROTOCOL_HEADERS, response.getMetadata());
exchange.setInMessage(inMessage);
MessageObserver observer = exchange.get(MessageObserver.class);
observer.onMessage(inMessage);
return;
}
} catch (IOException ex) {
throw new ProcessingException(ex);
}
}
}
}
Aggregations