Search in sources :

Example 1 with ModelManager

use of com.openmeap.model.ModelManager in project OpenMEAP by OpenMEAP.

the class WebViewServlet method service.

@SuppressWarnings("unchecked")
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    logger.trace("in service");
    ModelManager mgr = getModelManager();
    GlobalSettings settings = mgr.getGlobalSettings();
    String validTempPath = settings.validateTemporaryStoragePath();
    if (validTempPath != null) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, validTempPath);
    }
    String pathInfo = request.getPathInfo();
    String[] pathParts = pathInfo.split("[/]");
    if (pathParts.length < 4) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
    String remove = pathParts[1] + "/" + pathParts[2] + "/" + pathParts[3];
    String fileRelative = pathInfo.replace(remove, "");
    String applicationNameString = URLDecoder.decode(pathParts[APP_NAME_INDEX], FormConstants.CHAR_ENC_DEFAULT);
    String archiveHash = URLDecoder.decode(pathParts[APP_VER_INDEX], FormConstants.CHAR_ENC_DEFAULT);
    Application app = mgr.getModelService().findApplicationByName(applicationNameString);
    ApplicationArchive arch = mgr.getModelService().findApplicationArchiveByHashAndAlgorithm(app, archiveHash, "MD5");
    String authSalt = app.getProxyAuthSalt();
    String authToken = URLDecoder.decode(pathParts[AUTH_TOKEN_INDEX], FormConstants.CHAR_ENC_DEFAULT);
    try {
        if (!AuthTokenProvider.validateAuthToken(authSalt, authToken)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
        }
    } catch (DigestException e1) {
        throw new GenericRuntimeException(e1);
    }
    File fileFull = new File(arch.getExplodedPath(settings.getTemporaryStoragePath()).getAbsolutePath() + "/" + fileRelative);
    try {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(fileFull.toURL().toString());
        response.setContentType(mimeType);
        response.setContentLength(Long.valueOf(fileFull.length()).intValue());
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            // response.setStatus(HttpServletResponse.SC_FOUND);
            inputStream = new FileInputStream(fileFull);
            outputStream = response.getOutputStream();
            Utils.pipeInputStreamIntoOutputStream(inputStream, outputStream);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            response.getOutputStream().flush();
            response.getOutputStream().close();
        }
    } catch (FileNotFoundException e) {
        logger.error("Exception {}", e);
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    } catch (IOException ioe) {
        logger.error("Exception {}", ioe);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) GlobalSettings(com.openmeap.model.dto.GlobalSettings) IOException(java.io.IOException) ModelManager(com.openmeap.model.ModelManager) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) FileNameMap(java.net.FileNameMap) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive) FileInputStream(java.io.FileInputStream) DigestException(com.openmeap.digest.DigestException) Application(com.openmeap.model.dto.Application) File(java.io.File)

Example 2 with ModelManager

use of com.openmeap.model.ModelManager in project OpenMEAP by OpenMEAP.

the class AddModifyApplicationVersionsBackingTest method testFormSetup.

@Test
public void testFormSetup() {
    ModelManager mm = modelManager;
    // ////////////////
    // Verify the correct templateVariables are produced when no applicationId is passed in
    // You cannot modify an app version without an application, so there will be a minimal return
    Map<Object, Object> vars = new HashMap<Object, Object>();
    Map<Object, Object> parms = new HashMap<Object, Object>();
    AddModifyApplicationVersionBacking amab = new AddModifyApplicationVersionBacking();
    amab.setModelManager(mm);
    Collection<ProcessingEvent> events = amab.process(null, vars, parms);
    Assert.assertTrue(events.size() == 1 && ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
    Assert.assertTrue(vars.size() == 1 && vars.get(FormConstants.ENCODING_TYPE).equals("enctype=\"" + FormConstants.ENCTYPE_MULTIPART_FORMDATA + "\""));
    // /////////////////////
    // verify the correct templateVariables are produced with an invalid applcationId is passed
    vars = new HashMap<Object, Object>();
    parms = new HashMap<Object, Object>();
    parms.put(FormConstants.APP_ID, new String[] { "666" });
    amab = new AddModifyApplicationVersionBacking();
    amab.setModelManager(mm);
    events = amab.process(null, vars, parms);
    Assert.assertTrue(events.size() == 1 && ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
    Assert.assertTrue(vars.size() == 1 && vars.get(FormConstants.ENCODING_TYPE).equals("enctype=\"" + FormConstants.ENCTYPE_MULTIPART_FORMDATA + "\""));
    // ///////////////////
    // verify the correct templateVariables are produced with an valid applcationId,
    // but invalid versionId is passed
    vars = new HashMap<Object, Object>();
    parms = new HashMap<Object, Object>();
    parms.put(FormConstants.APP_ID, new String[] { "1" });
    parms.put("versionId", new String[] { "666" });
    amab = new AddModifyApplicationVersionBacking();
    amab.setModelManager(mm);
    events = amab.process(null, vars, parms);
    Assert.assertTrue(events.size() == 4 && ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
    Assert.assertTrue(vars.size() == 6);
    Assert.assertTrue(vars.get(FormConstants.ENCODING_TYPE).equals("enctype=\"" + FormConstants.ENCTYPE_MULTIPART_FORMDATA + "\""));
    Assert.assertTrue(vars.get("application") != null && ((Application) vars.get("application")).getName().compareTo("Application.name") == 0);
    Assert.assertTrue(vars.get("version") != null && ((ApplicationVersion) vars.get("version")).getIdentifier() == null);
    Assert.assertTrue(vars.get("hashTypes") != null && ((List) vars.get("hashTypes")).size() == HashAlgorithm.values().length);
    Assert.assertTrue(((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APPVER) == 0);
    // ////////////////////
    // verify the correct templateVariables are produced when
    // both a valid app id and version id are passed in
    vars = new HashMap<Object, Object>();
    parms = new HashMap<Object, Object>();
    parms.put(FormConstants.APP_ID, new String[] { "1" });
    parms.put("versionId", new String[] { "1" });
    amab = new AddModifyApplicationVersionBacking();
    amab.setModelManager(mm);
    events = amab.process(null, vars, parms);
    Assert.assertTrue(events.size() == 3);
    Assert.assertTrue(vars.size() == 6);
    Assert.assertTrue(vars.get(FormConstants.ENCODING_TYPE).equals("enctype=\"" + FormConstants.ENCTYPE_MULTIPART_FORMDATA + "\""));
    Assert.assertTrue(vars.get("application") != null && ((Application) vars.get("application")).getName().compareTo("Application.name") == 0);
    Assert.assertTrue(vars.get("version") != null && ((ApplicationVersion) vars.get("version")).getIdentifier().compareTo("ApplicationVersion.identifier.1") == 0);
    Assert.assertTrue(vars.get("hashTypes") != null && ((List) vars.get("hashTypes")).size() == HashAlgorithm.values().length);
    Assert.assertTrue(((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APPVER) == 0);
}
Also used : ApplicationVersion(com.openmeap.model.dto.ApplicationVersion) HashMap(java.util.HashMap) ProcessingEvent(com.openmeap.event.ProcessingEvent) ModelManager(com.openmeap.model.ModelManager) AddModifyApplicationVersionBacking(com.openmeap.admin.web.backing.AddModifyApplicationVersionBacking) Application(com.openmeap.model.dto.Application)

Example 3 with ModelManager

use of com.openmeap.model.ModelManager in project OpenMEAP by OpenMEAP.

the class AddModifyApplicationBackingTest method testCreateAndModifyApplications.

@Test
public void testCreateAndModifyApplications() {
    ModelTestUtils.resetTestDb();
    ModelTestUtils.createModel(null);
    ModelManager mm = ModelTestUtils.createModelManager();
    // ////////////////////////
    // Verify that we can use the backing to create a new application
    Map<Object, Object> vars = new HashMap<Object, Object>();
    Map<Object, Object> parms = new HashMap<Object, Object>();
    AddModifyApplicationBacking amab = null;
    parms.put(FormConstants.PROCESS_TARGET, new String[] { ProcessingTargets.ADDMODIFY_APP });
    parms.put(FormConstants.APP_ID, new String[] {});
    parms.put("name", new String[] { "Application.name.3" });
    parms.put(FormConstants.APP_DESCRIPTION, new String[] { "Application.description.3" });
    parms.put("deviceTypes", new String[] { "1" });
    amab = new AddModifyApplicationBacking();
    amab.setModelManager(mm);
    amab.process(null, vars, parms);
    Assert.assertTrue(vars.get(FormConstants.PROCESS_TARGET) != null && ((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APP) == 0);
    Assert.assertTrue(vars.get("application") != null && ((Application) vars.get("application")).getName().compareTo("Application.name.3") == 0);
    // ////////////////////////
    // Verify that inadequate data will throw an exception
    // and that exception will manifest as an event returned
    // that targets the message backing.
    vars = new HashMap<Object, Object>();
    parms = new HashMap<Object, Object>();
    parms.put(FormConstants.PROCESS_TARGET, new String[] { ProcessingTargets.ADDMODIFY_APP });
    parms.put(FormConstants.APP_ID, new String[] {});
    parms.put(FormConstants.APP_DESCRIPTION, new String[] { "Application.description.4" });
    parms.put("deviceTypes", new String[] { "1" });
    amab = new AddModifyApplicationBacking();
    amab.setModelManager(mm);
    Collection<ProcessingEvent> events = amab.process(null, vars, parms);
    Assert.assertTrue(events.size() > 0);
    Integer numFound = 0;
    for (ProcessingEvent event : events) {
        if (event.getTargets()[0].compareTo(ProcessingTargets.MESSAGES) == 0) {
            numFound++;
        }
    }
    Assert.assertTrue(numFound == 1);
    // ////////////////////////
    // Verify that we can use the backing to modify an application
    vars = new HashMap<Object, Object>();
    parms = new HashMap<Object, Object>();
    parms.put(FormConstants.PROCESS_TARGET, new String[] { ProcessingTargets.ADDMODIFY_APP });
    // we happen to know that the model creates an application with id 1 that is not named Application.name.3
    parms.put(FormConstants.APP_ID, new String[] { "1" });
    parms.put("name", new String[] { "Application.name.1" });
    parms.put(FormConstants.APP_DESCRIPTION, new String[] { "Application.description.1_modified" });
    parms.put("deviceTypes", new String[] { "1", "2" });
    amab = new AddModifyApplicationBacking();
    amab.setModelManager(mm);
    amab.process(null, vars, parms);
    Assert.assertTrue(vars.get(FormConstants.PROCESS_TARGET) != null && ((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APP) == 0);
    Assert.assertTrue(vars.get("application") != null && ((Application) vars.get("application")).getDescription().compareTo("Application.description.1_modified") == 0);
    // verify that the applicaiton we modified is otherwise uncorrupted.
    Application app = mm.getModelService().findByPrimaryKey(Application.class, 1L);
    Assert.assertTrue(app.getName() != null && app.getName().compareTo("Application.name.1") == 0);
}
Also used : HashMap(java.util.HashMap) ProcessingEvent(com.openmeap.event.ProcessingEvent) ModelManager(com.openmeap.model.ModelManager) Application(com.openmeap.model.dto.Application) Test(org.junit.Test)

Example 4 with ModelManager

use of com.openmeap.model.ModelManager in project OpenMEAP by OpenMEAP.

the class AddModifyApplicationBackingTest method testAddModifyFormSetup.

@Test
public void testAddModifyFormSetup() {
    ModelManager mm = ModelTestUtils.createModelManager();
    // ////////////////////////
    // Verify the correct templateVariables are produced when no applicationId is passed in
    Map<Object, Object> vars = new HashMap<Object, Object>();
    Map<Object, Object> parms = new HashMap<Object, Object>();
    AddModifyApplicationBacking amab = new AddModifyApplicationBacking();
    amab.setModelManager(mm);
    amab.process(null, vars, parms);
    Assert.assertTrue(vars.get(FormConstants.PROCESS_TARGET) != null && ((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APP) == 0);
    // ////////////////////////
    // Verify that passing in an applicationId will return the app in the model
    vars = new HashMap<Object, Object>();
    parms = new HashMap<Object, Object>();
    parms.put(FormConstants.APP_ID, new String[] { "1" });
    amab = new AddModifyApplicationBacking();
    amab.setModelManager(mm);
    amab.process(null, vars, parms);
    Assert.assertTrue(vars.get(FormConstants.PROCESS_TARGET) != null && ((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APP) == 0);
    Assert.assertTrue(vars.get("application") != null && ((Application) vars.get("application")).getId() == 1L);
}
Also used : HashMap(java.util.HashMap) ModelManager(com.openmeap.model.ModelManager) Test(org.junit.Test)

Example 5 with ModelManager

use of com.openmeap.model.ModelManager in project OpenMEAP by OpenMEAP.

the class ApplicationManagementServiceImpl method getApplication.

/*
	 * PRIVATE METHODS
	 */
private Application getApplication(String appName, String appVersionId) throws WebServiceException {
    if (appName == null || appVersionId == null) {
        throw new WebServiceException(WebServiceException.TypeEnum.APPLICATION_VERSION_NOTFOUND, "Both application name and version id must be specified.");
    }
    ModelManager manager = getModelManager();
    // we will need to verify that they have the latest version
    Application application = manager.getModelService().findApplicationByName(appName);
    if (application == null) {
        throw new WebServiceException(WebServiceException.TypeEnum.APPLICATION_NOTFOUND, "The application \"" + appName + "\" was not found.");
    }
    return application;
}
Also used : WebServiceException(com.openmeap.protocol.WebServiceException) ModelManager(com.openmeap.model.ModelManager) Application(com.openmeap.model.dto.Application)

Aggregations

ModelManager (com.openmeap.model.ModelManager)10 Application (com.openmeap.model.dto.Application)6 HashMap (java.util.HashMap)6 Test (org.junit.Test)5 ProcessingEvent (com.openmeap.event.ProcessingEvent)3 GlobalSettings (com.openmeap.model.dto.GlobalSettings)3 IOException (java.io.IOException)2 AuthorizerImpl (com.openmeap.AuthorizerImpl)1 AddModifyApplicationVersionBacking (com.openmeap.admin.web.backing.AddModifyApplicationVersionBacking)1 DigestException (com.openmeap.digest.DigestException)1 HttpResponse (com.openmeap.http.HttpResponse)1 ModelServiceImpl (com.openmeap.model.ModelServiceImpl)1 ApplicationArchive (com.openmeap.model.dto.ApplicationArchive)1 ApplicationVersion (com.openmeap.model.dto.ApplicationVersion)1 ClusterNode (com.openmeap.model.dto.ClusterNode)1 ModelEntityModifyEvent (com.openmeap.model.event.ModelEntityModifyEvent)1 WebServiceException (com.openmeap.protocol.WebServiceException)1 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)1 MockHttpRequestExecuter (com.openmeap.util.MockHttpRequestExecuter)1 DocumentProcessor (com.openmeap.web.DocumentProcessor)1