use of ai.elimu.model.admin.ApplicationVersion in project webapp by elimu-ai.
the class ApkController method handleRequest.
@RequestMapping(value = "/{packageName}-{versionCode}.apk", method = RequestMethod.GET)
public void handleRequest(@PathVariable String packageName, @PathVariable Integer versionCode, @RequestParam String deviceId, @RequestParam String checksum, @RequestParam Locale locale, @RequestParam String deviceModel, @RequestParam Integer osVersion, @RequestParam Integer appVersionCode, // Custom Project
@RequestParam(required = false) String licenseEmail, @RequestParam(required = false) String licenseNumber, @RequestParam(required = false) Long applicationId, HttpServletRequest request, HttpServletResponse response, OutputStream outputStream) {
logger.info("handleRequest");
logger.info("packageName: " + packageName);
logger.info("versionCode: " + versionCode);
logger.info("request.getQueryString(): " + request.getQueryString());
logger.info("request.getRemoteAddr(): " + request.getRemoteAddr());
// TODO: validate checksum
// See AppCollectionRestController#addInfrastructureApps
boolean isInfrastructureApp = "ai.elimu.appstore".equals(packageName) || "ai.elimu.appstore_custom".equals(packageName) || "ai.elimu.analytics".equals(packageName) || "ai.elimu.launcher".equals(packageName) || "ai.elimu.launcher_custom".equals(packageName);
Application application = null;
if (TextUtils.isBlank(licenseEmail) || isInfrastructureApp) {
application = applicationDao.readByPackageName(locale, packageName);
} else {
// Custom Project
License license = licenseDao.read(licenseEmail, licenseNumber);
if (license != null) {
// TODO: fetch Application based on License instead of additional applicationId parameter
application = applicationDao.read(applicationId);
}
}
ApplicationVersion applicationVersion = applicationVersionDao.read(application, versionCode);
response.setContentType(applicationVersion.getContentType());
byte[] bytes = applicationVersion.getBytes();
response.setContentLength(bytes.length);
try {
outputStream.write(bytes);
} catch (EOFException ex) {
// org.eclipse.jetty.io.EofException (occurs when download is aborted before completion)
logger.warn(ex);
} catch (IOException ex) {
logger.error(null, ex);
} finally {
try {
try {
outputStream.flush();
outputStream.close();
} catch (EOFException ex) {
// org.eclipse.jetty.io.EofException (occurs when download is aborted before completion)
logger.warn(ex);
}
} catch (IOException ex) {
logger.error(null, ex);
}
}
}
use of ai.elimu.model.admin.ApplicationVersion in project webapp by elimu-ai.
the class ApplicationVersionCreateController method handleSubmit.
@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(ApplicationVersion applicationVersion, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model, HttpSession session) {
logger.info("handleSubmit");
logger.info("applicationVersion.getVersionCode(): " + applicationVersion.getVersionCode());
if (applicationVersion.getVersionCode() == null) {
result.rejectValue("versionCode", "NotNull");
} else {
// Verify that the versionCode is higher than previous ones
List<ApplicationVersion> existingApplicationVersions = applicationVersionDao.readAll(applicationVersion.getApplication());
for (ApplicationVersion existingApplicationVersion : existingApplicationVersions) {
if (existingApplicationVersion.getVersionCode() >= applicationVersion.getVersionCode()) {
result.rejectValue("versionCode", "TooLow");
break;
}
}
}
if (multipartFile.isEmpty()) {
result.rejectValue("bytes", "NotNull");
} else {
try {
byte[] bytes = multipartFile.getBytes();
if (applicationVersion.getBytes() != null) {
String originalFileName = multipartFile.getOriginalFilename();
logger.info("originalFileName: " + originalFileName);
if (!originalFileName.endsWith(".apk")) {
result.rejectValue("bytes", "typeMismatch");
}
String contentType = multipartFile.getContentType();
logger.info("contentType: " + contentType);
applicationVersion.setContentType(contentType);
applicationVersion.setBytes(bytes);
Integer fileSizeInKb = bytes.length / 1024;
logger.info("fileSizeInKb: " + fileSizeInKb + " (" + (fileSizeInKb / 1024) + "MB)");
applicationVersion.setFileSizeInKb(fileSizeInKb);
String checksumMd5 = ChecksumHelper.calculateMD5(bytes);
logger.info("checksumMd5: " + checksumMd5);
applicationVersion.setChecksumMd5(checksumMd5);
ByteArrayApkFile byteArrayApkFile = new ByteArrayApkFile(bytes);
ApkMeta apkMeta = byteArrayApkFile.getApkMeta();
String versionName = apkMeta.getVersionName();
logger.info("versionName: " + versionName);
applicationVersion.setVersionName(versionName);
String label = apkMeta.getLabel();
logger.info("label: " + label);
applicationVersion.setLabel(label);
Integer minSdkVersion = Integer.valueOf(apkMeta.getMinSdkVersion());
logger.info("minSdkVersion: " + minSdkVersion);
applicationVersion.setMinSdkVersion(minSdkVersion);
byte[] icon = byteArrayApkFile.getIconFile().getData();
logger.info("icon.length: " + (icon.length / 1024) + "kB");
applicationVersion.setIcon(icon);
} else {
result.rejectValue("bytes", "NotNull");
}
} catch (IOException ex) {
logger.error(ex);
}
}
if (result.hasErrors()) {
model.addAttribute("applicationVersion", applicationVersion);
return "admin/application-version/create";
} else {
Contributor contributor = (Contributor) session.getAttribute("contributor");
applicationVersion.setContributor(contributor);
applicationVersion.setTimeUploaded(Calendar.getInstance());
applicationVersionDao.create(applicationVersion);
// Update the Application entity to reflect the latest changes
Application application = applicationVersion.getApplication();
if (application.getApplicationStatus() == ApplicationStatus.MISSING_APK) {
// If first APK file, change status of application to "ACTIVE"
application.setApplicationStatus(ApplicationStatus.ACTIVE);
}
applicationDao.update(application);
// Refresh REST API cache
jsonService.refreshApplications(application.getLocale());
boolean isInfrastructureApp = "ai.elimu.appstore".equals(application.getPackageName()) || "ai.elimu.appstore_custom".equals(application.getPackageName()) || "ai.elimu.analytics".equals(application.getPackageName()) || "ai.elimu.launcher".equals(application.getPackageName()) || "ai.elimu.launcher_custom".equals(application.getPackageName());
if (isInfrastructureApp) {
jsonService.refreshApplicationsInAppCollection();
}
if (EnvironmentContextLoaderListener.env == Environment.PROD) {
String text = URLEncoder.encode(contributor.getFirstName() + " just uploaded a new APK version:\n" + "• Language: " + applicationVersion.getApplication().getLocale().getLanguage() + "\n" + "• Package name: \"" + applicationVersion.getApplication().getPackageName() + "\"\n" + "• Version: " + applicationVersion.getVersionCode() + "\n" + "• Start command: " + applicationVersion.getStartCommand());
String iconUrl = contributor.getImageUrl();
SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.DEVELOPMENT), text, iconUrl, null);
}
return "redirect:/admin/application/edit/" + applicationVersion.getApplication().getId();
}
}
use of ai.elimu.model.admin.ApplicationVersion in project webapp by elimu-ai.
the class AppEditController method handleRequest.
@RequestMapping(method = RequestMethod.GET)
public String handleRequest(@PathVariable Long projectId, @PathVariable Long appCategoryId, @PathVariable Long appGroupId, @PathVariable Long applicationId, Model model) {
logger.info("handleRequest");
Project project = projectDao.read(projectId);
model.addAttribute("project", project);
AppCategory appCategory = appCategoryDao.read(appCategoryId);
model.addAttribute("appCategory", appCategory);
AppGroup appGroup = appGroupDao.read(appGroupId);
model.addAttribute("appGroup", appGroup);
Application application = applicationDao.read(applicationId);
model.addAttribute("application", application);
List<ApplicationVersion> applicationVersions = applicationVersionDao.readAll(application);
model.addAttribute("applicationVersions", applicationVersions);
model.addAttribute("applicationStatuses", ApplicationStatus.values());
return "project/app/edit";
}
use of ai.elimu.model.admin.ApplicationVersion in project webapp by elimu-ai.
the class JsonService method getApplications.
@Cacheable("applications")
public JSONArray getApplications(Locale locale) {
logger.info("getApplications");
Date dateStart = new Date();
JSONArray applications = new JSONArray();
for (Application application : applicationDao.readAll(locale)) {
ApplicationGson applicationGson = JavaToGsonConverter.getApplicationGson(application);
List<ApplicationVersionGson> applicationVersions = new ArrayList<>();
logger.info("applicationVersionDao.readAll(" + application.getPackageName() + ") - " + new Date());
for (ApplicationVersion applicationVersion : applicationVersionDao.readAll(application)) {
logger.info("applicationVersion: " + applicationVersion.getVersionCode() + " - " + new Date());
ApplicationVersionGson applicationVersionGson = JavaToGsonConverter.getApplicationVersionGson(applicationVersion);
applicationVersions.add(applicationVersionGson);
}
applicationGson.setApplicationVersions(applicationVersions);
String json = new Gson().toJson(applicationGson);
applications.put(new JSONObject(json));
}
Date dateEnd = new Date();
logger.info("getApplications duration: " + (dateEnd.getTime() - dateStart.getTime()) + " ms");
return applications;
}
Aggregations