Search in sources :

Example 1 with LocalStorageException

use of com.openmeap.thinclient.LocalStorageException 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 LocalStorageException

use of com.openmeap.thinclient.LocalStorageException in project OpenMEAP by OpenMEAP.

the class UpdateHandler method _handleUpdate.

private void _handleUpdate(UpdateStatus update, StatusChangeHandler eventHandler) throws UpdateException {
    String lastUpdateResult = config.getLastUpdateResult();
    boolean hasTimedOut = hasUpdatePendingTimedOut();
    if (!hasTimedOut && lastUpdateResult != null && lastUpdateResult.compareTo(UpdateResult.PENDING.toString()) == 0) {
        return;
    } else {
        config.setLastUpdateResult(UpdateResult.PENDING.toString());
    }
    UpdateHeader updateHeader = update.getUpdateHeader();
    // if the new version is the original version,
    // then we'll just update the app version, delete internal storage and
    // return
    String versionId = updateHeader.getVersionIdentifier();
    if (config.isVersionOriginal(versionId).booleanValue()) {
        _revertToOriginal(update, eventHandler);
        return;
    }
    if (!deviceHasEnoughSpace(update).booleanValue()) {
        // behavior. default behavior should be informative
        throw new UpdateException(UpdateResult.OUT_OF_SPACE, "Need more space to install than is available");
    }
    try {
        // behavior. default behavior should be informative
        if (!downloadToArchive(update, eventHandler).booleanValue()) {
            return;
        }
    } catch (Exception ioe) {
        // behavior. default behavior should be informative
        throw new UpdateException(UpdateResult.IO_EXCEPTION, "An issue occurred downloading the archive", ioe);
    }
    if (!archiveIsValid(update).booleanValue()) {
        throw new UpdateException(UpdateResult.HASH_MISMATCH, "The import archive integrity check failed");
    }
    installArchive(update);
    // at this point, the archive should be of no use to us
    try {
        storage.deleteImportArchive();
    } catch (LocalStorageException lse) {
        throw new UpdateException(UpdateResult.IO_EXCEPTION, "Could not delete import archive", lse);
    }
    try {
        storage.resetStorage();
    } catch (LocalStorageException lse) {
        throw new UpdateException(UpdateResult.IO_EXCEPTION, "Could not reset storage", lse);
    }
    config.setLastUpdateResult(UpdateResult.SUCCESS.toString());
    config.setApplicationVersion(update.getUpdateHeader().getVersionIdentifier());
    config.setArchiveHash(update.getUpdateHeader().getHash().getValue());
    String newPrefix = storage.getStorageRoot() + update.getUpdateHeader().getHash().getValue();
    config.setStorageLocation(newPrefix);
    config.setApplicationUpdated(Boolean.TRUE);
    if (eventHandler != null) {
        update.setComplete(true);
        eventHandler.onStatusChange(update);
    } else {
        activity.restart();
    }
}
Also used : UpdateHeader(com.openmeap.protocol.dto.UpdateHeader) LocalStorageException(com.openmeap.thinclient.LocalStorageException) LocalStorageException(com.openmeap.thinclient.LocalStorageException) WebServiceException(com.openmeap.protocol.WebServiceException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) IOException(java.io.IOException) HttpRequestException(com.openmeap.http.HttpRequestException)

Example 3 with LocalStorageException

use of com.openmeap.thinclient.LocalStorageException in project OpenMEAP by OpenMEAP.

the class JsApiCoreImpl method checkForUpdates.

public void checkForUpdates() {
    UpdateHeader updateHeader = null;
    WebServiceException err = null;
    try {
        try {
            updateHeader = updateHandler.checkForUpdate();
            webView.setUpdateHeader(updateHeader, err, activity.getStorage().getBytesFree());
        } catch (WebServiceException e) {
            webView.setUpdateHeader(null, e, activity.getStorage().getBytesFree());
        }
    } catch (LocalStorageException e) {
        ;
    }
}
Also used : WebServiceException(com.openmeap.protocol.WebServiceException) JsUpdateHeader(com.openmeap.protocol.json.JsUpdateHeader) UpdateHeader(com.openmeap.protocol.dto.UpdateHeader) LocalStorageException(com.openmeap.thinclient.LocalStorageException)

Example 4 with LocalStorageException

use of com.openmeap.thinclient.LocalStorageException in project OpenMEAP by OpenMEAP.

the class UpdateHandler method downloadToArchive.

/**
 * @param update
 * @param eventHandler
 * @return true if completed, false if interrupted
 * @throws IOException
 */
public Boolean downloadToArchive(UpdateStatus update, StatusChangeHandler eventHandler) throws UpdateException {
    // download the file to import.zip
    OutputStream os = null;
    InputStream is = null;
    HttpRequestExecuter requester = HttpRequestExecuterFactory.newDefault();
    HttpResponse updateRequestResponse;
    try {
        updateRequestResponse = requester.get(update.getUpdateHeader().getUpdateUrl());
    } catch (HttpRequestException e) {
        throw new UpdateException(UpdateResult.IO_EXCEPTION, "An issue occurred fetching the update archive", e);
    }
    if (updateRequestResponse.getStatusCode() != 200)
        throw new UpdateException(UpdateResult.RESPONSE_STATUS_CODE, "Status was " + updateRequestResponse.getStatusCode() + ", expecting 200");
    try {
        os = storage.getImportArchiveOutputStream();
        is = updateRequestResponse.getResponseBody();
        byte[] bytes = new byte[1024];
        int count = is.read(bytes);
        int contentLength = (int) updateRequestResponse.getContentLength();
        int contentDownloaded = 0;
        int lastContentDownloaded = contentDownloaded;
        int percent = contentLength / 100;
        while (count != (-1)) {
            os.write(bytes, 0, count);
            count = is.read(bytes);
            contentDownloaded += count;
            if (eventHandler != null && lastContentDownloaded + percent < contentDownloaded) {
                update.setBytesDownloaded(contentDownloaded);
                eventHandler.onStatusChange(update);
                lastContentDownloaded = contentDownloaded;
            }
            synchronized (interruptLock) {
                if (interrupt.booleanValue()) {
                    clearInterruptFlag();
                    throw new UpdateException(UpdateResult.INTERRUPTED, "Download of archive was interrupted");
                }
            }
        }
    } catch (IOException lse) {
        throw new UpdateException(UpdateResult.IO_EXCEPTION, lse.getMessage(), lse);
    } catch (LocalStorageException lse) {
        throw new UpdateException(UpdateResult.IO_EXCEPTION, lse.getMessage(), lse);
    } finally {
        try {
            storage.closeOutputStream(os);
            storage.closeInputStream(is);
        } catch (LocalStorageException e) {
            throw new UpdateException(UpdateResult.IO_EXCEPTION, e.getMessage(), e);
        }
        // have to hang on to the requester till the download is complete,
        // so that we can retain control over when the connection manager
        // shut's down
        requester.shutdown();
    }
    return Boolean.TRUE;
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpResponse(com.openmeap.http.HttpResponse) HttpRequestException(com.openmeap.http.HttpRequestException) IOException(java.io.IOException) HttpRequestExecuter(com.openmeap.http.HttpRequestExecuter) LocalStorageException(com.openmeap.thinclient.LocalStorageException)

Aggregations

LocalStorageException (com.openmeap.thinclient.LocalStorageException)4 IOException (java.io.IOException)3 HttpRequestException (com.openmeap.http.HttpRequestException)2 WebServiceException (com.openmeap.protocol.WebServiceException)2 UpdateHeader (com.openmeap.protocol.dto.UpdateHeader)2 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)2 OutputStream (java.io.OutputStream)2 HttpRequestExecuter (com.openmeap.http.HttpRequestExecuter)1 HttpResponse (com.openmeap.http.HttpResponse)1 JsUpdateHeader (com.openmeap.protocol.json.JsUpdateHeader)1 UpdateException (com.openmeap.thinclient.update.UpdateException)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1