|
Key
This line was removed.
This word was removed. This word was added.
This line was added.
|
Changes (8)
View Page Historyh1. Implementing a web method that deals with complex types and attachments
This example is an upload method: it receives path + file name information (where to store the file) as a parameter, and a file to upload (as a binary attachment).
h2. Text File Extraction
Note that the example contains a bug workaround (as of Petals 4 early releases) : the 1st attachment, if any, is ignored as it contains... a copy of the payload + soap envelope (sic). This may be fixed in your Petals release (and this hack should disappear from this doc as soon as the fix is spread enough).
This example receives a text file as an attachment and returns its content to the client.
Attachment's style is MTOM.
Attachment's style is MTOM.
{code}
package com.ebmwebsourcing;
package com.ebmwebsourcing;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
/**
* Here is a sample JAX-WS implementation.
*/
@WebService( serviceName="Test", targetNamespace="http://ebmwebsourcing.com", portName="TestPort" )
@BindingType(SOAPBinding.SOAP11HTTP_MTOM_BINDING)
public class Test {
/* (non-Javadoc)
* @see JaxWSInterface#HelloWorld()
*/
@WebMethod( operationName="extractContent" )
@WebResult( name="returnMessage" )
public String extractContent( DataHandler textDocument ) {
String result = "An error occurred.";
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
copyStream( textDocument.getInputStream(), os );
result = os.toString( "UTF-8" );
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* Copies the content from in into os.
* <p>
* Neither <i>in</i> nor <i>os</i> are closed by this method.<br />
* They must be explicitly closed after this method is called.
* </p>
*
* @param in
* @param os
* @throws IOException
*/
public static void copyStream( InputStream in, OutputStream os ) throws IOException {
byte[] buf = new byte[ 1024 ];
int len;
while((len = in.read( buf )) > 0) {
os.write( buf, 0, len );
}
}
}
{code}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
/**
* Here is a sample JAX-WS implementation.
*/
@WebService( serviceName="Test", targetNamespace="http://ebmwebsourcing.com", portName="TestPort" )
@BindingType(SOAPBinding.SOAP11HTTP_MTOM_BINDING)
public class Test {
/* (non-Javadoc)
* @see JaxWSInterface#HelloWorld()
*/
@WebMethod( operationName="extractContent" )
@WebResult( name="returnMessage" )
public String extractContent( DataHandler textDocument ) {
String result = "An error occurred.";
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
copyStream( textDocument.getInputStream(), os );
result = os.toString( "UTF-8" );
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* Copies the content from in into os.
* <p>
* Neither <i>in</i> nor <i>os</i> are closed by this method.<br />
* They must be explicitly closed after this method is called.
* </p>
*
* @param in
* @param os
* @throws IOException
*/
public static void copyStream( InputStream in, OutputStream os ) throws IOException {
byte[] buf = new byte[ 1024 ];
int len;
while((len = in.read( buf )) > 0) {
os.write( buf, 0, len );
}
}
}
{code}
h2. Upload method in SU main class (JAX-WS annotated)
This example is an upload method: it receives path + file name information (where to store the file) as a parameter, and a file to upload (as a binary attachment). In this example, we retrieve attachments directly from the Petals exchange.
{code}
@WebMethod( operationName="upload" )
@WebMethod( operationName="upload" )
/**
* Here is a sample JAX-WS implementation.
* Here is a sample JAX-WS implementation.
* <p>
* For more information about JAX-WS, please visit
* <b>https://jax-ws.dev.java.net/jax-ws-ea3/docs/annotations.html</b>.
* </p>
* For more information about JAX-WS, please visit
* <b>https://jax-ws.dev.java.net/jax-ws-ea3/docs/annotations.html</b>.
* </p>
*/
@WebService( serviceName="MailProxyService", targetNamespace="http://sample.ow2.org", portName="MailProxyServicePort" )
@WebService( serviceName="MailProxyService", targetNamespace="http://sample.ow2.org", portName="MailProxyServicePort" )