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);
}
}
}
}
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();
}
}
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) {
;
}
}
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;
}
Aggregations