forked from community/device-mgt-core
parent
c428cf1067
commit
f5feb897f9
@ -0,0 +1,68 @@
|
||||
package org.wso2.carbon.device.application.mgt.api;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
|
||||
import org.apache.cxf.jaxrs.provider.MultipartProvider;
|
||||
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
|
||||
import org.wso2.carbon.device.application.mgt.common.jaxrs.AnnotationExclusionStrategy;
|
||||
|
||||
import javax.mail.Multipart;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.ext.MessageBodyReader;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
@Provider
|
||||
@Consumes(MediaType.TEXT_PLAIN)
|
||||
public class MultipartCustomProvider implements MessageBodyReader<Object> {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Object readFrom(Class<Object> c, Type t, Annotation[] anns, MediaType mt, MultivaluedMap<String,
|
||||
String> headers, InputStream inputStream) throws IOException, WebApplicationException {
|
||||
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = inputStream.read(buffer)) != -1) {
|
||||
result.write(buffer, 0, length);
|
||||
}
|
||||
String jsonString = result.toString();
|
||||
|
||||
JsonObject obj = new JsonParser().parse(jsonString).getAsJsonObject();
|
||||
|
||||
return getGson().fromJson(obj, t);
|
||||
|
||||
}
|
||||
|
||||
private Gson getGson() {
|
||||
if (gson == null) {
|
||||
final GsonBuilder gsonBuilder = new GsonBuilder()
|
||||
.setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz")
|
||||
.setExclusionStrategies(new AnnotationExclusionStrategy());
|
||||
gson = gsonBuilder.create();
|
||||
}
|
||||
return gson;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.application.mgt.common.exception;
|
||||
|
||||
/**
|
||||
* Represents the exception thrown during storing and retrieving the artifacts.
|
||||
*/
|
||||
public class ApplicationStorageManagementException extends ApplicationManagementException{
|
||||
public ApplicationStorageManagementException(String message, Throwable ex) {
|
||||
super(message, ex);
|
||||
}
|
||||
|
||||
public ApplicationStorageManagementException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.application.mgt.common.services;
|
||||
|
||||
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This manages all the storage related requirements of Application.
|
||||
*/
|
||||
public interface ApplicationStorageManager {
|
||||
/**
|
||||
* To upload image artifacts related with an Application.
|
||||
*
|
||||
* @param applicationUUID UUID of the application
|
||||
* @param iconFile Icon File input stream
|
||||
* @param bannerFile Banner File input stream
|
||||
* @throws ApplicationStorageManagementException Application Storage Management Exception.
|
||||
*/
|
||||
public void uploadImageArtifacts(String applicationUUID, InputStream iconFile, InputStream bannerFile,
|
||||
List<InputStream> screenshots) throws ApplicationStorageManagementException;
|
||||
|
||||
/**
|
||||
* To upload release artifacts for an Application.
|
||||
* @param applicationUUID UUID of the application related with the release.
|
||||
* @param versionName Name of version of the Applcation Release.
|
||||
* @param binaryFile Binary File for the release.
|
||||
* @throws ApplicationStorageManagementException Application Storage Management Exception.
|
||||
*/
|
||||
public void uploadReleaseArtifacts(String applicationUUID, String versionName, InputStream binaryFile) throws
|
||||
ApplicationStorageManagementException;
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.application.mgt.core.config;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
/**
|
||||
* This represents Artifacts element in application-mgt configuration file.
|
||||
*/
|
||||
@XmlRootElement(name = "Artifacts")
|
||||
public class Artifacts {
|
||||
private String imageLocation;
|
||||
private String binaryLocation;
|
||||
|
||||
@XmlElement(name = "ImageLocation", required = true)
|
||||
public String getImageLocation() {
|
||||
return imageLocation;
|
||||
}
|
||||
|
||||
public void setImageLocation(String imageLocation) {
|
||||
this.imageLocation = imageLocation;
|
||||
}
|
||||
|
||||
@XmlElement(name = "BinaryLocation", required = true)
|
||||
public String getBinaryLocation() {
|
||||
return binaryLocation;
|
||||
}
|
||||
|
||||
public void setBinaryLocation(String binaryLocation) {
|
||||
this.binaryLocation = binaryLocation;
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.application.mgt.core.dao.impl.application.release;
|
||||
|
||||
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
|
||||
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl;
|
||||
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
|
||||
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* GenericApplicationReleaseDAOImpl holds the implementation of ApplicationRelease related DAO operations.
|
||||
*/
|
||||
public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements ApplicationReleaseDAO {
|
||||
|
||||
@Override
|
||||
public ApplicationRelease createRelease(ApplicationRelease applicationRelease) throws
|
||||
ApplicationManagementDAOException {
|
||||
Connection connection;
|
||||
PreparedStatement statement = null;
|
||||
ResultSet resultSet = null;
|
||||
String sql = "insert into APPM_APPLICATION_RELEASE(VERSION_NAME, RESOURCE, RELEASE_CHANNEL ,"
|
||||
+ "RELEASE_DETAILS, CREATED_AT, APPM_APPLICATION_ID, IS_DEFAULT) values (?, ?, ?, ?, ?, ?, ?)";
|
||||
int index = 0;
|
||||
boolean isBatchExecutionSupported = ConnectionManagerUtil.isBatchQuerySupported();
|
||||
|
||||
try {
|
||||
connection = this.getDBConnection();
|
||||
statement = connection.prepareStatement(sql);
|
||||
statement.setString(++index, applicationRelease.getVersionName());
|
||||
statement.setString(++index, applicationRelease.getResource());
|
||||
statement.setString(++index, String.valueOf(applicationRelease.getReleaseChannel()));
|
||||
statement.setString(++index, applicationRelease.getReleaseDetails());
|
||||
statement.setDate(++index, new Date(applicationRelease.getCreatedAt().getTime()));
|
||||
statement.setInt(++index, applicationRelease.getApplication().getId());
|
||||
statement.setBoolean(++index, applicationRelease.isDefault());
|
||||
statement.executeUpdate();
|
||||
resultSet = statement.getGeneratedKeys();
|
||||
if (resultSet.next()) {
|
||||
applicationRelease.setId(resultSet.getInt(1));
|
||||
}
|
||||
|
||||
if (applicationRelease.getProperties() != null && applicationRelease.getProperties().size() != 0) {
|
||||
sql = "INSERT INTO APPM_RELEASE_PROPERTY (PROP_KEY, PROP_VALUE, APPLICATION_RELEASE_ID) VALUES (?,?,?)";
|
||||
statement = connection.prepareStatement(sql);
|
||||
for (Object entry : applicationRelease.getProperties().entrySet()) {
|
||||
Map.Entry<String, String> property = (Map.Entry) entry;
|
||||
statement.setString(1, property.getKey());
|
||||
statement.setString(2, property.getValue());
|
||||
statement.setInt(3, applicationRelease.getId());
|
||||
if (isBatchExecutionSupported) {
|
||||
statement.addBatch();
|
||||
} else {
|
||||
statement.execute();
|
||||
}
|
||||
}
|
||||
if (isBatchExecutionSupported) {
|
||||
statement.executeBatch();
|
||||
}
|
||||
}
|
||||
return applicationRelease;
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException(
|
||||
"SQL Exception while trying to release an application (UUID : " + applicationRelease
|
||||
.getApplication().getUuid() + "), by executing the query " + sql, e);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException(
|
||||
"Database Connection Exception while trying to release the " + "applcation with UUID "
|
||||
+ applicationRelease.getApplication().getUuid(), e);
|
||||
} finally {
|
||||
Util.cleanupResources(statement, resultSet);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.application.mgt.core.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.application.mgt.common.Application;
|
||||
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;
|
||||
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
|
||||
import org.wso2.carbon.device.application.mgt.core.util.Constants;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class contains the default concrete implementation of ApplicationStorage Management.
|
||||
*/
|
||||
public class ApplicationStorageManagerImpl implements ApplicationStorageManager{
|
||||
private static final Log log = LogFactory.getLog(ApplicationStorageManagerImpl.class);
|
||||
|
||||
@Override
|
||||
public void uploadImageArtifacts(String applicationUUID, InputStream iconFileStream, InputStream bannerFileStream,
|
||||
List<InputStream> screenShotStreams) throws ApplicationStorageManagementException {
|
||||
Application application;
|
||||
try {
|
||||
application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID);
|
||||
} catch (ApplicationManagementException e) {
|
||||
throw new ApplicationStorageManagementException("Exception while retrieving the application details for "
|
||||
+ "the application with UUID " + applicationUUID);
|
||||
}
|
||||
if (application == null) {
|
||||
throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not "
|
||||
+ "exist. Cannot upload the artifacts to non-existing application.");
|
||||
}
|
||||
String artifactDirectoryPath = Constants.ARTIFACT_PATH + application.getId();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Artifact Directory Path for saving the artifacts related with application " + applicationUUID
|
||||
+ " is " + artifactDirectoryPath);
|
||||
}
|
||||
createArtifactDirectory(artifactDirectoryPath);
|
||||
if (iconFileStream != null) {
|
||||
String iconName = application.getIconName();
|
||||
iconName = (iconName == null) ? "icon" : iconName;
|
||||
try {
|
||||
saveFile(iconFileStream, artifactDirectoryPath + File.separator + iconName);
|
||||
} catch (IOException e) {
|
||||
throw new ApplicationStorageManagementException(
|
||||
"IO Exception while saving the icon file in the server for " + "the application "
|
||||
+ applicationUUID, e);
|
||||
}
|
||||
}
|
||||
if (bannerFileStream != null) {
|
||||
String bannerName = application.getBannerName();
|
||||
bannerName = (bannerName == null) ? "banner" : bannerName;
|
||||
try {
|
||||
saveFile(bannerFileStream, artifactDirectoryPath + File.separator + bannerName);
|
||||
} catch (IOException e) {
|
||||
throw new ApplicationStorageManagementException(
|
||||
"IO Exception while saving the banner file in the server for" + " the application "
|
||||
+ applicationUUID, e);
|
||||
}
|
||||
}
|
||||
if (screenShotStreams != null) {
|
||||
int count = 1;
|
||||
String screenshotName;
|
||||
List<String> screenShotNames = application.getScreenshots();
|
||||
boolean isScreenShotNameExist = (screenShotNames == null || screenShotNames.isEmpty());
|
||||
int screenShotNameLength = isScreenShotNameExist ? screenShotNames.size() : 0;
|
||||
for (InputStream screenshotStream : screenShotStreams) {
|
||||
try {
|
||||
if (isScreenShotNameExist && count <= screenShotNameLength) {
|
||||
screenshotName = screenShotNames.get(count);
|
||||
} else {
|
||||
screenshotName = "screenshot_" + count;
|
||||
}
|
||||
saveFile(screenshotStream, artifactDirectoryPath + File.separator + screenshotName);
|
||||
count++;
|
||||
} catch (IOException e) {
|
||||
throw new ApplicationStorageManagementException(
|
||||
"IO Exception while saving the screens hots for the " + "application " + applicationUUID,
|
||||
e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadReleaseArtifacts(String applicationUUID, String versionName, InputStream binaryFile)
|
||||
throws ApplicationStorageManagementException {
|
||||
Application application;
|
||||
try {
|
||||
application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID);
|
||||
} catch (ApplicationManagementException e) {
|
||||
throw new ApplicationStorageManagementException("Exception while retrieving the application details for "
|
||||
+ "the application with UUID " + applicationUUID);
|
||||
}
|
||||
if (application == null) {
|
||||
throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not "
|
||||
+ "exist. Cannot upload release artifacts for not existing application.");
|
||||
}
|
||||
String artifactDirectoryPath = Constants.ARTIFACT_PATH + application.getId();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Artifact Directory Path for saving the application release related artifacts related with "
|
||||
+ "application " + applicationUUID + " is " + artifactDirectoryPath);
|
||||
}
|
||||
|
||||
createArtifactDirectory(artifactDirectoryPath);
|
||||
if (binaryFile != null) {
|
||||
try {
|
||||
saveFile(binaryFile, artifactDirectoryPath + File.separator + versionName);
|
||||
} catch (IOException e) {
|
||||
throw new ApplicationStorageManagementException(
|
||||
"IO Exception while saving the release artifacts in the server for the application "
|
||||
+ applicationUUID, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* To save a file in a given location.
|
||||
*
|
||||
* @param inputStream Stream of the file.
|
||||
* @param path Path the file need to be saved in.
|
||||
*/
|
||||
private void saveFile(InputStream inputStream, String path) throws IOException {
|
||||
OutputStream outStream = null;
|
||||
try {
|
||||
byte[] buffer = new byte[inputStream.available()];
|
||||
inputStream.read(buffer);
|
||||
outStream = new FileOutputStream(new File(path));
|
||||
outStream.write(buffer);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
if (outStream != null) {
|
||||
outStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is responsible for creating artifact parent directories in the given path.
|
||||
*
|
||||
* @param artifactDirectoryPath Path for the artifact directory.
|
||||
* @throws ApplicationStorageManagementException Application Storage Management Exception.
|
||||
*/
|
||||
private void createArtifactDirectory(String artifactDirectoryPath) throws ApplicationStorageManagementException {
|
||||
File artifactDirectory = new File(artifactDirectoryPath);
|
||||
|
||||
if (!artifactDirectory.exists()) {
|
||||
if (!artifactDirectory.mkdirs()) {
|
||||
throw new ApplicationStorageManagementException(
|
||||
"Cannot create directories in the path to save the application related artifacts");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue