Search in sources :

Example 1 with ApplicationServiceException

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()));
}
Also used : Response(javax.ws.rs.core.Response) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) URI(java.net.URI) Test(org.junit.Test)

Example 2 with ApplicationServiceException

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);
            }
        }
    }
}
Also used : ApplicationNode(org.codice.ddf.admin.application.service.ApplicationNode) Dependency(org.apache.karaf.features.Dependency) Application(org.codice.ddf.admin.application.service.Application) Feature(org.apache.karaf.features.Feature) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(ddf.security.service.SecurityServiceException) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) HashSet(java.util.HashSet)

Example 3 with ApplicationServiceException

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);
    }
}
Also used : ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) File(java.io.File) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(ddf.security.service.SecurityServiceException) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException)

Example 4 with ApplicationServiceException

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;
}
Also used : Response(javax.ws.rs.core.Response) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) File(java.io.File) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 5 with ApplicationServiceException

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;
}
Also used : Response(javax.ws.rs.core.Response) ZipFileApplicationDetails(org.codice.ddf.admin.application.service.impl.ZipFileApplicationDetails) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) File(java.io.File) Application(org.codice.ddf.admin.application.service.Application) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)24 Test (org.junit.Test)13 Application (org.codice.ddf.admin.application.service.Application)7 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)7 URI (java.net.URI)6 Logger (org.slf4j.Logger)6 Appender (ch.qos.logback.core.Appender)5 SecurityServiceException (ddf.security.service.SecurityServiceException)5 File (java.io.File)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)5 ArgumentMatcher (org.mockito.ArgumentMatcher)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Response (javax.ws.rs.core.Response)3 Feature (org.apache.karaf.features.Feature)3 FeaturesService (org.apache.karaf.features.FeaturesService)3 ApplicationStatus (org.codice.ddf.admin.application.service.ApplicationStatus)3 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)3 SkipUnstableTest (org.codice.ddf.itests.common.annotations.SkipUnstableTest)3