此服务中的方法应按以下方式调用:
Methods from this service should be called as follows:
package com.waveaccess.someproject.commons.service;
import com.google.common.base.Joiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.Date;
import java.util.List;
/**
* @author Maksim Kanev
*/
@Service
public class SharePointService {
private static final Logger log = LoggerFactory.getLogger(SharePointService.class);
@Autowired
private SharePointServiceCached serviceCached;
@Autowired
private RestTemplate restTemplate;
public String performHttpRequest(HttpMethod method, String path) throws Exception {
Long executionDateTime = serviceCached.parseExecutionDateTime(new Date());
String securityToken = serviceCached.receiveSecurityToken(executionDateTime);
List<String> cookies = serviceCached.getSignInCookies(securityToken);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Cookie", Joiner.on(';').join(cookies));
RequestEntity<String> requestEntity = new RequestEntity<>(headers, method, new URI(path));
ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
String responseBody = responseEntity.getBody();
log.debug(responseBody);
return responseBody;
}
public String performHttpRequest(String path, String json, boolean isUpdate, boolean isWithDigest) throws Exception {
Long executionDateTime = serviceCached.parseExecutionDateTime(new Date());
String securityToken = serviceCached.receiveSecurityToken(executionDateTime);
List<String> cookies = serviceCached.getSignInCookies(securityToken);
String formDigestValue = serviceCached.getFormDigestValue(cookies);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Cookie", Joiner.on(';').join(cookies));
headers.add("Content-type", "application/json;odata=verbose");
if (isWithDigest) {
headers.add("X-RequestDigest", formDigestValue);
}
if (isUpdate) {
headers.add("X-HTTP-Method", "MERGE");
headers.add("IF-MATCH", "*");
}
RequestEntity<String> requestEntity = new RequestEntity<>(json, headers, HttpMethod.POST, new URI(path));
ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
String responseBody = responseEntity.getBody();
log.debug(responseBody);
return responseBody;
}
public String attachFile(String path, byte[] file) throws Exception {
Long executionDateTime = serviceCached.parseExecutionDateTime(new Date());
String securityToken = serviceCached.receiveSecurityToken(executionDateTime);
List<String> cookies = serviceCached.getSignInCookies(securityToken);
String formDigestValue = serviceCached.getFormDigestValue(cookies);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Cookie", Joiner.on(';').join(cookies));
headers.add("X-RequestDigest", formDigestValue);
headers.add("content-length", String.valueOf(file.length));
RequestEntity<byte[]> requestEntity = new RequestEntity<>(file, headers, HttpMethod.POST, new URI(path));
ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
String responseBody = responseEntity.getBody();
log.debug(responseBody);
return responseBody;
}
}
XPathExpressionFactoryBean
的配置:
package com.waveaccess.someproject.commons.config;
import com.waveaccess.someproject.commons.config.properties.SharePointProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.xml.xpath.XPathExpressionFactoryBean;
import java.util.HashMap;
import java.util.Map;
/**
* @author Maksim Kanev
*/
@Configuration
@EnableConfigurationProperties({SharePointProperties.class})
public class SharePointConfiguration {
@Bean
public XPathExpressionFactoryBean securityTokenExpressionFactoryBean() {
XPathExpressionFactoryBean xPathExpressionFactoryBean = new XPathExpressionFactoryBean();
xPathExpressionFactoryBean.setExpression("/S:Envelope/S:Body/wst:RequestSecurityTokenResponse/wst:RequestedSecurityToken/wsse:BinarySecurityToken");
Map<String, String> namespaces = new HashMap<>();
namespaces.put("S", "http://www.w3.org/2003/05/soap-envelope");
namespaces.put("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
namespaces.put("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
namespaces.put("wsa", "http://www.w3.org/2005/08/addressing");
namespaces.put("wst", "http://schemas.xmlsoap.org/ws/2005/02/trust");
xPathExpressionFactoryBean.setNamespaces(namespaces);
return xPathExpressionFactoryBean;
}
}
最后是 SharePointProperties
:
package com.waveaccess.someproject.commons.config.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author Maksim Kanev
*/
@ConfigurationProperties("sharepoint")
public class SharePointProperties {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}