1 57 58 package org.apache.wsif.providers.soap.apacheaxis; 59 60 import java.awt.Image ; 61 import java.io.IOException ; 62 import java.io.InputStream ; 63 64 import javax.activation.DataHandler ; 65 import javax.mail.MessagingException ; 66 import javax.mail.internet.MimeMultipart ; 67 import javax.swing.ImageIcon ; 68 import javax.xml.namespace.QName ; 69 import javax.xml.soap.SOAPException ; 70 import javax.xml.transform.dom.DOMSource ; 71 import javax.xml.transform.sax.SAXSource ; 72 import javax.xml.transform.stream.StreamSource ; 73 74 import org.apache.axis.attachments.AttachmentPart; 75 import org.apache.axis.client.Call; 76 import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory; 77 import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory; 78 import org.apache.wsif.WSIFException; 79 import org.apache.wsif.WSIFMessage; 80 import org.apache.wsif.logging.Trc; 81 82 90 public class MIMEHelper { 91 92 public static void registerAttachmentType(Call call, QName type) { 93 call.registerTypeMapping( 94 DataHandler .class, 95 type, 96 JAFDataHandlerSerializerFactory.class, 97 JAFDataHandlerDeserializerFactory.class); 98 } 99 100 public static boolean addAttachementIfMIMEPart(Call call, Object o) { 101 Trc.entry(null, call, o); 102 boolean ok; 103 104 if (o instanceof DataHandler ) { 105 call.addAttachmentPart(new AttachmentPart((DataHandler )o)); 106 ok = true; 107 } else { 108 ok = false; 109 } 110 111 Trc.exit(new Boolean (ok)); 112 return ok; 113 } 114 115 public static AttachmentPart getAttachementPart(Object o) throws WSIFException { 116 Trc.entry(null, o); 117 AttachmentPart ap = null; 118 if (o instanceof DataHandler ) { 119 ap = new AttachmentPart((DataHandler ) o); 120 } else { 121 throw new WSIFException("Object is not a DataHandler: " + o); 122 } 123 Trc.exit(ap); 124 return ap; 125 } 126 127 public static void setMIMEMessagePart( 128 WSIFMessage msg, 129 String name, 130 Object value, 131 Class type) 132 throws WSIFException { 133 Trc.entry(null, msg, name, value, type); 134 135 Class valueType = value == null ? null : value.getClass(); 136 try { 137 if (valueType != null 138 && DataHandler .class.equals(type) 139 && AttachmentPart.class.isAssignableFrom(valueType)) { 140 AttachmentPart ap = (AttachmentPart) value; 141 DataHandler dh = ap.getDataHandler(); 142 msg.setObjectPart(name, dh); 143 } else if ( 144 valueType 147 != null 148 && (String .class.equals(type) 149 || Image .class.equals(type) 150 || StreamSource .class.equals(type) 151 || DOMSource .class.equals(type) 152 || SAXSource .class.equals(type) 153 || MimeMultipart .class.equals(type)) 154 && AttachmentPart.class.isAssignableFrom(valueType)) { 155 156 AttachmentPart ap = (AttachmentPart) value; 157 InputStream is = ap.getDataHandler().getInputStream(); 158 159 if (String .class.equals(type)) { 160 byte[] bBuff = new byte[is.available()]; 161 is.read(bBuff); 162 msg.setObjectPart(name, new String (bBuff)); 163 } else if (Image .class.equals(type)) { 164 byte[] bBuff = new byte[is.available()]; 165 is.read(bBuff); 166 msg.setObjectPart(name, new ImageIcon (bBuff).getImage()); 167 } else if (StreamSource .class.equals(type)) 168 msg.setObjectPart(name, new StreamSource (is)); 170 else if (DOMSource .class.equals(type)) 171 throw new WSIFException("DOMSource is not supported"); 172 else if (SAXSource .class.equals(type)) 173 throw new WSIFException("SAXSource is not supported"); 174 else if (MimeMultipart .class.equals(type)) 175 msg.setObjectPart( 177 name, 178 new MimeMultipart (ap.getDataHandler().getDataSource())); 179 } else if ( 180 valueType != null 181 && type != null && !type.isPrimitive() 183 && !(type.isAssignableFrom(valueType))) { 184 throw new WSIFException( 185 "return value " 186 + value 187 + " has unexpected type " 188 + valueType 189 + " instead of " 190 + type); 191 } else 192 msg.setObjectPart(name, value); 193 } catch (SOAPException se) { 194 Trc.exception(se); 195 throw new WSIFException( 196 "WSIFOperation_ApacheAxis.setMessagePart messageName=" 197 + (msg.getName() == null ? "null" : msg.getName()) 198 + " partName=" 199 + name 200 + " caught " 201 + se); 202 } catch (IOException ioe) { 203 Trc.exception(ioe); 204 throw new WSIFException( 205 "WSIFOperation_ApacheAxis.setMessagePart messageName=" 206 + (msg.getName() == null ? "null" : msg.getName()) 207 + " partName=" 208 + name 209 + " caught " 210 + ioe); 211 } catch (MessagingException me) { 212 Trc.exception(me); 213 throw new WSIFException( 214 "WSIFOperation_ApacheAxis.setMessagePart messageName=" 215 + (msg.getName() == null ? "null" : msg.getName()) 216 + " partName=" 217 + name 218 + " caught " 219 + me); 220 } 221 222 Trc.exit(); 223 } 224 225 } 226 | Popular Tags |