// lookup on the OBR RepositoryAdmin service
ServiceReference repositoryAdminReference = bundleContext.getServiceReference(RepositoryAdmin.class.getName());
if (repositoryAdminReference == null) {
throw new ServletException("OBR repository admin service is not available");
}
RepositoryAdmin repositoryAdmin = (RepositoryAdmin) bundleContext.getService(repositoryAdminReference);
if (repositoryAdmin == null) {
bundleContext.ungetService(repositoryAdminReference);
throw new ServletException("OBR repository admin service is not available");
}
String uri = request.getPathInfo();
// remove the starting /
uri = uri.substring(1);
// listing the repositories
if (request.getParameter("repositories") != null) {
ServiceReference caveRepositoryServiceReference = bundleContext.getServiceReference(CaveRepositoryService.class.getName());
if (caveRepositoryServiceReference != null) {
CaveRepositoryService caveRepositoryService = (CaveRepositoryService) bundleContext.getService(caveRepositoryServiceReference);
if (caveRepositoryService != null) {
CaveRepository[] caveRepositories = caveRepositoryService.getRepositories();
response.setContentType("text/plain");
PrintWriter writer = response.getWriter();
for (CaveRepository caveRepository : caveRepositories) {
writer.println(caveRepository.getName());
}
writer.flush();
writer.close();
}
bundleContext.ungetService(caveRepositoryServiceReference);
}
return;
}
// wrapping content (repository.xml or directly artifacts)
try {
URL url = null;
if (uri.endsWith("-repository.xml")) {
// the user wants to get the Cave repository repository.xml
// the expected format is {cave-repo-name}-repository.xml
int index = uri.indexOf("-repository.xml");
String caveRepositoryName = uri.substring(0, index);
ServiceReference caveRepositoryServiceReference = bundleContext.getServiceReference(CaveRepositoryService.class.getName());
if (caveRepositoryServiceReference != null) {
CaveRepositoryService caveRepositoryService = (CaveRepositoryService) bundleContext.getService(caveRepositoryServiceReference);
if (caveRepositoryService != null) {
CaveRepository caveRepository = caveRepositoryService.getRepository(caveRepositoryName);
if (caveRepository != null) {
url = caveRepository.getRepositoryXml();
response.setContentType("text/xml");
}
}
bundleContext.ungetService(caveRepositoryServiceReference);
}
} else {
Resource[] resources = repositoryAdmin.discoverResources("(uri=*" + uri + ")");
if (resources.length == 0) {
throw new ServletException("No resource found with URI " + uri);
}
if (resources.length > 1) {
throw new ServletException("Multiple resources found with URI " + uri);