com.microsoft.graph.concurrency.ChunkedUploadProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.document.library.opener.onedrive.web
Show all versions of com.liferay.document.library.opener.onedrive.web
Liferay Document Library Opener OneDrive Web
// ------------------------------------------------------------------------------
// Copyright (c) 2017 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ------------------------------------------------------------------------------
package com.microsoft.graph.concurrency;
import com.microsoft.graph.concurrency.ChunkedUploadResponseHandler;
import com.microsoft.graph.concurrency.IProgressCallback;
import com.microsoft.graph.requests.extensions.ChunkedUploadRequest;
import com.microsoft.graph.requests.extensions.ChunkedUploadResult;
import com.microsoft.graph.models.extensions.IGraphServiceClient;
import com.microsoft.graph.models.extensions.UploadSession;
import com.microsoft.graph.options.Option;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidParameterException;
import java.util.List;
/**
* ChunkedUpload service provider
*
* @param the upload item type
*/
public class ChunkedUploadProvider {
/**
* The default chunk size for upload. Currently set to 5 MiB.
*/
private static final int DEFAULT_CHUNK_SIZE = 5 * 1024 * 1024;
/**
* The required chunk size increment by OneDrive service, which is 320 KiB
*/
private static final int REQUIRED_CHUNK_SIZE_INCREMENT = 320 * 1024;
/**
* The maximum chunk size for a single upload allowed by OneDrive service.
* Currently the value is 60 MiB.
*/
private static final int MAXIMUM_CHUNK_SIZE = 60 * 1024 * 1024;
/**
* The default retry times for a simple chunk upload if failure happened
*/
private static final int MAXIMUM_RETRY_TIMES = 3;
/**
* The client
*/
private final IGraphServiceClient client;
/**
* The input stream
*/
private final InputStream inputStream;
/**
* The upload session URL
*/
private final String uploadUrl;
/**
* The stream size
*/
private final long streamSize;
/**
* The upload response handler
*/
private final ChunkedUploadResponseHandler responseHandler;
/**
* The counter for how many bytes have been read from input stream
*/
private int readSoFar;
/**
* Creates the ChunkedUploadProvider
*
* @param uploadSession the initial upload session
* @param client the Graph client
* @param inputStream the input stream
* @param streamSize the stream size
* @param uploadTypeClass the upload type class
*/
public ChunkedUploadProvider(final UploadSession uploadSession,
final IGraphServiceClient client,
final InputStream inputStream,
final long streamSize,
final Class uploadTypeClass) {
if (uploadSession == null) {
throw new InvalidParameterException("Upload session is null.");
}
if (client == null) {
throw new InvalidParameterException("OneDrive client is null.");
}
if (inputStream == null) {
throw new InvalidParameterException("Input stream is null.");
}
if (streamSize <= 0) {
throw new InvalidParameterException("Stream size should larger than 0.");
}
this.client = client;
this.readSoFar = 0;
this.inputStream = inputStream;
this.streamSize = streamSize;
this.uploadUrl = uploadSession.uploadUrl;
this.responseHandler = new ChunkedUploadResponseHandler(uploadTypeClass);
}
/**
* Uploads content to remote upload session based on the input stream
*
* @param options the upload options
* @param callback the progress callback invoked during uploading
* @param configs the optional configurations for the upload options. [0] should be the customized chunk
* size and [1] should be the maxRetry for upload retry.
* @throws IOException the IO exception that occurred during upload
*/
public void upload(final List