Add stream base file uploading #338

Merged
pahansith merged 2 commits from rajitha/device-mgt-core:stream-api into master 8 months ago

@ -0,0 +1,52 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.common;
import java.io.InputStream;
public class ChunkDescriptor {
private FileDescriptor associateFileDescriptor;
private long size;
private InputStream chunk;
public FileDescriptor getAssociateFileDescriptor() {
return associateFileDescriptor;
}
public void setAssociateFileDescriptor(FileDescriptor associateFileDescriptor) {
this.associateFileDescriptor = associateFileDescriptor;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public InputStream getChunk() {
return chunk;
}
public void setChunk(InputStream chunk) {
this.chunk = chunk;
}
}

@ -0,0 +1,79 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.common;
import java.io.InputStream;
public class FileDescriptor {
private String fileName;
private String extension;
private String fullQualifiedName;
private String absolutePath;
private long actualFileSize;
private InputStream file;
public InputStream getFile() {
return file;
}
public void setFile(InputStream file) {
this.file = file;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public String getAbsolutePath() {
return absolutePath;
}
public void setAbsolutePath(String absolutePath) {
this.absolutePath = absolutePath;
}
public long getActualFileSize() {
return actualFileSize;
}
public void setActualFileSize(long actualFileSize) {
this.actualFileSize = actualFileSize;
}
public String getFullQualifiedName() {
return fullQualifiedName;
}
public void setFullQualifiedName(String fullQualifiedName) {
this.fullQualifiedName = fullQualifiedName;
}
}

@ -0,0 +1,60 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.common;
public class FileMetaEntry {
private String fileName;
private String extension;
private long size;
private String absolutePath;
public String getAbsolutePath() {
return absolutePath;
}
public void setAbsolutePath(String absolutePath) {
this.absolutePath = absolutePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
}

@ -0,0 +1,108 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.common;
import java.util.Objects;
public class TransferLink {
private static final String SCHEMA_SEPARATOR = "://";
private static final String URL_SEPARATOR = "/";
private static final String COLON = ":";
private final String schema;
private final String host;
private final String port;
private final String endpoint;
private final String artifactHolderUUID;
private TransferLink(String schema, String host, String port, String endpoint, String artifactHolderUUID) {
this.schema = schema;
this.host = host;
this.port = port;
this.endpoint = endpoint;
this.artifactHolderUUID = artifactHolderUUID;
}
public String getDirectTransferLink() {
return schema + SCHEMA_SEPARATOR + host + COLON + port + URL_SEPARATOR + endpoint + URL_SEPARATOR + artifactHolderUUID;
}
public String getRelativeTransferLink() {
return endpoint + URL_SEPARATOR + artifactHolderUUID;
}
@Override
public String toString() {
return getDirectTransferLink();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TransferLink that = (TransferLink) o;
return Objects.equals(schema, that.schema) && Objects.equals(host, that.host) && Objects.equals(port, that.port)
&& Objects.equals(endpoint, that.endpoint) && Objects.equals(artifactHolderUUID, that.artifactHolderUUID);
}
@Override
public int hashCode() {
return Objects.hash(schema, host, port, endpoint, artifactHolderUUID);
}
public static class TransferLinkBuilder {
private static final String DEFAULT_SCHEMA = "https";
private static final String ENDPOINT = "application-mgt-publisher/v1.0/applications/uploads";
private static final String IOT_GW_HOST_ENV_VAR = "iot.gateway.host";
private static final String IOT_GW_HTTPS_PORT_ENV_VAR = "iot.gateway.https.port";
private static final String IOT_GW_HTTP_PORT_ENV_VAR = "iot.gateway.http.port";
private String schema;
private String endpoint;
private final String artifactHolderUUID;
public TransferLinkBuilder(String artifactHolderUUID) {
this.schema = DEFAULT_SCHEMA;
this.endpoint = ENDPOINT;
this.artifactHolderUUID = artifactHolderUUID;
}
public TransferLinkBuilder withSchema(String schema) {
this.schema = schema;
return this;
}
public TransferLinkBuilder withEndpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}
public TransferLink build() {
return new TransferLink(this.schema, resolveHost(), resolvePort(), this.endpoint, this.artifactHolderUUID);
}
private String resolveHost() {
return System.getProperty(IOT_GW_HOST_ENV_VAR);
}
private String resolvePort() {
return Objects.equals(this.schema, DEFAULT_SCHEMA) ? System.getProperty(IOT_GW_HTTPS_PORT_ENV_VAR)
: System.getProperty(IOT_GW_HTTP_PORT_ENV_VAR);
}
}
}

@ -0,0 +1,30 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.common.exception;
public class FileDownloaderServiceException extends Exception {
public FileDownloaderServiceException(String msg) {
super(msg);
}
public FileDownloaderServiceException(String msg, Throwable t) {
super(msg, t);
}
}

@ -0,0 +1,31 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.common.exception;
public class FileTransferServiceException extends Exception {
public FileTransferServiceException(String msg) {
super(msg);
}
public FileTransferServiceException(String msg, Throwable throwable) {
super(msg, throwable);
}
}

@ -371,11 +371,9 @@ public interface ApplicationManager {
* *
* @param releaseUuid UUID of the application release. * @param releaseUuid UUID of the application release.
* @param entAppReleaseWrapper {@link ApplicationReleaseDTO} * @param entAppReleaseWrapper {@link ApplicationReleaseDTO}
* @param applicationArtifact {@link ApplicationArtifact}
* @return If the application release is updated correctly True returns, otherwise retuen False * @return If the application release is updated correctly True returns, otherwise retuen False
*/ */
ApplicationRelease updateEntAppRelease(String releaseUuid, EntAppReleaseWrapper entAppReleaseWrapper, ApplicationRelease updateEntAppRelease(String releaseUuid, EntAppReleaseWrapper entAppReleaseWrapper) throws ApplicationManagementException;
ApplicationArtifact applicationArtifact) throws ApplicationManagementException;
/** /**
@ -383,33 +381,27 @@ public interface ApplicationManager {
* *
* @param releaseUuid UUID of the application release. * @param releaseUuid UUID of the application release.
* @param publicAppReleaseWrapper {@link ApplicationReleaseDTO} * @param publicAppReleaseWrapper {@link ApplicationReleaseDTO}
* @param applicationArtifact {@link ApplicationArtifact}
* @return If the application release is updated correctly True returns, otherwise retuen False * @return If the application release is updated correctly True returns, otherwise retuen False
*/ */
ApplicationRelease updatePubAppRelease(String releaseUuid, PublicAppReleaseWrapper publicAppReleaseWrapper, ApplicationRelease updatePubAppRelease(String releaseUuid, PublicAppReleaseWrapper publicAppReleaseWrapper) throws ApplicationManagementException;
ApplicationArtifact applicationArtifact) throws ApplicationManagementException;
/** /**
* Use to update existing web app release * Use to update existing web app release
* *
* @param releaseUuid UUID of the application release. * @param releaseUuid UUID of the application release.
* @param webAppReleaseWrapper {@link ApplicationReleaseDTO} * @param webAppReleaseWrapper {@link ApplicationReleaseDTO}
* @param applicationArtifact {@link ApplicationArtifact}
* @return If the application release is updated correctly True returns, otherwise retuen False * @return If the application release is updated correctly True returns, otherwise retuen False
*/ */
ApplicationRelease updateWebAppRelease(String releaseUuid, WebAppReleaseWrapper webAppReleaseWrapper, ApplicationRelease updateWebAppRelease(String releaseUuid, WebAppReleaseWrapper webAppReleaseWrapper) throws ApplicationManagementException;
ApplicationArtifact applicationArtifact) throws ApplicationManagementException;
/** /**
* Use to update existing custom app release * Use to update existing custom app release
* *
* @param releaseUuid UUID of the application release. * @param releaseUuid UUID of the application release.
* @param customAppReleaseWrapper {@link ApplicationReleaseDTO} * @param customAppReleaseWrapper {@link ApplicationReleaseDTO}
* @param applicationArtifact {@link ApplicationArtifact}
* @return If the application release is updated correctly True returns, otherwise retuen False * @return If the application release is updated correctly True returns, otherwise retuen False
*/ */
ApplicationRelease updateCustomAppRelease(String releaseUuid, CustomAppReleaseWrapper customAppReleaseWrapper, ApplicationRelease updateCustomAppRelease(String releaseUuid, CustomAppReleaseWrapper customAppReleaseWrapper) throws ApplicationManagementException;
ApplicationArtifact applicationArtifact) throws ApplicationManagementException;
/** /**
* To validate the application creating request * To validate the application creating request

@ -0,0 +1,29 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.common.services;
import io.entgra.device.mgt.core.application.mgt.common.FileDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.exception.FileDownloaderServiceException;
import java.net.URL;
public interface FileDownloaderService {
FileDescriptor download(URL downloadUrl) throws FileDownloaderServiceException;
}

@ -0,0 +1,73 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.common.services;
import io.entgra.device.mgt.core.application.mgt.common.ChunkDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.FileDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.FileMetaEntry;
import io.entgra.device.mgt.core.application.mgt.common.TransferLink;
import io.entgra.device.mgt.core.application.mgt.common.exception.FileTransferServiceException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.NotFoundException;
import java.io.InputStream;
import java.net.URL;
public interface FileTransferService {
/**
* Create an upload link
* @param fileMetaEntry {@link FileMetaEntry}
* @return {@link TransferLink}
* @throws FileTransferServiceException Throws when error encountered while generating upload link
*/
TransferLink generateUploadLink(FileMetaEntry fileMetaEntry) throws FileTransferServiceException;
/**
* Resolve {@link ChunkDescriptor} using artifactHolder UUID and a given chunk
* @param artifactHolder Artifact holder's UUID string
* @param chunk Data chunk
* @return {@link ChunkDescriptor}
* @throws FileTransferServiceException Throws when error encountered while resolving chunk descriptor
* @throws NotFoundException Throws when artifact holder not exists in the file system
*/
ChunkDescriptor resolve(String artifactHolder, InputStream chunk) throws FileTransferServiceException, NotFoundException;
/**
* Write chunk of data
* @param chunkDescriptor {@link ChunkDescriptor}
* @throws FileTransferServiceException Throws when error encountered while writing chunk
*/
void writeChunk(ChunkDescriptor chunkDescriptor) throws FileTransferServiceException;
/**
* Check if the provided download url point to a file which exists on the local env or not
* @param downloadUrl Download URL
* @return Returns true if the download URL point to a file which resides in local
* @throws FileTransferServiceException Throws when error encountered while checking
*/
boolean isExistsOnLocal(URL downloadUrl) throws FileTransferServiceException;
/**
* Resolve {@link FileDescriptor} from a given download URL
* @param downloadUrl Download URL
* @return {@link java.io.FileDescriptor}
* @throws FileTransferServiceException Throws when error encountered while resolving file descriptor
*/
FileDescriptor resolve(URL downloadUrl) throws FileTransferServiceException;
}

@ -83,6 +83,44 @@ public class CustomAppReleaseWrapper {
@ApiModelProperty(name = "icon", @ApiModelProperty(name = "icon",
value = "banner of the application") value = "banner of the application")
private Base64File banner; private Base64File banner;
private boolean remoteStatus;
public boolean isRemoteStatus() {
return remoteStatus;
}
public void setRemoteStatus(boolean remoteStatus) {
this.remoteStatus = remoteStatus;
}
private String artifactLink;
private List<String> screenshotLinks;
private String iconLink;
private String bannerLink;
public String getArtifactLink() {
return artifactLink;
}
public void setArtifactLink(String artifactLink) {
this.artifactLink = artifactLink;
}
public List<String> getScreenshotLinks() {
return screenshotLinks;
}
public void setScreenshotLinks(List<String> screenshotLinks) {
this.screenshotLinks = screenshotLinks;
}
public String getIconLink() {
return iconLink;
}
public void setIconLink(String iconLink) {
this.iconLink = iconLink;
}
public String getReleaseType() { public String getReleaseType() {
return releaseType; return releaseType;
@ -173,4 +211,12 @@ public class CustomAppReleaseWrapper {
public void setBanner(Base64File banner) { public void setBanner(Base64File banner) {
this.banner = banner; this.banner = banner;
} }
public String getBannerLink() {
return bannerLink;
}
public void setBannerLink(String bannerLink) {
this.bannerLink = bannerLink;
}
} }

@ -86,6 +86,44 @@ public class EntAppReleaseWrapper {
@ApiModelProperty(name = "icon", @ApiModelProperty(name = "icon",
value = "banner of the application") value = "banner of the application")
private Base64File banner; private Base64File banner;
private boolean remoteStatus;
public boolean isRemoteStatus() {
return remoteStatus;
}
public void setRemoteStatus(boolean remoteStatus) {
this.remoteStatus = remoteStatus;
}
private String artifactLink;
private List<String> screenshotLinks;
private String iconLink;
private String bannerLink;
public String getArtifactLink() {
return artifactLink;
}
public void setArtifactLink(String artifactLink) {
this.artifactLink = artifactLink;
}
public List<String> getScreenshotLinks() {
return screenshotLinks;
}
public void setScreenshotLinks(List<String> screenshotLinks) {
this.screenshotLinks = screenshotLinks;
}
public String getIconLink() {
return iconLink;
}
public void setIconLink(String iconLink) {
this.iconLink = iconLink;
}
public String getReleaseType() { public String getReleaseType() {
return releaseType; return releaseType;
@ -174,4 +212,12 @@ public class EntAppReleaseWrapper {
public void setBanner(Base64File banner) { public void setBanner(Base64File banner) {
this.banner = banner; this.banner = banner;
} }
public String getBannerLink() {
return bannerLink;
}
public void setBannerLink(String bannerLink) {
this.bannerLink = bannerLink;
}
} }

@ -86,6 +86,18 @@ public class PublicAppReleaseWrapper {
@ApiModelProperty(name = "icon", @ApiModelProperty(name = "icon",
value = "banner of the application") value = "banner of the application")
private Base64File banner; private Base64File banner;
private boolean remoteStatus;
public boolean isRemoteStatus() {
return remoteStatus;
}
public void setRemoteStatus(boolean remoteStatus) {
this.remoteStatus = remoteStatus;
}
private List<String> screenshotLinks;
private String iconLink;
private String bannerLink;
public String getReleaseType() { public String getReleaseType() {
return releaseType; return releaseType;
@ -162,4 +174,28 @@ public class PublicAppReleaseWrapper {
public void setBanner(Base64File banner) { public void setBanner(Base64File banner) {
this.banner = banner; this.banner = banner;
} }
public List<String> getScreenshotLinks() {
return screenshotLinks;
}
public void setScreenshotLinks(List<String> screenshotLinks) {
this.screenshotLinks = screenshotLinks;
}
public String getIconLink() {
return iconLink;
}
public void setIconLink(String iconLink) {
this.iconLink = iconLink;
}
public String getBannerLink() {
return bannerLink;
}
public void setBannerLink(String bannerLink) {
this.bannerLink = bannerLink;
}
} }

@ -0,0 +1,41 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.common.wrapper;
public class TransferLinkWrapper {
private String directTransferLink;
private String relativeTransferLink;
public String getDirectTransferLink() {
return directTransferLink;
}
public void setDirectTransferLink(String directTransferLink) {
this.directTransferLink = directTransferLink;
}
public String getRelativeTransferLink() {
return relativeTransferLink;
}
public void setRelativeTransferLink(String relativeTransferLink) {
this.relativeTransferLink = relativeTransferLink;
}
}

@ -77,6 +77,42 @@ public class WebAppReleaseWrapper {
@ApiModelProperty(name = "icon", @ApiModelProperty(name = "icon",
value = "banner of the application") value = "banner of the application")
private Base64File banner; private Base64File banner;
private boolean remoteStatus;
public boolean isRemoteStatus() {
return remoteStatus;
}
public void setRemoteStatus(boolean remoteStatus) {
this.remoteStatus = remoteStatus;
}
private List<String> screenshotLinks;
private String iconLink;
private String bannerLink;
public List<String> getScreenshotLinks() {
return screenshotLinks;
}
public void setScreenshotLinks(List<String> screenshotLinks) {
this.screenshotLinks = screenshotLinks;
}
public String getIconLink() {
return iconLink;
}
public void setIconLink(String iconLink) {
this.iconLink = iconLink;
}
public String getBannerLink() {
return bannerLink;
}
public void setBannerLink(String bannerLink) {
this.bannerLink = bannerLink;
}
public String getReleaseType() { public String getReleaseType() {
return releaseType; return releaseType;

@ -105,6 +105,9 @@
<systemPropertyVariables> <systemPropertyVariables>
<jacoco-agent.destfile>${basedir}/target/coverage-reports/jacoco-unit.exec</jacoco-agent.destfile> <jacoco-agent.destfile>${basedir}/target/coverage-reports/jacoco-unit.exec</jacoco-agent.destfile>
<log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration> <log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>
<iot.gateway.http.port>8280</iot.gateway.http.port>
<iot.gateway.https.port>8280</iot.gateway.https.port>
<iot.gateway.host>test</iot.gateway.host>
</systemPropertyVariables> </systemPropertyVariables>
</configuration> </configuration>
</plugin> </plugin>
@ -112,7 +115,6 @@
</build> </build>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
@ -367,6 +369,21 @@
<artifactId>org.wso2.carbon.ntask.core</artifactId> <artifactId>org.wso2.carbon.ntask.core</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -0,0 +1,30 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.core.exception;
public class FileTransferServiceHelperUtilException extends Exception {
public FileTransferServiceHelperUtilException(String msg) {
super(msg);
}
public FileTransferServiceHelperUtilException(String msg, Throwable t) {
super(msg, t);
}
}

@ -18,11 +18,14 @@
package io.entgra.device.mgt.core.application.mgt.core.impl; package io.entgra.device.mgt.core.application.mgt.core.impl;
import io.entgra.device.mgt.core.application.mgt.common.exception.FileDownloaderServiceException;
import io.entgra.device.mgt.core.application.mgt.common.exception.FileTransferServiceException;
import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException;
import io.entgra.device.mgt.core.device.mgt.common.Base64File; import io.entgra.device.mgt.core.device.mgt.common.Base64File;
import io.entgra.device.mgt.core.application.mgt.core.dao.SPApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.SPApplicationDAO;
import io.entgra.device.mgt.core.application.mgt.core.util.ApplicationManagementUtil; import io.entgra.device.mgt.core.application.mgt.core.util.ApplicationManagementUtil;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.App;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
@ -103,6 +106,8 @@ import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -145,6 +150,55 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override @Override
public <T> Application createApplication(T app, boolean isPublished) throws ApplicationManagementException { public <T> Application createApplication(T app, boolean isPublished) throws ApplicationManagementException {
return createApplicationBasedOnRemoteStatus(app, isPublished);
}
/**
* Create the application based on the release wrapper's remote status. If the remote status is true, then
* the application creation will take place asynchronously.
* @param app Application release wrapper
* @param isPublished Publish status
* @return {@link Application}
* @throws ApplicationManagementException Throws when error occurred while application creation
*/
@SuppressWarnings("unchecked")
private <T> Application createApplicationBasedOnRemoteStatus(T app, boolean isPublished) throws ApplicationManagementException {
if (ApplicationManagementUtil.getRemoteStatus(app)) {
List<?> releaseWrappers = ApplicationManagementUtil.deriveApplicationWithoutRelease(app);
Application createdApplication = triggerApplicationCreation(app, isPublished);
if (createdApplication == null) {
throw new ApplicationManagementException("Null retrieved for created application.");
}
try {
if (releaseWrappers != null && !releaseWrappers.isEmpty()) {
if (app instanceof ApplicationWrapper) {
((ApplicationWrapper) app).setEntAppReleaseWrappers((List<EntAppReleaseWrapper>) releaseWrappers);
createApplicationReleaseBasedOnRemoteStatus(createdApplication.getId(),
((ApplicationWrapper) app).getEntAppReleaseWrappers().get(0), isPublished);
} else if (app instanceof CustomAppWrapper) {
((CustomAppWrapper) app).setCustomAppReleaseWrappers((List<CustomAppReleaseWrapper>) releaseWrappers);
createApplicationReleaseBasedOnRemoteStatus(createdApplication.getId(),
((CustomAppWrapper) app).getCustomAppReleaseWrappers().get(0), isPublished);
} else {
throw new ApplicationManagementException("Unsupported release wrapper received");
}
}
return createdApplication;
} catch (ResourceManagementException e) {
throw new ApplicationManagementException("Error encountered while creating deploying artifact", e);
}
}
return triggerApplicationCreation(app, isPublished);
}
/**
* Trigger the application creation process
* @param app Application release wrapper
* @param isPublished Publish status
* @return {@link Application}
* @throws ApplicationManagementException Throws when error occurred while creating the application
*/
private <T> Application triggerApplicationCreation(T app, boolean isPublished) throws ApplicationManagementException {
ApplicationDTO applicationDTO = uploadReleaseArtifactIfExist(app); ApplicationDTO applicationDTO = uploadReleaseArtifactIfExist(app);
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
@ -172,22 +226,110 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
/**
* Create application release based on remote status. If the remote status is true, then the
* application release creation will take place asynchronously.
* @param appId Application id
* @param releaseWrapper Release wrapper
* @param isPublished Publish status
* @return {@link Application}
* @throws ApplicationManagementException Throws when error occurred while deploying the release
* @throws ResourceManagementException Throws when error occurred while deploying the release
*/
private <T> ApplicationRelease createApplicationReleaseBasedOnRemoteStatus(int appId, T releaseWrapper, boolean isPublished)
throws ApplicationManagementException, ResourceManagementException {
if (ApplicationManagementUtil.getRemoteStatusFromWrapper(releaseWrapper)) {
triggerReleaseAsynchronously(appId, releaseWrapper, isPublished);
} else {
if (releaseWrapper instanceof EntAppReleaseWrapper) {
return triggerEntAppRelease(appId, (EntAppReleaseWrapper) releaseWrapper, isPublished);
}
if (releaseWrapper instanceof CustomAppReleaseWrapper) {
return triggerCustomAppRelease(appId, (CustomAppReleaseWrapper) releaseWrapper, isPublished);
}
throw new ApplicationManagementException("Unsupported release wrapper received");
}
return new ApplicationRelease();
}
/**
* Trigger release creation asynchronously
* @param appId Application id
* @param releaseWrapper Release wrapper
* @param isPublished Publish status
*/
private <T> void triggerReleaseAsynchronously(int appId, T releaseWrapper, boolean isPublished) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
new Thread(() -> {
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
if (releaseWrapper instanceof EntAppReleaseWrapper &&
((EntAppReleaseWrapper) releaseWrapper).isRemoteStatus()) {
triggerEntAppRelease(appId, (EntAppReleaseWrapper) releaseWrapper, isPublished);
}else if (releaseWrapper instanceof CustomAppReleaseWrapper &&
((CustomAppReleaseWrapper) releaseWrapper).isRemoteStatus()) {
triggerCustomAppRelease(appId, (CustomAppReleaseWrapper) releaseWrapper, isPublished);
} else {
throw new ApplicationManagementException("Unsupported release wrapper received");
}
} catch (ApplicationManagementException | ResourceManagementException e) {
log.error("Error encountered while deploying remote application release", e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}).start();
}
/**
* Trigger enterprise application creation
* @param appId Application id
* @param releaseWrapper Release wrapper
* @param isPublished Publish status
* @return {@link ApplicationRelease}
* @throws ApplicationManagementException Throws when error encountered while creating enterprise application
*/
private ApplicationRelease triggerEntAppRelease(int appId, EntAppReleaseWrapper releaseWrapper, boolean isPublished)
throws ApplicationManagementException{
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIconLink(), releaseWrapper.getScreenshotLinks(),
releaseWrapper.getArtifactLink(), releaseWrapper.getBannerLink());
ApplicationDTO applicationDTO = applicationManager.getApplication(appId);
DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper);
releaseDTO = uploadEntAppReleaseArtifacts(releaseDTO, artifact, deviceType.getName(), true);
try {
return createRelease(applicationDTO, releaseDTO, ApplicationType.ENTERPRISE, isPublished);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating ent app release for application with the name: " + applicationDTO.getName();
log.error(msg, e);
deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue()));
throw new ApplicationManagementException(msg, e);
}
} catch (MalformedURLException e) {
String msg = "Malformed URL link received as a downloadable link";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (FileDownloaderServiceException e) {
String msg = "Error encountered while downloading application release artifacts for app id " + appId;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
}
}
@Override @Override
public ApplicationRelease createEntAppRelease(int appId, EntAppReleaseWrapper releaseWrapper, boolean isPublished) public ApplicationRelease createEntAppRelease(int appId, EntAppReleaseWrapper releaseWrapper, boolean isPublished)
throws ApplicationManagementException { throws ApplicationManagementException {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), releaseWrapper.getScreenshots(),
releaseWrapper.getBinaryFile(), releaseWrapper.getBanner());
ApplicationDTO applicationDTO = applicationManager.getApplication(appId);
DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper);
releaseDTO = uploadEntAppReleaseArtifacts(releaseDTO, artifact, deviceType.getName(), true);
try { try {
return createRelease(applicationDTO, releaseDTO, ApplicationType.ENTERPRISE, isPublished); return createApplicationReleaseBasedOnRemoteStatus(appId, releaseWrapper, isPublished);
} catch (ApplicationManagementException e) { } catch (ResourceManagementException e) {
String msg = "Error occurred while creating ent app release for application with the name: " + applicationDTO.getName(); String msg = "Error occurred while creating enterprise app release for the app id " + appId;
log.error(msg, e); log.error(msg, e);
deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue()));
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} }
} }
@ -196,17 +338,27 @@ public class ApplicationManagerImpl implements ApplicationManager {
public ApplicationRelease createWebAppRelease(int appId, WebAppReleaseWrapper releaseWrapper, boolean isPublished) public ApplicationRelease createWebAppRelease(int appId, WebAppReleaseWrapper releaseWrapper, boolean isPublished)
throws ApplicationManagementException, ResourceManagementException { throws ApplicationManagementException, ResourceManagementException {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationDTO applicationDTO = applicationManager.getApplication(appId);
ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(),
releaseWrapper.getScreenshots(), null, releaseWrapper.getBanner());
ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper);
releaseDTO = uploadWebAppReleaseArtifacts(releaseDTO, artifact);
try { try {
return createRelease(applicationDTO, releaseDTO, ApplicationType.WEB_CLIP, isPublished); ApplicationDTO applicationDTO = applicationManager.getApplication(appId);
} catch (ApplicationManagementException e) { ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIconLink(), releaseWrapper.getScreenshotLinks(),
String msg = "Error occurred while creating web app release for application with the name: " + applicationDTO.getName(); null, releaseWrapper.getBannerLink());
ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper);
releaseDTO = uploadWebAppReleaseArtifacts(releaseDTO, artifact);
try {
return createRelease(applicationDTO, releaseDTO, ApplicationType.WEB_CLIP, isPublished);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating web app release for application with the name: " + applicationDTO.getName();
log.error(msg, e);
deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue()));
throw new ApplicationManagementException(msg, e);
}
} catch (MalformedURLException e) {
String msg = "Malformed URL link received as a downloadable link";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (FileDownloaderServiceException e) {
String msg = "Error encountered while downloading application release artifacts";
log.error(msg, e); log.error(msg, e);
deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue()));
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} }
} }
@ -215,38 +367,69 @@ public class ApplicationManagerImpl implements ApplicationManager {
public ApplicationRelease createPubAppRelease(int appId, PublicAppReleaseWrapper releaseWrapper, boolean isPublished) throws public ApplicationRelease createPubAppRelease(int appId, PublicAppReleaseWrapper releaseWrapper, boolean isPublished) throws
ResourceManagementException, ApplicationManagementException { ResourceManagementException, ApplicationManagementException {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationDTO applicationDTO = applicationManager.getApplication(appId);
DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(),
releaseWrapper.getScreenshots(), null, releaseWrapper.getBanner());
ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper);
releaseDTO = uploadPubAppReleaseArtifacts(releaseDTO, artifact, deviceType.getName());
try { try {
return createRelease(applicationDTO, releaseDTO, ApplicationType.PUBLIC, isPublished); ApplicationDTO applicationDTO = applicationManager.getApplication(appId);
} catch (ApplicationManagementException e) { DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
String msg = "Error occurred while creating ent public release for application with the name: " + applicationDTO.getName(); ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIconLink(), releaseWrapper.getScreenshotLinks(),
null, releaseWrapper.getBannerLink());
ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper);
releaseDTO = uploadPubAppReleaseArtifacts(releaseDTO, artifact, deviceType.getName());
try {
return createRelease(applicationDTO, releaseDTO, ApplicationType.PUBLIC, isPublished);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating ent public release for application with the name: " + applicationDTO.getName();
log.error(msg, e);
deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue()));
throw new ApplicationManagementException(msg, e);
}
} catch (MalformedURLException e) {
String msg = "Malformed URL link received as a downloadable link";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (FileDownloaderServiceException e) {
String msg = "Error encountered while downloading application release artifacts";
log.error(msg, e); log.error(msg, e);
deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue()));
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} }
} }
@Override @Override
public ApplicationRelease createCustomAppRelease(int appId, CustomAppReleaseWrapper releaseWrapper, boolean isPublished) public ApplicationRelease createCustomAppRelease(int appId, CustomAppReleaseWrapper releaseWrapper, boolean isPublished)
throws ApplicationManagementException {
try {
return createApplicationReleaseBasedOnRemoteStatus(appId, releaseWrapper, isPublished);
} catch (ResourceManagementException e) {
String msg = "Error occurred while creating enterprise app release";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
}
}
private ApplicationRelease triggerCustomAppRelease(int appId, CustomAppReleaseWrapper releaseWrapper, boolean isPublished)
throws ResourceManagementException, ApplicationManagementException { throws ResourceManagementException, ApplicationManagementException {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationDTO applicationDTO = applicationManager.getApplication(appId);
DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(),
releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), releaseWrapper.getBanner());
ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper);
releaseDTO = uploadCustomAppReleaseArtifacts(releaseDTO, artifact, deviceType.getName());
try { try {
return createRelease(applicationDTO, releaseDTO, ApplicationType.CUSTOM, isPublished); ApplicationDTO applicationDTO = applicationManager.getApplication(appId);
} catch (ApplicationManagementException e) { DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
String msg = "Error occurred while creating custom app release for application with the name: " + applicationDTO.getName(); ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIconLink(), releaseWrapper.getScreenshotLinks(),
releaseWrapper.getArtifactLink(), releaseWrapper.getBannerLink());
ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper);
releaseDTO = uploadCustomAppReleaseArtifacts(releaseDTO, artifact, deviceType.getName());
try {
return createRelease(applicationDTO, releaseDTO, ApplicationType.CUSTOM, isPublished);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating custom app release for application with the name: " + applicationDTO.getName();
log.error(msg, e);
deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue()));
throw new ApplicationManagementException(msg, e);
}
} catch (MalformedURLException e) {
String msg = "Malformed URL link received as a downloadable link";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (FileDownloaderServiceException e) {
String msg = "Error encountered while downloading application release artifacts";
log.error(msg, e); log.error(msg, e);
deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue()));
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} }
} }
@ -279,27 +462,27 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (app instanceof ApplicationWrapper) { if (app instanceof ApplicationWrapper) {
ApplicationWrapper wrapper = (ApplicationWrapper) app; ApplicationWrapper wrapper = (ApplicationWrapper) app;
EntAppReleaseWrapper releaseWrapper = wrapper.getEntAppReleaseWrappers().get(0); EntAppReleaseWrapper releaseWrapper = wrapper.getEntAppReleaseWrappers().get(0);
artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIconLink(), releaseWrapper.getScreenshotLinks(),
releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), releaseWrapper.getBanner()); releaseWrapper.getArtifactLink(), releaseWrapper.getBannerLink());
releaseDTO = uploadEntAppReleaseArtifacts(releaseDTO, releaseDTO = uploadEntAppReleaseArtifacts(releaseDTO,
artifact, wrapper.getDeviceType(), false); artifact, wrapper.getDeviceType(), false);
} else if (app instanceof PublicAppWrapper) { } else if (app instanceof PublicAppWrapper) {
PublicAppWrapper wrapper = (PublicAppWrapper) app; PublicAppWrapper wrapper = (PublicAppWrapper) app;
PublicAppReleaseWrapper releaseWrapper = wrapper.getPublicAppReleaseWrappers().get(0); PublicAppReleaseWrapper releaseWrapper = wrapper.getPublicAppReleaseWrappers().get(0);
artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIconLink(), releaseWrapper.getScreenshotLinks(),
releaseWrapper.getScreenshots(), null, releaseWrapper.getBanner()); null, releaseWrapper.getBannerLink());
releaseDTO = uploadPubAppReleaseArtifacts(releaseDTO, artifact, wrapper.getDeviceType()); releaseDTO = uploadPubAppReleaseArtifacts(releaseDTO, artifact, wrapper.getDeviceType());
} else if (app instanceof WebAppWrapper) { } else if (app instanceof WebAppWrapper) {
WebAppWrapper wrapper = (WebAppWrapper) app; WebAppWrapper wrapper = (WebAppWrapper) app;
WebAppReleaseWrapper releaseWrapper = wrapper.getWebAppReleaseWrappers().get(0); WebAppReleaseWrapper releaseWrapper = wrapper.getWebAppReleaseWrappers().get(0);
artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIconLink(), releaseWrapper.getScreenshotLinks(),
releaseWrapper.getScreenshots(), null, releaseWrapper.getBanner()); null, releaseWrapper.getBannerLink());
releaseDTO = uploadWebAppReleaseArtifacts(releaseDTO, artifact); releaseDTO = uploadWebAppReleaseArtifacts(releaseDTO, artifact);
} else if (app instanceof CustomAppWrapper) { } else if (app instanceof CustomAppWrapper) {
CustomAppWrapper wrapper = (CustomAppWrapper) app; CustomAppWrapper wrapper = (CustomAppWrapper) app;
CustomAppReleaseWrapper releaseWrapper = wrapper.getCustomAppReleaseWrappers().get(0); CustomAppReleaseWrapper releaseWrapper = wrapper.getCustomAppReleaseWrappers().get(0);
artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIconLink(), releaseWrapper.getScreenshotLinks(),
releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), releaseWrapper.getBanner()); releaseWrapper.getArtifactLink(), releaseWrapper.getBannerLink());
try { try {
releaseDTO = uploadCustomAppReleaseArtifacts(releaseDTO, artifact, wrapper.getDeviceType()); releaseDTO = uploadCustomAppReleaseArtifacts(releaseDTO, artifact, wrapper.getDeviceType());
} catch (ResourceManagementException e) { } catch (ResourceManagementException e) {
@ -316,6 +499,14 @@ public class ApplicationManagerImpl implements ApplicationManager {
String msg = "Error Occurred when uploading artifacts of the web clip: " + applicationDTO.getName(); String msg = "Error Occurred when uploading artifacts of the web clip: " + applicationDTO.getName();
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (MalformedURLException e) {
String msg = "Malformed URL link received as a downloadable link";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (FileDownloaderServiceException e) {
String msg = "Error encountered while downloading application release artifacts";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} }
applicationDTO.getApplicationReleaseDTOs().clear(); applicationDTO.getApplicationReleaseDTOs().clear();
applicationDTO.getApplicationReleaseDTOs().add(releaseDTO); applicationDTO.getApplicationReleaseDTOs().add(releaseDTO);
@ -3133,11 +3324,13 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
@Override @Override
public ApplicationRelease updateEntAppRelease(String releaseUuid, EntAppReleaseWrapper entAppReleaseWrapper, public ApplicationRelease updateEntAppRelease(String releaseUuid, EntAppReleaseWrapper entAppReleaseWrapper) throws ApplicationManagementException {
ApplicationArtifact applicationArtifact) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try { try {
ApplicationArtifact applicationArtifact = ApplicationManagementUtil.
constructApplicationArtifact(entAppReleaseWrapper.getIconLink(), entAppReleaseWrapper.getScreenshotLinks(),
entAppReleaseWrapper.getArtifactLink(), entAppReleaseWrapper.getBannerLink());
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUuid, tenantId); ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUuid, tenantId);
DeviceType deviceTypeObj = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); DeviceType deviceTypeObj = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
@ -3204,17 +3397,25 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ "UUID:" + releaseUuid; + "UUID:" + releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (MalformedURLException e) {
throw new ApplicationManagementException("Malformed downloadable URL received for the Public app " +
"release UUID: " + releaseUuid);
} catch (FileDownloaderServiceException e) {
throw new ApplicationManagementException("Error encountered while downloading artifact for the Public app " +
"release UUID: " + releaseUuid);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override @Override
public ApplicationRelease updatePubAppRelease(String releaseUuid, PublicAppReleaseWrapper publicAppReleaseWrapper, public ApplicationRelease updatePubAppRelease(String releaseUuid, PublicAppReleaseWrapper publicAppReleaseWrapper) throws ApplicationManagementException {
ApplicationArtifact applicationArtifact) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try { try {
ApplicationArtifact applicationArtifact = ApplicationManagementUtil.
constructApplicationArtifact(publicAppReleaseWrapper.getIconLink(), publicAppReleaseWrapper.getScreenshotLinks(),
null, publicAppReleaseWrapper.getBannerLink());
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUuid, tenantId); ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUuid, tenantId);
validateAppReleaseUpdating(publicAppReleaseWrapper, applicationDTO, applicationArtifact, validateAppReleaseUpdating(publicAppReleaseWrapper, applicationDTO, applicationArtifact,
@ -3273,17 +3474,25 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ "release UUID:" + releaseUuid; + "release UUID:" + releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (MalformedURLException e) {
throw new ApplicationManagementException("Malformed downloadable URL received for the Public app " +
"release UUID: " + releaseUuid);
} catch (FileDownloaderServiceException e) {
throw new ApplicationManagementException("Error encountered while downloading artifact for the Public app " +
"release UUID: " + releaseUuid);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override @Override
public ApplicationRelease updateWebAppRelease(String releaseUuid, WebAppReleaseWrapper webAppReleaseWrapper, public ApplicationRelease updateWebAppRelease(String releaseUuid, WebAppReleaseWrapper webAppReleaseWrapper) throws ApplicationManagementException {
ApplicationArtifact applicationArtifact) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try { try {
ApplicationArtifact applicationArtifact = ApplicationManagementUtil.
constructApplicationArtifact(webAppReleaseWrapper.getIconLink(), webAppReleaseWrapper.getScreenshotLinks(),
null, webAppReleaseWrapper.getBannerLink());
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUuid, tenantId); ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUuid, tenantId);
validateAppReleaseUpdating(webAppReleaseWrapper, applicationDTO, applicationArtifact, validateAppReleaseUpdating(webAppReleaseWrapper, applicationDTO, applicationArtifact,
@ -3338,18 +3547,27 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ "release UUID:" + releaseUuid; + "release UUID:" + releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (MalformedURLException e) {
throw new ApplicationManagementException("Malformed downloadable URL received for the Public app " +
"release UUID: " + releaseUuid);
} catch (FileDownloaderServiceException e) {
throw new ApplicationManagementException("Error encountered while downloading artifact for the Public app " +
"release UUID: " + releaseUuid);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override @Override
public ApplicationRelease updateCustomAppRelease(String releaseUuid, public ApplicationRelease updateCustomAppRelease(String releaseUuid, CustomAppReleaseWrapper customAppReleaseWrapper)
CustomAppReleaseWrapper customAppReleaseWrapper, ApplicationArtifact applicationArtifact)
throws ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
try { try {
ApplicationArtifact applicationArtifact = ApplicationManagementUtil.
constructApplicationArtifact(customAppReleaseWrapper.getIconLink(),
customAppReleaseWrapper.getScreenshotLinks(), customAppReleaseWrapper.getArtifactLink(),
customAppReleaseWrapper.getBannerLink());
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUuid, tenantId); ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUuid, tenantId);
AtomicReference<ApplicationReleaseDTO> applicationReleaseDTO = new AtomicReference<>( AtomicReference<ApplicationReleaseDTO> applicationReleaseDTO = new AtomicReference<>(
@ -3468,6 +3686,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ "UUID:" + releaseUuid; + "UUID:" + releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (MalformedURLException e) {
throw new ApplicationManagementException("Malformed downloadable URL received for the Public app " +
"release UUID: " + releaseUuid);
} catch (FileDownloaderServiceException e) {
throw new ApplicationManagementException("Error encountered while downloading artifact for the Public app " +
"release UUID: " + releaseUuid);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
@ -3903,30 +4127,24 @@ public class ApplicationManagerImpl implements ApplicationManager {
public void validateEntAppReleaseCreatingRequest(EntAppReleaseWrapper releaseWrapper, String deviceType) public void validateEntAppReleaseCreatingRequest(EntAppReleaseWrapper releaseWrapper, String deviceType)
throws RequestValidatingException, ApplicationManagementException { throws RequestValidatingException, ApplicationManagementException {
validateReleaseCreatingRequest(releaseWrapper, deviceType); validateReleaseCreatingRequest(releaseWrapper, deviceType);
validateBinaryArtifact(releaseWrapper.getBinaryFile());
validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots());
} }
@Override @Override
public void validateCustomAppReleaseCreatingRequest(CustomAppReleaseWrapper releaseWrapper, String deviceType) public void validateCustomAppReleaseCreatingRequest(CustomAppReleaseWrapper releaseWrapper, String deviceType)
throws RequestValidatingException, ApplicationManagementException { throws RequestValidatingException, ApplicationManagementException {
validateReleaseCreatingRequest(releaseWrapper, deviceType); validateReleaseCreatingRequest(releaseWrapper, deviceType);
validateBinaryArtifact(releaseWrapper.getBinaryFile());
validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots());
} }
@Override @Override
public void validateWebAppReleaseCreatingRequest(WebAppReleaseWrapper releaseWrapper) public void validateWebAppReleaseCreatingRequest(WebAppReleaseWrapper releaseWrapper)
throws RequestValidatingException, ApplicationManagementException { throws RequestValidatingException, ApplicationManagementException {
validateReleaseCreatingRequest(releaseWrapper, Constants.ANY); validateReleaseCreatingRequest(releaseWrapper, Constants.ANY);
validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots());
} }
@Override @Override
public void validatePublicAppReleaseCreatingRequest(PublicAppReleaseWrapper releaseWrapper, String deviceType) public void validatePublicAppReleaseCreatingRequest(PublicAppReleaseWrapper releaseWrapper, String deviceType)
throws RequestValidatingException, ApplicationManagementException { throws RequestValidatingException, ApplicationManagementException {
validateReleaseCreatingRequest(releaseWrapper, deviceType); validateReleaseCreatingRequest(releaseWrapper, deviceType);
validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots());
validatePublicAppReleasePackageName(releaseWrapper.getPackageName()); validatePublicAppReleasePackageName(releaseWrapper.getPackageName());
} }

@ -0,0 +1,147 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.core.impl;
import io.entgra.device.mgt.core.application.mgt.common.FileDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.FileMetaEntry;
import io.entgra.device.mgt.core.application.mgt.common.TransferLink;
import io.entgra.device.mgt.core.application.mgt.common.exception.FileDownloaderServiceException;
import io.entgra.device.mgt.core.application.mgt.common.exception.FileTransferServiceException;
import io.entgra.device.mgt.core.application.mgt.common.services.FileDownloaderService;
import io.entgra.device.mgt.core.application.mgt.common.services.FileTransferService;
import io.entgra.device.mgt.core.application.mgt.core.internal.DataHolder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class FileDownloaderServiceProvider {
private static final Log log = LogFactory.getLog(FileDownloaderServiceProvider.class);
private static final FileTransferService fileTransferService = DataHolder.getInstance().getFileTransferService();
private static final LocalFileDownloaderService localFileDownloaderService = new LocalFileDownloaderService();
private static final RemoteFileDownloaderService remoteFileDownloaderService = new RemoteFileDownloaderService();
public static FileDownloaderService getFileDownloaderService(URL downloadUrl) throws FileDownloaderServiceException {
try {
if (fileTransferService.isExistsOnLocal(downloadUrl)) {
return localFileDownloaderService;
}
return remoteFileDownloaderService;
} catch (FileTransferServiceException e) {
String msg = "Error encountered while acquiring file downloader service";
log.error(msg, e);
throw new FileDownloaderServiceException(msg, e);
}
}
/**
* Class holing the implementation of the local file downloading service
*/
private static class LocalFileDownloaderService implements FileDownloaderService {
@Override
public FileDescriptor download(URL downloadUrl) throws FileDownloaderServiceException {
try {
return fileTransferService.resolve(downloadUrl);
} catch (FileTransferServiceException e) {
String msg = "Error encountered while downloading file pointing by " + downloadUrl;
log.error(msg, e);
throw new FileDownloaderServiceException(msg, e);
}
}
}
/**
* Class holing the implementation of the remote file downloading service
*/
private static class RemoteFileDownloaderService implements FileDownloaderService {
private static final OkHttpClient okhttpClient =
new OkHttpClient.Builder().connectTimeout(500, TimeUnit.MILLISECONDS).build();
@Override
public FileDescriptor download(URL downloadUrl) throws FileDownloaderServiceException {
FileMetaEntry fileMetaEntry = getFileMetaEntry(downloadUrl);
try {
TransferLink transferLink = fileTransferService.generateUploadLink(fileMetaEntry);
FileDescriptor fileDescriptor = fileTransferService.resolve(new URL(transferLink.getDirectTransferLink()
+ "/" + fileMetaEntry.getFileName() + "." + fileMetaEntry.getExtension()));
FileUtils.copyURLToFile(downloadUrl, new File(fileDescriptor.getAbsolutePath()),
15000, 3600000);
return fileDescriptor;
} catch (FileTransferServiceException | IOException e) {
String msg = "Error encountered while downloading file";
log.error(msg, e);
throw new FileDownloaderServiceException(msg, e);
}
}
/**
* Generate the {@link FileMetaEntry} from the remote file
* @param downloadUrl Remote file URL
* @return {@link FileMetaEntry}
* @throws FileDownloaderServiceException Throws when error encountered while generating {@link FileMetaEntry}
*/
private FileMetaEntry getFileMetaEntry(URL downloadUrl) throws FileDownloaderServiceException {
Request request = new Request.Builder().url(downloadUrl).head().build();
try (Response response = okhttpClient.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new FileDownloaderServiceException("Unexpected response code received for the remote url " + downloadUrl);
}
String contentDisposition = response.header("Content-Disposition");
String[] fileNameSegments = getFileNameSegments(contentDisposition);
FileMetaEntry fileMetaEntry = new FileMetaEntry();
fileMetaEntry.setSize(Long.parseLong(Objects.requireNonNull(response.header("Content-Length"))));
fileMetaEntry.setFileName(fileNameSegments[0] + "-" + UUID.randomUUID());
fileMetaEntry.setExtension(fileNameSegments[1]);
return fileMetaEntry;
} catch (IOException e) {
throw new FileDownloaderServiceException("IO error occurred while constructing file name for the remote url " + downloadUrl);
}
}
/**
* Extract file name segments(filename & extensions) from content disposition header
* @param contentDisposition Content disposition header value
* @return Array of name segments
* @throws FileDownloaderServiceException Throws when error occurred while extracting name segments
*/
private static String[] getFileNameSegments(String contentDisposition) throws FileDownloaderServiceException {
if (contentDisposition == null) {
throw new FileDownloaderServiceException("Cannot determine the file name for the remote file");
}
String []contentDispositionSegments = contentDisposition.split("=");
if (contentDispositionSegments.length != 2) {
throw new FileDownloaderServiceException("Error encountered when constructing file name");
}
String fullQualifiedName = contentDispositionSegments[contentDispositionSegments.length - 1].replace("\"", "");
String []fileNameSegments = fullQualifiedName.split("\\.(?=[^.]+$)");
if (fileNameSegments.length != 2) {
throw new FileDownloaderServiceException("Error encountered when constructing file name");
}
return fileNameSegments;
}
}
}

@ -0,0 +1,124 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.core.impl;
import io.entgra.device.mgt.core.application.mgt.common.ChunkDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.FileDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.FileMetaEntry;
import io.entgra.device.mgt.core.application.mgt.common.TransferLink;
import io.entgra.device.mgt.core.application.mgt.common.exception.FileTransferServiceException;
import io.entgra.device.mgt.core.application.mgt.common.services.FileTransferService;
import io.entgra.device.mgt.core.application.mgt.core.exception.FileTransferServiceHelperUtilException;
import io.entgra.device.mgt.core.application.mgt.core.util.FileTransferServiceHelperUtil;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.NotFoundException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
public class FileTransferServiceImpl implements FileTransferService {
private final static Log log = LogFactory.getLog(FileTransferServiceImpl.class);
private static volatile FileTransferServiceImpl INSTANCE;
private FileTransferServiceImpl() throws FileTransferServiceException {
try {
FileTransferServiceHelperUtil.createDefaultRootStructure();
} catch (FileTransferServiceHelperUtilException e) {
String msg = "Error occurred while initializing file transfer service";
log.error(msg, e);
throw new FileTransferServiceException(msg, e);
}
}
public static FileTransferService getInstance() throws FileTransferServiceException{
if (INSTANCE == null) {
synchronized (FileTransferServiceImpl.class) {
if (INSTANCE == null) {
INSTANCE = new FileTransferServiceImpl();
}
}
}
return INSTANCE;
}
@Override
public TransferLink generateUploadLink(FileMetaEntry fileMetaEntry) throws FileTransferServiceException {
try {
Path artifactHolder = FileTransferServiceHelperUtil.createNewArtifactHolder(fileMetaEntry);
String []pathSegments = artifactHolder.toString().split(FileSystems.getDefault().getSeparator());
TransferLink.TransferLinkBuilder transferLinkBuilder =
new TransferLink.TransferLinkBuilder(pathSegments[pathSegments.length - 1]);
return transferLinkBuilder.build();
} catch (FileTransferServiceHelperUtilException e) {
String msg = "Error encountered while generating upload link";
log.error(msg, e);
throw new FileTransferServiceException(msg, e);
}
}
@Override
public ChunkDescriptor resolve(String artifactHolder, InputStream chunk) throws FileTransferServiceException, NotFoundException {
ChunkDescriptor chunkDescriptor = new ChunkDescriptor();
try {
FileTransferServiceHelperUtil.populateChunkDescriptor(artifactHolder, chunk, chunkDescriptor);
return chunkDescriptor;
} catch (FileTransferServiceHelperUtilException e) {
String msg = "Error occurred while resolving chuck descriptor for " + artifactHolder;
log.error(msg);
throw new FileTransferServiceException(msg, e);
}
}
@Override
public void writeChunk(ChunkDescriptor chunkDescriptor) throws FileTransferServiceException {
try {
FileTransferServiceHelperUtil.writeChunk(chunkDescriptor);
} catch (FileTransferServiceHelperUtilException e) {
String msg = "Failed to write data to artifact located in " + chunkDescriptor.getAssociateFileDescriptor().getAbsolutePath();
log.error(msg);
throw new FileTransferServiceException(msg, e);
}
}
@Override
public boolean isExistsOnLocal(URL downloadUrl) throws FileTransferServiceException {
try {
return FileTransferServiceHelperUtil.resolve(downloadUrl) != null;
} catch (FileTransferServiceHelperUtilException e) {
String msg = "Error occurred while checking the existence of artifact on the local environment";
log.error(msg, e);
throw new FileTransferServiceException(msg, e);
}
}
@Override
public FileDescriptor resolve(URL downloadUrl) throws FileTransferServiceException {
try {
return FileTransferServiceHelperUtil.resolve(downloadUrl);
} catch (FileTransferServiceHelperUtilException e) {
String msg = "Error occurred while resolving file descriptor pointing from " + downloadUrl;
log.error(msg, e);
throw new FileTransferServiceException(msg, e);
}
}
}

@ -21,6 +21,7 @@ import io.entgra.device.mgt.core.application.mgt.common.config.LifecycleState;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationStorageManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationStorageManager;
import io.entgra.device.mgt.core.application.mgt.common.services.AppmDataHandler; import io.entgra.device.mgt.core.application.mgt.common.services.AppmDataHandler;
import io.entgra.device.mgt.core.application.mgt.common.services.FileTransferService;
import io.entgra.device.mgt.core.application.mgt.common.services.ReviewManager; import io.entgra.device.mgt.core.application.mgt.common.services.ReviewManager;
import io.entgra.device.mgt.core.application.mgt.common.services.SPApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.SPApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager;
@ -28,6 +29,7 @@ import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationM
import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager; import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager;
import io.entgra.device.mgt.core.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import io.entgra.device.mgt.core.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
import io.entgra.device.mgt.core.application.mgt.core.impl.AppmDataHandlerImpl; import io.entgra.device.mgt.core.application.mgt.core.impl.AppmDataHandlerImpl;
import io.entgra.device.mgt.core.application.mgt.core.impl.FileTransferServiceImpl;
import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager; import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager;
import io.entgra.device.mgt.core.application.mgt.core.task.ScheduledAppSubscriptionTaskManager; import io.entgra.device.mgt.core.application.mgt.core.task.ScheduledAppSubscriptionTaskManager;
import io.entgra.device.mgt.core.application.mgt.core.util.ApplicationManagementUtil; import io.entgra.device.mgt.core.application.mgt.core.util.ApplicationManagementUtil;
@ -123,6 +125,10 @@ public class ApplicationManagementServiceComponent {
DataHolder.getInstance().setVppApplicationManager(vppApplicationManager); DataHolder.getInstance().setVppApplicationManager(vppApplicationManager);
bundleContext.registerService(VPPApplicationManager.class.getName(), vppApplicationManager, null); bundleContext.registerService(VPPApplicationManager.class.getName(), vppApplicationManager, null);
FileTransferService fileTransferService = FileTransferServiceImpl.getInstance();
DataHolder.getInstance().setFileTransferService(fileTransferService);
bundleContext.registerService(FileTransferService.class.getName(), fileTransferService, null);
ScheduledAppSubscriptionTaskManager taskManager = new ScheduledAppSubscriptionTaskManager(); ScheduledAppSubscriptionTaskManager taskManager = new ScheduledAppSubscriptionTaskManager();
taskManager.scheduleCleanupTask(); taskManager.scheduleCleanupTask();

@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.internal;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationStorageManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationStorageManager;
import io.entgra.device.mgt.core.application.mgt.common.services.AppmDataHandler; import io.entgra.device.mgt.core.application.mgt.common.services.AppmDataHandler;
import io.entgra.device.mgt.core.application.mgt.common.services.FileTransferService;
import io.entgra.device.mgt.core.application.mgt.common.services.SPApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.SPApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.ReviewManager; import io.entgra.device.mgt.core.application.mgt.common.services.ReviewManager;
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager;
@ -55,6 +56,7 @@ public class DataHolder {
private AppmDataHandler configManager; private AppmDataHandler configManager;
private TaskService taskService; private TaskService taskService;
private FileTransferService fileTransferService;
private static final DataHolder applicationMgtDataHolder = new DataHolder(); private static final DataHolder applicationMgtDataHolder = new DataHolder();
@ -153,4 +155,12 @@ public class DataHolder {
public void setVppApplicationManager(VPPApplicationManager vppApplicationManager) { public void setVppApplicationManager(VPPApplicationManager vppApplicationManager) {
this.vppApplicationManager = vppApplicationManager; this.vppApplicationManager = vppApplicationManager;
} }
public FileTransferService getFileTransferService() {
return fileTransferService;
}
public void setFileTransferService(FileTransferService fileTransferService) {
this.fileTransferService = fileTransferService;
}
} }

@ -74,6 +74,7 @@ public class APIUtil {
private static volatile AppmDataHandler appmDataHandler; private static volatile AppmDataHandler appmDataHandler;
private static volatile VPPApplicationManager vppApplicationManager; private static volatile VPPApplicationManager vppApplicationManager;
private static volatile MetadataManagementService metadataManagementService; private static volatile MetadataManagementService metadataManagementService;
private static volatile FileTransferService fileTransferService;
public static SPApplicationManager getSPApplicationManager() { public static SPApplicationManager getSPApplicationManager() {
if (SPApplicationManager == null) { if (SPApplicationManager == null) {
@ -591,4 +592,16 @@ public class APIUtil {
} }
return metadataManagementService; return metadataManagementService;
} }
public static FileTransferService getFileTransferService() {
if (fileTransferService == null) {
synchronized (APIUtil.class) {
if (fileTransferService == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
fileTransferService = (FileTransferService) ctx.getOSGiService(FileTransferService.class, null);
}
}
}
return fileTransferService;
}
} }

@ -19,16 +19,20 @@ package io.entgra.device.mgt.core.application.mgt.core.util;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationArtifact; import io.entgra.device.mgt.core.application.mgt.common.ApplicationArtifact;
import io.entgra.device.mgt.core.application.mgt.common.FileDataHolder; import io.entgra.device.mgt.core.application.mgt.common.FileDataHolder;
import io.entgra.device.mgt.core.application.mgt.common.FileDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.LifecycleChanger; import io.entgra.device.mgt.core.application.mgt.common.LifecycleChanger;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.ItuneAppDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ItuneAppDTO;
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
import io.entgra.device.mgt.core.application.mgt.common.exception.FileDownloaderServiceException;
import io.entgra.device.mgt.core.application.mgt.common.exception.FileTransferServiceException;
import io.entgra.device.mgt.core.application.mgt.common.exception.InvalidConfigurationException; import io.entgra.device.mgt.core.application.mgt.common.exception.InvalidConfigurationException;
import io.entgra.device.mgt.core.application.mgt.common.exception.RequestValidatingException; import io.entgra.device.mgt.core.application.mgt.common.exception.RequestValidatingException;
import io.entgra.device.mgt.core.application.mgt.common.response.Application; import io.entgra.device.mgt.core.application.mgt.common.response.Application;
import io.entgra.device.mgt.core.application.mgt.common.response.Category; import io.entgra.device.mgt.core.application.mgt.common.response.Category;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationStorageManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationStorageManager;
import io.entgra.device.mgt.core.application.mgt.common.services.FileTransferService;
import io.entgra.device.mgt.core.application.mgt.common.services.ReviewManager; import io.entgra.device.mgt.core.application.mgt.common.services.ReviewManager;
import io.entgra.device.mgt.core.application.mgt.common.services.SPApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.SPApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager;
@ -45,10 +49,12 @@ import io.entgra.device.mgt.core.application.mgt.common.wrapper.WebAppWrapper;
import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager; import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager;
import io.entgra.device.mgt.core.application.mgt.core.config.Extension; import io.entgra.device.mgt.core.application.mgt.core.config.Extension;
import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException;
import io.entgra.device.mgt.core.application.mgt.core.impl.FileDownloaderServiceProvider;
import io.entgra.device.mgt.core.application.mgt.core.impl.VppApplicationManagerImpl; import io.entgra.device.mgt.core.application.mgt.core.impl.VppApplicationManagerImpl;
import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager; import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager;
import io.entgra.device.mgt.core.device.mgt.common.Base64File; import io.entgra.device.mgt.core.device.mgt.common.Base64File;
import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants;
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.App;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService;
import io.entgra.device.mgt.core.device.mgt.core.common.util.FileUtil; import io.entgra.device.mgt.core.device.mgt.core.common.util.FileUtil;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.MetadataManagementServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.MetadataManagementServiceImpl;
@ -159,6 +165,49 @@ public class ApplicationManagementUtil {
return applicationArtifact; return applicationArtifact;
} }
public static ApplicationArtifact constructApplicationArtifact(String iconLink, List<String> screenshotLinks, String artifactLink, String bannerLink)
throws MalformedURLException, FileDownloaderServiceException {
ApplicationArtifact applicationArtifact = new ApplicationArtifact();
FileDescriptor fileDescriptor;
if (artifactLink != null) {
URL artifactLinkUrl = new URL(artifactLink);
fileDescriptor = FileDownloaderServiceProvider.getFileDownloaderService(artifactLinkUrl).download(artifactLinkUrl);
applicationArtifact.setInstallerName(fileDescriptor.getFullQualifiedName());
applicationArtifact.setInstallerStream(fileDescriptor.getFile());
}
if (iconLink != null) {
URL iconLinkUrl = new URL(iconLink);
fileDescriptor = FileDownloaderServiceProvider.getFileDownloaderService(iconLinkUrl).download(iconLinkUrl);
applicationArtifact.setIconName(fileDescriptor.getFullQualifiedName());
applicationArtifact.setIconStream(fileDescriptor.getFile());
}
if (bannerLink != null) {
URL bannerLinkUrl = new URL(bannerLink);
fileDescriptor = FileDownloaderServiceProvider.getFileDownloaderService(bannerLinkUrl).download(bannerLinkUrl);
applicationArtifact.setBannerName(fileDescriptor.getFullQualifiedName());
applicationArtifact.setBannerStream(fileDescriptor.getFile());
}
if (screenshotLinks != null) {
Map<String, InputStream> screenshotData = new TreeMap<>();
// This is to handle cases in which multiple screenshots have the same name
Map<String, Integer> screenshotNameCount = new HashMap<>();
URL screenshotLinkUrl;
for (String screenshotLink : screenshotLinks) {
screenshotLinkUrl = new URL(screenshotLink);
fileDescriptor = FileDownloaderServiceProvider.getFileDownloaderService(screenshotLinkUrl).download(screenshotLinkUrl);
String screenshotName = fileDescriptor.getFullQualifiedName();
screenshotNameCount.put(screenshotName, screenshotNameCount.getOrDefault(screenshotName, 0) + 1);
screenshotName = FileUtil.generateDuplicateFileName(screenshotName, screenshotNameCount.get(screenshotName));
screenshotData.put(screenshotName, fileDescriptor.getFile());
}
applicationArtifact.setScreenshots(screenshotData);
}
return applicationArtifact;
}
/** /**
* *
* @param base64File Base64File that should be converted to FileDataHolder bean * @param base64File Base64File that should be converted to FileDataHolder bean
@ -251,6 +300,41 @@ public class ApplicationManagementUtil {
throw new IllegalArgumentException("Provided bean does not belong to an Application Wrapper"); throw new IllegalArgumentException("Provided bean does not belong to an Application Wrapper");
} }
public static <T> boolean getRemoteStatus(T appWrapper) {
if (!isReleaseAvailable(appWrapper)) {
return false;
}
if (appWrapper instanceof ApplicationWrapper) {
return getRemoteStatusFromWrapper(((ApplicationWrapper) appWrapper).getEntAppReleaseWrappers().get(0));
}
if (appWrapper instanceof PublicAppWrapper) {
return getRemoteStatusFromWrapper(((PublicAppWrapper) appWrapper).getPublicAppReleaseWrappers().get(0));
}
if (appWrapper instanceof WebAppWrapper) {
return getRemoteStatusFromWrapper(((WebAppWrapper) appWrapper).getWebAppReleaseWrappers().get(0));
}
if (appWrapper instanceof CustomAppWrapper) {
return getRemoteStatusFromWrapper(((CustomAppWrapper) appWrapper).getCustomAppReleaseWrappers().get(0));
}
throw new IllegalArgumentException("Provided bean does not belong to an Application Wrapper");
}
public static <T> boolean getRemoteStatusFromWrapper(T releaseWrapper) {
if (releaseWrapper instanceof EntAppReleaseWrapper) {
return ((EntAppReleaseWrapper) releaseWrapper).isRemoteStatus();
}
if (releaseWrapper instanceof PublicAppReleaseWrapper) {
return ((PublicAppReleaseWrapper) releaseWrapper).isRemoteStatus();
}
if (releaseWrapper instanceof WebAppReleaseWrapper) {
return ((WebAppReleaseWrapper) releaseWrapper).isRemoteStatus();
}
if (releaseWrapper instanceof CustomAppReleaseWrapper) {
return ((CustomAppReleaseWrapper) releaseWrapper).isRemoteStatus();
}
throw new IllegalArgumentException("Provided bean does not belong to an Release Wrapper");
}
public static <T> T getInstance(Extension extension, Class<T> cls) throws InvalidConfigurationException { public static <T> T getInstance(Extension extension, Class<T> cls) throws InvalidConfigurationException {
try { try {
Class theClass = Class.forName(extension.getClassName()); Class theClass = Class.forName(extension.getClassName());
@ -279,13 +363,12 @@ public class ApplicationManagementUtil {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
List<Category> categories = applicationManager.getRegisteredCategories(); List<Category> categories = applicationManager.getRegisteredCategories();
if (product != null && product.getVersion() != null) { if (product != null && product.getVersion() != null) {
// Generate artifacts
ApplicationArtifact applicationArtifact = generateArtifacts(product);
List<String> packageNamesOfApps = new ArrayList<>(); List<String> packageNamesOfApps = new ArrayList<>();
packageNamesOfApps.add(product.getPackageName()); packageNamesOfApps.add(product.getPackageName());
List<Application> existingApps = applicationManager.getApplications(packageNamesOfApps); List<Application> existingApps = applicationManager.getApplications(packageNamesOfApps);
PublicAppReleaseWrapper publicAppReleaseWrapper = generatePublicAppReleaseWrapper(product);
if (existingApps != null && existingApps.size() > 0) { if (existingApps != null && existingApps.size() > 0) {
Application app = existingApps.get(0); Application app = existingApps.get(0);
@ -293,7 +376,6 @@ public class ApplicationManagementUtil {
ApplicationUpdateWrapper applicationUpdateWrapper = generatePubAppUpdateWrapper(product, categories); ApplicationUpdateWrapper applicationUpdateWrapper = generatePubAppUpdateWrapper(product, categories);
applicationManager.updateApplication(app.getId(), applicationUpdateWrapper); applicationManager.updateApplication(app.getId(), applicationUpdateWrapper);
PublicAppReleaseWrapper publicAppReleaseWrapper = new PublicAppReleaseWrapper();
if (app.getSubMethod() if (app.getSubMethod()
.equalsIgnoreCase(Constants.ApplicationProperties.FREE_SUB_METHOD)) { .equalsIgnoreCase(Constants.ApplicationProperties.FREE_SUB_METHOD)) {
publicAppReleaseWrapper.setPrice(0.0); publicAppReleaseWrapper.setPrice(0.0);
@ -306,56 +388,48 @@ public class ApplicationManagementUtil {
publicAppReleaseWrapper.setVersion(product.getVersion()); publicAppReleaseWrapper.setVersion(product.getVersion());
publicAppReleaseWrapper.setSupportedOsVersions("4.0-12.3"); publicAppReleaseWrapper.setSupportedOsVersions("4.0-12.3");
applicationManager.updatePubAppRelease(app.getApplicationReleases().get(0).getUuid(), applicationManager.updatePubAppRelease(app.getApplicationReleases().get(0).getUuid(),
publicAppReleaseWrapper, applicationArtifact); publicAppReleaseWrapper);
return; return;
} }
} else { } else {
// Generate App wrapper // Generate App wrapper
PublicAppWrapper publicAppWrapper = generatePubAppWrapper(product, categories); PublicAppWrapper publicAppWrapper = generatePubAppWrapper(product, categories);
PublicAppReleaseWrapper appReleaseWrapper = new PublicAppReleaseWrapper();
if (publicAppWrapper.getSubMethod()
.equalsIgnoreCase(Constants.ApplicationProperties.FREE_SUB_METHOD)) {
appReleaseWrapper.setPrice(0.0);
} else {
appReleaseWrapper.setPrice(1.0);
}
appReleaseWrapper.setDescription(product.getDescription());
appReleaseWrapper.setReleaseType("ga");
appReleaseWrapper.setVersion(product.getVersion());
appReleaseWrapper.setPackageName(product.getPackageName());
appReleaseWrapper.setSupportedOsVersions("4.0-12.3");
publicAppWrapper.setPublicAppReleaseWrappers( publicAppWrapper.setPublicAppReleaseWrappers(
Arrays.asList(new PublicAppReleaseWrapper[]{appReleaseWrapper})); Arrays.asList(new PublicAppReleaseWrapper[]{publicAppReleaseWrapper}));
try { Application application = applicationManager.createApplication(publicAppWrapper, false);
updateImages(appReleaseWrapper, applicationArtifact.getIconName(), if (application != null && (application.getApplicationReleases().get(0).getCurrentStatus() == null
applicationArtifact.getIconStream(), applicationArtifact.getScreenshots()); || application.getApplicationReleases().get(0).getCurrentStatus().equals("CREATED"))) {
String uuid = application.getApplicationReleases().get(0).getUuid();
Application application = applicationManager.createApplication(publicAppWrapper, false); LifecycleChanger lifecycleChanger = new LifecycleChanger();
if (application != null && (application.getApplicationReleases().get(0).getCurrentStatus() == null lifecycleChanger.setAction("IN-REVIEW");
|| application.getApplicationReleases().get(0).getCurrentStatus().equals("CREATED"))) { applicationManager.changeLifecycleState(uuid, lifecycleChanger);
String uuid = application.getApplicationReleases().get(0).getUuid(); lifecycleChanger.setAction("APPROVED");
LifecycleChanger lifecycleChanger = new LifecycleChanger(); applicationManager.changeLifecycleState(uuid, lifecycleChanger);
lifecycleChanger.setAction("IN-REVIEW"); lifecycleChanger.setAction("PUBLISHED");
applicationManager.changeLifecycleState(uuid, lifecycleChanger); applicationManager.changeLifecycleState(uuid, lifecycleChanger);
lifecycleChanger.setAction("APPROVED");
applicationManager.changeLifecycleState(uuid, lifecycleChanger);
lifecycleChanger.setAction("PUBLISHED");
applicationManager.changeLifecycleState(uuid, lifecycleChanger);
}
} catch (IOException e) {
String msg = "Error while downloading images of release.";
log.error(msg);
throw new ApplicationManagementException(msg, e);
} }
} }
} }
} }
private static PublicAppReleaseWrapper generatePublicAppReleaseWrapper(ItuneAppDTO product) {
PublicAppReleaseWrapper publicAppReleaseWrapper = new PublicAppReleaseWrapper();
publicAppReleaseWrapper.setDescription(product.getDescription());
publicAppReleaseWrapper.setReleaseType("ga");
publicAppReleaseWrapper.setVersion(product.getVersion());
publicAppReleaseWrapper.setPackageName(product.getPackageName());
publicAppReleaseWrapper.setSupportedOsVersions("4.0-12.3");
publicAppReleaseWrapper.setIconLink(product.getIconURL());
publicAppReleaseWrapper.setRemoteStatus(false);
List<String> screenshotUrls = new ArrayList<>(Collections.nCopies(3, product.getIconURL()));
publicAppReleaseWrapper.setScreenshotLinks(screenshotUrls);
publicAppReleaseWrapper.setPrice(1.0);
return publicAppReleaseWrapper;
}
private static PublicAppWrapper generatePubAppWrapper(ItuneAppDTO product, List<Category> categories) { private static PublicAppWrapper generatePubAppWrapper(ItuneAppDTO product, List<Category> categories) {
PublicAppWrapper publicAppWrapper = new PublicAppWrapper(); PublicAppWrapper publicAppWrapper = new PublicAppWrapper();
publicAppWrapper.setName(product.getTitle()); publicAppWrapper.setName(product.getTitle());
@ -560,4 +634,19 @@ public class ApplicationManagementUtil {
return sanitizedName; return sanitizedName;
} }
} }
public static <T> List<?> deriveApplicationWithoutRelease(T app) {
List<?> releaseWrappers = null;
if (app instanceof ApplicationWrapper) {
ApplicationWrapper applicationWrapper = (ApplicationWrapper) app;
releaseWrappers = applicationWrapper.getEntAppReleaseWrappers();
applicationWrapper.setEntAppReleaseWrappers(Collections.emptyList());
}
if (app instanceof CustomAppWrapper) {
CustomAppWrapper applicationWrapper = (CustomAppWrapper) app;
releaseWrappers = applicationWrapper.getCustomAppReleaseWrappers();
applicationWrapper.setCustomAppReleaseWrappers(Collections.emptyList());
}
return releaseWrappers;
}
} }

@ -0,0 +1,236 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.core.util;
import com.google.gson.Gson;
import io.entgra.device.mgt.core.application.mgt.common.ChunkDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.FileDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.FileMetaEntry;
import io.entgra.device.mgt.core.application.mgt.core.exception.FileTransferServiceHelperUtilException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.NotFoundException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Objects;
import java.util.UUID;
public class FileTransferServiceHelperUtil {
private static final Log log = LogFactory.getLog(FileTransferServiceHelperUtil.class);
private static final String ROOT = "iot-artifact-holder";
private static final String SYSTEM_PROPERTY_TEMP_DIR = "java.io.tmpdir";
private static final String META_ENTRY_FILE_NAME = ".meta.json";
private static final Gson gson = new Gson();
public static void createDefaultRootStructure() throws FileTransferServiceHelperUtilException {
try {
Path root = Paths.get(System.getProperty(SYSTEM_PROPERTY_TEMP_DIR), ROOT);
if (Files.notExists(root)) {
setMinimumPermissions(Files.createDirectory(root));
}
if (!Files.isDirectory(root)) {
throw new FileTransferServiceHelperUtilException(root.toAbsolutePath() + " is not a directory");
}
setMinimumPermissions(root);
} catch (IOException e) {
String msg = "Error encountered while creating default artifact root structure";
log.error(msg, e);
throw new FileTransferServiceHelperUtilException(msg, e);
}
}
public static Path createNewArtifactHolder(FileMetaEntry fileMetaEntry) throws FileTransferServiceHelperUtilException {
try {
Path artifactHolder = Paths.get(System.getProperty(SYSTEM_PROPERTY_TEMP_DIR), ROOT, UUID.randomUUID().toString());
if (Files.exists(artifactHolder)) {
throw new FileTransferServiceHelperUtilException("Artifact holder already exists in " + artifactHolder);
}
setMinimumPermissions(Files.createDirectory(artifactHolder));
createMetaEntry(fileMetaEntry, artifactHolder);
createArtifactFile(fileMetaEntry, artifactHolder);
return artifactHolder;
} catch (IOException e) {
String msg = "Error occurred while creating artifact holder";
log.error(msg, e);
throw new FileTransferServiceHelperUtilException(msg, e);
}
}
public static void populateChunkDescriptor(String artifactHolder, InputStream chunk, ChunkDescriptor chunkDescriptor)
throws FileTransferServiceHelperUtilException, NotFoundException {
Path holder = locateArtifactHolder(artifactHolder);
Path metaEntry = locateMetaEntry(holder);
chunkDescriptor.setChunk(chunk);
FileDescriptor fileDescriptor = new FileDescriptor();
populateFileDescriptor(metaEntry, holder, fileDescriptor);
chunkDescriptor.setAssociateFileDescriptor(fileDescriptor);
}
public static void populateFileDescriptor(String artifactHolder, FileDescriptor fileDescriptor)
throws FileTransferServiceHelperUtilException, NotFoundException {
Path holder = locateArtifactHolder(artifactHolder);
Path metaEntry = locateMetaEntry(holder);
populateFileDescriptor(metaEntry, holder, fileDescriptor);
}
public static void populateFileDescriptor(Path metaEntry, Path artifactHolder, FileDescriptor fileDescriptor) throws FileTransferServiceHelperUtilException {
try {
byte []metaEntryByteContent = Files.readAllBytes(metaEntry);
FileMetaEntry fileMetaEntry = gson.fromJson(new String(metaEntryByteContent, StandardCharsets.UTF_8), FileMetaEntry.class);
fileDescriptor.setFileName(fileMetaEntry.getFileName());
fileDescriptor.setActualFileSize(fileMetaEntry.getSize());
fileDescriptor.setFullQualifiedName(fileMetaEntry.getFileName() + "." + fileMetaEntry.getExtension());
Path artifact = artifactHolder.resolve(fileDescriptor.getFullQualifiedName());
fileDescriptor.setAbsolutePath(artifact.toAbsolutePath().toString());
fileDescriptor.setExtension(fileMetaEntry.getExtension());
fileDescriptor.setFile(Files.newInputStream(artifact));
} catch (IOException e) {
String msg = "Error encountered while populating chuck descriptor";
log.error(msg, e);
throw new FileTransferServiceHelperUtilException(msg, e);
}
}
private static Path locateArtifactHolder(String artifactHolder) throws FileTransferServiceHelperUtilException, NotFoundException {
Path holder = Paths.get(System.getProperty(SYSTEM_PROPERTY_TEMP_DIR), ROOT, artifactHolder);
if (Files.notExists(holder)) {
throw new NotFoundException(holder.toAbsolutePath() + " is not exists");
}
if (!Files.isDirectory(holder)) {
throw new FileTransferServiceHelperUtilException(holder.toFile().getAbsolutePath() + " is not a directory");
}
return holder;
}
private static Path locateMetaEntry(Path artifactHolder) throws FileTransferServiceHelperUtilException {
Path metaEntry = artifactHolder.resolve(META_ENTRY_FILE_NAME);
if (Files.notExists(metaEntry) || Files.isDirectory(metaEntry)) {
throw new FileTransferServiceHelperUtilException("Can't locate " + META_ENTRY_FILE_NAME);
}
if (!Files.isReadable(metaEntry)) {
throw new FileTransferServiceHelperUtilException("Unreadable " + META_ENTRY_FILE_NAME);
}
return metaEntry;
}
public static void writeChunk(ChunkDescriptor chunkDescriptor) throws FileTransferServiceHelperUtilException {
if (chunkDescriptor == null) {
throw new FileTransferServiceHelperUtilException("Received null for chuck descriptor");
}
FileDescriptor fileDescriptor = chunkDescriptor.getAssociateFileDescriptor();
if (fileDescriptor == null) {
throw new FileTransferServiceHelperUtilException("Target file descriptor is missing for retrieved chunk");
}
Path artifact = Paths.get(fileDescriptor.getAbsolutePath());
try {
InputStream chuckStream = chunkDescriptor.getChunk();
byte []chunk = new byte[chuckStream.available()];
chuckStream.read(chunk);
Files.write(artifact, chunk, StandardOpenOption.CREATE, StandardOpenOption.SYNC, StandardOpenOption.APPEND);
} catch (IOException e) {
String msg = "Error encountered while writing to the " + artifact;
log.error(msg, e);
throw new FileTransferServiceHelperUtilException(msg, e);
}
}
public static FileDescriptor resolve(URL downloadUrl) throws FileTransferServiceHelperUtilException {
if (downloadUrl == null) {
throw new FileTransferServiceHelperUtilException("Received null for download url");
}
if (!Objects.equals(System.getProperty("iot.gateway.host"), downloadUrl.getHost())) {
if (log.isDebugEnabled()) {
log.debug("Host not match with " + System.getProperty("iot.gateway.host"));
}
return null;
}
String []urlPathSegments = downloadUrl.getPath().split("/");
if (urlPathSegments.length < 2) {
if (log.isDebugEnabled()) {
log.debug("URL patch segments contain less than 2 segments");
}
return null;
}
String file = urlPathSegments[urlPathSegments.length - 1];
String artifactHolder = urlPathSegments[urlPathSegments.length - 2];
try {
FileDescriptor fileDescriptor = new FileDescriptor();
populateFileDescriptor(artifactHolder, fileDescriptor);
if (!Objects.equals(file, fileDescriptor.getFullQualifiedName())) {
if (log.isDebugEnabled()) {
log.debug("File name not equal to the file exists in the local");
}
return null;
}
return fileDescriptor;
} catch (NotFoundException e) {
if (log.isDebugEnabled()) {
log.debug("Local URL not found in the system");
}
return null;
}
}
private static void setMinimumPermissions(Path path) throws FileTransferServiceHelperUtilException {
File file = path.toFile();
if (!file.setReadable(true, true)) {
throw new FileTransferServiceHelperUtilException("Failed to set read permission for " + file.getAbsolutePath());
}
if (!file.setWritable(true, true)) {
throw new FileTransferServiceHelperUtilException("Failed to set write permission for " + file.getAbsolutePath());
}
}
private static void createMetaEntry(FileMetaEntry fileMetaEntry, Path artifactHolder) throws FileTransferServiceHelperUtilException {
try {
Path metaEntry = artifactHolder.resolve(META_ENTRY_FILE_NAME);
String fileMetaJsonContent = gson.toJson(fileMetaEntry);
Files.write(metaEntry, fileMetaJsonContent.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE, StandardOpenOption.SYNC);
} catch (IOException e) {
throw new FileTransferServiceHelperUtilException("Error encountered while creating meta entry", e);
}
}
private static void createArtifactFile(FileMetaEntry fileMetaEntry, Path artifactHolder) throws FileTransferServiceHelperUtilException {
try {
Path artifactFile = artifactHolder.resolve(fileMetaEntry.getFileName() + "." + fileMetaEntry.getExtension());
fileMetaEntry.setAbsolutePath(artifactFile.toAbsolutePath().toString());
Files.createFile(artifactFile);
setMinimumPermissions(artifactFile);
} catch (IOException e) {
throw new FileTransferServiceHelperUtilException("Error encountered while creating artifact file", e);
}
}
}

@ -19,8 +19,13 @@ package io.entgra.device.mgt.core.application.mgt.core.management;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationArtifact; import io.entgra.device.mgt.core.application.mgt.common.ApplicationArtifact;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationList; import io.entgra.device.mgt.core.application.mgt.common.ApplicationList;
import io.entgra.device.mgt.core.application.mgt.common.ChunkDescriptor;
import io.entgra.device.mgt.core.application.mgt.common.FileMetaEntry;
import io.entgra.device.mgt.core.application.mgt.common.Filter; import io.entgra.device.mgt.core.application.mgt.common.Filter;
import io.entgra.device.mgt.core.application.mgt.common.LifecycleState; import io.entgra.device.mgt.core.application.mgt.common.LifecycleState;
import io.entgra.device.mgt.core.application.mgt.common.TransferLink;
import io.entgra.device.mgt.core.application.mgt.common.services.FileTransferService;
import io.entgra.device.mgt.core.application.mgt.core.impl.FileTransferServiceImpl;
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.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Attachment;
@ -52,6 +57,10 @@ import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTypeVersion;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -104,26 +113,66 @@ public class ApplicationManagementTest extends BaseTestCase {
releaseWrapper.setSupportedOsVersions("4.0-7.0"); releaseWrapper.setSupportedOsVersions("4.0-7.0");
File banner = new File("src/test/resources/samples/app1/banner1.jpg"); FileTransferService fileTransferService = FileTransferServiceImpl.getInstance();
File icon = new File("src/test/resources/samples/app1/icon.png"); DataHolder.getInstance().setFileTransferService(fileTransferService);
File ss1 = new File("src/test/resources/samples/app1/shot1.png");
File ss2 = new File("src/test/resources/samples/app1/shot2.png"); FileMetaEntry metaEntry = new FileMetaEntry();
File ss3 = new File("src/test/resources/samples/app1/shot3.png"); TransferLink transferLink;
String []segments;
Base64File bannerBase64 = new Base64File("banner", FileUtil.fileToBase64String(banner)); ChunkDescriptor chunkDescriptor;
Base64File iconBase64 = new Base64File("icon", FileUtil.fileToBase64String(icon));
Base64File ss1Base64 = new Base64File("ss1", FileUtil.fileToBase64String(ss1)); metaEntry.setFileName("banner1");
Base64File ss2Base64 = new Base64File("ss2", FileUtil.fileToBase64String(ss2)); metaEntry.setExtension("jpg");
Base64File ss3Base64 = new Base64File("ss3", FileUtil.fileToBase64String(ss3)); metaEntry.setSize(179761);
transferLink = fileTransferService.generateUploadLink(metaEntry);
File apk = new File("src/test/resources/samples/app1/sample.apk"); segments = transferLink.getRelativeTransferLink().split("/");
Base64File apkBase64 = new Base64File("apk", FileUtil.fileToBase64String(apk)); chunkDescriptor = fileTransferService.
resolve(segments[segments.length-1], Files.newInputStream(
Paths.get("src/test/resources/samples/app1/banner1.jpg")));
releaseWrapper.setBanner(bannerBase64); fileTransferService.writeChunk(chunkDescriptor);
releaseWrapper.setIcon(iconBase64); releaseWrapper.setBannerLink(transferLink.getDirectTransferLink() + "/banner1.jpg");
releaseWrapper.setBinaryFile(apkBase64);
releaseWrapper.setScreenshots(Arrays.asList(ss1Base64, ss2Base64, ss3Base64)); metaEntry.setFileName("icon");
metaEntry.setExtension("png");
metaEntry.setSize(41236);
transferLink = fileTransferService.generateUploadLink(metaEntry);
segments = transferLink.getRelativeTransferLink().split("/");
chunkDescriptor = fileTransferService.
resolve(segments[segments.length-1], Files.newInputStream(
Paths.get("src/test/resources/samples/app1/icon.png")));
fileTransferService.writeChunk(chunkDescriptor);
releaseWrapper.setIconLink(transferLink.getDirectTransferLink() + "/icon.png");
List<String> screenshotPaths = Arrays.asList("src/test/resources/samples/app1/shot1.png",
"src/test/resources/samples/app1/shot2.png", "src/test/resources/samples/app1/shot3.png");
List<String> screenshotLinks = new ArrayList<>();
String []pathSegments;
for (String path: screenshotPaths) {
pathSegments = path.split("/");
String fullQualifiedName = pathSegments[pathSegments.length - 1];
String []nameSegments = fullQualifiedName.split("\\.(?=[^.]+$)");
metaEntry.setFileName(nameSegments[0]);
metaEntry.setExtension(nameSegments[1]);
metaEntry.setSize(41236);
transferLink = fileTransferService.generateUploadLink(metaEntry);
segments = transferLink.getRelativeTransferLink().split("/");
chunkDescriptor = fileTransferService.
resolve(segments[segments.length-1], Files.newInputStream(Paths.get(path)));
fileTransferService.writeChunk(chunkDescriptor);
screenshotLinks.add(transferLink.getDirectTransferLink() + "/" + fullQualifiedName);
}
releaseWrapper.setScreenshotLinks(screenshotLinks);
metaEntry.setFileName("sample");
metaEntry.setExtension("apk");
metaEntry.setSize(6259412);
TransferLink apkTransferLink = fileTransferService.generateUploadLink(metaEntry);
segments = apkTransferLink.getRelativeTransferLink().split("/");
chunkDescriptor = fileTransferService.
resolve(segments[segments.length-1], Files.newInputStream(Paths.get("src/test/resources/samples/app1/sample.apk")));
fileTransferService.writeChunk(chunkDescriptor);
releaseWrapper.setArtifactLink(apkTransferLink.getDirectTransferLink() + "/sample.apk");
releaseWrapper.setRemoteStatus(false);
entAppReleaseWrappers.add(releaseWrapper); entAppReleaseWrappers.add(releaseWrapper);
applicationWrapper.setEntAppReleaseWrappers(entAppReleaseWrappers); applicationWrapper.setEntAppReleaseWrappers(entAppReleaseWrappers);

@ -419,6 +419,7 @@
<Scope>dm:admin:cea:update</Scope> <Scope>dm:admin:cea:update</Scope>
<Scope>dm:admin:cea:delete</Scope> <Scope>dm:admin:cea:delete</Scope>
<Scope>dm:admin:cea:sync</Scope> <Scope>dm:admin:cea:sync</Scope>
<Scope>am:pub:app:upload</Scope>
</Scopes> </Scopes>
<SSOConfiguration> <SSOConfiguration>
<Issuer>device-mgt</Issuer> <Issuer>device-mgt</Issuer>

@ -2177,8 +2177,8 @@
<json.smart.version>1.3</json.smart.version> <json.smart.version>1.3</json.smart.version>
<google.gson.version>2.8.5</google.gson.version> <google.gson.version>2.8.5</google.gson.version>
<google.guava.version>31.0.1-jre</google.guava.version> <google.guava.version>31.0.1-jre</google.guava.version>
<squareup.okhttp3.version>4.6.0</squareup.okhttp3.version> <squareup.okhttp3.version>4.12.0</squareup.okhttp3.version>
<okio.version>1.13.0</okio.version> <okio.version>3.6.0</okio.version>
<github.openfeign.version>9.3.1</github.openfeign.version> <github.openfeign.version>9.3.1</github.openfeign.version>
<jsr311.version>1.1.1</jsr311.version> <jsr311.version>1.1.1</jsr311.version>
<commons.logging.version>1.2</commons.logging.version> <commons.logging.version>1.2</commons.logging.version>

Loading…
Cancel
Save