Mon, 04 May 2009 21:10:41 -0700
6658158: Mutable statics in SAAJ (findbugs)
6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs)
Reviewed-by: darcy
duke@1 | 1 | /* |
tbell@45 | 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
tbell@45 | 25 | /* |
tbell@45 | 26 | * $Id: AttachmentPartImpl.java,v 1.1.1.1.2.1 2007/11/27 07:19:29 kumarjayanti Exp $ |
tbell@45 | 27 | * $Revision: 1.1.1.1.2.1 $ |
tbell@45 | 28 | * $Date: 2007/11/27 07:19:29 $ |
tbell@45 | 29 | */ |
tbell@45 | 30 | |
tbell@45 | 31 | |
duke@1 | 32 | package com.sun.xml.internal.messaging.saaj.soap; |
duke@1 | 33 | |
duke@1 | 34 | import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
duke@1 | 35 | |
duke@1 | 36 | import com.sun.xml.internal.messaging.saaj.packaging.mime.util.ASCIIUtility; |
duke@1 | 37 | |
duke@1 | 38 | import com.sun.xml.internal.messaging.saaj.packaging.mime.Header; |
duke@1 | 39 | import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimePartDataSource; |
duke@1 | 40 | import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.InternetHeaders; |
duke@1 | 41 | import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart; |
duke@1 | 42 | import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility; |
duke@1 | 43 | import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; |
duke@1 | 44 | import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants; |
duke@1 | 45 | |
duke@1 | 46 | import javax.activation.CommandMap; |
duke@1 | 47 | import javax.activation.DataHandler; |
duke@1 | 48 | import javax.activation.MailcapCommandMap; |
duke@1 | 49 | import javax.xml.soap.AttachmentPart; |
duke@1 | 50 | import javax.xml.soap.MimeHeader; |
duke@1 | 51 | import javax.xml.soap.MimeHeaders; |
duke@1 | 52 | import javax.xml.soap.SOAPException; |
duke@1 | 53 | import java.io.IOException; |
duke@1 | 54 | import java.io.InputStream; |
duke@1 | 55 | import java.io.ByteArrayOutputStream; |
duke@1 | 56 | import java.io.ByteArrayInputStream; |
duke@1 | 57 | import java.io.OutputStream; |
duke@1 | 58 | import java.util.Iterator; |
duke@1 | 59 | import java.util.List; |
duke@1 | 60 | import java.util.logging.Level; |
duke@1 | 61 | import java.util.logging.Logger; |
duke@1 | 62 | |
duke@1 | 63 | import javax.activation.*; |
duke@1 | 64 | import javax.xml.soap.*; |
duke@1 | 65 | |
duke@1 | 66 | /** |
duke@1 | 67 | * Implementation of attachments. |
duke@1 | 68 | * |
duke@1 | 69 | * @author Anil Vijendran (akv@eng.sun.com) |
duke@1 | 70 | */ |
duke@1 | 71 | public class AttachmentPartImpl extends AttachmentPart { |
duke@1 | 72 | |
tbell@50 | 73 | protected static final Logger log = |
duke@1 | 74 | Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, |
duke@1 | 75 | "com.sun.xml.internal.messaging.saaj.soap.LocalStrings"); |
duke@1 | 76 | |
duke@1 | 77 | static { |
duke@1 | 78 | try { |
duke@1 | 79 | CommandMap map = CommandMap.getDefaultCommandMap(); |
duke@1 | 80 | if (map instanceof MailcapCommandMap) { |
duke@1 | 81 | MailcapCommandMap mailMap = (MailcapCommandMap) map; |
duke@1 | 82 | String hndlrStr = ";;x-java-content-handler="; |
duke@1 | 83 | mailMap.addMailcap( |
duke@1 | 84 | "text/xml" |
duke@1 | 85 | + hndlrStr |
duke@1 | 86 | + "com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler"); |
duke@1 | 87 | mailMap.addMailcap( |
duke@1 | 88 | "application/xml" |
duke@1 | 89 | + hndlrStr |
duke@1 | 90 | + "com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler"); |
duke@1 | 91 | mailMap.addMailcap( |
duke@1 | 92 | "application/fastinfoset" |
duke@1 | 93 | + hndlrStr |
duke@1 | 94 | + "com.sun.xml.internal.messaging.saaj.soap.FastInfosetDataContentHandler"); |
tbell@45 | 95 | /* Image DataContentHandler handles all image types |
duke@1 | 96 | mailMap.addMailcap( |
duke@1 | 97 | "image/jpeg" |
duke@1 | 98 | + hndlrStr |
duke@1 | 99 | + "com.sun.xml.internal.messaging.saaj.soap.JpegDataContentHandler"); |
duke@1 | 100 | mailMap.addMailcap( |
duke@1 | 101 | "image/gif" |
duke@1 | 102 | + hndlrStr |
tbell@45 | 103 | + "com.sun.xml.internal.messaging.saaj.soap.GifDataContentHandler");*/ |
duke@1 | 104 | /*mailMap.addMailcap( |
duke@1 | 105 | "multipart/*" |
duke@1 | 106 | + hndlrStr |
duke@1 | 107 | + "com.sun.xml.internal.messaging.saaj.soap.MultipartDataContentHandler");*/ |
duke@1 | 108 | mailMap.addMailcap( |
duke@1 | 109 | "image/*" |
duke@1 | 110 | + hndlrStr |
duke@1 | 111 | + "com.sun.xml.internal.messaging.saaj.soap.ImageDataContentHandler"); |
duke@1 | 112 | mailMap.addMailcap( |
duke@1 | 113 | "text/plain" |
duke@1 | 114 | + hndlrStr |
duke@1 | 115 | + "com.sun.xml.internal.messaging.saaj.soap.StringDataContentHandler"); |
duke@1 | 116 | } else { |
duke@1 | 117 | throw new SOAPExceptionImpl("Default CommandMap is not a MailcapCommandMap"); |
duke@1 | 118 | } |
duke@1 | 119 | } catch (Throwable t) { |
duke@1 | 120 | log.log( |
duke@1 | 121 | Level.SEVERE, |
duke@1 | 122 | "SAAJ0508.soap.cannot.register.handlers", |
duke@1 | 123 | t); |
duke@1 | 124 | if (t instanceof RuntimeException) { |
duke@1 | 125 | throw (RuntimeException) t; |
duke@1 | 126 | } else { |
duke@1 | 127 | throw new RuntimeException(t.getLocalizedMessage()); |
duke@1 | 128 | } |
duke@1 | 129 | } |
duke@1 | 130 | }; |
duke@1 | 131 | |
duke@1 | 132 | private final MimeHeaders headers; |
duke@1 | 133 | private MimeBodyPart rawContent = null; |
duke@1 | 134 | private DataHandler dataHandler = null; |
duke@1 | 135 | |
duke@1 | 136 | public AttachmentPartImpl() { |
duke@1 | 137 | headers = new MimeHeaders(); |
duke@1 | 138 | } |
duke@1 | 139 | |
duke@1 | 140 | public int getSize() throws SOAPException { |
duke@1 | 141 | byte[] bytes; |
duke@1 | 142 | |
duke@1 | 143 | if ((rawContent == null) && (dataHandler == null)) |
duke@1 | 144 | return 0; |
duke@1 | 145 | |
duke@1 | 146 | if (rawContent != null) { |
duke@1 | 147 | try { |
duke@1 | 148 | return rawContent.getSize(); |
duke@1 | 149 | } catch (Exception ex) { |
duke@1 | 150 | log.log( |
duke@1 | 151 | Level.SEVERE, |
duke@1 | 152 | "SAAJ0573.soap.attachment.getrawbytes.ioexception", |
duke@1 | 153 | new String[] { ex.getLocalizedMessage()}); |
duke@1 | 154 | throw new SOAPExceptionImpl("Raw InputStream Error: " + ex); |
duke@1 | 155 | } |
duke@1 | 156 | } else { |
duke@1 | 157 | ByteOutputStream bout = new ByteOutputStream(); |
duke@1 | 158 | try { |
duke@1 | 159 | dataHandler.writeTo(bout); |
duke@1 | 160 | } catch (IOException ex) { |
duke@1 | 161 | log.log( |
duke@1 | 162 | Level.SEVERE, |
duke@1 | 163 | "SAAJ0501.soap.data.handler.err", |
duke@1 | 164 | new String[] { ex.getLocalizedMessage()}); |
duke@1 | 165 | throw new SOAPExceptionImpl("Data handler error: " + ex); |
duke@1 | 166 | } |
duke@1 | 167 | bytes = bout.getBytes(); |
duke@1 | 168 | if (bytes != null) |
duke@1 | 169 | return bytes.length; |
duke@1 | 170 | } |
duke@1 | 171 | return -1; |
duke@1 | 172 | } |
duke@1 | 173 | |
duke@1 | 174 | public void clearContent() { |
duke@1 | 175 | dataHandler = null; |
duke@1 | 176 | rawContent = null; |
duke@1 | 177 | } |
duke@1 | 178 | |
duke@1 | 179 | public Object getContent() throws SOAPException { |
duke@1 | 180 | try { |
duke@1 | 181 | if (dataHandler != null) { |
duke@1 | 182 | return getDataHandler().getContent(); |
duke@1 | 183 | } else if (rawContent != null) { |
duke@1 | 184 | return rawContent.getContent(); |
duke@1 | 185 | } else { |
duke@1 | 186 | log.severe("SAAJ0572.soap.no.content.for.attachment"); |
duke@1 | 187 | throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
duke@1 | 188 | } |
duke@1 | 189 | } catch (Exception ex) { |
duke@1 | 190 | log.log(Level.SEVERE, "SAAJ0575.soap.attachment.getcontent.exception", ex); |
duke@1 | 191 | throw new SOAPExceptionImpl(ex.getLocalizedMessage()); |
duke@1 | 192 | } |
duke@1 | 193 | } |
duke@1 | 194 | |
duke@1 | 195 | public void setContent(Object object, String contentType) |
duke@1 | 196 | throws IllegalArgumentException { |
duke@1 | 197 | DataHandler dh = new DataHandler(object, contentType); |
duke@1 | 198 | |
duke@1 | 199 | setDataHandler(dh); |
duke@1 | 200 | } |
duke@1 | 201 | |
duke@1 | 202 | |
duke@1 | 203 | public DataHandler getDataHandler() throws SOAPException { |
duke@1 | 204 | if (dataHandler == null) { |
duke@1 | 205 | if (rawContent != null) { |
duke@1 | 206 | return new DataHandler(new MimePartDataSource(rawContent)); |
duke@1 | 207 | } |
duke@1 | 208 | log.severe("SAAJ0502.soap.no.handler.for.attachment"); |
duke@1 | 209 | throw new SOAPExceptionImpl("No data handler associated with this attachment"); |
duke@1 | 210 | } |
duke@1 | 211 | return dataHandler; |
duke@1 | 212 | } |
duke@1 | 213 | |
duke@1 | 214 | public void setDataHandler(DataHandler dataHandler) |
duke@1 | 215 | throws IllegalArgumentException { |
duke@1 | 216 | if (dataHandler == null) { |
duke@1 | 217 | log.severe("SAAJ0503.soap.no.null.to.dataHandler"); |
duke@1 | 218 | throw new IllegalArgumentException("Null dataHandler argument to setDataHandler"); |
duke@1 | 219 | } |
duke@1 | 220 | this.dataHandler = dataHandler; |
duke@1 | 221 | rawContent = null; |
duke@1 | 222 | |
duke@1 | 223 | log.log( |
duke@1 | 224 | Level.FINE, |
duke@1 | 225 | "SAAJ0580.soap.set.Content-Type", |
duke@1 | 226 | new String[] { dataHandler.getContentType()}); |
duke@1 | 227 | setMimeHeader("Content-Type", dataHandler.getContentType()); |
duke@1 | 228 | } |
duke@1 | 229 | |
duke@1 | 230 | public void removeAllMimeHeaders() { |
duke@1 | 231 | headers.removeAllHeaders(); |
duke@1 | 232 | } |
duke@1 | 233 | |
duke@1 | 234 | public void removeMimeHeader(String header) { |
duke@1 | 235 | headers.removeHeader(header); |
duke@1 | 236 | } |
duke@1 | 237 | |
duke@1 | 238 | public String[] getMimeHeader(String name) { |
duke@1 | 239 | return headers.getHeader(name); |
duke@1 | 240 | } |
duke@1 | 241 | |
duke@1 | 242 | public void setMimeHeader(String name, String value) { |
duke@1 | 243 | headers.setHeader(name, value); |
duke@1 | 244 | } |
duke@1 | 245 | |
duke@1 | 246 | public void addMimeHeader(String name, String value) { |
duke@1 | 247 | headers.addHeader(name, value); |
duke@1 | 248 | } |
duke@1 | 249 | |
duke@1 | 250 | public Iterator getAllMimeHeaders() { |
duke@1 | 251 | return headers.getAllHeaders(); |
duke@1 | 252 | } |
duke@1 | 253 | |
duke@1 | 254 | public Iterator getMatchingMimeHeaders(String[] names) { |
duke@1 | 255 | return headers.getMatchingHeaders(names); |
duke@1 | 256 | } |
duke@1 | 257 | |
duke@1 | 258 | public Iterator getNonMatchingMimeHeaders(String[] names) { |
duke@1 | 259 | return headers.getNonMatchingHeaders(names); |
duke@1 | 260 | } |
duke@1 | 261 | |
duke@1 | 262 | boolean hasAllHeaders(MimeHeaders hdrs) { |
duke@1 | 263 | if (hdrs != null) { |
duke@1 | 264 | Iterator i = hdrs.getAllHeaders(); |
duke@1 | 265 | while (i.hasNext()) { |
duke@1 | 266 | MimeHeader hdr = (MimeHeader) i.next(); |
duke@1 | 267 | String[] values = headers.getHeader(hdr.getName()); |
duke@1 | 268 | boolean found = false; |
duke@1 | 269 | |
duke@1 | 270 | if (values != null) { |
duke@1 | 271 | for (int j = 0; j < values.length; j++) |
duke@1 | 272 | if (hdr.getValue().equalsIgnoreCase(values[j])) { |
duke@1 | 273 | found = true; |
duke@1 | 274 | break; |
duke@1 | 275 | } |
duke@1 | 276 | } |
duke@1 | 277 | |
duke@1 | 278 | if (!found) { |
duke@1 | 279 | return false; |
duke@1 | 280 | } |
duke@1 | 281 | } |
duke@1 | 282 | } |
duke@1 | 283 | return true; |
duke@1 | 284 | } |
duke@1 | 285 | |
duke@1 | 286 | MimeBodyPart getMimePart() throws SOAPException { |
duke@1 | 287 | try { |
duke@1 | 288 | if (rawContent != null) { |
duke@1 | 289 | copyMimeHeaders(headers, rawContent); |
duke@1 | 290 | return rawContent; |
duke@1 | 291 | } |
duke@1 | 292 | |
duke@1 | 293 | MimeBodyPart envelope = new MimeBodyPart(); |
duke@1 | 294 | |
duke@1 | 295 | envelope.setDataHandler(dataHandler); |
duke@1 | 296 | copyMimeHeaders(headers, envelope); |
duke@1 | 297 | |
duke@1 | 298 | return envelope; |
duke@1 | 299 | } catch (Exception ex) { |
duke@1 | 300 | log.severe("SAAJ0504.soap.cannot.externalize.attachment"); |
duke@1 | 301 | throw new SOAPExceptionImpl("Unable to externalize attachment", ex); |
duke@1 | 302 | } |
duke@1 | 303 | } |
duke@1 | 304 | |
duke@1 | 305 | public static void copyMimeHeaders(MimeHeaders headers, MimeBodyPart mbp) |
duke@1 | 306 | throws SOAPException { |
duke@1 | 307 | |
duke@1 | 308 | Iterator i = headers.getAllHeaders(); |
duke@1 | 309 | |
duke@1 | 310 | while (i.hasNext()) |
duke@1 | 311 | try { |
duke@1 | 312 | MimeHeader mh = (MimeHeader) i.next(); |
duke@1 | 313 | |
duke@1 | 314 | mbp.setHeader(mh.getName(), mh.getValue()); |
duke@1 | 315 | } catch (Exception ex) { |
duke@1 | 316 | log.severe("SAAJ0505.soap.cannot.copy.mime.hdr"); |
duke@1 | 317 | throw new SOAPExceptionImpl("Unable to copy MIME header", ex); |
duke@1 | 318 | } |
duke@1 | 319 | } |
duke@1 | 320 | |
duke@1 | 321 | public static void copyMimeHeaders(MimeBodyPart mbp, AttachmentPartImpl ap) |
duke@1 | 322 | throws SOAPException { |
duke@1 | 323 | try { |
duke@1 | 324 | List hdr = mbp.getAllHeaders(); |
duke@1 | 325 | int sz = hdr.size(); |
duke@1 | 326 | for( int i=0; i<sz; i++ ) { |
duke@1 | 327 | Header h = (Header)hdr.get(i); |
duke@1 | 328 | if(h.getName().equalsIgnoreCase("Content-Type")) |
duke@1 | 329 | continue; // skip |
duke@1 | 330 | ap.addMimeHeader(h.getName(), h.getValue()); |
duke@1 | 331 | } |
duke@1 | 332 | } catch (Exception ex) { |
duke@1 | 333 | log.severe("SAAJ0506.soap.cannot.copy.mime.hdrs.into.attachment"); |
duke@1 | 334 | throw new SOAPExceptionImpl( |
duke@1 | 335 | "Unable to copy MIME headers into attachment", |
duke@1 | 336 | ex); |
duke@1 | 337 | } |
duke@1 | 338 | } |
duke@1 | 339 | |
duke@1 | 340 | public void setBase64Content(InputStream content, String contentType) |
duke@1 | 341 | throws SOAPException { |
duke@1 | 342 | dataHandler = null; |
duke@1 | 343 | try { |
duke@1 | 344 | InputStream decoded = MimeUtility.decode(content, "base64"); |
duke@1 | 345 | InternetHeaders hdrs = new InternetHeaders(); |
duke@1 | 346 | hdrs.setHeader("Content-Type", contentType); |
duke@1 | 347 | //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart |
duke@1 | 348 | // Ctor with inputStream causes problems based on the InputStream |
duke@1 | 349 | // has markSupported()==true |
duke@1 | 350 | ByteOutputStream bos = new ByteOutputStream(); |
duke@1 | 351 | bos.write(decoded); |
duke@1 | 352 | rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount()); |
duke@1 | 353 | setMimeHeader("Content-Type", contentType); |
duke@1 | 354 | } catch (Exception e) { |
duke@1 | 355 | log.log(Level.SEVERE, "SAAJ0578.soap.attachment.setbase64content.exception", e); |
duke@1 | 356 | throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
duke@1 | 357 | } |
duke@1 | 358 | } |
duke@1 | 359 | |
duke@1 | 360 | public InputStream getBase64Content() throws SOAPException { |
duke@1 | 361 | InputStream stream; |
duke@1 | 362 | if (rawContent != null) { |
duke@1 | 363 | try { |
duke@1 | 364 | stream = rawContent.getInputStream(); |
duke@1 | 365 | } catch (Exception e) { |
duke@1 | 366 | log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e); |
duke@1 | 367 | throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
duke@1 | 368 | } |
duke@1 | 369 | } else if (dataHandler != null) { |
duke@1 | 370 | try { |
duke@1 | 371 | stream = dataHandler.getInputStream(); |
duke@1 | 372 | } catch (IOException e) { |
duke@1 | 373 | log.severe("SAAJ0574.soap.attachment.datahandler.ioexception"); |
duke@1 | 374 | throw new SOAPExceptionImpl("DataHandler error" + e); |
duke@1 | 375 | } |
duke@1 | 376 | } else { |
duke@1 | 377 | log.severe("SAAJ0572.soap.no.content.for.attachment"); |
duke@1 | 378 | throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
duke@1 | 379 | } |
duke@1 | 380 | |
duke@1 | 381 | //TODO: Write a BASE64EncoderInputStream instead, |
duke@1 | 382 | // this code below is inefficient |
duke@1 | 383 | // where we are trying to read the whole attachment first |
duke@1 | 384 | int len; |
duke@1 | 385 | int size = 1024; |
duke@1 | 386 | byte [] buf; |
duke@1 | 387 | if (stream != null) { |
duke@1 | 388 | try { |
duke@1 | 389 | ByteArrayOutputStream bos = new ByteArrayOutputStream(size); |
duke@1 | 390 | //TODO: try and optimize this on the same lines as |
duke@1 | 391 | // ByteOutputStream : to eliminate the temp buffer here |
duke@1 | 392 | OutputStream ret = MimeUtility.encode(bos, "base64"); |
duke@1 | 393 | buf = new byte[size]; |
duke@1 | 394 | while ((len = stream.read(buf, 0, size)) != -1) { |
duke@1 | 395 | ret.write(buf, 0, len); |
duke@1 | 396 | } |
duke@1 | 397 | ret.flush(); |
duke@1 | 398 | buf = bos.toByteArray(); |
duke@1 | 399 | return new ByteArrayInputStream(buf); |
duke@1 | 400 | } catch (Exception e) { |
duke@1 | 401 | // throw new SOAPException |
duke@1 | 402 | log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e); |
duke@1 | 403 | throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
duke@1 | 404 | } |
duke@1 | 405 | } else { |
duke@1 | 406 | //throw new SOAPException |
duke@1 | 407 | log.log(Level.SEVERE,"SAAJ0572.soap.no.content.for.attachment"); |
duke@1 | 408 | throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
duke@1 | 409 | } |
duke@1 | 410 | } |
duke@1 | 411 | |
duke@1 | 412 | public void setRawContent(InputStream content, String contentType) |
duke@1 | 413 | throws SOAPException { |
duke@1 | 414 | dataHandler = null; |
duke@1 | 415 | try { |
duke@1 | 416 | InternetHeaders hdrs = new InternetHeaders(); |
duke@1 | 417 | hdrs.setHeader("Content-Type", contentType); |
duke@1 | 418 | //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart |
duke@1 | 419 | // Ctor with inputStream causes problems based on whether the InputStream has |
duke@1 | 420 | // markSupported()==true or false |
duke@1 | 421 | ByteOutputStream bos = new ByteOutputStream(); |
duke@1 | 422 | bos.write(content); |
duke@1 | 423 | rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount()); |
duke@1 | 424 | setMimeHeader("Content-Type", contentType); |
duke@1 | 425 | } catch (Exception e) { |
duke@1 | 426 | log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e); |
duke@1 | 427 | throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
duke@1 | 428 | } |
duke@1 | 429 | } |
duke@1 | 430 | |
duke@1 | 431 | /* |
duke@1 | 432 | public void setRawContentBytes(byte[] content, String contentType) |
duke@1 | 433 | throws SOAPException { |
duke@1 | 434 | if (content == null) { |
duke@1 | 435 | throw new SOAPExceptionImpl("Null content passed to setRawContentBytes"); |
duke@1 | 436 | } |
duke@1 | 437 | dataHandler = null; |
duke@1 | 438 | try { |
duke@1 | 439 | InternetHeaders hdrs = new InternetHeaders(); |
duke@1 | 440 | hdrs.setHeader("Content-Type", contentType); |
duke@1 | 441 | rawContent = new MimeBodyPart(hdrs, content, content.length); |
duke@1 | 442 | setMimeHeader("Content-Type", contentType); |
duke@1 | 443 | } catch (Exception e) { |
duke@1 | 444 | log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e); |
duke@1 | 445 | throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
duke@1 | 446 | } |
duke@1 | 447 | } */ |
duke@1 | 448 | |
duke@1 | 449 | public void setRawContentBytes( |
duke@1 | 450 | byte[] content, int off, int len, String contentType) |
duke@1 | 451 | throws SOAPException { |
duke@1 | 452 | if (content == null) { |
duke@1 | 453 | throw new SOAPExceptionImpl("Null content passed to setRawContentBytes"); |
duke@1 | 454 | } |
duke@1 | 455 | dataHandler = null; |
duke@1 | 456 | try { |
duke@1 | 457 | InternetHeaders hdrs = new InternetHeaders(); |
duke@1 | 458 | hdrs.setHeader("Content-Type", contentType); |
duke@1 | 459 | rawContent = new MimeBodyPart(hdrs, content, off, len); |
duke@1 | 460 | setMimeHeader("Content-Type", contentType); |
duke@1 | 461 | } catch (Exception e) { |
duke@1 | 462 | log.log(Level.SEVERE, |
duke@1 | 463 | "SAAJ0576.soap.attachment.setrawcontent.exception", e); |
duke@1 | 464 | throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
duke@1 | 465 | } |
duke@1 | 466 | } |
duke@1 | 467 | |
duke@1 | 468 | public InputStream getRawContent() throws SOAPException { |
duke@1 | 469 | if (rawContent != null) { |
duke@1 | 470 | try { |
duke@1 | 471 | return rawContent.getInputStream(); |
duke@1 | 472 | } catch (Exception e) { |
duke@1 | 473 | log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e); |
duke@1 | 474 | throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
duke@1 | 475 | } |
duke@1 | 476 | } else if (dataHandler != null) { |
duke@1 | 477 | try { |
duke@1 | 478 | return dataHandler.getInputStream(); |
duke@1 | 479 | } catch (IOException e) { |
duke@1 | 480 | log.severe("SAAJ0574.soap.attachment.datahandler.ioexception"); |
duke@1 | 481 | throw new SOAPExceptionImpl("DataHandler error" + e); |
duke@1 | 482 | } |
duke@1 | 483 | } else { |
duke@1 | 484 | log.severe("SAAJ0572.soap.no.content.for.attachment"); |
duke@1 | 485 | throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
duke@1 | 486 | } |
duke@1 | 487 | } |
duke@1 | 488 | |
duke@1 | 489 | //TODO: investigate size differences in mime.AttachImageTest |
duke@1 | 490 | public byte[] getRawContentBytes() throws SOAPException { |
duke@1 | 491 | InputStream ret; |
duke@1 | 492 | if (rawContent != null) { |
duke@1 | 493 | try { |
duke@1 | 494 | ret = rawContent.getInputStream(); |
duke@1 | 495 | return ASCIIUtility.getBytes(ret); |
duke@1 | 496 | } catch (Exception e) { |
duke@1 | 497 | log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e); |
duke@1 | 498 | throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
duke@1 | 499 | } |
duke@1 | 500 | } else if (dataHandler != null) { |
duke@1 | 501 | try { |
duke@1 | 502 | ret = dataHandler.getInputStream(); |
duke@1 | 503 | return ASCIIUtility.getBytes(ret); |
duke@1 | 504 | } catch (IOException e) { |
duke@1 | 505 | log.severe("SAAJ0574.soap.attachment.datahandler.ioexception"); |
duke@1 | 506 | throw new SOAPExceptionImpl("DataHandler error" + e); |
duke@1 | 507 | } |
duke@1 | 508 | } else { |
duke@1 | 509 | log.severe("SAAJ0572.soap.no.content.for.attachment"); |
duke@1 | 510 | throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
duke@1 | 511 | } |
duke@1 | 512 | } |
duke@1 | 513 | |
duke@1 | 514 | // attachments are equal if they are the same reference |
duke@1 | 515 | public boolean equals(Object o) { |
duke@1 | 516 | return (this == o); |
duke@1 | 517 | } |
duke@1 | 518 | |
duke@1 | 519 | public MimeHeaders getMimeHeaders() { |
duke@1 | 520 | return headers; |
duke@1 | 521 | } |
duke@1 | 522 | |
duke@1 | 523 | } |