Posts Tagged ‘web services’
Upload file to Microsoft Office Sharepoint Server 2007
Microsoft Office Sharepoint Server 2007 (aka MOSS2007) provides a web services catalog in order to perform a large collection of operations on it. Morever, if enabled, WebDAV access is available.
The obvious way to upload a file to MOSS Shared Documents is the use of WebDAV protocol, however Microsoft includes a web service to perform this operation additionally. Below, both methods are shown.
Using WebDAV protocol
Required libraries: commons-httpclient-3.1.jar, commons-codec-1.3.jar, commons-logging-1.1.1.jar, log4j-1.2.15.jar, slf4j-api-1.5.6.jar, slf4j-log4j12-1.5.6.jar, jackrabbit-webdav-1.4.jar
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;
public class UploadFileMOSSWebDAV {
// Proxy parameters
static String proxyHost = "proxy.server.com";
static Integer proxyPort = 80;
static String proxyUser = "user";
static String proxyPass = "pass";
// Sharepoint parameters
static String sharepointUser = "user";
static String sharepointPass = "pass";
static String sharepointDomain = "DOMAIN";
// Shared Documents folder root
static String sharedDocumentsRoot = "http://sharepoint.server.com/sites/IdSite/Shared Documents/";
/**
* Note: Setting credentials with AuthScope.ANY authentication scope (null value for host and/or realm) is
* highly discouraged in production applications.
* @param args
* @throws Exception
*/
public static void main(String... args) throws Exception {
// HTTP client with authentication enabled
HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
// Proxy configuration
client.getHostConfiguration().setProxy(proxyHost, proxyPort);
client.getState().setProxyCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(proxyUser, proxyPass));
// Sharepoint authentication
Credentials defaultcreds =
new NTCredentials(sharepointUser, sharepointPass, "localhost", sharepointDomain);
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
// Upload local file test.txt to sharedDocumentsRoot/SomeFolder/test.txt
PutMethod method = new PutMethod(sharedDocumentsRoot + "SomeFolder/test.txt");
RequestEntity requestEntity =
new InputStreamRequestEntity(new FileInputStream(new File("c:/temp/test.txt")));
method.setRequestEntity(requestEntity);
client.executeMethod(method);
System.out.println(method.getStatusCode() + " "+ method.getStatusText());
}
}
Using MOSS Copy web service
Required libraries: commons-httpclient-3.1.jar, commons-codec-1.3.jar, commons-logging-1.1.1.jar, log4j-1.2.15.jar, activation-1.1.1.jar, saaj-api-1.3.jar, saaj-impl-1.3.2.jar, spring-core-2.5.6.jar, spring-context-2.5.6.jar, spring-beans-2.5.6.jar, spring-oxm-1.5.6.jar, spring-ws-core-1.5.6.jar, spring-xml-1.5.6.jar, xmlbeans tool
Resources: Getting started with Sharepoint Web Services
1. Retrieve WSDL from Sharepoint
WSDL for Copy web service can be found at http://sharepoint.server.com/sites/IdSite/_vti_bin/Copy.asmx?WSDL
2. Extract XSD from WSDL
Extract “s:schema” node from WSDL and include namespaces on root element
< ?xml version="1.0" encoding="utf-8"?>
<s_schema xmlns_s="http://www.w3.org/2001/XMLSchema"
xmlns_tns="http://schemas.microsoft.com/sharepoint/soap/"
elementFormDefault="qualified"
targetNamespace="http://schemas.microsoft.com/sharepoint/soap/">
[...]
</s_schema>
Note. “:” replaced by “_” for visualization.
3. Generate XMLBeans objects for marshal
Use XMLBeans Ant Task to generate Java objects in order to perform marshalling and unmarshalling operations from XML
4. Web service client
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.util.Base64;
import org.springframework.oxm.xmlbeans.XmlBeansMarshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;
import org.springframework.ws.transport.http.CommonsHttpMessageSender;
import com.microsoft.schemas.sharepoint.soap.CopyIntoItemsDocument;
import com.microsoft.schemas.sharepoint.soap.DestinationUrlCollection;
import com.microsoft.schemas.sharepoint.soap.CopyIntoItemsDocument.CopyIntoItems;
public class UploadFileMOSSWS extends WebServiceGatewaySupport {
// Proxy parameters
static String proxyHost = "proxy.server.com";
static Integer proxyPort = 80;
static String proxyUser = "user";
static String proxyPass = "pass";
// Sharepoint parameters
static String sharepointUser = "user";
static String sharepointPass = "pass";
static String sharepointDomain = "DOMAIN";
// Shared Documents folder root
static String sharedDocumentsRoot = "http://sharepoint.server.com/sites/IdSite/Shared Documents/";
// WS Uri
static String defaultWSUri = "http://sharepoint.server.com/sites/IdSite/_vti_bin/Copy.asmx";
/**
* Note: Setting credentials with AuthScope.ANY authentication scope (null value for host and/or realm) is
* highly discouraged in production applications.
* @param args
* @throws Exception
*/
public static void main(String... args) throws Exception {
// XMLBeans request object
CopyIntoItemsDocument request = CopyIntoItemsDocument.Factory.newInstance();
CopyIntoItems cii = request.addNewCopyIntoItems();
DestinationUrlCollection duc = DestinationUrlCollection.Factory.newInstance();
duc.addNewString().setStringValue(sharedDocumentsRoot + "SomeFolder/test.txt");
cii.setDestinationUrls(duc);
cii.addNewFields().addNewFieldInformation();
// Sample file
String fileContent = "";
FileInputStream fis = new FileInputStream(new File("c:/temp/test.txt"));
while (fis.available() > 0) {
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fileContent = fileContent + new String(buffer);
}
cii.setStream(Base64.encode(fileContent.getBytes()));
// MOSS WS Copy File invocation with SOAP Action CopyIntoItems
System.out.println(new UploadFileMOSSWS().
callWs(request, "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"));
}
/**
* Generic Spring WS Client
* @param input
* @param soapAction
* @return
*/
private XmlObject callWs(XmlObject input, String soapAction) {
XmlObject result = null;
// WS Client
WebServiceTemplate wst = getWebServiceTemplate();
XmlBeansMarshaller marshaller = new XmlBeansMarshaller();
wst.setMarshaller(marshaller);
wst.setUnmarshaller(marshaller);
wst.setDefaultUri(defaultWSUri);
// HTTP Client configuration
HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
client.getHostConfiguration().setProxy(proxyHost, proxyPort);
client.getState().setProxyCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(proxyUser, proxyPass));
Credentials defaultcreds =
new NTCredentials(sharepointUser, sharepointPass, "localhost", sharepointDomain);
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
CommonsHttpMessageSender messageSender = new CommonsHttpMessageSender(client);
wst.setMessageSender(messageSender);
// WS invocation with SOAP Action
result = (XmlObject)wst.marshalSendAndReceive(input, new SoapActionCallback(soapAction));
return result;
}
}
WebSphere 6.1 web service client using SSL
It’s hard to develop web services using native WebSphere 6.1 support without the aid of IBM developing tools such as Application Server Toolkit or Rational Application Developer. Even it’s hard to find, download and install these tools.
IBM provides a bunch of detailed information and manuals on every product, but this fact does not guarantee sometimes an easy way to desired solution.
How to invoke a web service using SSL? The response to this question can be found on many places: IBM Redbooks, some articles from IBM, official product documentation… And all of them base the solution on the use of Rational Application Developer to modify XML web service descriptor ibm-webservicesclient-bnd.xmi. This should be fine, if Rational Application Developer were a free tool.
To avoid the use of this product, XML manual modification it’s also possible according to this information. Just only including a reference to a WebSphere Application Server SSL Configuration makes the work:
<sslconfig name="mynode/DefaultSSLSettings"/>
I’ve never understand IBM’s vision, in my opinion information must be sufficient but not extensive.
AXIS2 + JIBX web service client step by step
Below a low level tutorial on generating web services clients based on AXIS2 and JIBX marshalling. In real world, many of this tasks can be automatized using some tool like ANT or MAVEN.
0. Required tools
1. Initial resources
- XSD file for JIBX
- WSDL file for AXIS2
service.xsd
< ?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ts="http://foo.bar.com"
targetNamespace="http://foo.bar.com"
elementFormDefault="qualified" attributeFormDefault="qualified">
<element name="fooRequest">
<complextype>
<sequence>
<element name="requestHeader">
<complextype>
<sequence>
<element name="type" type="string"/>
<element name="version" type="string"/>
</sequence>
</complextype>
</element>
<element name="requestData">
<complextype>
<sequence>
<element name="idObject" type="int"/>
</sequence>
</complextype>
</element>
</sequence>
</complextype>
</element>
<element name="fooResponse">
<complextype>
<sequence>
<element name="responseHeader">
<complextype>
<sequence>
<element name="type" type="string"/>
<element name="version" type="string"/>
<element name="responseCode" type="string"/>
<element name="responseDesc" type="string"/>
</sequence>
</complextype>
</element>
<element name="responseData" minOccurs="0">
<complextype>
<sequence>
<element name="name" type="string"/>
<element name="attributes" type="string"/>
</sequence>
</complextype>
</element>
</sequence>
</complextype>
</element>
</schema>
service.wsdl
< ?xml version="1.0" encoding="UTF-8"?>
<wsdl_definitions xmlns_wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns_schema="http://foo.bar.com"
xmlns_soap="http://schemas.xmlsoap.org/wsdl/soap/"
targetNamespace="http://foo.bar.com">
<wsdl_types>
<schema xmlns="http_//www.w3.org/2001/XMLSchema"
xmlns_ts="http://foo.bar.com"
targetNamespace="http://foo.bar.com"
elementFormDefault="qualified" attributeFormDefault="qualified">
<element name="fooRequest">
<complextype>
<sequence>
<element name="requestHeader">
<complextype>
<sequence>
<element name="type" type="string"/>
<element name="version" type="string"/>
</sequence>
</complextype>
</element>
<element name="requestData">
<complextype>
<sequence>
<element name="idObject" type="int"/>
</sequence>
</complextype>
</element>
</sequence>
</complextype>
</element>
<element name="fooResponse">
<complextype>
<sequence>
<element name="responseHeader">
<complextype>
<sequence>
<element name="type" type="string"/>
<element name="version" type="string"/>
<element name="responseCode" type="string"/>
<element name="responseDesc" type="string"/>
</sequence>
</complextype>
</element>
<element name="responseData" minOccurs="0">
<complextype>
<sequence>
<element name="name" type="string"/>
<element name="attributes" type="string"/>
</sequence>
</complextype>
</element>
</sequence>
</complextype>
</element>
</schema>
</wsdl_types>
<wsdl_message name="fooRequest">
<wsdl_part element="schema_fooRequest" name="fooRequest"/>
</wsdl_message>
<wsdl_message name="fooResponse">
<wsdl_part element="schema_fooResponse" name="fooResponse"/>
</wsdl_message>
<wsdl_porttype name="Foo">
<wsdl_operation name="foo">
<wsdl_input message="schema_fooRequest" name="fooRequest"/>
<wsdl_output message="schema_fooResponse" name="fooResponse"/>
</wsdl_operation>
</wsdl_porttype>
<wsdl_binding name="FooBinding" type="schema_Foo">
<soap_binding style="document" transport="http_//schemas.xmlsoap.org/soap/http"/>
<wsdl_operation name="foo">
<soap_operation soapAction=""/>
<wsdl_input name="fooRequest">
<soap_body use="literal"/>
</wsdl_input>
<wsdl_output name="fooResponse">
<soap_body use="literal"/>
</wsdl_output>
</wsdl_operation>
</wsdl_binding>
<wsdl_service name="FooService">
<wsdl_port binding="schema_FooBinding" name="FooPort">
<soap_address location="http_//localhost/APP/services"/>
</wsdl_port>
</wsdl_service>
</wsdl_definitions>
Note. Character “:” changed by “_” for visualization purpouses in “service.wsdl”
2. Generate JIBX binding
[Previous tasks: copy service.xsd file on JIBX home directory]
Command line (JIBX home)
Input: java -jar xsd2jibx.jar service.xsd
Output: com/bar/foo/binding.xml, com/bar/foo/FooRequest.java, com/bar/foo/FooRequestRequestData.java, com/bar/foo/FooRequestRequestHeader.java, com/bar/foo/FooResponse.java, com/bar/foo/FooResponseResponseData.java, com/bar/foo/FooResponseResponseHeader.java
3. Java compiler
Command line (JIBX home)
Input: javac -sourcepath com/bar/foo/*.java
Output: com/bar/foo/FooRequest.class, com/bar/foo/FooRequestRequestData.class, com/bar/foo/FooRequestRequestHeader.class, com/bar/foo/FooResponse.class, com/bar/foo/FooResponseResponseData.class, com/bar/foo/FooResponseResponseHeader.class
4. Binding compiler
Command line (JIBX home)
Input: java -jar jibx-bind.jar com/bar/foo/binding.xml
Output: com/bar/foo/JiBX_com_bar_foo_bindingFactory.class, com/bar/foo/JiBX_com_bar_foo_bindingFooRequest_access.class, com/bar/foo/JiBX_com_bar_foo_bindingFooRequestRequestData_access.class, com/bar/foo/JiBX_com_bar_foo_bindingFooRequestRequestHeader_access.class, com/bar/foo/JiBX_com_bar_foo_bindingFooResponse_access.class, com/bar/foo/JiBX_com_bar_foo_bindingFooResponseResponseData_access.class, com/bar/foo/JiBX_com_bar_foo_bindingFooResponseResponseHeader_access.class
5. Generate AXIS2 client
[Previous tasks: copy service.wsdl file, binding.xml (generated on step 2) and "com" folder (generated on step 4) on AXIS2 home directory]
Command line (AXIS2 home)
Input: wsdl2java -uri service.wsdl -o build -ss -sd -g -d jibx -ssi -Ebindingfile binding.xml
Output: build/src/com/bar/foo/FooService.java, build/src/com/bar/foo/FooServiceCallbackHandler.java, build/src/com/bar/foo/FooServiceMessageReceiverInOut.java, build/src/com/bar/foo/FooServiceSkeleton.java, build/src/com/bar/foo/FooServiceSkeletonInterface.java, build/src/com/bar/foo/FooServiceStub.java
6. AXIS2 Client usage
[Previous tasks: config a Java project including all the resources generated by now, JIBX libraries and AXIS2 libraries.]
import com.bar.foo;
FooRequest fooRequest = new FooRequest();
FooRequestRequestHeader fooRequestHeader = new FooRequestHeader();
fooRequestHeader.setType("1");
fooRequestHeader.setVersion("1");
FooRequestRequestData fooRequestData = new FooRequestData();
fooRequestData.setIdObject(1);
fooRequest.setRequestHeader(fooRequestHeader);
fooRequest.setRequestData(fooRequestData);
FooServiceStub fooStub = new FooServiceStub();
FooResponse fooResponse = fooStub.foo(fooRequest);
System.out.println(fooResponse.getResponseData().getName());
System.out.println(fooResponse.getResponseData().getAttributes());