Add application artifact dowloading API

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent e1bad46558
commit c9f928915e

@ -52,12 +52,12 @@ import javax.ws.rs.core.Response;
)
)
@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)
public interface ArtifactDownloadAPI {
@GET
@Path("/download-artifact/{uuid}/{fileName}")
@Path("/{uuid}/{fileName}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@ApiOperation(
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.LogFactory;
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.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 javax.ws.rs.GET;
@ -31,6 +32,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
/**
* Implementation of ApplicationDTO Management related APIs.
@ -44,20 +46,26 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI {
@GET
@Override
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/download-artifact/{uuid}/{fileName}")
public Response getArtifact(
@PathParam("uuid") String uuid,
@Path("/{uuid}/{fileName}")
public Response getArtifact(@PathParam("uuid") String uuid,
@PathParam("fileName") String fileName) {
AppmDataHandler dataHandler = APIUtil.getDataHandler();
try {
UIConfiguration uiConfiguration = dataHandler.getUIConfiguration();
return Response.status(Response.Status.OK).entity(uiConfiguration).build();
InputStream fileInputStream = dataHandler.getArtifactStream(uuid, fileName);
return Response.status(Response.Status.OK).entity(fileInputStream).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) {
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);
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.services.AppmDataHandler;
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.GET;
@ -46,15 +45,8 @@ public class ConfigRetrieveAPIImpl implements ConfigRetrieveAPI {
@Path("/ui-config")
public Response getUiConfig() {
AppmDataHandler dataHandler = APIUtil.getDataHandler();
try {
UIConfiguration uiConfiguration = dataHandler.getUIConfiguration();
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
*
* @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;
PreparedStatement stmt = null;
ResultSet rs = null;
ApplicationReleaseArtifactPaths applicationReleaseArtifactPaths = null;
String releaseHashValue = null;
try {
conn = this.getDBConnection();
String sql = "SELECT "
+ "AR.APP_HASH_VALUE AS HASH_VALUE "
+ "FROM AP_APP_RELEASE "
+ "FROM AP_APP_RELEASE AR "
+ "WHERE AR.UUID = ? AND AR.TENANT_ID = ?;";
stmt = conn.prepareStatement(sql);

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

@ -17,9 +17,9 @@
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.device.application.mgt.common.ApplicationReleaseArtifactPaths;
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.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.Util;
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 java.io.InputStream;
@ -37,42 +38,54 @@ import java.io.InputStream;
public class AppmDataHandlerImpl implements AppmDataHandler {
private UIConfiguration uiConfiguration;
private static final Log log = LogFactory.getLog(AppmDataHandlerImpl.class);
public AppmDataHandlerImpl(UIConfiguration config) {
this.uiConfiguration = config;
}
@Override
public UIConfiguration getUIConfiguration() throws ApplicationManagementException {
public UIConfiguration getUIConfiguration() {
return this.uiConfiguration;
}
@Override
// throws ApplicationManagementException
public InputStream getArtifactStream(String uuid, String artifactName) {
public InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager();
ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO();
String artifactPath;
String appReleaseHashValue = null;
String appReleaseHashValue;
try {
ConnectionManagerUtil.openDBConnection();
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;
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) {
// todo throw
// throw new ApplicationManagementException();
// e.printStackTrace();
String msg =
"Error occurred when retrieving application release hash value for given application release UUID: "
+ uuid;
log.error(msg);
throw new ApplicationManagementException(msg);
} catch (ApplicationStorageManagementException e) {
// todo throw
// throw new ApplicationManagementException();
// 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 {
File sourceFile = new File(filePath);
if (!sourceFile.exists()){
return null;
}
try (InputStream inputStream = new FileInputStream(filePath)){
try (InputStream inputStream = new FileInputStream(sourceFile)){
return inputStream;
} catch (FileNotFoundException e) {
String msg = "Couldn't file the file in file path: " + filePath;

Loading…
Cancel
Save