Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 16 additions & 38 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,28 @@
<organizationUrl>http://github.com/kikovalle</organizationUrl>
</developer>
</developers>

<properties>
<java.version>8</java.version> <!-- Replaces source/target-->
<java.version>17</java.version> <!-- Replaces source/target/ Spring Framework 6.1.x: JDK 17-23-->

<!-- Dependencies -->
<spring.framework>6.0.0</spring.framework>
<serializer.version>2.8.1</serializer.version>
<httpclient.version>5.1.3</httpclient.version>
<commons-io.version>2.11.0</commons-io.version>
<slf4j-api.version>2.0.2</slf4j-api.version>
<json.version>20220924</json.version>
<guava.version>[31.1-jre,)</guava.version>
<spring.framework>6.1.14</spring.framework>
<httpclient.version>5.4</httpclient.version>
<commons-io.version>2.17.0</commons-io.version>
<slf4j-api.version>2.0.16</slf4j-api.version>
<json.version>20240303</json.version>
<guava.version>33.3.1-jre</guava.version>
<junit.version>4.13.2</junit.version>
<testng.version>7.6.1</testng.version> <!-- 7.6.1 has vulnerabilities from SnakeYAML 1.30 -->
<snakeyaml.version>1.33</snakeyaml.version> <!-- Temporary replacement dependency -->
<gson.version>2.9.1</gson.version>
<httpmime.version>4.5.13</httpmime.version>
<jakarta.xml.ws.version>4.0.0</jakarta.xml.ws.version>
<testng.version>7.10.2</testng.version>
<gson.version>2.11.0</gson.version>
<jakarta.xml.ws.version>4.0.2</jakarta.xml.ws.version>

<!-- Plugins -->
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
<maven-javadoc-plugin.version>3.4.1</maven-javadoc-plugin.version>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
<maven-javadoc-plugin.version>3.10.1</maven-javadoc-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<nexus-staging-maven-plugin.version>1.7.0</nexus-staging-maven-plugin.version>
<maven-gpg-plugin.version>3.2.7</maven-gpg-plugin.version>
</properties>

<build>
Expand Down Expand Up @@ -170,32 +167,13 @@
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<!-- This 1.30 dependency have several vulnerabilities-->
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- This version does not have any known vulnerabilities atm. related to the SnakeYAML dependency in TestNG -->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpmime.version}</version>
</dependency>
<dependency>
<!-- JDK 11+ dependency -->
<groupId>jakarta.xml.ws</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.util.Objects;
import java.util.function.Supplier;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand Down Expand Up @@ -681,7 +681,7 @@ public JSONObject createFolder(String baseFolderRemoteRelativeUrl, String folder

@Override
public JSONObject moveFolder(String sourceRelativeServerUrl, String destinyRelativeServerUrl) throws Exception {
LOG.debug("createFolder sourceRelativeServerUrl {} destinyRelativeServerUrl {}", sourceRelativeServerUrl, destinyRelativeServerUrl);
LOG.debug("moveFolder sourceRelativeServerUrl {} destinyRelativeServerUrl {}", sourceRelativeServerUrl, destinyRelativeServerUrl);
headers = headerHelper.getPostHeaders("");

RequestEntity<String> requestEntity = new RequestEntity<>("",
Expand All @@ -703,7 +703,7 @@ public JSONObject moveFolder(String sourceRelativeServerUrl, String destinyRelat
*/
@Override
public JSONObject moveFile(String sourceRelativeServerUrl, String destinyRelativeServerUrl) throws Exception {
LOG.debug("createFolder sourceRelativeServerUrl {} destinyRelativeServerUrl {}", sourceRelativeServerUrl, destinyRelativeServerUrl);
LOG.debug("moveFile sourceRelativeServerUrl {} destinyRelativeServerUrl {}", sourceRelativeServerUrl, destinyRelativeServerUrl);
headers = headerHelper.getPostHeaders("");

RequestEntity<String> requestEntity = new RequestEntity<>("",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.panxoloto.sharepoint.rest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.function.Supplier;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.hc.client5.http.auth.AuthSchemeFactory;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.NTCredentials;
import org.apache.hc.client5.http.auth.StandardAuthScheme;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.auth.NTLMSchemeFactory;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.config.Lookup;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand Down Expand Up @@ -60,10 +66,17 @@ public PLGSharepointOnPremisesClient(String user,
String passwd, String domain, String spSiteUrl, String spSitePrefix) {
super();

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(user, passwd, spSiteUrl, domain));
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(null, -1), new NTCredentials(user, passwd.toCharArray(), spSiteUrl, domain));
Lookup<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
.register(StandardAuthScheme.NTLM, new NTLMSchemeFactory()).build();
RequestConfig requestConfig = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Collections.singletonList(StandardAuthScheme.NTLM)).build();

CloseableHttpClient httpClient = httpClientBuilderSupplier.get()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultAuthSchemeRegistry(authSchemeRegistry)
.setDefaultRequestConfig(requestConfig)
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
Expand Down Expand Up @@ -565,7 +578,11 @@ public JSONObject uploadFile(String folder, Resource resource, String fileName,
MultiValueMap<String, String> headers = headerHelper.getPostHeaders("");
headers.remove("Content-Length");

RequestEntity<Resource> requestEntity = new RequestEntity<>(resource,
byte[] bytes = resource.getContentAsByteArray();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to Change into byte[] because without Content-Length header upload result was corrupted file
so used byte[] bytes = resource.getContentAsByteArray(); to get file upload size

headers.add("Content-Length","" + bytes.length);
LOG.debug("Content-Length {}", bytes.length);

RequestEntity<byte[]> requestEntity = new RequestEntity<>(bytes,
headers, HttpMethod.POST,
this.tokenHelper.getSharepointSiteUrl(
"/_api/web/GetFolderByServerRelativeUrl('" + folder
Expand Down Expand Up @@ -719,7 +736,7 @@ public JSONObject createFolder(String baseFolderRemoteRelativeUrl, String folder
*/
@Override
public JSONObject moveFolder(String sourceRelativeServerUrl, String destinyRelativeServerUrl) throws Exception {
LOG.debug("createFolder sourceRelativeServerUrl {} destinyRelativeServerUrl {}", new Object[] {sourceRelativeServerUrl, destinyRelativeServerUrl});
LOG.debug("moveFolder sourceRelativeServerUrl {} destinyRelativeServerUrl {}", new Object[] {sourceRelativeServerUrl, destinyRelativeServerUrl});
MultiValueMap<String, String> headers = headerHelper.getPostHeaders("");

RequestEntity<String> requestEntity = new RequestEntity<>("",
Expand All @@ -741,7 +758,7 @@ public JSONObject moveFolder(String sourceRelativeServerUrl, String destinyRelat
*/
@Override
public JSONObject moveFile(String sourceRelativeServerUrl, String destinyRelativeServerUrl) throws Exception {
LOG.debug("createFolder sourceRelativeServerUrl {} destinyRelativeServerUrl {}", new Object[] {sourceRelativeServerUrl, destinyRelativeServerUrl});
LOG.debug("moveFile sourceRelativeServerUrl {} destinyRelativeServerUrl {}", new Object[] {sourceRelativeServerUrl, destinyRelativeServerUrl});
MultiValueMap<String, String> headers = headerHelper.getPostHeaders("");

RequestEntity<String> requestEntity = new RequestEntity<>("",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestFactory;
Expand Down Expand Up @@ -91,13 +91,13 @@ public DeferredCloseClientHttpResponse(ClientHttpResponse delegate) {
private boolean isStream = false;

@Override
public HttpStatus getStatusCode() throws IOException {
public HttpStatusCode getStatusCode() throws IOException {
return delegate.getStatusCode();
}

@Override
public int getRawStatusCode() throws IOException {
return delegate.getRawStatusCode();
return delegate.getStatusCode().value();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import javax.xml.transform.TransformerException;

import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
import java.util.function.Supplier;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.entity.mime.StringBody;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -74,20 +74,17 @@ public String getToken() {
private void getTenantId() throws Exception {
String url = siteURL+"/_vti_bin/client.svc/";
HttpGet get = new HttpGet(url);
get.setHeader(org.apache.http.HttpHeaders.AUTHORIZATION, "Bearer");
get.setHeader(HttpHeaders.AUTHORIZATION, "Bearer");
CloseableHttpClient httpClient = httpClientBuilderSupplier.get().build();

try (CloseableHttpResponse response = httpClient.execute(get, (HttpContext) null)) {
Header[] headers = response.getHeaders("WWW-Authenticate");
for (Header h : headers) {
HeaderElement[] elements = h.getElements();
for (HeaderElement e : elements) {
if ("Bearer realm".equals(e.getName())) {
spOnlineRealm = e.getValue();
}
if ("client_id".equals(e.getName())) {
spOnlineClientId = e.getValue();
}
if ("Bearer realm".equals(h.getName())) {
spOnlineRealm = h.getValue();
}
if ("client_id".equals(h.getName())) {
spOnlineClientId = h.getValue();
}
}
}
Expand Down Expand Up @@ -124,7 +121,7 @@ private HttpEntity fillInSPOnlineTokenRequestData() throws MalformedURLException
String resourceStr = spOnlineClientId + "/" + url.getHost() + "@" + spOnlineRealm;

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setMode(HttpMultipartMode.LEGACY);
StringBody stringBody = new StringBody("client_credentials", ContentType.MULTIPART_FORM_DATA);
builder.addPart("grant_type", stringBody);
stringBody = new StringBody(clientId, ContentType.MULTIPART_FORM_DATA);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.panxoloto.sharepoint.rest.helper;

import org.apache.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

Expand Down