package com.sun.xml.internal.ws.client.sei;
import com.sun.xml.internal.ws.api.message.Attachment;
import com.sun.xml.internal.ws.api.message.Headers;
import com.sun.xml.internal.ws.api.message.Message;
import com.sun.xml.internal.ws.message.ByteArrayAttachment;
import com.sun.xml.internal.ws.message.DataHandlerAttachment;
import com.sun.xml.internal.ws.message.JAXBAttachment;
import com.sun.xml.internal.ws.model.ParameterImpl;
import com.sun.xml.internal.ws.spi.db.XMLBridge;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.UUID;
import javax.activation.DataHandler;
import javax.xml.transform.Source;
import javax.xml.ws.WebServiceException;
abstract class MessageFiller {
protected final int methodPos;
protected MessageFiller( int methodPos) {
this.methodPos = methodPos;
}
abstract void fillIn(Object[] methodArgs, Message msg);
static abstract class AttachmentFiller extends MessageFiller {
protected final ParameterImpl param;
protected final ValueGetter getter;
protected final String mimeType;
private final String contentIdPart;
protected AttachmentFiller(ParameterImpl param, ValueGetter getter) {
super(param.getIndex());
this.param = param;
this.getter = getter;
mimeType = param.getBinding().getMimeType();
try {
contentIdPart = URLEncoder.encode(param.getPartName(), "UTF-8")+'=';
} catch (UnsupportedEncodingException e) {
throw new WebServiceException(e);
}
}
public static MessageFiller createAttachmentFiller(ParameterImpl param, ValueGetter getter) {
Class type = (Class)param.getTypeInfo().type;
if (DataHandler.class.isAssignableFrom(type) || Source.class.isAssignableFrom(type)) {
return new DataHandlerFiller(param, getter);
} else if (byte[].class==type) {
return new ByteArrayFiller(param, getter);
} else if(isXMLMimeType(param.getBinding().getMimeType())) {
return new JAXBFiller(param, getter);
} else {
return new DataHandlerFiller(param, getter);
}
}
String getContentId() {
return contentIdPart+UUID.randomUUID()+"@jaxws.sun.com";
}
}
private static class ByteArrayFiller extends AttachmentFiller {
protected ByteArrayFiller(ParameterImpl param, ValueGetter getter) {
super(param, getter);
}
void fillIn(Object[] methodArgs, Message msg) {
String contentId = getContentId();
Object obj = getter.get(methodArgs[methodPos]);
Attachment att = new ByteArrayAttachment(contentId,(byte[])obj,mimeType);
msg.getAttachments().add(att);
}
}
private static class DataHandlerFiller extends AttachmentFiller {
protected DataHandlerFiller(ParameterImpl param, ValueGetter getter) {
super(param, getter);
}
void fillIn(Object[] methodArgs, Message msg) {
String contentId = getContentId();
Object obj = getter.get(methodArgs[methodPos]);
DataHandler dh = (obj instanceof DataHandler) ? (DataHandler)obj : new DataHandler(obj,mimeType);
Attachment att = new DataHandlerAttachment(contentId, dh);
msg.getAttachments().add(att);
}
}
private static class JAXBFiller extends AttachmentFiller {
protected JAXBFiller(ParameterImpl param, ValueGetter getter) {
super(param, getter);
}
void fillIn(Object[] methodArgs, Message msg) {
String contentId = getContentId();
Object obj = getter.get(methodArgs[methodPos]);
Attachment att = new JAXBAttachment(contentId, obj, param.getXMLBridge(), mimeType);
msg.getAttachments().add(att);
}
}
static final class extends MessageFiller {
private final XMLBridge ;
private final ValueGetter ;
protected (int methodPos, XMLBridge bridge, ValueGetter getter) {
super(methodPos);
this.bridge = bridge;
this.getter = getter;
}
void (Object[] methodArgs, Message msg) {
Object value = getter.get(methodArgs[methodPos]);
msg.getHeaders().add(Headers.create(bridge,value));
}
}
private static boolean isXMLMimeType(String mimeType){
return (mimeType.equals("text/xml") || mimeType.equals("application/xml"));
}
}