1 57 58 package org.apache.soap.encoding.soapenc; 59 60 import java.beans.*; 61 import java.io.*; 62 import java.util.*; 63 import java.lang.reflect.*; 64 import org.w3c.dom.*; 65 import org.apache.soap.util.*; 66 import org.apache.soap.util.xml.*; 67 import org.apache.soap.util.mime.*; 68 import org.apache.soap.*; 69 import org.apache.soap.rpc.*; 70 import javax.activation.*; 71 import javax.mail.*; 72 import javax.mail.internet.*; 73 74 90 public class MimePartSerializer implements Serializer, Deserializer { 91 public void marshall(String inScopeEncStyle, Class javaType, Object src, 92 Object context, Writer sink, NSStack nsStack, 93 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 94 throws IllegalArgumentException , IOException { 95 nsStack.pushScope(); 96 97 if ((src != null) && 98 !(src instanceof InputStream) && 99 !(src instanceof DataSource) && 100 !(src instanceof MimeBodyPart) && 101 !(src instanceof DataHandler)) 102 throw new IllegalArgumentException ("Tried to pass a '" + 103 src.getClass().toString() + "' to MimePartSerializer"); 104 105 if (src == null) { 106 SoapEncUtils.generateNullStructure(inScopeEncStyle, Object .class, 107 null, sink, nsStack, xjmr); 108 } else { 109 DataSource ds = null; 111 DataHandler dh = null; 112 MimeBodyPart bp = null; 113 if (src instanceof InputStream) 114 ds = new ByteArrayDataSource((InputStream)src, 115 "application/octet-stream"); 116 else if (src instanceof DataSource) 117 ds = (DataSource)src; 118 if (ds != null) 119 dh = new DataHandler(ds); 120 else if (src instanceof DataHandler) 121 dh = (DataHandler)src; 122 if (dh != null) { 123 bp = new MimeBodyPart(); 124 try { 125 bp.setDataHandler(dh); 126 } catch(MessagingException me) { 127 throw new IllegalArgumentException ( 128 "Invalid InputStream/DataSource/DataHandler: " + me); 129 } 130 } else if (src instanceof MimeBodyPart) { 131 bp = (MimeBodyPart)src; 132 } 133 135 String cid = null; 137 try { 138 cid = bp.getContentID(); 139 } catch(MessagingException me) { 140 } 141 if (cid == null) { 142 cid = MimeUtils.getUniqueValue(); 143 try { 144 bp.setHeader(Constants.HEADER_CONTENT_ID, '<' + cid + '>'); 145 } catch(MessagingException me) { 146 throw new IllegalArgumentException ("Could not set Content-ID: " 147 + me); 148 } 149 } 150 151 try { 153 ctx.addBodyPart(bp); 154 } catch(MessagingException me) { 155 throw new IllegalArgumentException ("Could not add attachment: " 156 + me); 157 } 158 159 sink.write('<' + context.toString()); 161 162 sink.write(' ' + Constants.ATTR_REFERENCE + " =\"cid:" 164 + java.net.URLEncoder.encode(cid) + '"'); 165 166 sink.write("/>"); 167 } 168 169 nsStack.popScope(); 170 } 171 172 public Bean unmarshall(String inScopeEncStyle, QName elementType, 173 Node src, XMLJavaMappingRegistry xjmr, 174 SOAPContext ctx) 175 throws IllegalArgumentException { 176 177 Element paramEl = (Element)src; 178 179 DataHandler dh = null; 180 if (!SoapEncUtils.isNull(paramEl)) { 181 String uri = paramEl.getAttribute(Constants.ATTR_REFERENCE); 182 183 try { 184 MimeBodyPart bp = null; 185 try { 186 bp = ctx.findBodyPart(uri); 187 } catch(NullPointerException npe) { 188 } catch(ClassCastException cce) { 189 } 190 if (bp == null) { 191 throw new IllegalArgumentException ( 192 "Attachment tag \"" + paramEl.getTagName() 193 + "\" refers to a Mime attachment with label \"" 194 + uri + "\" which could not be found."); 195 } else 196 dh = bp.getDataHandler(); 197 } catch(MessagingException me) { 198 throw new IllegalArgumentException ( 199 "Failed to read attachment for tag \"" 200 + paramEl.getTagName() 201 + "\" with label \"" + uri + "\": " + me); 202 } 203 } 204 return new Bean(javax.activation.DataHandler .class, dh); 205 } 206 } 207 | Popular Tags |