Add application artifact dowloading API

4.x.x
lasanthaDLPDS 6 years ago
parent e1bad46558
commit c9f928915e

@ -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,26 @@ 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(); return Response.status(Response.Status.OK).entity(fileInputStream).build();
} catch (NotFoundException e) {
}catch (ApplicationManagementException e) { String msg = "Couldn't find an application release for UUID: " + uuid + " and file name: " + fileName;
String msg = "Error occurred while getting the application list for publisher "; 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) {
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;
}catch (ApplicationStorageManagementException e) { log.error(msg);
// todo throw throw new ApplicationManagementException(msg);
// throw new ApplicationManagementException(); } catch (ApplicationStorageManagementException e) {
// e.printStackTrace(); String msg = "Error occurred when getting input stream of the " + artifactName + " file.";
log.error(msg);
throw new ApplicationManagementException(msg);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
return null;
} }
} }

@ -127,8 +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()){
return null;
}
try (InputStream inputStream = new FileInputStream(filePath)){ try (InputStream inputStream = new FileInputStream(sourceFile)){
return inputStream; return inputStream;
} 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;

Loading…
Cancel
Save