use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.
the class ApplicationUploadEndpointTest method testApplicationUploadEndpointCreateApplicationServiceException.
/**
* Tests the {@link ApplicationUploadEndpoint#create(MultipartBody, UriInfo)} method
* for the case where an ApplicationServiceException is thrown
*
* @throws Exception
*/
@Test
public void testApplicationUploadEndpointCreateApplicationServiceException() throws Exception {
ApplicationUploadEndpoint applicationUploadEndpoint = new ApplicationUploadEndpoint(testAppService);
doThrow(new ApplicationServiceException()).when(testAppService).addApplication(Mockito.any(URI.class));
applicationUploadEndpoint.setDefaultFileLocation(TEST_FILE_LOCATION);
Response response = applicationUploadEndpoint.create(testMultipartBody, testUriInfo);
Response expectedResponse = Response.serverError().build();
assertThat("Response should report server error.", response.getStatus(), is(expectedResponse.getStatus()));
}
use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.
the class ApplicationServiceImpl method traverseDependencies.
/**
* Finds a parent and children dependencies for each app. Needs to be run twice
* in order to get full dependency correlations.
*
* @param appMap Application Map containing all the application nodes.
* @param filteredApplications Set containing all the application nodes minus those in the ignored list
* @param reportDebug Boolean that allows debug statements to be output or not. Only reason
* why this exists is because this function will be called twice and only
* the second set of statements will be relevant
*/
private void traverseDependencies(Map<Application, ApplicationNodeImpl> appMap, Set<Application> filteredApplications, boolean reportDebug) {
// find dependencies in each app and add them into correct node
for (Entry<Application, ApplicationNodeImpl> curAppNode : appMap.entrySet()) {
try {
// main feature will contain dependencies
Feature mainFeature = curAppNode.getKey().getMainFeature();
if (null == mainFeature) {
if (reportDebug) {
LOGGER.debug("Application \"{}\" does not contain a main feature", curAppNode.getKey().getName());
}
continue;
}
// eliminate duplications with a set
Set<Dependency> dependencies = new HashSet<>(mainFeature.getDependencies());
// remove any features that are local to the application
dependencies.removeAll(curAppNode.getKey().getFeatures());
// loop through all of the features that are left to determine
// where they are from
Set<Application> depAppSet = new HashSet<>();
for (Dependency curDepFeature : dependencies) {
Application dependencyApp = findFeature(featuresService.getFeature(curDepFeature.getName()), filteredApplications);
if (dependencyApp != null) {
if (dependencyApp.equals(curAppNode.getKey())) {
if (reportDebug) {
LOGGER.debug("Self-dependency");
}
continue;
} else {
if (reportDebug) {
LOGGER.debug("Application {} depends on the feature {} which is located in application {}.", curAppNode.getKey().getName(), curDepFeature.getName(), dependencyApp.getName());
}
depAppSet.add(dependencyApp);
}
}
}
if (!depAppSet.isEmpty()) {
Application parentApp;
if (depAppSet.size() > 1) {
parentApp = findCommonParent(depAppSet, appMap);
if (parentApp == null) {
if (reportDebug) {
LOGGER.debug("Found more than 1 application dependency for application {}. Could not determine which one is the correct parent. Application will be sent back as root application.", curAppNode.getKey().getName());
}
continue;
}
} else {
parentApp = depAppSet.iterator().next();
}
// update the dependency app with a new child
ApplicationNode parentAppNode = appMap.get(parentApp);
parentAppNode.getChildren().add(curAppNode.getValue());
curAppNode.getValue().setParent(parentAppNode);
} else {
if (reportDebug) {
LOGGER.debug("No dependency applications found for {}. This will be sent back as a root application.", curAppNode.getKey().getName());
}
}
// ApplicationServiceException from DDF and Exception from Karaf
// (FeaturesService)
} catch (Exception e) {
if (reportDebug) {
LOGGER.warn("Encountered error while determining dependencies for \"{}\". This may cause an incomplete application hierarchy to be created.", curAppNode.getKey().getName(), e);
}
}
}
}
use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.
the class ApplicationServiceImpl method addApplication.
@Override
public void addApplication(URI applicationURL) throws ApplicationServiceException {
try {
if (applicationURL.toString().startsWith("file:")) {
applicationURL = ApplicationFileInstaller.install(new File(applicationURL));
LOGGER.info("Installing newly added feature repo: {}", applicationURL);
}
featuresService.addRepository(applicationURL, false);
} catch (Exception e) {
LOGGER.warn("Could not add new application due to error.", e);
throw new ApplicationServiceException(e);
}
}
use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.
the class ApplicationUploadEndpoint method create.
@POST
@Path("/")
public Response create(MultipartBody multipartBody, @Context UriInfo requestUriInfo) {
LOGGER.trace("ENTERING: create");
Response response;
List<Attachment> attachmentList = multipartBody.getAllAttachments();
File newFile = null;
for (Attachment attachment : attachmentList) {
newFile = createFileFromAttachement(attachment);
}
try {
if (newFile != null) {
appService.addApplication(newFile.toURI());
}
Response.ResponseBuilder responseBuilder = Response.ok();
response = responseBuilder.build();
} catch (ApplicationServiceException e) {
LOGGER.warn("Unable to add the application to the server: {}", newFile, e);
Response.ResponseBuilder responseBuilder = Response.serverError();
response = responseBuilder.build();
}
LOGGER.trace("EXITING: create");
return response;
}
use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.
the class ApplicationUploadEndpoint method update.
@POST
@Path("/update")
@Produces("application/json")
public Response update(MultipartBody multipartBody, @Context UriInfo requestUriInfo) {
LOGGER.trace("ENTERING: update");
Response response;
List<Attachment> attachmentList = multipartBody.getAllAttachments();
File newFile = null;
for (Attachment attachment : attachmentList) {
newFile = createFileFromAttachement(attachment);
}
try {
if (newFile != null) {
ZipFileApplicationDetails appDetails = ApplicationFileInstaller.getAppDetails(newFile);
if (appDetails != null) {
// lets get the existing app if it exists.
Application existingApplication = appService.getApplication(appDetails.getName());
// assume false until proved
boolean wasExistingAppStarted = false;
// otherwise.
if (existingApplication != null) {
wasExistingAppStarted = appService.isApplicationStarted(existingApplication);
appService.removeApplication(existingApplication);
}
appService.addApplication(newFile.toURI());
// if application was started before it was removed, lets try and start it.
if (wasExistingAppStarted) {
appService.startApplication(appDetails.getName());
}
} else {
throw new ApplicationServiceException("No Application details could be extracted from the provided file.");
}
} else {
throw new ApplicationServiceException("No file attachment provided.");
}
// we need to output valid JSON to the client so fileupload can correctly call
// done/fail callbacks correctly.
Response.ResponseBuilder responseBuilder = Response.ok("{\"status\":\"success\"}").type("application/json");
response = responseBuilder.build();
} catch (ApplicationServiceException e) {
LOGGER.warn("Unable to update an application on the server: {}", newFile, e);
Response.ResponseBuilder responseBuilder = Response.serverError();
response = responseBuilder.build();
}
LOGGER.trace("EXITING: update");
return response;
}
Aggregations