Search in sources :

Example 1 with ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive 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 ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive in project OpenMEAP by OpenMEAP.

the class AddModifyApplicationVersionBacking method fillInApplicationVersionFromParameters.

private void fillInApplicationVersionFromParameters(Application app, ApplicationVersion version, List<ProcessingEvent> events, Map<Object, Object> parameterMap) {
    version.setIdentifier(firstValue("identifier", parameterMap));
    if (version.getArchive() == null) {
        version.setArchive(new ApplicationArchive());
        version.getArchive().setApplication(app);
    }
    version.setApplication(app);
    version.setNotes(firstValue("notes", parameterMap));
    Boolean archiveUncreated = true;
    // if there was an uploadArchive, then attempt to auto-assemble the rest of parameters
    if (parameterMap.get("uploadArchive") != null) {
        if (!(parameterMap.get("uploadArchive") instanceof FileItem)) {
            events.add(new MessagesEvent("Uploaded file not processed!  Is the archive storage path set in settings?"));
        } else {
            FileItem item = (FileItem) parameterMap.get("uploadArchive");
            Long size = item.getSize();
            if (size > 0) {
                try {
                    File tempFile = ServletUtils.tempFileFromFileItem(modelManager.getGlobalSettings().getTemporaryStoragePath(), item);
                    ApplicationArchive archive = version.getArchive();
                    archive.setNewFileUploaded(tempFile.getAbsolutePath());
                    archiveUncreated = false;
                } catch (Exception ioe) {
                    logger.error("An error transpired creating an uploadArchive temp file: {}", ioe);
                    events.add(new MessagesEvent(ioe.getMessage()));
                    return;
                } finally {
                    item.delete();
                }
            } else {
                events.add(new MessagesEvent("Uploaded file not processed!  Is the archive storage path set in settings?"));
            }
        }
    }
    // else there was no zip archive uploaded
    if (archiveUncreated) {
        ApplicationArchive archive = version.getArchive();
        archive.setHashAlgorithm(firstValue("hashType", parameterMap));
        archive.setHash(firstValue("hash", parameterMap));
        ApplicationArchive arch = modelManager.getModelService().findApplicationArchiveByHashAndAlgorithm(app, archive.getHash(), archive.getHashAlgorithm());
        if (arch != null) {
            version.setArchive(arch);
            archive = arch;
        }
        archive.setUrl(firstValue("url", parameterMap));
        if (notEmpty("bytesLength", parameterMap)) {
            archive.setBytesLength(Integer.valueOf(firstValue("bytesLength", parameterMap)));
        }
        if (notEmpty("bytesLengthUncompressed", parameterMap)) {
            archive.setBytesLengthUncompressed(Integer.valueOf(firstValue("bytesLengthUncompressed", parameterMap)));
        }
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) MessagesEvent(com.openmeap.event.MessagesEvent) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive) ZipFile(java.util.zip.ZipFile) File(java.io.File) InvalidPropertiesException(com.openmeap.model.InvalidPropertiesException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive in project OpenMEAP by OpenMEAP.

the class DeploymentAddModifyNotifier method addRequestParameters.

@Override
protected void addRequestParameters(ModelEntity modelEntity, Map<String, Object> parms) {
    ApplicationArchive archive = (ApplicationArchive) (((Deployment) modelEntity).getApplicationArchive());
    parms.put(UrlParamConstants.APPARCH_FILE, archive.getFile(getModelManager().getGlobalSettings().getTemporaryStoragePath()));
    super.addRequestParameters(archive, parms);
}
Also used : Deployment(com.openmeap.model.dto.Deployment) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive)

Example 4 with ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive in project OpenMEAP by OpenMEAP.

the class ModelServiceRefreshNotifier method makeRequest.

/**
	 * This MUST remain state-less
	 * 
	 * @param <T>
	 * @param thisUrl
	 * @param obj
	 */
@Override
protected void makeRequest(final URL url, final Event<ModelEntity> mesg) throws ClusterNotificationException {
    com.openmeap.http.HttpResponse httpResponse = null;
    String simpleName = null;
    String thisUrl = url.toString() + "/" + ServletNameConstants.SERVICE_MANAGEMENT + "/";
    ModelEntity obj = mesg.getPayload();
    // I am not using obj.getClass().getSimpleName() because of Hibernate Proxy object wrapping
    if (obj instanceof Application)
        simpleName = "Application";
    else if (obj instanceof ApplicationVersion)
        simpleName = "ApplicationVersion";
    else if (obj instanceof ApplicationArchive)
        simpleName = "ApplicationArchive";
    else if (obj instanceof ApplicationInstallation)
        simpleName = "ApplicationInstallation";
    else if (obj instanceof GlobalSettings)
        simpleName = "GlobalSettings";
    else
        return;
    Hashtable<String, Object> parms = new Hashtable<String, Object>();
    parms.put(UrlParamConstants.ACTION, ModelEntityEventAction.MODEL_REFRESH.getActionName());
    parms.put(UrlParamConstants.AUTH_TOKEN, newAuthToken());
    parms.put(UrlParamConstants.REFRESH_TYPE, simpleName);
    parms.put(UrlParamConstants.REFRESH_OBJ_PKID, obj.getPk());
    try {
        logger.debug("Refresh post to {} for {} with id {}", new Object[] { thisUrl, simpleName, obj.getPk() });
        httpResponse = getHttpRequestExecuter().postData(thisUrl, parms);
        Utils.consumeInputStream(httpResponse.getResponseBody());
        int statusCode = httpResponse.getStatusCode();
        if (statusCode != 200) {
            String exMesg = "HTTP " + statusCode + " returned for refresh post to " + thisUrl + " for " + simpleName + " with id " + obj.getPk();
            logger.error(exMesg);
            throw new ClusterNotificationException(url, exMesg);
        }
    } catch (Exception e) {
        String exMesg = "Refresh post to " + thisUrl + " for " + simpleName + " with id " + obj.getPk() + " threw an exception";
        logger.error(exMesg, e);
        throw new ClusterNotificationException(url, exMesg, e);
    }
}
Also used : ApplicationVersion(com.openmeap.model.dto.ApplicationVersion) Hashtable(java.util.Hashtable) ClusterNotificationException(com.openmeap.cluster.ClusterNotificationException) GlobalSettings(com.openmeap.model.dto.GlobalSettings) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive) ClusterNotificationException(com.openmeap.cluster.ClusterNotificationException) ApplicationInstallation(com.openmeap.model.dto.ApplicationInstallation) ModelEntity(com.openmeap.model.ModelEntity) Application(com.openmeap.model.dto.Application)

Example 5 with ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive in project OpenMEAP by OpenMEAP.

the class ModelServiceImpl method findApplicationArchiveByHashAndAlgorithm.

@Override
public ApplicationArchive findApplicationArchiveByHashAndAlgorithm(Application app, String hash, String hashAlgorithm) {
    Query q = entityManager.createQuery("select distinct ar " + "from ApplicationArchive ar " + "join fetch ar.application app " + "where ar.hash=:hash " + "and ar.hashAlgorithm=:hashAlgorithm " + "and app.id=:appId");
    q.setParameter("hash", hash);
    q.setParameter("hashAlgorithm", hashAlgorithm);
    q.setParameter("appId", app.getId());
    q.setMaxResults(1);
    try {
        ApplicationArchive o = (ApplicationArchive) q.getSingleResult();
        return (ApplicationArchive) o;
    } catch (NoResultException nre) {
        return null;
    }
}
Also used : Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive)

Aggregations

ApplicationArchive (com.openmeap.model.dto.ApplicationArchive)19 File (java.io.File)6 MessagesEvent (com.openmeap.event.MessagesEvent)5 Application (com.openmeap.model.dto.Application)5 GlobalSettings (com.openmeap.model.dto.GlobalSettings)5 ApplicationVersion (com.openmeap.model.dto.ApplicationVersion)4 Deployment (com.openmeap.model.dto.Deployment)4 IOException (java.io.IOException)4 PersistenceException (javax.persistence.PersistenceException)4 DigestException (com.openmeap.digest.DigestException)3 EventHandlingException (com.openmeap.event.EventHandlingException)3 InvalidPropertiesException (com.openmeap.model.InvalidPropertiesException)3 ClusterNotificationException (com.openmeap.cluster.ClusterNotificationException)2 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)2 FileInputStream (java.io.FileInputStream)2 ZipFile (java.util.zip.ZipFile)2 FileItem (org.apache.commons.fileupload.FileItem)2 ClusterHandlingException (com.openmeap.cluster.ClusterHandlingException)1 ModelEntity (com.openmeap.model.ModelEntity)1 ModelManager (com.openmeap.model.ModelManager)1