parent
dd03a073b2
commit
3798134011
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue