1 17 package org.apache.axis2.attachments; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.PushbackInputStream ; 22 import java.util.HashMap ; 23 24 import javax.activation.DataHandler ; 25 import javax.mail.MessagingException ; 26 import javax.mail.Part ; 27 import javax.mail.internet.ContentType ; 28 import javax.mail.internet.MimeBodyPart ; 29 import javax.mail.internet.ParseException ; 30 31 import org.apache.axis2.om.OMException; 32 33 36 public class MIMEHelper { 37 40 public static final String MTOM_TYPE = "application/xop+xml"; 41 42 45 public static final String SWA_TYPE = "text/xml"; 46 47 51 public static final String ROOT_PART = "SoapPart"; 52 53 56 ContentType contentType; 57 58 61 byte[] boundary; 62 63 68 String applicationType = null; 69 70 75 PushbackInputStream pushbackInStream; 76 77 81 HashMap bodyPartsMap; 82 83 86 int partIndex = 0; 87 88 String firstPartId=null; 89 90 boolean fileCacheEnable = false; 91 92 String attachmentRepoDir = null; 93 94 public MIMEHelper(InputStream inStream, String contentTypeString, 95 boolean fileCacheEnable, String attachmentRepoDir) 96 throws OMException { 97 this.attachmentRepoDir = attachmentRepoDir; 98 this.fileCacheEnable = fileCacheEnable; 99 bodyPartsMap = new HashMap (); 100 try { 101 contentType = new ContentType (contentTypeString); 102 } catch (ParseException e) { 103 throw new OMException( 104 "Invalid Content Type Field in the Mime Message" 105 + e.toString()); 106 } 107 this.boundary = ("--" + contentType.getParameter("boundary")) 109 .getBytes(); 110 111 pushbackInStream = new PushbackInputStream (inStream, 114 (this.boundary.length + 2)); 115 116 while (true) { 119 int value; 120 try { 121 value = pushbackInStream.read(); 122 if ((byte) value == boundary[0]) { 123 int boundaryIndex = 0; 124 while ((boundaryIndex < boundary.length) 125 && ((byte) value == boundary[boundaryIndex])) { 126 value = pushbackInStream.read(); 127 if (value == -1) 128 throw new OMException( 129 "Unexpected End of Stream while searching for first Mime Boundary"); 130 boundaryIndex++; 131 } 132 if (boundaryIndex == boundary.length) { pushbackInStream.read(); 134 break; 135 } 136 } else if ((byte) value == -1) { 137 throw new OMException( 138 "Mime parts not found. Stream ended while searching for the boundary"); 139 } 140 } catch (IOException e1) { 141 throw new OMException("Stream Error" + e1.toString()); 142 } 143 } 144 } 145 146 public MIMEHelper(InputStream inStream, String contentTypeString) 147 throws OMException { 148 this(inStream,contentTypeString,false,null); 149 } 150 151 155 public String getAttachmentSpecType() { 156 if (this.applicationType == null) { 157 applicationType = contentType.getParameter("type"); 158 if (applicationType.equalsIgnoreCase(MTOM_TYPE)) { 159 this.applicationType = MTOM_TYPE; 160 } else if (applicationType.equalsIgnoreCase(SWA_TYPE)) { 161 this.applicationType = SWA_TYPE; 162 } else { 163 throw new OMException( 164 "Invalid Application type. Support available for MTOM & SwA only."); 165 } 166 } 167 return this.applicationType; 168 } 169 170 175 public InputStream getSOAPPartInputStream() throws OMException { 176 String rootContentID = contentType.getParameter("start"); 177 if (rootContentID==null) 179 { 180 if (partIndex==0) 181 { 182 getNextPart(); 183 } 184 rootContentID=firstPartId; 185 } 186 else 187 { 188 rootContentID.trim(); 189 rootContentID = rootContentID 190 .substring(1, (rootContentID.length() - 1)); 191 } 192 DataHandler dh; 193 try { 194 dh = getDataHandler(rootContentID); 195 if (dh == null) { 196 throw new OMException( 197 "Mandatory Root MIME part containing the SOAP Envelope is missing"); 198 } 199 return dh.getInputStream(); 200 } catch (IOException e) { 201 throw new OMException( 202 "Problem with DataHandler of the Root Mime Part. " + e); 203 } 204 } 205 206 214 public DataHandler getDataHandler(String blobContentID) throws OMException { 215 216 Part bodyPart; 217 blobContentID = "<" + blobContentID + ">"; 218 boolean attachmentFound = false; 219 220 if (bodyPartsMap.containsKey(blobContentID)) { 223 bodyPart = (Part ) bodyPartsMap.get(blobContentID); 224 attachmentFound = true; 225 DataHandler dh; 226 try { 227 dh = bodyPart.getDataHandler(); 228 } catch (MessagingException e) { 229 throw new OMException("Problem with Mime Body Part No " 230 + partIndex + ". " + e); 231 } 232 return dh; 233 } else { 234 try { 235 while (true) { 236 bodyPart = this.getNextPart(); 237 if (bodyPart == null) { 238 return null; 239 } 240 if (bodyPartsMap.containsKey(blobContentID)) { 241 bodyPart = (Part ) bodyPartsMap.get(blobContentID); 242 DataHandler dh = bodyPart.getDataHandler(); 243 return dh; 244 } 245 } 246 } catch (MessagingException e) { 247 throw new OMException("Invalid Mime Message " + e.toString()); 248 } 249 } 250 251 } 252 253 258 private MimeBodyPart getMimeBodyPart() throws OMException { 260 MimeBodyPart mimeBodyPart = null; 261 262 MimeBodyPartInputStream partStream; 263 partStream = new MimeBodyPartInputStream(pushbackInStream, 264 boundary); 265 try { 266 mimeBodyPart = new MimeBodyPart (partStream); 267 } catch (MessagingException e) { 268 throw new OMException("Problem reading Mime Part No " 269 + (partIndex + 1) + ". " + e); 270 } 271 272 partIndex++; 273 return mimeBodyPart; 274 } 275 276 282 private MimeBodyPart getRootMimeBodyPart() throws OMException { 283 MimeBodyPart rootMimeBodyPart; 284 if (bodyPartsMap.isEmpty()) { 285 rootMimeBodyPart = getMimeBodyPart(); 286 bodyPartsMap.put(ROOT_PART, rootMimeBodyPart); 287 } else { 288 rootMimeBodyPart = (MimeBodyPart ) bodyPartsMap.get(ROOT_PART); 289 } 290 return rootMimeBodyPart; 291 } 292 293 private Part getNextPart() throws OMException { 294 MimeBodyPart nextMimeBodyPart; 295 nextMimeBodyPart = getMimeBodyPart(); 296 if (nextMimeBodyPart != null) { 297 String partContentID; 298 try { 299 partContentID = nextMimeBodyPart.getContentID(); 300 if (partContentID==null & partIndex==1) 301 { 302 bodyPartsMap.put("firstPart", nextMimeBodyPart); 303 firstPartId = "firstPart"; 304 return nextMimeBodyPart; 305 } 306 else if (partIndex==1) 307 { 308 firstPartId = partContentID; 309 } 310 if (fileCacheEnable) 311 { 312 PartOnFile part = new PartOnFile(nextMimeBodyPart,partContentID,attachmentRepoDir); 313 return part; 314 } 315 else{ 316 bodyPartsMap.put(partContentID, nextMimeBodyPart); 317 return nextMimeBodyPart; 318 } 319 } catch (MessagingException e) { 320 throw new OMException( 321 "Error Reading Content-ID from Mime Part No " 322 + partIndex + ". " + e); 323 } catch (Exception e) { 324 throw new OMException( 325 "Error Creating File Storage Part" 326 + partIndex + ". " + e); 327 } 328 } else 329 return null; 330 } 331 332 } | Popular Tags |