Adding APK file validation functionalty

To implement apk file validation we are using external library. APK parser: https://github.com/hsiafan/apk-parser
4.x.x
lasantha 7 years ago
parent 460a356bee
commit 6b0e281112

@ -61,20 +61,22 @@ public interface ApplicationStorageManager {
* To upload release artifacts for an Application.
*
* @param applicationRelease Application Release Object.
* @param appType Application Type.
* @param binaryFile Binary File for the release.
* @throws ResourceManagementException Resource Management Exception.
*/
ApplicationRelease uploadReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile)
ApplicationRelease uploadReleaseArtifact(ApplicationRelease applicationRelease, String appType, InputStream binaryFile)
throws ResourceManagementException;
/**
* To upload release artifacts for an Application.
*
* @param applicationRelease applicationRelease Application release of a particular application.
* @param appType Type of the application
* @param binaryFile Binary File for the release.
* @throws ApplicationStorageManagementException Resource Management Exception.
*/
ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile)
ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, String appType, InputStream binaryFile)
throws ApplicationStorageManagementException;
/**

@ -76,13 +76,25 @@
org.wso2.carbon.user.api.*,
org.wso2.carbon.ndatasource.core,
org.wso2.carbon,
javax.annotation,
org.bouncycastle.cert,
org.bouncycastle.cert.jcajce,
org.bouncycastle.cms,
org.bouncycastle.jce.provider,
org.bouncycastle.util,
org.xml.sax,
org.xml.sax.helpers,
org.apache.commons.io,
org.apache.commons.codec.binary;version="${commons-codec.wso2.osgi.version.range}",
org.apache.commons.codec.digest;version="${commons-codec.wso2.osgi.version.range}",
org.wso2.carbon.base,
org.wso2.carbon.device.mgt.core.dto.*;version="${carbon.device.mgt.version}",
org.wso2.carbon.device.mgt.core.dao.*;version="${carbon.device.mgt.version}"
org.wso2.carbon.device.mgt.core.dao.*;version="${carbon.device.mgt.version}",
net.dongliu.*
</Import-Package>
<Embed-Dependency>apk-parser;scope=compile|runtime;inline=false</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Embed-StripGroup>true</Embed-StripGroup>
<Export-Package>
!org.wso2.carbon.device.application.mgt.core.internal.*,
org.wso2.carbon.device.application.mgt.core.*
@ -169,6 +181,11 @@
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.core</artifactId>
</dependency>
<dependency>
<groupId>net.dongliu</groupId>
<artifactId>apk-parser</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies>
</project>

@ -19,12 +19,15 @@
package org.wso2.carbon.device.application.mgt.core.impl;
import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.ApplicationType;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
@ -33,6 +36,7 @@ import org.wso2.carbon.device.application.mgt.core.util.Constants;
import org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@ -155,13 +159,41 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
}
@Override
public ApplicationRelease uploadReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile)
public ApplicationRelease uploadReleaseArtifact(ApplicationRelease applicationRelease, String appType, InputStream binaryFile)
throws ResourceManagementException {
String artifactDirectoryPath;
String md5OfApp;
md5OfApp = getMD5(binaryFile);
//todo validate binary file.
try {
if (ApplicationType.ANDROID.toString().equals(appType)){
String prefix = "stream2file";
String suffix = ".apk";
Boolean isTempDelete;
File tempFile = File.createTempFile(prefix, suffix);
FileOutputStream out = new FileOutputStream(tempFile);
IOUtils.copy(binaryFile, out);
ApkFile apkFile = new ApkFile(tempFile);
ApkMeta apkMeta = apkFile.getApkMeta();
applicationRelease.setVersion(apkMeta.getVersionName());
isTempDelete = tempFile.delete();
if (!isTempDelete) {
log.error("Temporary created APK file deletion failed");
}
}else if (ApplicationType.iOS.toString().equals(appType)){
//todo iOS ipa validate
}else if (ApplicationType.WEB_CLIP.toString().equals(appType)){
//todo Web Clip validate
}else{
throw new ApplicationStorageManagementException("Application Type doesn't match with supporting " +
"application types " + applicationRelease.getUuid());
}
if (md5OfApp != null) {
artifactDirectoryPath = storagePath + md5OfApp;
@ -170,30 +202,33 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
log.debug("Artifact Directory Path for saving the application release related artifacts related with "
+ "application UUID " + applicationRelease.getUuid() + " is " + artifactDirectoryPath);
}
try {
saveFile(binaryFile, artifactDirectoryPath + Constants.RELEASE_ARTIFACT);
applicationRelease.setAppStoredLoc(artifactDirectoryPath);
String artifactPath = artifactDirectoryPath + Constants.RELEASE_ARTIFACT;
saveFile(binaryFile, artifactPath);
applicationRelease.setAppStoredLoc(artifactPath);
applicationRelease.setAppHashValue(md5OfApp);
} else {
throw new ApplicationStorageManagementException("Error occurred while md5sum value retrieving process: " +
"application UUID " + applicationRelease.getUuid());
}
} catch (IOException e) {
throw new ApplicationStorageManagementException(
"IO Exception while saving the release artifacts in the server for the application UUID "
+ applicationRelease.getUuid(), e);
}
} else {
log.error("Verify application existence and md5sum value retrieving process");
}
return applicationRelease;
}
@Override
public ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile)
throws ApplicationStorageManagementException {
public ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, String appType,
InputStream binaryFile) throws ApplicationStorageManagementException {
if (binaryFile != null) {
try {
deleteApplicationReleaseArtifacts(applicationRelease.getAppStoredLoc());
applicationRelease = uploadReleaseArtifacts(applicationRelease, binaryFile);
applicationRelease = uploadReleaseArtifact(applicationRelease, appType, binaryFile);
} catch (ApplicationStorageManagementException e) {
throw new ApplicationStorageManagementException("Application Artifact doesn't contains in the System", e);
} catch (ResourceManagementException e) {

Loading…
Cancel
Save