Skip to main content

Cannot make jax-ws webservice send response with attachments.

No replies
rwi
Offline
Joined: 2012-11-05

How can I send responses from web-service with attachments.
I have an issue with jax-ws web-service presented as an ejb steteless bean.
The method "getConfig" should return a packed string as an attachment.
But nothing works as it should as you can see in soapUI:
soapUI

class "MyService"

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

import javax.activation.DataHandler;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebParam.Mode;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
import javax.xml.bind.DatatypeConverter;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

@SOAPBinding(
parameterStyle = ParameterStyle.WRAPPED,
style = Style.DOCUMENT,
use = Use.LITERAL)
@Stateless(
description = "stateless description",
mappedName = "EJB-mapped-service",
name = "EJB-WS-MyWeb")
@WebService(
name = "MyWeb",
portName = "MyWebPort",
serviceName = "services")
public class MyService implements MyServiceRemote, MyServiceLocal {

@Resource(
description = "web service context",
name = "webServiceContext")
private WebServiceContext webServiceContext;

@WebMethod(
action = "getConfig",
operationName = "getConfig")
public void getConfig(
@WebParam(
mode = Mode.IN,
name = "name",
partName = "PartName")
String name)
throws Exception {

String fileContent = "aaaabbbbbcccccdddddeeeee";

MessageContext mc = webServiceContext.getMessageContext();
Map<String, DataHandler> attachments = new HashMap<String, DataHandler>();

DataHandler dataHandler = new DataHandler(packXmlData(fileContent), "application/gzip");
attachments.put("RETURN_FROM_SERVER", dataHandler);

if (mc.containsKey(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS)) {
mc.remove(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);
}

mc.put(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS, attachments);
}

public static Object packXmlData(String source) {

ByteArrayOutputStream buffer = new ByteArrayOutputStream(source.length() * 2);
try {
GZIPOutputStream out = new GZIPOutputStream(buffer);
out.write(source.getBytes());
out.flush();
out.close();
} catch (Exception e) {
return new ByteArrayOutputStream();
}

String encode = DatatypeConverter.printBase64Binary(buffer.toByteArray());

return encode;
}
}

Interface "MyServiceRemote"

import javax.ejb.Remote;

@Remote
public interface MyServiceRemote {
void getConfig(String name) throws Exception;
}

Interface "MyServiceLocal"

import javax.ejb.Local;

@Local
public interface MyServiceLocal {
void getConfig(String name) throws Exception;
}

Please, help.

AttachmentSize
EJBPro.zip2.72 KB
soap.png37.33 KB