1 57 58 package org.apache.soap.rpc; 59 60 import java.io.*; 61 import java.util.*; 62 import org.apache.soap.util.*; 63 import org.apache.soap.*; 64 import org.apache.soap.encoding.*; 65 import org.apache.soap.server.*; 66 import org.apache.soap.util.mime.*; 67 import javax.mail.*; 68 import javax.mail.internet.*; 69 import javax.activation.*; 70 71 79 public class SOAPContext { 80 protected MimeMultipart parts; 81 protected Hashtable bag = new Hashtable(); 82 protected ClassLoader loader = null ; 83 84 90 protected boolean rootPartSet = false; 91 92 95 private static final String [] ignoreHeaders = 96 {"Message-ID", "Mime-Version"}; 97 98 private static final String DEFAULT_BASEURI = "thismessage:/"; 99 100 103 public SOAPContext() { 104 parts = null; 105 } 106 107 112 public void readMultipart(DataSource ds) throws MessagingException { 113 parts = new MimeMultipart(ds); 114 } 115 116 122 public MimeBodyPart getBodyPart(int index) { 123 127 if (parts == null) { 128 return null; 129 } 130 try { 131 return (MimeBodyPart)parts.getBodyPart(index); 132 } 133 catch (MessagingException me) { 134 throw new IndexOutOfBoundsException (me.getMessage()); 135 } 136 catch (IndexOutOfBoundsException iobe) { 137 return null; 138 } 139 catch (NullPointerException npe) { 140 return null; 141 } 142 } 143 144 151 public MimeBodyPart getBodyPart(String CID) { 152 if (parts == null) { 153 return null; 154 } 155 try { 156 return (MimeBodyPart)parts.getBodyPart(CID); 157 } 158 catch (MessagingException me) { 159 return null; 160 } 161 catch (NullPointerException npe) { 162 return null; 163 } 164 catch (IndexOutOfBoundsException iobe) { 165 return null; 166 } 167 } 168 169 184 public MimeBodyPart findBodyPart(String uri) { 185 if (parts == null || uri == null) { 186 return null; 187 } 188 try { 189 if (uri.length() > 4 && 190 uri.substring(0, 4).equalsIgnoreCase("cid:")) { 191 String cid = MimeUtils.decode(uri.substring(4)); 193 if (cid.charAt(0) != '<' || cid.charAt(cid.length()) != '>') 196 cid = '<' + cid + '>'; 197 198 try { 199 return (MimeBodyPart)parts.getBodyPart(cid); 200 } catch(NullPointerException npe) { 201 } 202 } else { 203 return findPartByLocation(uri); 205 } 206 207 } catch (MessagingException me) { 208 } catch (NullPointerException npe) { 209 } 210 return null; 211 } 212 213 221 public String getBaseURI() { 222 String baseUri = null; 223 try { 224 baseUri = getRootPart().getHeader( 225 Constants.HEADER_CONTENT_LOCATION, null); 226 } catch(MessagingException me) { 227 } 228 if (baseUri == null) 229 baseUri = DEFAULT_BASEURI; 230 return baseUri; 231 } 232 233 248 public MimeBodyPart findPartByLocation(String uri) { 249 try { 250 String baseUri = getBaseURI(); 251 uri = normalizeURI(uri, baseUri); 252 if (uri == null) 253 return null; 254 for (int i = 0; i < parts.getCount(); i++) { 255 MimeBodyPart part = getBodyPart(i); 256 if (part != null) { 257 String partUri = part.getHeader( 258 Constants.HEADER_CONTENT_LOCATION, null); 259 if (uri.equals(normalizeURI(partUri, baseUri))) 260 return part; 261 } 262 } 263 } catch(MessagingException me) { 264 } 265 return null; 266 } 267 268 273 private String normalizeURI(String uri, String baseUri) { 274 int p1 = uri.indexOf(':'); 276 if (p1 >= 0) { 277 String scheme = uri.substring(0, p1); 278 if (scheme.indexOf('/') == -1 && 279 scheme.indexOf('?') == -1 && 280 scheme.indexOf('#') == -1) 281 return uri; 283 } 284 return baseUri + uri; 286 } 287 288 298 public void addBodyPart(MimeBodyPart part) throws MessagingException { 299 addBodyPart(part, -1); 301 } 302 303 319 public void addBodyPart(MimeBodyPart part, int index) 320 throws MessagingException, IllegalArgumentException { 321 if (parts == null) 322 parts = new MimeMultipart( 323 Constants.HEADERVAL_MULTIPART_CONTENT_SUBTYPE); 324 DataHandler dh = part.getDataHandler(); 327 try { 328 MimeType ctype = new MimeType(dh.getContentType()); 329 part.setHeader(Constants.HEADER_CONTENT_TYPE, 330 ctype.toString()); 331 if (dh.getDataSource() instanceof ByteArrayDataSource) 332 part.setHeader(Constants.HEADER_CONTENT_LENGTH, 333 String.valueOf(((ByteArrayDataSource)dh 334 .getDataSource()) 335 .getSize())); 336 345 if (ctype.match("application/octet-stream") || 346 ctype.match("image/*") || 347 ctype.match("audio/*") || 348 ctype.match("video/*")) 349 part.setHeader("Content-Transfer-Encoding", "8bit"); 350 } catch(MessagingException me) { 351 throw new IllegalArgumentException ( 352 "Invalid InputStream/DataSource/DataHandler metadata: " 353 + me); 354 } catch(MimeTypeParseException mtpe) { 355 throw new IllegalArgumentException ("Invalid Mime type \"" 356 + dh.getContentType() 357 + "\": " + mtpe); 358 } 359 if (index == -1) 360 parts.addBodyPart(part); 361 else 362 parts.addBodyPart(part, index); 363 } 364 365 368 public void removeBodyPart(MimeBodyPart part) throws MessagingException { 369 parts.removeBodyPart(part); 370 } 371 372 382 public void setRootPart(MimeBodyPart part) throws MessagingException { 383 String rootCid = '<' + MimeUtils.getUniqueValue() + '>'; 384 part.setHeader(Constants.HEADER_CONTENT_ID, rootCid); 385 if (rootPartSet) 386 parts.removeBodyPart(getRootPart()); 387 addBodyPart(part, 0); 388 rootPartSet = true; 389 } 390 391 399 public void setRootPart(String s, String contentType) 400 throws MessagingException, IOException { 401 setRootPart(s.getBytes("UTF8"), contentType); 402 } 403 404 413 public void setRootPart(byte[] b, String contentType) 414 throws MessagingException { 415 ByteArrayDataSource ds = new ByteArrayDataSource(b, contentType); 416 DataHandler dh = new DataHandler(ds); 417 MimeBodyPart bp = new MimeBodyPart(); 418 bp.setDataHandler(dh); 419 bp.setHeader(Constants.HEADER_CONTENT_LENGTH, 420 String.valueOf(ds.getSize())); 421 422 bp.setHeader("Content-Transfer-Encoding", "8bit"); 425 426 setRootPart(bp); 427 } 428 429 437 public MimeBodyPart getRootPart() throws MessagingException { 438 MimeBodyPart rootPart = null; 439 if (getCount() > 1) { 440 String startCid = new ContentType( 441 parts.getContentType()).getParameter("start"); 442 if (startCid != null) 443 rootPart = getBodyPart(MimeUtils.decode(startCid)); 444 } 445 if (rootPart == null) 446 rootPart = getBodyPart(0); 447 return rootPart; 448 } 449 450 458 public void setSubType(String subtype) throws MessagingException { 459 if (parts == null) 460 parts = new MimeMultipart(subtype); 461 else 462 parts.setSubType(subtype); 463 } 464 465 468 public boolean isRootPartSet() { 469 return rootPartSet; 470 } 471 472 477 public int getCount() throws MessagingException { 478 if (parts == null) 479 return 0; 480 else 481 return parts.getCount(); 482 } 483 484 491 public String getContentType() throws MessagingException { 492 if (parts == null) 493 return null; 494 else 495 if (parts.getCount() == 1) 496 return getRootPart().getContentType(); 497 else 498 return parts.getContentType(); 499 } 500 501 508 public void writeTo(OutputStream os) 509 throws IOException, MessagingException { 510 int count = getCount(); 511 if (count == 0) 512 throw new IOException("Message is empty!"); 513 else if (count == 1) { 514 getRootPart().writeTo(os); 515 } 516 else { 517 Session session = Session.getDefaultInstance(new Properties(), null); 518 MimeMessage msg = new MimeMessage(session); 519 msg.setContent(parts); 520 msg.saveChanges(); 521 msg.writeTo(os, ignoreHeaders); 522 } 523 } 524 525 528 public void setProperty(String name, Object value) { 529 bag.put( name, value ); 530 } 531 532 535 public Object getProperty(String name) { 536 return( bag.get(name) ); 537 } 538 539 542 public Object removeProperty(String name) { 543 return( bag.remove(name) ); 544 } 545 546 549 public Enumeration getPropertyNames() { 550 return( bag.keys() ); 551 } 552 553 public void setClassLoader(ClassLoader cl) { 554 loader = cl; 555 } 556 557 public ClassLoader getClassLoader() { 558 return loader ; 559 } 560 561 public Class loadClass(String className) throws ClassNotFoundException { 562 if ( loader == null ) 563 return( Class.forName( className ) ); 564 return( Class.forName( className, true, loader ) ); 565 } 566 567 570 public String toString() { 571 StringWriter sw = new StringWriter(); 572 PrintWriter pw = new PrintWriter(sw); 573 574 pw.print("[Parts={"); 575 576 if (parts != null) { 577 try { 578 for (int i = 0; i < getCount(); i++) { 579 if (i > 0) { 580 pw.print(", "); 581 } 582 583 MimeBodyPart mbp = getBodyPart(i); 584 pw.print("[cid:" + mbp.getContentID() 585 + " type: " + mbp.getContentType() 586 + " enc: " + mbp.getEncoding() + "]"); 587 } 588 } 589 catch(MessagingException me) { 590 me.printStackTrace(); 591 } 592 } 593 594 pw.print("}]"); 595 596 return sw.toString(); 597 } 598 } 599 | Popular Tags |