I'm trying to develop a put method to create file on Sharepoint using graph API. Creating an upload session allow to upload files from 4Mo to 60Mo. I created this session and obtain a response which looks like this.
HTTP/1.1 200 OK
Content-Type: application/json
{
"uploadUrl": "https://url_for_uploading_file",
"expirationDateTime": "2015-01-29T09:21:55.523Z"
}
Based on the documentation, I have to retrieve the uploadUrl and send my file. https://learn.microsoft.com/fr-fr/graph/api/driveitem-createuploadsession?view=graph-rest-1.0
I created a method that receives the request, the upload url and my token. This method allow to set httpPut params.
public SharePointDocumentResponseModel putFileRequest(SharePointDocumentByNameRequest request,
String token) throws IOException {
SharePointDocumentResponseModel returnValue = new SharePointDocumentResponseModel();
filePath = request.getFilePath();
fileContent = FileUtils.readFileToByteArray(new File(request.getFilePath()));
byte[][] fileArrays = Utils.splitChunks(fileContent);
int size = fileContent.length;
long chunkSize = Constants.MAX_FILE_SPLIT;
int numberOfChunks = (int) (size / chunkSize);
int minRange = 0;
int maxRange = 0;
int count = 0;
SortedMap<Integer,Integer> ranges = new TreeMap<>();
// TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
// public X509Certificate[] getAcceptedIssuers() {
// return null;
// }
// public void checkClientTrusted(X509Certificate[] certs, String authType) {
// }
// public void checkServerTrusted(X509Certificate[] certs, String authType) {
// }
// }};
System.out.println("number of chunks " + (numberOfChunks+1));
for(int i = 0; i <= numberOfChunks; i++) {
maxRange += chunkSize;
if(maxRange < size) {
ranges.put(minRange, maxRange);
} else {
ranges.put(minRange, size);
}
minRange = maxRange + 1;
}
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPut httpPut = new HttpPut(String.valueOf(request.getUploadUrl()));
httpPut.setHeader("Authorization", "Bearer " + token);
httpPut.setHeader("Content-Type", "text/json;charset=UTF-8");
//httpPut.setHeader("Accept", "application/json");
httpPut.setHeader("Connection", "Keep-Alive");
httpPut.setHeader("Cache-Control", "no-cache");
// Install the all-trusting trust manager
// try {
// SSLContext sc = SSLContext.getInstance("TLSv1.2");
// sc.init(null, trustAllCerts, new SecureRandom());
// HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// } catch (Exception e) {
// e.printStackTrace();
// }
// splitter le fichier
for (Map.Entry<Integer, Integer> range: ranges.entrySet()) {
int step = range.getValue() - range.getKey();
//httpPut.setHeader("Content-Range",
// "bytes " + range.getKey() + "-" + range.getValue() + "/" + fileContent.length);
httpPut.setHeader("Content-Range",
"bytes " + range.getKey() + "-" + range.getValue() + "/" + Constants.CHUNK_SIZE);
InputStreamEntity inputStreamEntity =
new InputStreamEntity(
new ByteArrayInputStream(fileArrays[count]),
fileContent.length
);
count++;
//inputStreamEntity.setChunked(true);
httpPut.setEntity(inputStreamEntity);
System.out.println("\nSending 'PUT' request to URL : " + request.getUploadUrl());
System.out.println("Executing request : " + httpPut.getRequestLine());
try (CloseableHttpResponse response = httpclient.execute(httpPut)) {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
//returnValue = gson.fromJson(response.getEntity(), SharePointDocumentResponseModel.class);
}
}
//InputStreamEntity inputStreamEntity = new InputStreamEntity(
// new FileInputStream(file), -1, ContentType.APPLICATION_OCTET_STREAM);
} finally {
httpclient.close();
}
return returnValue;
}
At this stage the return value has not been set. I obtain a SSL Exception and a connection reset. I tried to add a trustAllCerts TrustManager array object to solve this issue but the exception is also thrown. On my library a temporary document is created.
Exception in thread "main" javax.net.ssl.SSLException: Connection reset
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:127)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:321)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:259)
at java.base/sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1314)
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:839)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at fr.dsidiff.api.sharepoint.request.HttpRequest.putFileRequest(HttpRequest.java:363)
at fr.dsidiff.api.sharepoint.service.impl.DocumentServiceImpl.uploadWithSession(DocumentServiceImpl.java:39)
at fr.dsidiff.api.sharepoint.SharePointCaller.getResponse(SharePointCaller.java:410)
at fr.dsidiff.api.sharepoint.SharePointCaller.createDocument(SharePointCaller.java:363)
at fr.dsidiff.api.sharepoint.tests.DocumentCallTest.createDocumentByNameBigFile(DocumentCallTest.java:51)
at fr.dsidiff.api.sharepoint.tests.DocumentCallTest.main(DocumentCallTest.java:29)
Suppressed: java.net.SocketException: Connection reset by peer: socket write error
at java.base/java.net.SocketOutputStream.socketWrite0(Native Method)
at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:110)
at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150)
at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:352)
... 27 more
Caused by: java.net.SocketException: Connection reset
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:186)
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:448)
at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68)
at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1104)
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:823)
... 23 more
It's nothing to do with Microsoft Graph API you may want to open MS Graph Explorer or POSTMAN, test it out the above API will work. SSL/JDK related error here. Here's the related bug on this - https://bugs.openjdk.java.net/browse/JDK-8241372