1 8 9 package org.jboss.axis.attachments; 10 11 12 import org.jboss.axis.Part; 13 import org.jboss.axis.transport.http.HTTPConstants; 14 import org.jboss.axis.utils.Messages; 15 import org.jboss.logging.Logger; 16 17 import javax.activation.DataHandler ; 18 19 20 24 25 28 public class MultiPartDimeInputStream extends MultiPartInputStream 29 { 30 private static Logger log = Logger.getLogger(MultiPartDimeInputStream.class.getName()); 31 32 protected java.util.HashMap parts = new java.util.HashMap (); 33 protected java.util.LinkedList orderedParts = new java.util.LinkedList (); 34 protected int rootPartLength = 0; 35 protected boolean closed = false; protected boolean eos = false; protected DimeDelimitedInputStream dimeDelimitedStream = null; 39 protected java.io.InputStream soapStream = null; protected byte[] boundary = null; 41 protected java.io.ByteArrayInputStream cachedSOAPEnvelope = null; protected String contentId = null; 44 45 50 public MultiPartDimeInputStream(java.io.InputStream is) 51 throws java.io.IOException 52 { 53 super(null); soapStream = dimeDelimitedStream = new DimeDelimitedInputStream(is); contentId = dimeDelimitedStream.getContentId(); 56 } 57 58 public Part getAttachmentByReference(final String [] id) 59 throws org.jboss.axis.AxisFault 60 { 61 Part ret = null; 63 64 try 65 { 66 for (int i = id.length - 1; ret == null && i > -1; --i) 67 { 68 ret = (AttachmentPartImpl)parts.get(id[i]); 69 } 70 71 if (null == ret) 72 { 73 ret = readTillFound(id); 74 } 75 log.debug(Messages.getMessage("return02", 76 "getAttachmentByReference(\"" + id + "\"", 77 (ret == null ? "null" : ret.toString()))); 78 } 79 catch (java.io.IOException e) 80 { 81 throw new org.jboss.axis.AxisFault(e.getClass().getName() 82 + e.getMessage()); 83 } 84 return ret; 85 } 86 87 protected void addPart(String contentId, String locationId, 88 AttachmentPartImpl ap) 89 { 90 if (contentId != null && contentId.trim().length() != 0) 92 parts.put(contentId, ap); 93 orderedParts.add(ap); 94 } 95 96 protected static final String [] READ_ALL = {" * \0 ".intern()}; 98 99 protected void readAll() throws org.jboss.axis.AxisFault 100 { 101 try 102 { 103 readTillFound(READ_ALL); 104 } 105 catch (Exception e) 106 { 107 throw org.jboss.axis.AxisFault.makeFault(e); 108 } 109 } 110 111 public java.util.Collection getAttachments() 112 throws org.jboss.axis.AxisFault 113 { 114 readAll(); 115 return new java.util.LinkedList (orderedParts); 116 } 117 118 123 124 protected Part readTillFound(final String [] id) 125 throws java.io.IOException 126 { 127 if (dimeDelimitedStream == null) 128 { 129 return null; 131 } 132 Part ret = null; 133 134 try 135 { 136 137 if (soapStream != null) 138 { if (!eos) 140 { 142 java.io.ByteArrayOutputStream soapdata = 143 new java.io.ByteArrayOutputStream (1024 * 8); 144 145 byte[] buf = new byte[1024 * 16]; 146 int byteread = 0; 147 148 do 149 { 150 byteread = soapStream.read(buf); 151 if (byteread > 0) 152 { 153 soapdata.write(buf, 0, byteread); 154 } 155 156 } 157 while (byteread > -1); 158 soapdata.close(); 159 soapStream.close(); 160 soapStream = new java.io.ByteArrayInputStream (soapdata.toByteArray()); 161 } 162 dimeDelimitedStream = dimeDelimitedStream.getNextStream(); 163 } 164 166 if (null != dimeDelimitedStream) 167 { 168 do 169 { 170 String contentId = dimeDelimitedStream.getContentId(); 171 String contentType = dimeDelimitedStream.getType(); 172 173 if (contentType != null && !dimeDelimitedStream.getDimeTypeNameFormat().equals(DimeTypeNameFormat.MIME)) 174 { 175 contentType = "application/uri; uri='" + contentType + "'"; 176 } 177 178 179 ManagedMemoryDataSource source = new ManagedMemoryDataSource(dimeDelimitedStream, contentType); 180 DataHandler dh = new DataHandler (source); 181 182 AttachmentPartImpl ap = new AttachmentPartImpl(dh); 183 if (contentId != null) 184 { 185 ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, contentId); 186 } 187 188 addPart(contentId, "", ap); 189 190 for (int i = id.length - 1; ret == null && i > -1; --i) 191 { 192 if (contentId != null && id[i].equals(contentId)) 193 { ret = ap; 195 } 196 } 197 198 dimeDelimitedStream = 199 dimeDelimitedStream.getNextStream(); 200 201 } 202 while (null == ret && null != dimeDelimitedStream); 203 } 204 } 205 catch (Exception e) 206 { 207 throw org.jboss.axis.AxisFault.makeFault(e); 208 } 209 210 return ret; 211 } 212 213 219 public String getContentLocation() 220 { 221 return null; 222 } 223 224 230 public String getContentId() 231 { 232 return contentId; 233 } 234 235 238 239 public int read(byte[] b, int off, int len) 240 throws java.io.IOException 241 { 242 if (closed) 243 { 244 throw new java.io.IOException (Messages.getMessage("streamClosed")); 245 } 246 if (eos) 247 { 248 return -1; 249 } 250 int read = soapStream.read(b, off, len); 251 252 if (read < 0) 253 { 254 eos = true; 255 } 256 return read; 257 } 258 259 public int read(byte[] b) throws java.io.IOException 260 { 261 return read(b, 0, b.length); 262 } 263 264 public int read() throws java.io.IOException 265 { 266 if (closed) 267 { 268 throw new java.io.IOException (Messages.getMessage("streamClosed")); 269 } 270 if (eos) 271 { 272 return -1; 273 } 274 int ret = soapStream.read(); 275 276 if (ret < 0) 277 { 278 eos = true; 279 } 280 return ret; 281 } 282 283 public void close() throws java.io.IOException 284 { 285 closed = true; 286 soapStream.close(); 287 } 288 } 289 | Popular Tags |