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