Merge branch 'application-mgt-new' of https://gitlab.com/tcdlpds/carbon-device-mgt into application-mgt-new

feature/appm-store/pbac
Jayasanka 6 years ago
commit 0095d188b7

@ -52,12 +52,12 @@ import javax.ws.rs.core.Response;
) )
) )
@Path("/artifact") @Path("/artifact")
@Api(value = "ApplicationDTO Management Artifact Downloading Service", description = "This API carries all application management artifact downloading services") @Api(value = "ApplicationDTO Management Artifact Downloading Service")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public interface ArtifactDownloadAPI { public interface ArtifactDownloadAPI {
@GET @GET
@Path("/download-artifact/{uuid}/{fileName}") @Path("/{uuid}/{fileName}")
@Produces(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_OCTET_STREAM)
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_OCTET_STREAM, produces = MediaType.APPLICATION_OCTET_STREAM,

@ -20,9 +20,10 @@ package org.wso2.carbon.device.application.mgt.api.services.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.api.services.ArtifactDownloadAPI; import org.wso2.carbon.device.application.mgt.api.services.ArtifactDownloadAPI;
import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler;
import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -31,6 +32,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.InputStream;
/** /**
* Implementation of ApplicationDTO Management related APIs. * Implementation of ApplicationDTO Management related APIs.
@ -44,20 +46,31 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI {
@GET @GET
@Override @Override
@Produces(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/download-artifact/{uuid}/{fileName}") @Path("/{uuid}/{fileName}")
public Response getArtifact( public Response getArtifact(@PathParam("uuid") String uuid,
@PathParam("uuid") String uuid,
@PathParam("fileName") String fileName) { @PathParam("fileName") String fileName) {
AppmDataHandler dataHandler = APIUtil.getDataHandler(); AppmDataHandler dataHandler = APIUtil.getDataHandler();
try { try {
UIConfiguration uiConfiguration = dataHandler.getUIConfiguration(); InputStream fileInputStream = dataHandler.getArtifactStream(uuid, fileName);
return Response.status(Response.Status.OK).entity(uiConfiguration).build(); Response.ResponseBuilder response = Response
.ok(fileInputStream, MediaType.APPLICATION_OCTET_STREAM);
response.status(Response.Status.OK);
// response.type("application/html");
response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
return response.build();
} catch (NotFoundException e) {
String msg = "Couldn't find an application release for UUID: " + uuid + " and file name: " + fileName;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (BadRequestException e) {
String msg = "Invalid data is used with the request to get input stream of the application release. UUID: "
+ uuid + " and file name: " + fileName;
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while getting the application list for publisher "; String msg = "Error occurred while getting the application release artifact file. ";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} }
} }
} }

@ -23,7 +23,6 @@ import org.wso2.carbon.device.application.mgt.api.services.ConfigRetrieveAPI;
import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration;
import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler;
import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -46,15 +45,8 @@ public class ConfigRetrieveAPIImpl implements ConfigRetrieveAPI {
@Path("/ui-config") @Path("/ui-config")
public Response getUiConfig() { public Response getUiConfig() {
AppmDataHandler dataHandler = APIUtil.getDataHandler(); AppmDataHandler dataHandler = APIUtil.getDataHandler();
try {
UIConfiguration uiConfiguration = dataHandler.getUIConfiguration(); UIConfiguration uiConfiguration = dataHandler.getUIConfiguration();
return Response.status(Response.Status.OK).entity(uiConfiguration).build(); return Response.status(Response.Status.OK).entity(uiConfiguration).build();
}catch (ApplicationManagementException e) {
String msg = "Error occurred while getting the application list for publisher ";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
} }
} }

@ -27,9 +27,8 @@ public interface AppmDataHandler {
* Get UI configuration which is defined in the app-manager.xml * Get UI configuration which is defined in the app-manager.xml
* *
* @return {@link UIConfiguration} UI configuration * @return {@link UIConfiguration} UI configuration
* @throws ApplicationManagementException Exceptions of the ApplicationDTO management.
*/ */
UIConfiguration getUIConfiguration() throws ApplicationManagementException; UIConfiguration getUIConfiguration();
InputStream getArtifactStream(String md5sum, String artifactName) throws ApplicationManagementException; InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException;
} }

@ -663,13 +663,12 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
ApplicationReleaseArtifactPaths applicationReleaseArtifactPaths = null;
String releaseHashValue = null; String releaseHashValue = null;
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
String sql = "SELECT " String sql = "SELECT "
+ "AR.APP_HASH_VALUE AS HASH_VALUE " + "AR.APP_HASH_VALUE AS HASH_VALUE "
+ "FROM AP_APP_RELEASE " + "FROM AP_APP_RELEASE AR "
+ "WHERE AR.UUID = ? AND AR.TENANT_ID = ?;"; + "WHERE AR.UUID = ? AND AR.TENANT_ID = ?;";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);

@ -1947,6 +1947,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
String basePath = artifactDownloadEndpoint + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid(); String basePath = artifactDownloadEndpoint + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid();
ApplicationRelease applicationRelease = new ApplicationRelease(); ApplicationRelease applicationRelease = new ApplicationRelease();
applicationRelease.setDescription(applicationReleaseDTO.getDescription()); applicationRelease.setDescription(applicationReleaseDTO.getDescription());
applicationRelease.setUuid(applicationReleaseDTO.getUuid());
applicationRelease.setReleaseType(applicationReleaseDTO.getReleaseType()); applicationRelease.setReleaseType(applicationReleaseDTO.getReleaseType());
applicationRelease.setPrice(applicationReleaseDTO.getPrice()); applicationRelease.setPrice(applicationReleaseDTO.getPrice());
applicationRelease.setIsSharedWithAllTenants(applicationReleaseDTO.getIsSharedWithAllTenants()); applicationRelease.setIsSharedWithAllTenants(applicationReleaseDTO.getIsSharedWithAllTenants());

@ -17,9 +17,9 @@
package org.wso2.carbon.device.application.mgt.core.impl; package org.wso2.carbon.device.application.mgt.core.impl;
import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.ApplicationReleaseArtifactPaths;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
@ -29,7 +29,8 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.Constants;
import java.io.InputStream; import java.io.InputStream;
@ -37,42 +38,54 @@ import java.io.InputStream;
public class AppmDataHandlerImpl implements AppmDataHandler { public class AppmDataHandlerImpl implements AppmDataHandler {
private UIConfiguration uiConfiguration; private UIConfiguration uiConfiguration;
private static final Log log = LogFactory.getLog(AppmDataHandlerImpl.class);
public AppmDataHandlerImpl(UIConfiguration config) { public AppmDataHandlerImpl(UIConfiguration config) {
this.uiConfiguration = config; this.uiConfiguration = config;
} }
@Override @Override
public UIConfiguration getUIConfiguration() throws ApplicationManagementException { public UIConfiguration getUIConfiguration() {
return this.uiConfiguration; return this.uiConfiguration;
} }
@Override @Override
// throws ApplicationManagementException public InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException {
public InputStream getArtifactStream(String uuid, String artifactName) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager();
ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO();
String artifactPath; String artifactPath;
String appReleaseHashValue;
String appReleaseHashValue = null;
try { try {
ConnectionManagerUtil.openDBConnection();
appReleaseHashValue = applicationReleaseDAO.getReleaseHashValue(uuid, tenantId); appReleaseHashValue = applicationReleaseDAO.getReleaseHashValue(uuid, tenantId);
if (appReleaseHashValue == null) {
String msg = "Could't find application release for UUID: " + uuid + ". Hence try with valid UUID.";
log.error(msg);
throw new NotFoundException(msg);
}
artifactPath = appReleaseHashValue + Constants.FORWARD_SLASH + artifactName; artifactPath = appReleaseHashValue + Constants.FORWARD_SLASH + artifactName;
return applicationStorageManager.getFileSttream(artifactPath); InputStream inputStream = applicationStorageManager.getFileSttream(artifactPath);
if (inputStream == null) {
String msg = "Couldn't file the file in the file system. File path: " + artifactPath;
log.error(msg);
throw new ApplicationManagementException(msg);
}
return inputStream;
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
// todo throw String msg =
// throw new ApplicationManagementException(); "Error occurred when retrieving application release hash value for given application release UUID: "
// e.printStackTrace(); + uuid;
log.error(msg);
throw new ApplicationManagementException(msg);
} catch (ApplicationStorageManagementException e) { } catch (ApplicationStorageManagementException e) {
// todo throw String msg = "Error occurred when getting input stream of the " + artifactName + " file.";
// throw new ApplicationManagementException(); log.error(msg);
// e.printStackTrace(); throw new ApplicationManagementException(msg);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
return null;
} }
} }

@ -127,9 +127,11 @@ public class StorageManagementUtil {
*/ */
public static InputStream getInputStream (String filePath) throws IOException { public static InputStream getInputStream (String filePath) throws IOException {
File sourceFile = new File(filePath); File sourceFile = new File(filePath);
if (!sourceFile.exists()){
try (InputStream inputStream = new FileInputStream(filePath)){ return null;
return inputStream; }
try {
return new FileInputStream(sourceFile);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
String msg = "Couldn't file the file in file path: " + filePath; String msg = "Couldn't file the file in file path: " + filePath;
log.error(msg); log.error(msg);

@ -207,7 +207,11 @@ public class HandlerUtil {
resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8");
proxyResponse.setExecutorResponse(null); proxyResponse.setExecutorResponse(null);
try (PrintWriter writer = resp.getWriter()) { try (PrintWriter writer = resp.getWriter()) {
writer.write(gson.toJson(proxyResponse)); if (proxyResponse.getCode() == HttpStatus.SC_OK){
writer.write(gson.toJson(proxyResponse.getData()));
} else{
writer.write(proxyResponse.getData());
}
} }
} }

Loading…
Cancel
Save