Search in sources :

Example 6 with Result

use of com.openmeap.protocol.dto.Result in project OpenMEAP by OpenMEAP.

the class ApplicationManagementServlet method handleArchiveDownload.

private Result handleArchiveDownload(HttpServletRequest request, HttpServletResponse response) {
    Result res = new Result();
    Error err = new Error();
    res.setError(err);
    GlobalSettings settings = modelManager.getGlobalSettings();
    Map properties = this.getServicesWebProperties();
    String nodeKey = (String) properties.get("clusterNodeUrlPrefix");
    ClusterNode clusterNode = settings.getClusterNode(nodeKey);
    if (nodeKey == null || clusterNode == null) {
        // TODO: create a configuration error code
        err.setCode(ErrorCode.UNDEFINED);
        err.setMessage("A configuration is missing.  Please consult the error logs.");
        logger.error("For each node in the cluster, the property or environment variable OPENMEAP_CLUSTER_NODE_URL_PREFIX must match the \"Service Url Prefix\" value configured in the administrative interface.  This value is currently " + nodeKey + ".");
        return res;
    }
    String pathValidation = clusterNode.validateFileSystemStoragePathPrefix();
    if (pathValidation != null) {
        err.setCode(ErrorCode.UNDEFINED);
        err.setMessage("A configuration is missing.  Please consult the error logs.");
        logger.error("There is an issue with the location at \"File-system Storage Prefix\".  " + pathValidation);
        return res;
    }
    String hash = request.getParameter(UrlParamConstants.APPARCH_HASH);
    String hashAlg = request.getParameter(UrlParamConstants.APPARCH_HASH_ALG);
    String fileName = null;
    if (hash == null || hashAlg == null) {
        // look in the apps directory for the archive specified
        String appName = request.getParameter(UrlParamConstants.APP_NAME);
        String versionId = request.getParameter(UrlParamConstants.APP_VERSION);
        ApplicationVersion appVersion = modelManager.getModelService().findAppVersionByNameAndId(appName, versionId);
        if (appVersion == null) {
            String mesg = "The application version " + versionId + " was not found for application " + appName;
            err.setCode(ErrorCode.APPLICATION_VERSION_NOTFOUND);
            err.setMessage(mesg);
            logger.warn(mesg);
            return res;
        }
        String auth = request.getParameter(UrlParamConstants.AUTH_TOKEN);
        com.openmeap.model.dto.Application app = appVersion.getApplication();
        try {
            if (auth == null || !AuthTokenProvider.validateAuthToken(app.getProxyAuthSalt(), auth)) {
                err.setCode(ErrorCode.AUTHENTICATION_FAILURE);
                err.setMessage("The \"auth\" token presented is not recognized, missing, or empty.");
                return res;
            }
        } catch (DigestException e) {
            throw new GenericRuntimeException(e);
        }
        hash = appVersion.getArchive().getHash();
        hashAlg = appVersion.getArchive().getHashAlgorithm();
        fileName = app.getName() + " - " + appVersion.getIdentifier();
    } else {
        fileName = hashAlg + "-" + hash;
    }
    File file = ApplicationArchive.getFile(clusterNode.getFileSystemStoragePathPrefix(), hashAlg, hash);
    if (!file.exists()) {
        String mesg = "The application archive with " + hashAlg + " hash " + hash + " was not found.";
        // TODO: create an enumeration for this error
        err.setCode(ErrorCode.UNDEFINED);
        err.setMessage(mesg);
        logger.warn(mesg);
        return res;
    }
    try {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(file.toURL().toString());
        response.setContentType(mimeType);
        response.setContentLength(Long.valueOf(file.length()).intValue());
        URLCodec codec = new URLCodec();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".zip\";");
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));
            outputStream = response.getOutputStream();
            Utils.pipeInputStreamIntoOutputStream(inputStream, outputStream);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        //if(outputStream!=null) {outputStream.close();}
        }
        response.flushBuffer();
    } catch (FileNotFoundException e) {
        logger.error("Exception {}", e);
    } catch (IOException ioe) {
        logger.error("Exception {}", ioe);
    }
    return null;
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) ApplicationVersion(com.openmeap.model.dto.ApplicationVersion) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) Error(com.openmeap.protocol.dto.Error) GlobalSettings(com.openmeap.model.dto.GlobalSettings) IOException(java.io.IOException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) FileNameMap(java.net.FileNameMap) FileInputStream(java.io.FileInputStream) Result(com.openmeap.protocol.dto.Result) URLCodec(org.apache.commons.codec.net.URLCodec) BufferedInputStream(java.io.BufferedInputStream) DigestException(com.openmeap.digest.DigestException) FileNameMap(java.net.FileNameMap) Map(java.util.Map) File(java.io.File)

Example 7 with Result

use of com.openmeap.protocol.dto.Result in project OpenMEAP by OpenMEAP.

the class ApplicationManagementServlet method connectionOpenRequest.

/**
	 * Pulls parameters out of the request and passes them to the ApplicationManagementPortType bean pulled from the WebApplicationContext
	 * 
	 * @param req
	 * @return
	 */
public Result connectionOpenRequest(HttpServletRequest req) {
    WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    ConnectionOpenRequest request = createConnectionOpenRequest(req);
    Result result = new Result();
    try {
        ConnectionOpenResponse response = ((ApplicationManagementService) context.getBean("applicationManagementService")).connectionOpen(request);
        result.setConnectionOpenResponse(response);
    } catch (WebServiceException wse) {
        Error err = new Error();
        err.setCode(wse.getType().asErrorCode());
        err.setMessage(wse.getMessage());
        result.setError(err);
    }
    return result;
}
Also used : WebServiceException(com.openmeap.protocol.WebServiceException) Error(com.openmeap.protocol.dto.Error) ConnectionOpenRequest(com.openmeap.protocol.dto.ConnectionOpenRequest) ConnectionOpenResponse(com.openmeap.protocol.dto.ConnectionOpenResponse) ApplicationManagementService(com.openmeap.protocol.ApplicationManagementService) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Result(com.openmeap.protocol.dto.Result)

Aggregations

Result (com.openmeap.protocol.dto.Result)7 JSONObjectBuilder (com.openmeap.json.JSONObjectBuilder)3 ApplicationVersion (com.openmeap.model.dto.ApplicationVersion)3 Error (com.openmeap.protocol.dto.Error)3 JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)3 HttpResponse (com.openmeap.http.HttpResponse)2 GlobalSettings (com.openmeap.model.dto.GlobalSettings)2 WebServiceException (com.openmeap.protocol.WebServiceException)2 ConnectionOpenResponse (com.openmeap.protocol.dto.ConnectionOpenResponse)2 JSONException (com.openmeap.thirdparty.org.json.me.JSONException)2 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)2 File (java.io.File)2 IOException (java.io.IOException)2 Hashtable (java.util.Hashtable)2 DigestException (com.openmeap.digest.DigestException)1 HttpRequestException (com.openmeap.http.HttpRequestException)1 Application (com.openmeap.model.dto.Application)1 ClusterNode (com.openmeap.model.dto.ClusterNode)1 ApplicationManagementService (com.openmeap.protocol.ApplicationManagementService)1 ConnectionOpenRequest (com.openmeap.protocol.dto.ConnectionOpenRequest)1