h2. Introduction
This document is a complement to Petals SE-JSR181 component documentation. It assumes that you already know how to generate a JSR181 SU using PetalsStudio : most code examples detailed here are embedded in the JSR181 SU main class, pointed as <jsr181:class> in the jbi.xml of the SU.
h2. 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).
h3. Upload method in SU main class (JAX-WS annotated)
{code}
@WebMethod( operationName="upload" )
public String upload(@WebParam( name="destination" ) UploadDestination dest) {
// Get the JBI context
JBIContext jbiContext = JBIContextManager.getJBIContext();
Exchange exchange = jbiContext.getExchange();
try {
Set<DataHandler> atts = exchange.getInMessageAttachments();
int n = 0;
for(DataHandler att : atts) {
if(n == 1) {
FileOutputStream out = new FileOutputStream(dest.getPath() + File.separator + dest.getName());
InputStream in = att.getInputStream();
byte buf[] = new byte[256];
while(in.read(buf) >= 0) {
out.write(buf);
}
out.close();
}
n++;
}
} catch (Exception e) {
Logger.getLogger( getClass().getName()).severe("Upload error: " + e.getMessage());
}
return "upload destination=" + dest;
}
{code}
h3. Class for complex type parameter
This class is a simple JavaBean (POJO with getter/setter methods for each field : here, fields are "path" and "name", as the class represents an "upload destination" = a file).
{code}
package org.ow2.petals.usecase.jsr181;
public class UploadDestination {
String name_ = "/tmp";
String path_ = "petals-attachment";
public String getName() {
return name_;
}
public void setName(String name) {
this.name_ = name;
}
public String getPath() {
return path_;
}
public void setPath(String path) {
this.path_ = path;
}
public String toString() { return (path_ == null ? "" : path_) + "/" + name_; }
}
{code}
This document is a complement to Petals SE-JSR181 component documentation. It assumes that you already know how to generate a JSR181 SU using PetalsStudio : most code examples detailed here are embedded in the JSR181 SU main class, pointed as <jsr181:class> in the jbi.xml of the SU.
h2. 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).
h3. Upload method in SU main class (JAX-WS annotated)
{code}
@WebMethod( operationName="upload" )
public String upload(@WebParam( name="destination" ) UploadDestination dest) {
// Get the JBI context
JBIContext jbiContext = JBIContextManager.getJBIContext();
Exchange exchange = jbiContext.getExchange();
try {
Set<DataHandler> atts = exchange.getInMessageAttachments();
int n = 0;
for(DataHandler att : atts) {
if(n == 1) {
FileOutputStream out = new FileOutputStream(dest.getPath() + File.separator + dest.getName());
InputStream in = att.getInputStream();
byte buf[] = new byte[256];
while(in.read(buf) >= 0) {
out.write(buf);
}
out.close();
}
n++;
}
} catch (Exception e) {
Logger.getLogger( getClass().getName()).severe("Upload error: " + e.getMessage());
}
return "upload destination=" + dest;
}
{code}
h3. Class for complex type parameter
This class is a simple JavaBean (POJO with getter/setter methods for each field : here, fields are "path" and "name", as the class represents an "upload destination" = a file).
{code}
package org.ow2.petals.usecase.jsr181;
public class UploadDestination {
String name_ = "/tmp";
String path_ = "petals-attachment";
public String getName() {
return name_;
}
public void setName(String name) {
this.name_ = name;
}
public String getPath() {
return path_;
}
public void setPath(String path) {
this.path_ = path;
}
public String toString() { return (path_ == null ? "" : path_) + "/" + name_; }
}
{code}