use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.
the class JAXRSInInterceptor method processRequest.
private void processRequest(Message message, Exchange exchange) throws IOException {
ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(message);
RequestPreprocessor rp = providerFactory.getRequestPreprocessor();
if (rp != null) {
rp.preprocess(message, new UriInfoImpl(message, null));
}
// Global pre-match request filters
if (JAXRSUtils.runContainerRequestFilters(providerFactory, message, true, null)) {
return;
}
// HTTP method
String httpMethod = HttpUtils.getProtocolHeader(message, Message.HTTP_REQUEST_METHOD, HttpMethod.POST, true);
// Path to match
String rawPath = HttpUtils.getPathToMatch(message, true);
Map<String, List<String>> protocolHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
// Content-Type
String requestContentType = null;
List<String> ctHeaderValues = protocolHeaders.get(Message.CONTENT_TYPE);
if (ctHeaderValues != null && !ctHeaderValues.isEmpty()) {
requestContentType = ctHeaderValues.get(0);
message.put(Message.CONTENT_TYPE, requestContentType);
}
if (requestContentType == null) {
requestContentType = (String) message.get(Message.CONTENT_TYPE);
if (requestContentType == null) {
requestContentType = MediaType.WILDCARD;
}
}
// Accept
String acceptTypes = null;
List<String> acceptHeaderValues = protocolHeaders.get(Message.ACCEPT_CONTENT_TYPE);
if (acceptHeaderValues != null && !acceptHeaderValues.isEmpty()) {
acceptTypes = acceptHeaderValues.get(0);
message.put(Message.ACCEPT_CONTENT_TYPE, acceptTypes);
}
if (acceptTypes == null) {
acceptTypes = HttpUtils.getProtocolHeader(message, Message.ACCEPT_CONTENT_TYPE, null);
if (acceptTypes == null) {
acceptTypes = "*/*";
message.put(Message.ACCEPT_CONTENT_TYPE, acceptTypes);
}
}
final List<MediaType> acceptContentTypes;
try {
acceptContentTypes = JAXRSUtils.sortMediaTypes(acceptTypes, JAXRSUtils.MEDIA_TYPE_Q_PARAM);
} catch (IllegalArgumentException ex) {
throw ExceptionUtils.toNotAcceptableException(null, null);
}
exchange.put(Message.ACCEPT_CONTENT_TYPE, acceptContentTypes);
// 1. Matching target resource class
List<ClassResourceInfo> resources = JAXRSUtils.getRootResources(message);
Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources = JAXRSUtils.selectResourceClass(resources, rawPath, message);
if (matchedResources == null) {
org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("NO_ROOT_EXC", BUNDLE, message.get(Message.REQUEST_URI), rawPath);
Level logLevel = JAXRSUtils.getExceptionLogLevel(message, NotFoundException.class);
LOG.log(logLevel == null ? Level.FINE : logLevel, errorMsg.toString());
Response resp = JAXRSUtils.createResponse(resources, message, errorMsg.toString(), Response.Status.NOT_FOUND.getStatusCode(), false);
throw ExceptionUtils.toNotFoundException(null, resp);
}
MultivaluedMap<String, String> matchedValues = new MetadataMap<>();
final OperationResourceInfo ori;
try {
ori = JAXRSUtils.findTargetMethod(matchedResources, message, httpMethod, matchedValues, requestContentType, acceptContentTypes, true, true);
setExchangeProperties(message, exchange, ori, matchedValues, resources.size());
} catch (WebApplicationException ex) {
if (JAXRSUtils.noResourceMethodForOptions(ex.getResponse(), httpMethod)) {
Response response = JAXRSUtils.createResponse(resources, null, null, 200, true);
exchange.put(Response.class, response);
return;
}
throw ex;
}
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Request path is: " + rawPath);
LOG.fine("Request HTTP method is: " + httpMethod);
LOG.fine("Request contentType is: " + requestContentType);
LOG.fine("Accept contentType is: " + acceptTypes);
LOG.fine("Found operation: " + ori.getMethodToInvoke().getName());
}
// Global and name-bound post-match request filters
if (!ori.isSubResourceLocator() && JAXRSUtils.runContainerRequestFilters(providerFactory, message, false, ori.getNameBindings())) {
return;
}
// Process parameters
List<Object> params = JAXRSUtils.processParameters(ori, matchedValues, message);
message.setContent(List.class, params);
}
use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.
the class ServerProviderFactoryTest method testCustomTestAndWadlHandler.
@Test
public void testCustomTestAndWadlHandler() {
ServerProviderFactory pf = ServerProviderFactory.getInstance();
assertEquals(1, pf.getPreMatchContainerRequestFilters().size());
assertTrue(pf.getPreMatchContainerRequestFilters().get(0).getProvider() instanceof WadlGenerator);
List<Object> providers = new ArrayList<>();
WadlGenerator wg = new WadlGenerator();
providers.add(wg);
TestHandler th = new TestHandler();
providers.add(th);
pf.setUserProviders(providers);
assertEquals(2, pf.getPreMatchContainerRequestFilters().size());
assertSame(wg, pf.getPreMatchContainerRequestFilters().get(0).getProvider());
assertSame(th, pf.getPreMatchContainerRequestFilters().get(1).getProvider());
}
use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.
the class ServerProviderFactoryTest method testCustomTestHandler.
@Test
public void testCustomTestHandler() {
ServerProviderFactory pf = ServerProviderFactory.getInstance();
assertEquals(1, pf.getPreMatchContainerRequestFilters().size());
assertTrue(pf.getPreMatchContainerRequestFilters().get(0).getProvider() instanceof WadlGenerator);
TestHandler th = new TestHandler();
pf.setUserProviders(Collections.singletonList(th));
assertEquals(2, pf.getPreMatchContainerRequestFilters().size());
assertTrue(pf.getPreMatchContainerRequestFilters().get(0).getProvider() instanceof WadlGenerator);
assertSame(th, pf.getPreMatchContainerRequestFilters().get(1).getProvider());
}
Aggregations