use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project cxf by apache.
the class JAXRSUtilsTest method testFindTargetResourceClass.
@Test
public void testFindTargetResourceClass() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
String contentTypes = "*/*";
// If acceptContentTypes does not specify a specific Mime type, the
// method is declared with a most specific ProduceMime type is selected.
OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books/123/", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/json,application/xml;q=0.9"));
assertNotNull(ori);
assertEquals("getBookJSON", ori.getMethodToInvoke().getName());
// test
ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/json"));
assertNotNull(ori);
assertEquals("getBookJSON", ori.getMethodToInvoke().getName());
// test
ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertNotNull(ori);
assertEquals("getBook", ori.getMethodToInvoke().getName());
// test
ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertNotNull(ori);
assertEquals("getBooks", ori.getMethodToInvoke().getName());
// test find POST
ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books", "POST", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertNotNull(ori);
assertEquals("addBook", ori.getMethodToInvoke().getName());
// test find PUT
ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books", "PUT", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertEquals("updateBook", ori.getMethodToInvoke().getName());
// test find DELETE
ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books/123", "DELETE", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertNotNull(ori);
assertEquals("deleteBook", ori.getMethodToInvoke().getName());
}
use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project cxf by apache.
the class JAXRSUtilsTest method testFindTargetResourceClassWithTemplates.
@Test
public void testFindTargetResourceClassWithTemplates() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreTemplates.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
String contentTypes = "*/*";
// If acceptContentTypes does not specify a specific Mime type, the
// method is declared with a most specific ProduceMime type is selected.
MetadataMap<String, String> values = new MetadataMap<>();
OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/1/2/", "GET", values, contentTypes, getTypes("*/*"));
assertNotNull(ori);
assertEquals("getBooks", ori.getMethodToInvoke().getName());
assertEquals("Only id and final match groups should be there", 2, values.size());
assertEquals("2 {id} values should've been picked up", 2, values.get("id").size());
assertEquals("FINAL_MATCH_GROUP should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
assertEquals("First {id} is 1", "1", values.getFirst("id"));
assertEquals("Second id is 2", "2", values.get("id").get(1));
values = new MetadataMap<>();
ori = findTargetResourceClass(resources, createMessage(), "/2", "POST", values, contentTypes, getTypes("*/*"));
assertNotNull(ori);
assertEquals("updateBookStoreInfo", ori.getMethodToInvoke().getName());
assertEquals("Only id and final match groups should be there", 2, values.size());
assertEquals("Only single {id} should've been picked up", 1, values.get("id").size());
assertEquals("FINAL_MATCH_GROUP should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
assertEquals("Only the first {id} should've been picked up", "2", values.getFirst("id"));
values = new MetadataMap<>();
ori = findTargetResourceClass(resources, createMessage(), "/3/4", "PUT", values, contentTypes, getTypes("*/*"));
assertNotNull(ori);
assertEquals("updateBook", ori.getMethodToInvoke().getName());
assertEquals("Only the first {id} should've been picked up", 3, values.size());
assertEquals("Only the first {id} should've been picked up", 1, values.get("id").size());
assertEquals("Only the first {id} should've been picked up", 1, values.get("bookId").size());
assertEquals("Only the first {id} should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
assertEquals("Only the first {id} should've been picked up", "3", values.getFirst("id"));
assertEquals("Only the first {id} should've been picked up", "4", values.getFirst("bookId"));
}
use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project cxf by apache.
the class JAXRSUtilsTest method testSelectBetweenMultipleResourceClasses.
@Test
public void testSelectBetweenMultipleResourceClasses() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class, org.apache.cxf.jaxrs.resources.BookStore.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
assertEquals(org.apache.cxf.jaxrs.resources.BookStore.class, firstResourceClass(resources, "/bookstore"));
assertEquals(org.apache.cxf.jaxrs.resources.BookStore.class, firstResourceClass(resources, "/bookstore/"));
assertEquals(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class, firstResourceClass(resources, "/bookstore/bar"));
}
use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project cxf by apache.
the class JAXRSUtilsTest method testSelectBetweenMultipleResourceClasses2.
@Test
public void testSelectBetweenMultipleResourceClasses2() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class, org.apache.cxf.jaxrs.resources.TestResourceTemplate2.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class, firstResourceClass(resources, "/1"));
assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class, firstResourceClass(resources, "/1/"));
assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate2.class, firstResourceClass(resources, "/1/foo"));
assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate2.class, firstResourceClass(resources, "/1/foo/bar"));
}
use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project cxf by apache.
the class JAXRSClientFactoryBean method createWebClient.
/**
* Creates a WebClient instance
* @return WebClient instance
*/
public WebClient createWebClient() {
String serviceAddress = getAddress();
int queryIndex = serviceAddress != null ? serviceAddress.lastIndexOf('?') : -1;
if (queryIndex != -1) {
serviceAddress = serviceAddress.substring(0, queryIndex);
}
Service service = new JAXRSServiceImpl(serviceAddress, getServiceName());
getServiceFactory().setService(service);
try {
Endpoint ep = createEndpoint();
this.getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_CLIENT_CREATE, ep);
ClientState actualState = getActualState();
WebClient client = actualState == null ? new WebClient(getAddress(), getProperties()) : new WebClient(actualState);
initClient(client, ep, actualState == null);
notifyLifecycleManager(client);
this.getServiceFactory().sendEvent(FactoryBeanListener.Event.CLIENT_CREATED, client, ep);
return client;
} catch (Exception ex) {
LOG.severe(ex.getClass().getName() + " : " + ex.getLocalizedMessage());
throw new RuntimeException(ex);
}
}
Aggregations