1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
  27 
  28 import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
  29 import com.sun.xml.internal.messaging.saaj.soap.AttachmentPartImpl;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.util.List;
  33 import javax.activation.DataSource;
  34 import javax.xml.soap.AttachmentPart;
  35 import com.sun.xml.internal.org.jvnet.mimepull.MIMEConfig;
  36 import com.sun.xml.internal.org.jvnet.mimepull.MIMEMessage;
  37 import com.sun.xml.internal.org.jvnet.mimepull.MIMEPart;
  38 
  39 /**
  40  *
  41  * @author Kumar
  42  */
  43 public class MimePullMultipart  extends MimeMultipart {
  44 
  45     private InputStream in = null;
  46     private String boundary = null;
  47     private MIMEMessage mm = null;
  48     private DataSource dataSource = null;
  49     private ContentType contType = null;
  50     private String startParam = null;
  51     private MIMEPart soapPart = null;
  52 
  53     public MimePullMultipart(DataSource ds, ContentType ct)
  54         throws MessagingException {
  55         parsed = false;
  56         if (ct==null)
  57             contType = new ContentType(ds.getContentType());
  58         else
  59             contType = ct;
  60 
  61         dataSource = ds;
  62         boundary = contType.getParameter("boundary");
  63     }
  64 
  65     public  MIMEPart readAndReturnSOAPPart() throws  MessagingException {
  66          if (soapPart != null) {
  67             throw new MessagingException("Inputstream from datasource was already consumed");
  68          }
  69          readSOAPPart();
  70          return soapPart;
  71 
  72     }
  73 
  74     protected  void readSOAPPart() throws  MessagingException {
  75         try {
  76             if (soapPart != null) {
  77                 return;
  78             }
  79             in = dataSource.getInputStream();
  80             MIMEConfig config = new MIMEConfig(); //use defaults
  81             mm = new MIMEMessage(in, boundary, config);
  82             String st = contType.getParameter("start");
  83             if(startParam == null) {
  84                 soapPart = mm.getPart(0);
  85             } else {
  86                   // Strip <...> from root part's Content-I
  87                 if (st != null && st.length() > 2 && st.charAt(0) == '<' && st.charAt(st.length()-1) == '>') {
  88                     st = st.substring(1, st.length()-1);
  89                 }
  90                 startParam = st;
  91                 soapPart = mm.getPart(startParam);
  92 
  93             }
  94         } catch (IOException ex) {
  95             throw new MessagingException("No inputstream from datasource", ex);
  96         }
  97     }
  98 
  99     public void parseAll() throws MessagingException {
 100         if (parsed) {
 101             return;
 102         }
 103         if (soapPart == null) {
 104             readSOAPPart();
 105         }
 106 
 107         List<MIMEPart> prts = mm.getAttachments();
 108         for(MIMEPart part : prts) {
 109             if (part != soapPart) {
 110                 AttachmentPart attach = new AttachmentPartImpl(part);
 111                 this.addBodyPart(new MimeBodyPart(part));
 112             }
 113        }
 114        parsed = true;
 115     }
 116 
 117     protected  void parse() throws MessagingException {
 118         parseAll();
 119     }
 120 
 121 }