Search in sources :

Example 1 with GenericRuntimeException

use of com.openmeap.util.GenericRuntimeException in project OpenMEAP by OpenMEAP.

the class LocalStorageImpl method unzipImportArchive.

public void unzipImportArchive(UpdateStatus update) throws LocalStorageException {
    // at this point, we've verified that:
    //   1) we have enough space on the device
    //   2) the archive downloaded is what was expected
    ZipInputStream zis = null;
    String newPrefix = "com.openmeap.storage." + update.getUpdateHeader().getHash().getValue();
    File hashHolder = null;
    String hashRootAbsolutePath = "";
    try {
        hashHolder = new File(activity.getFilesDir(), newPrefix);
        hashHolder.mkdir();
        hashRootAbsolutePath = hashHolder.getAbsolutePath();
    } catch (Exception e) {
        System.out.println("Exception thrown while creating hash folder.");
        System.out.println(e);
    }
    try {
        zis = new ZipInputStream(getImportArchiveInputStream());
        ZipEntry ze;
        while ((ze = zis.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                //		    		continue;
                try {
                    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    System.out.println("Writing directory structure in phone memory.");
                    File directoryStructure = new File(hashRootAbsolutePath, ze.getName());
                    directoryStructure.mkdirs();
                } catch (Exception e) {
                    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    System.out.println("Exception thrown while writing directory structure.");
                    System.out.println(e);
                }
            } else {
                try {
                    String osSeperator = System.getProperty("file.separator");
                    int seperatorLastIndex = ze.getName().lastIndexOf(osSeperator);
                    String fileName = ze.getName().substring(seperatorLastIndex + 1, ze.getName().length());
                    String fileNameParentDirectoryPrefix = "";
                    String absolutePathFromPrefix = "";
                    if (seperatorLastIndex != -1 && seperatorLastIndex != 0) {
                        fileNameParentDirectoryPrefix = ze.getName().substring(0, seperatorLastIndex);
                        absolutePathFromPrefix = hashRootAbsolutePath + osSeperator + fileNameParentDirectoryPrefix;
                    } else {
                        absolutePathFromPrefix = hashRootAbsolutePath + osSeperator;
                    }
                    URI osResourePathForThisFile = URI.create(absolutePathFromPrefix);
                    File writableFileReference = new File(osResourePathForThisFile.getPath(), fileName);
                    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(writableFileReference.getAbsolutePath(), true), 1024);
                    try {
                        byte[] buffer = new byte[1024];
                        int count;
                        while ((count = zis.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, count);
                        }
                    } catch (Exception e) {
                        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                        System.out.println("Exception while writing file contents.");
                        System.out.println(e);
                    } finally {
                        outputStream.close();
                    }
                } catch (Exception e) {
                    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    System.out.println("Unknown exception.");
                    System.out.println(e);
                }
            }
        //		    	Commenting following code to make use of file:/// alternate to content://
        //		        OutputStream baos = openFileOutputStream(newPrefix,ze.getName());		        
        //		        try {
        //		        	byte[] buffer = new byte[1024];
        //		        	int count;
        //		        	while ((count = zis.read(buffer)) != -1) {
        //		        		baos.write(buffer, 0, count);
        //		        	}
        //		        }
        //		        catch( Exception e ) {
        //		        	;// TODO: something, for the love of god.
        //		        }
        //		        finally {
        //		        	baos.close();
        //		        }
        }
    } catch (Exception e) {
        throw new LocalStorageException(e);
    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                throw new GenericRuntimeException(e);
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) URI(java.net.URI) LocalStorageException(com.openmeap.thinclient.LocalStorageException) LocalStorageException(com.openmeap.thinclient.LocalStorageException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UpdateException(com.openmeap.thinclient.update.UpdateException) ZipInputStream(java.util.zip.ZipInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with GenericRuntimeException

use of com.openmeap.util.GenericRuntimeException in project OpenMEAP by OpenMEAP.

the class AppMgmtClientFactory method newDefault.

public static ApplicationManagementService newDefault(String serviceUrl) {
    try {
        ApplicationManagementService service = (ApplicationManagementService) defaultClient.newInstance();
        service.setServiceUrl(serviceUrl);
        service.setHttpRequestExecuter(HttpRequestExecuterFactory.newDefault());
        return service;
    } catch (Exception e) {
        throw new GenericRuntimeException(e);
    }
}
Also used : ApplicationManagementService(com.openmeap.protocol.ApplicationManagementService) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException)

Example 3 with GenericRuntimeException

use of com.openmeap.util.GenericRuntimeException 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 4 with GenericRuntimeException

use of com.openmeap.util.GenericRuntimeException in project OpenMEAP by OpenMEAP.

the class DigestInputStreamFactory method setStaticDigests.

public void setStaticDigests(Hashtable digests) {
    Enumeration enumer = digests.keys();
    while (enumer.hasMoreElements()) {
        String key = (String) enumer.nextElement();
        String clazz = (String) digests.get(key);
        try {
            this.digests.put(key, DigestInputStreamFactory.class.forName(clazz));
        } catch (ClassNotFoundException e) {
            throw new GenericRuntimeException(e);
        }
    }
}
Also used : Enumeration(java.util.Enumeration) GenericRuntimeException(com.openmeap.util.GenericRuntimeException)

Example 5 with GenericRuntimeException

use of com.openmeap.util.GenericRuntimeException in project OpenMEAP by OpenMEAP.

the class JsApiCoreImpl method performUpdate.

/**
	 * 
	 * @param header JSON of the update header
	 * @param statusCallBack a status change callback
	 */
public void performUpdate(final String header, final String statusCallBack) {
    // needs to immediately return control to the calling javascript,
    // and pass back download status information via the callback function.
    JsUpdateHeader jsUpdateHeader = null;
    try {
        jsUpdateHeader = new JsUpdateHeader(header);
    } catch (JSONException e) {
        throw new GenericRuntimeException(e);
    }
    UpdateHeader reloadedHeader = jsUpdateHeader.getWrappedObject();
    if (reloadedHeader != null) {
        updateHandler.handleUpdate(reloadedHeader, new UpdateHandler.StatusChangeHandler() {

            public void onStatusChange(UpdateStatus update) {
                try {
                    JSONObject js = new JSONObject("{update:" + header + "}");
                    UpdateException error = update.getError();
                    js.put("bytesDownloaded", update.getBytesDownloaded());
                    js.put("complete", update.getComplete());
                    js.put("error", error != null ? new JsError(error.getUpdateResult().toString(), error.getMessage()).toJSONObject() : null);
                    webView.executeJavascriptFunction(statusCallBack, new String[] { js.toString() });
                } catch (JSONException e) {
                    throw new GenericRuntimeException(e);
                }
            }
        });
    }
}
Also used : UpdateHandler(com.openmeap.thinclient.update.UpdateHandler) UpdateStatus(com.openmeap.thinclient.update.UpdateStatus) JsError(com.openmeap.protocol.json.JsError) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JsUpdateHeader(com.openmeap.protocol.json.JsUpdateHeader) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JsUpdateHeader(com.openmeap.protocol.json.JsUpdateHeader) UpdateHeader(com.openmeap.protocol.dto.UpdateHeader) UpdateException(com.openmeap.thinclient.update.UpdateException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException)

Aggregations

GenericRuntimeException (com.openmeap.util.GenericRuntimeException)12 DigestException (com.openmeap.digest.DigestException)5 IOException (java.io.IOException)5 GlobalSettings (com.openmeap.model.dto.GlobalSettings)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 Application (com.openmeap.model.dto.Application)2 ApplicationArchive (com.openmeap.model.dto.ApplicationArchive)2 UpdateHeader (com.openmeap.protocol.dto.UpdateHeader)2 LocalStorageException (com.openmeap.thinclient.LocalStorageException)2 UpdateException (com.openmeap.thinclient.update.UpdateException)2 JSONException (com.openmeap.thirdparty.org.json.me.JSONException)2 JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)2 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 FileNameMap (java.net.FileNameMap)2 HttpRequestException (com.openmeap.http.HttpRequestException)1 ModelManager (com.openmeap.model.ModelManager)1