use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class ValidationUtils method getResourceInstance.
public static Object getResourceInstance(Message message) {
final OperationResourceInfo ori = message.getExchange().get(OperationResourceInfo.class);
if (ori == null) {
return null;
}
if (!ori.getClassResourceInfo().isRoot()) {
return message.getExchange().get("org.apache.cxf.service.object.last");
}
final ResourceProvider resourceProvider = ori.getClassResourceInfo().getResourceProvider();
if (!resourceProvider.isSingleton()) {
String error = "Service object is not a singleton, use a custom invoker to validate";
LOG.warning(error);
return null;
}
return resourceProvider.getInstance(message);
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class JAXRSServiceFactoryBeanTest method verifyOp.
private boolean verifyOp(Set<OperationResourceInfo> ops, String opName, String httpMethod, boolean isSubresource) {
for (OperationResourceInfo ori : ops) {
if (opName.equals(ori.getMethodToInvoke().getName())) {
assertEquals(httpMethod, ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertEquals(isSubresource, ori.isSubResourceLocator());
return true;
}
}
return false;
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class JAXRSServiceFactoryBeanTest method testNoSubResources.
@Test
public void testNoSubResources() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setEnableStaticResolution(true);
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
assertEquals(1, resources.size());
// Verify root ClassResourceInfo: BookStoreNoSubResource
ClassResourceInfo rootCri = resources.get(0);
assertNotNull(rootCri.getURITemplate());
URITemplate template = rootCri.getURITemplate();
MultivaluedMap<String, String> values = new MetadataMap<>();
assertTrue(template.match("/bookstore/books/123", values));
assertFalse(rootCri.hasSubResources());
MethodDispatcher md = rootCri.getMethodDispatcher();
assertEquals(7, md.getOperationResourceInfos().size());
Set<OperationResourceInfo> ops = md.getOperationResourceInfos();
assertTrue("No operation found", verifyOp(ops, "getBook", "GET", false));
assertTrue("No operation found", verifyOp(ops, "getBookStoreInfo", "GET", false));
assertTrue("No operation found", verifyOp(ops, "getBooks", "GET", false));
assertTrue("No operation found", verifyOp(ops, "getBookJSON", "GET", false));
assertTrue("No operation found", verifyOp(ops, "addBook", "POST", false));
assertTrue("No operation found", verifyOp(ops, "updateBook", "PUT", false));
assertTrue("No operation found", verifyOp(ops, "deleteBook", "DELETE", false));
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class CustomJAXRSInvoker method invoke.
@Override
public Object invoke(Exchange exchange, Object requestParams, Object resourceObject) {
OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
Method m = ori.getMethodToInvoke();
Class<?> realClass = ClassHelper.getRealClass(exchange.getBus(), resourceObject);
Principal p = new SecurityContextImpl(exchange.getInMessage()).getUserPrincipal();
if (realClass == SecureBookStore.class && "getThatBook".equals(m.getName()) && "baddy".equals(p.getName())) {
return new MessageContentsList(Response.status(Response.Status.FORBIDDEN).build());
}
return super.invoke(exchange, requestParams, resourceObject);
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class SseInterceptor method handleMessage.
public void handleMessage(Message message) {
// Not an SSE invocation, skipping it in favor of normal processing
if (message.get(SseEventSink.class) == null) {
return;
}
if (!isRequestor(message) && message.get(SseInterceptor.class) == null) {
message.put(SseInterceptor.class, this);
final Exchange exchange = message.getExchange();
OperationResourceInfo ori = (OperationResourceInfo) exchange.get(OperationResourceInfo.class.getName());
if (ori != null) {
Response.ResponseBuilder builder = Response.ok();
HttpServletResponse servletResponse = null;
final ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(message);
final Object response = message.get(AbstractHTTPDestination.HTTP_RESPONSE);
if (response instanceof HttpServletResponse) {
servletResponse = (HttpServletResponse) response;
builder = Response.status(servletResponse.getStatus());
for (final String header : servletResponse.getHeaderNames()) {
final Collection<String> headers = servletResponse.getHeaders(header);
addHeader(builder, header, headers);
}
}
// Run the filters
try {
final ResponseImpl responseImpl = (ResponseImpl) builder.build();
final Message outMessage = getOutMessage(message);
JAXRSUtils.runContainerResponseFilters(providerFactory, responseImpl, outMessage, ori, ori.getAnnotatedMethod());
if (servletResponse != null) {
servletResponse.setStatus(responseImpl.getStatus());
final Map<String, List<String>> userHeaders = CastUtils.cast((Map<?, ?>) outMessage.get(Message.PROTOCOL_HEADERS));
if (userHeaders != null) {
for (Map.Entry<String, List<String>> entry : userHeaders.entrySet()) {
setHeader(servletResponse, entry);
}
}
final MultivaluedMap<String, String> headers = responseImpl.getStringHeaders();
if (headers != null) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
setHeader(servletResponse, entry);
}
}
}
} catch (Throwable ex) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, ex.getMessage(), ex);
}
}
}
}
}
Aggregations