Merge branch 'application-mgt-new' into 'application-mgt-new'

fixing app get and few issues in app create

See merge request entgra/carbon-device-mgt!8
feature/appm-store/pbac
Dharmakeerthi Lasantha 6 years ago
commit 56048907e6

@ -119,31 +119,32 @@ public class Util {
Application application = null; Application application = null;
int applicatioId; int applicatioId;
int iteration = 0; int iteration = 0;
if (rs != null) {
while (rs.next()) {
if (iteration == 0) {
application = new Application();
applicatioId = rs.getInt("APP_ID");
application.setId(applicatioId);
application.setName(rs.getString("APP_NAME"));
application.setType(rs.getString("APP_TYPE"));
application.setAppCategory(rs.getString("APP_CATEGORY"));
application.setSubType(rs.getString("SUB_TYPE"));
application.setPaymentCurrency(rs.getString("CURRENCY"));
application.setIsRestricted(rs.getBoolean("RESTRICTED"));
}
while (rs.next()) { Tag tag = new Tag();
if (iteration == 0) { tag.setTagName(rs.getString("APP_TAG"));
application = new Application(); UnrestrictedRole unrestrictedRole = new UnrestrictedRole();
applicatioId = rs.getInt("APP_ID"); unrestrictedRole.setRole(rs.getString("ROLE"));
application.setId(applicatioId); if (application.getTags().contains(tag)) {
application.setName(rs.getString("APP_NAME")); application.getTags().add(tag);
application.setType(rs.getString("APP_TYPE")); }
application.setAppCategory(rs.getString("APP_CATEGORY")); if (application.getUnrestrictedRoles().contains(unrestrictedRole)) {
application.setSubType(rs.getString("SUB_TYPE")); application.getUnrestrictedRoles().add(unrestrictedRole);
application.setPaymentCurrency(rs.getString("CURRENCY")); }
application.setIsRestricted(rs.getBoolean("RESTRICTED")); iteration++;
}
Tag tag = new Tag();
tag.setTagName(rs.getString("APP_TAG"));
UnrestrictedRole unrestrictedRole = new UnrestrictedRole();
unrestrictedRole.setRole(rs.getString("ROLE"));
if (application.getTags().contains(tag)) {
application.getTags().add(tag);
}
if (application.getUnrestrictedRoles().contains(unrestrictedRole)) {
application.getUnrestrictedRoles().add(unrestrictedRole);
} }
iteration++;
} }
return application; return application;

@ -66,7 +66,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
stmt = conn.prepareStatement("INSERT INTO AP_APP (NAME, TYPE, APP_CATEGORY, SUB_TYPE, RESTRICTED, " stmt = conn.prepareStatement("INSERT INTO AP_APP (NAME, TYPE, APP_CATEGORY, SUB_TYPE, RESTRICTED, "
+ "TENANT_ID, DM_DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + "TENANT_ID, DM_DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?, ?, ?)",
Statement.RETURN_GENERATED_KEYS); Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, application.getName()); stmt.setString(1, application.getName());
stmt.setString(2, application.getType()); stmt.setString(2, application.getType());
@ -159,7 +159,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} }
sql += " LIMIT ? OFFSET ? ORDER BY " + filter.getSortBy() + " APP_ID;"; String defaultSortOrder = "ASC";
if (filter.getSortBy() != null && !filter.getSortBy().isEmpty()) {
defaultSortOrder = filter.getSortBy();
}
sql += " ORDER BY APP_ID " + defaultSortOrder +" LIMIT ? OFFSET ? ";
pagination.setLimit(filter.getLimit()); pagination.setLimit(filter.getLimit());
pagination.setOffset(filter.getOffset()); pagination.setOffset(filter.getOffset());
@ -167,7 +171,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(paramIndex, tenantId); stmt.setInt(paramIndex++, tenantId);
stmt.setString(paramIndex++, AppLifecycleState.REMOVED.toString()); stmt.setString(paramIndex++, AppLifecycleState.REMOVED.toString());
if (filter.getAppType() != null) { if (filter.getAppType() != null) {
@ -181,7 +185,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} }
stmt.setInt(paramIndex++, filter.getLimit()); if (filter.getLimit() == 0) {
stmt.setInt(paramIndex++, 100);
} else {
stmt.setInt(paramIndex++, filter.getLimit());
}
stmt.setInt(paramIndex, filter.getOffset()); stmt.setInt(paramIndex, filter.getOffset());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
applicationList.setApplications(Util.loadApplications(rs)); applicationList.setApplications(Util.loadApplications(rs));

@ -66,7 +66,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
String sql = "INSERT INTO AP_APP_RELEASE (VERSION,TENANT_ID,UUID,RELEASE_TYPE, PACKAGE_NAME, APP_PRICE," String sql = "INSERT INTO AP_APP_RELEASE (VERSION,TENANT_ID,UUID,RELEASE_TYPE, PACKAGE_NAME, APP_PRICE,"
+ "STORED_LOCATION, BANNER_LOCATION, SC_1_LOCATION,SC_2_LOCATION,SC_3_LOCATION, APP_HASH_VALUE," + "STORED_LOCATION, BANNER_LOCATION, SC_1_LOCATION,SC_2_LOCATION,SC_3_LOCATION, APP_HASH_VALUE,"
+ "SHARED_WITH_ALL_TENANTS, APP_META_INFO,CREATED_BY,AP_APP_ID) " + "SHARED_WITH_ALL_TENANTS, APP_META_INFO,CREATED_BY,AP_APP_ID) "
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
int index = 0; int index = 0;
String generatedColumns[] = {"ID"}; String generatedColumns[] = {"ID"};
@ -80,6 +80,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
statement.setString(++index, String.valueOf(applicationRelease.getPackageName())); statement.setString(++index, String.valueOf(applicationRelease.getPackageName()));
statement.setDouble(++index, applicationRelease.getPrice()); statement.setDouble(++index, applicationRelease.getPrice());
statement.setString(++index, applicationRelease.getAppStoredLoc()); statement.setString(++index, applicationRelease.getAppStoredLoc());
statement.setString(++index, applicationRelease.getBannerLoc());
statement.setString(++index, applicationRelease.getScreenshotLoc1()); statement.setString(++index, applicationRelease.getScreenshotLoc1());
statement.setString(++index, applicationRelease.getScreenshotLoc2()); statement.setString(++index, applicationRelease.getScreenshotLoc2());
statement.setString(++index, applicationRelease.getScreenshotLoc3()); statement.setString(++index, applicationRelease.getScreenshotLoc3());

@ -50,6 +50,7 @@ import org.wso2.carbon.device.application.mgt.core.exception.ValidationException
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -105,8 +106,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
ApplicationRelease applicationRelease; ApplicationRelease applicationRelease;
List<ApplicationRelease> applicationReleases = new ArrayList<>(); List<ApplicationRelease> applicationReleases = new ArrayList<>();
try { try {
ConnectionManagerUtil.openDBConnection();
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
deviceType = this.deviceTypeDAO.getDeviceType(application.getDeviceType(), tenantId); MAMDeviceConnectorImpl mamDeviceConnector = new MAMDeviceConnectorImpl();
deviceType = mamDeviceConnector.getDeviceManagementService().getDeviceType(application.getDeviceType());
if (deviceType == null) { if (deviceType == null) {
log.error("Device type is not matched with application type"); log.error("Device type is not matched with application type");
@ -136,12 +139,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
applicationRelease = application.getApplicationReleases().get(0); applicationRelease = application.getApplicationReleases().get(0);
applicationRelease = this.applicationReleaseDAO applicationRelease = this.applicationReleaseDAO
.createRelease(applicationRelease, application.getId(), tenantId); .createRelease(applicationRelease, appId, tenantId);
LifecycleState lifecycleState = new LifecycleState(); LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState);
applicationRelease.setLifecycleState(lifecycleState); applicationRelease.setLifecycleState(lifecycleState);
applicationReleases.add(applicationRelease); applicationReleases.add(applicationRelease);
@ -152,19 +155,17 @@ public class ApplicationManagerImpl implements ApplicationManager {
return application; return application;
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device type id of " + application.getType();
log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while adding application"; String msg = "Error occurred while adding application";
log.error(msg, e); log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (DeviceManagementException e) {
e.printStackTrace();
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); //ConnectionManagerUtil.closeDBConnection(); //todo: check this again
} }
return null;
} }
@Override @Override
@ -546,14 +547,13 @@ public class ApplicationManagerImpl implements ApplicationManager {
Application application; Application application;
boolean isAppAllowed = false; boolean isAppAllowed = false;
try { try {
ConnectionManagerUtil.openDBConnection();
application = ApplicationManagementDAOFactory.getApplicationDAO() application = ApplicationManagementDAOFactory.getApplicationDAO()
.getApplicationById(applicationId, tenantId); .getApplicationById(applicationId, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
return application; return application;
} }
if (!application.getUnrestrictedRoles().isEmpty()) { if (application != null && !application.getUnrestrictedRoles().isEmpty()) {
if (isRoleExists(application.getUnrestrictedRoles(), userName)) { if (isRoleExists(application.getUnrestrictedRoles(), userName)) {
isAppAllowed = true; isAppAllowed = true;
} }
@ -569,8 +569,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"User-store exception while getting application with the " + "application id " + applicationId, e); "User-store exception while getting application with the " + "application id " + applicationId, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -707,7 +705,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state) public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state)
throws ApplicationManagementException { throws ApplicationManagementException {
try { try {
ConnectionManagerUtil.openDBConnection();
Application application = getApplicationIfAccessible(applicationId); Application application = getApplicationIfAccessible(applicationId);
ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid); ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid);
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -728,8 +725,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException("Failed to add lifecycle state", e); throw new ApplicationManagementException("Failed to add lifecycle state", e);
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
throw new ApplicationManagementException("Lifecycle State Validation failed", e); throw new ApplicationManagementException("Lifecycle State Validation failed", e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -808,10 +803,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
private Filter validateFilter(Filter filter) { private Filter validateFilter(Filter filter) {
if (filter != null) { if (filter != null) {
if (!SortingOrder.ASC.toString().equals(filter.getSortBy()) &&
!SortingOrder.DESC.toString().equals(filter.getSortBy())) {
return null;
}
if (filter.getAppType() != null) { if (filter.getAppType() != null) {
boolean isValidRequest = false; boolean isValidRequest = false;
for (ApplicationType applicationType : ApplicationType.values()) { for (ApplicationType applicationType : ApplicationType.values()) {

@ -193,6 +193,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
String artifactDirectoryPath; String artifactDirectoryPath;
String md5OfApp; String md5OfApp;
InputStream[] cloneInputStream = cloneInputStream(binaryFile);
md5OfApp = getMD5(binaryFile); md5OfApp = getMD5(binaryFile);
if (md5OfApp == null) { if (md5OfApp == null) {
@ -201,11 +202,11 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
"application UUID " + applicationRelease.getUuid()); "application UUID " + applicationRelease.getUuid());
} }
if (DeviceType.ANDROID.toString().equals(deviceType)) { if (DeviceType.ANDROID.toString().equalsIgnoreCase(deviceType)) {
ApkMeta apkMeta = ArtifactsParser.readAndroidManifestFile(binaryFile); ApkMeta apkMeta = ArtifactsParser.readAndroidManifestFile(cloneInputStream[2]);
applicationRelease.setVersion(apkMeta.getVersionName()); applicationRelease.setVersion(apkMeta.getVersionName());
applicationRelease.setPackageName(apkMeta.getPackageName()); applicationRelease.setPackageName(apkMeta.getPackageName());
} else if (DeviceType.IOS.toString().equals(deviceType)) { } else if (DeviceType.IOS.toString().equalsIgnoreCase(deviceType)) {
NSDictionary plistInfo = ArtifactsParser.readiOSManifestFile(binaryFile); NSDictionary plistInfo = ArtifactsParser.readiOSManifestFile(binaryFile);
applicationRelease applicationRelease
.setVersion(plistInfo.objectForKey(ArtifactsParser.IPA_BUNDLE_VERSION_KEY).toString()); .setVersion(plistInfo.objectForKey(ArtifactsParser.IPA_BUNDLE_VERSION_KEY).toString());
@ -223,8 +224,8 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
+ "application UUID " + applicationRelease.getUuid() + " is " + artifactDirectoryPath); + "application UUID " + applicationRelease.getUuid() + " is " + artifactDirectoryPath);
} }
String artifactPath = artifactDirectoryPath + Constants.RELEASE_ARTIFACT; String artifactPath = artifactDirectoryPath + File.separator + Constants.RELEASE_ARTIFACT +".apk";
saveFile(binaryFile, artifactPath); saveFile(cloneInputStream[1], artifactPath);
applicationRelease.setAppStoredLoc(artifactPath); applicationRelease.setAppStoredLoc(artifactPath);
applicationRelease.setAppHashValue(md5OfApp); applicationRelease.setAppHashValue(md5OfApp);
@ -241,6 +242,38 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
return applicationRelease; return applicationRelease;
} }
public InputStream[] cloneInputStream(InputStream inputStream) throws ApplicationStorageManagementException {
ByteArrayOutputStream byteArrayOutputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = inputStream.read(buffer)) > -1 ) {
byteArrayOutputStream.write(buffer, 0, len);
}
byteArrayOutputStream.flush();
InputStream stream1 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
InputStream stream2 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
InputStream stream3 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return new InputStream[]{stream1, stream2, stream3};
} catch (IOException e) {
throw new ApplicationStorageManagementException("Error occurred while cloning input stream ", e);
} finally {
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
}
}
}
}
@Override @Override
public ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, String appType, public ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, String appType,
String deviceType, InputStream binaryFile) throws ApplicationStorageManagementException, String deviceType, InputStream binaryFile) throws ApplicationStorageManagementException,

@ -226,7 +226,7 @@ public interface ApplicationManagementAPI {
@POST @POST
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes("multipart/mixed")
@ApiOperation( @ApiOperation(
consumes = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -260,7 +260,7 @@ public interface ApplicationManagementAPI {
name = "application", name = "application",
value = "The application that need to be created.", value = "The application that need to be created.",
required = true) required = true)
@Valid Application application, @Multipart("application") Application application,
@ApiParam( @ApiParam(
name = "binaryFile", name = "binaryFile",
value = "Binary file of uploading application", value = "Binary file of uploading application",
@ -277,10 +277,20 @@ public interface ApplicationManagementAPI {
required = true) required = true)
@Multipart(value = "banner") Attachment bannerFile, @Multipart(value = "banner") Attachment bannerFile,
@ApiParam( @ApiParam(
name = "screenshot", name = "screenshot1",
value = "Screen Shots of the uploading application", value = "Screen Shots of the uploading application",
required = true) required = true)
@Multipart(value = "screenshot") List<Attachment> attachmentList @Multipart(value = "screenshot1") Attachment screenshot1,
@ApiParam(
name = "screenshot2",
value = "Screen Shots of the uploading application",
required = false)
@Multipart(value = "screenshot2") Attachment screenshot2,
@ApiParam(
name = "screenshot3",
value = "Screen Shots of the uploading application",
required = false)
@Multipart(value = "screenshot3") Attachment screenshot3
); );
@DELETE @DELETE

@ -48,6 +48,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
/** /**
@ -106,13 +107,15 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} }
@POST @POST
@Consumes("application/json") @Consumes("multipart/mixed")
public Response createApplication( public Response createApplication(
@Valid Application application, @Multipart("application") Application application,
@Multipart("binaryFile") Attachment binaryFile, @Multipart("binaryFile") Attachment binaryFile,
@Multipart("icon") Attachment iconFile, @Multipart("icon") Attachment iconFile,
@Multipart("banner") Attachment bannerFile, @Multipart("banner") Attachment bannerFile,
@Multipart("screenshot") List<Attachment> attachmentList) { @Multipart("screenshot1") Attachment screenshot1,
@Multipart("screenshot2") Attachment screenshot2,
@Multipart("screenshot3") Attachment screenshot3) {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
InputStream iconFileStream; InputStream iconFileStream;
@ -120,6 +123,15 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
List<InputStream> attachments = new ArrayList<>(); List<InputStream> attachments = new ArrayList<>();
List<ApplicationRelease> applicationReleases = new ArrayList<>(); List<ApplicationRelease> applicationReleases = new ArrayList<>();
ApplicationRelease applicationRelease; ApplicationRelease applicationRelease;
List<Attachment> attachmentList = new ArrayList<>();
attachmentList.add(screenshot1);
if(screenshot2 != null) {
attachmentList.add(screenshot2);
}
if(screenshot3 != null) {
attachmentList.add(screenshot3);
}
try { try {
if (!isValidAppCreatingRequest(binaryFile, iconFile, bannerFile, attachmentList, application)) { if (!isValidAppCreatingRequest(binaryFile, iconFile, bannerFile, attachmentList, application)) {
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();

Loading…
Cancel
Save