Search in sources :

Example 1 with Error

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

the class ApplicationManagementServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Result result = new Result();
    Error err = new Error();
    String action = req.getParameter(UrlParamConstants.ACTION);
    if (action == null)
        action = "";
    if (action.equals("connection-open-request")) {
        result = connectionOpenRequest(req);
    } else if (action.equals("archiveDownload")) {
        result = handleArchiveDownload(req, resp);
        if (result == null) {
            return;
        }
    } else {
        err.setCode(ErrorCode.MISSING_PARAMETER);
        err.setMessage("The \"action\" parameter is not recognized, missing, or empty.");
        result.setError(err);
    }
    try {
        JSONObjectBuilder builder = new JSONObjectBuilder();
        resp.setContentType("text/javascript");
        JSONObject jsonResult = builder.toJSON(result);
        resp.getOutputStream().write(jsonResult.toString().getBytes());
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Error(com.openmeap.protocol.dto.Error) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) Result(com.openmeap.protocol.dto.Result)

Example 2 with Error

use of com.openmeap.protocol.dto.Error 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 3 with Error

use of com.openmeap.protocol.dto.Error 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

Error (com.openmeap.protocol.dto.Error)3 Result (com.openmeap.protocol.dto.Result)3 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)2 DigestException (com.openmeap.digest.DigestException)1 JSONObjectBuilder (com.openmeap.json.JSONObjectBuilder)1 ApplicationVersion (com.openmeap.model.dto.ApplicationVersion)1 ClusterNode (com.openmeap.model.dto.ClusterNode)1 GlobalSettings (com.openmeap.model.dto.GlobalSettings)1 ApplicationManagementService (com.openmeap.protocol.ApplicationManagementService)1 WebServiceException (com.openmeap.protocol.WebServiceException)1 ConnectionOpenRequest (com.openmeap.protocol.dto.ConnectionOpenRequest)1 ConnectionOpenResponse (com.openmeap.protocol.dto.ConnectionOpenResponse)1 JSONException (com.openmeap.thirdparty.org.json.me.JSONException)1 JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1