KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > attachments > MultiPartDimeInputStream


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

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 JavaDoc;
18
19
20 /**
21  *
22  * @author Rick Rineholt
23  */

24
25 /**
26  * This simulates the multipart stream
27  */

28 public class MultiPartDimeInputStream extends MultiPartInputStream
29 {
30    private static Logger log = Logger.getLogger(MultiPartDimeInputStream.class.getName());
31
32    protected java.util.HashMap JavaDoc parts = new java.util.HashMap JavaDoc();
33    protected java.util.LinkedList JavaDoc orderedParts = new java.util.LinkedList JavaDoc();
34    protected int rootPartLength = 0;
35    protected boolean closed = false; //If true the stream has been closed.
36
protected boolean eos = false; //This is set once the SOAP packet has reached the end of stream.
37
//This stream controls and manages the boundary.
38
protected DimeDelimitedInputStream dimeDelimitedStream = null;
39    protected java.io.InputStream JavaDoc soapStream = null; //Set the soap stream once found.
40
protected byte[] boundary = null;
41    protected java.io.ByteArrayInputStream JavaDoc cachedSOAPEnvelope = null; //Caches the soap stream if it is
42
//Still open and a reference to read data in a later attachment occurs.
43
protected String JavaDoc contentId = null;
44
45    /**
46     * Multipart stream.
47     *
48     * @param is the true input stream from where the source.
49     */

50    public MultiPartDimeInputStream(java.io.InputStream JavaDoc is)
51            throws java.io.IOException JavaDoc
52    {
53       super(null); //don't cache this stream.
54
soapStream = dimeDelimitedStream = new DimeDelimitedInputStream(is); //The Soap stream must always be first
55
contentId = dimeDelimitedStream.getContentId();
56    }
57
58    public Part getAttachmentByReference(final String JavaDoc[] id)
59            throws org.jboss.axis.AxisFault
60    {
61       //First see if we have read it in yet.
62
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 JavaDoc 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 JavaDoc contentId, String JavaDoc locationId,
88                           AttachmentPartImpl ap)
89    {
90       //For DIME streams Content-Location is ignored.
91
if (contentId != null && contentId.trim().length() != 0)
92          parts.put(contentId, ap);
93       orderedParts.add(ap);
94    }
95
96    //Shouldn't never match
97
protected static final String JavaDoc[] 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 JavaDoc e)
106       {
107          throw org.jboss.axis.AxisFault.makeFault(e);
108       }
109    }
110
111    public java.util.Collection JavaDoc getAttachments()
112            throws org.jboss.axis.AxisFault
113    {
114       readAll();
115       return new java.util.LinkedList JavaDoc(orderedParts);
116    }
117
118    /**
119     * This will read streams in till the one that is needed is found.
120     *
121     * @param id is the stream being sought.
122     */

123
124    protected Part readTillFound(final String JavaDoc[] id)
125            throws java.io.IOException JavaDoc
126    {
127       if (dimeDelimitedStream == null)
128       {
129          //The whole stream has been consumed already
130
return null;
131       }
132       Part ret = null;
133
134       try
135       {
136
137          if (soapStream != null)
138          { //Still on the SOAP stream.
139
if (!eos)
140             { //The SOAP packet has not been fully read yet. Need to store it away.
141

142                java.io.ByteArrayOutputStream JavaDoc soapdata =
143                        new java.io.ByteArrayOutputStream JavaDoc(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 JavaDoc(soapdata.toByteArray());
161             }
162             dimeDelimitedStream = dimeDelimitedStream.getNextStream();
163          }
164          //Now start searching for the data.
165

166          if (null != dimeDelimitedStream)
167          {
168             do
169             {
170                String JavaDoc contentId = dimeDelimitedStream.getContentId();
171                String JavaDoc 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 JavaDoc dh = new DataHandler JavaDoc(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                   { //This is the part being sought
194
ret = ap;
195                   }
196                }
197
198                dimeDelimitedStream =
199                        dimeDelimitedStream.getNextStream();
200
201             }
202             while (null == ret && null != dimeDelimitedStream);
203          }
204       }
205       catch (Exception JavaDoc e)
206       {
207          throw org.jboss.axis.AxisFault.makeFault(e);
208       }
209
210       return ret;
211    }
212
213    /**
214     * Return the content location.
215     *
216     * @return the Content-Location of the stream.
217     * Null if no content-location specified.
218     */

219    public String JavaDoc getContentLocation()
220    {
221       return null;
222    }
223
224    /**
225     * Return the content id of the stream
226     *
227     * @return the Content-Location of the stream.
228     * Null if no content-location specified.
229     */

230    public String JavaDoc getContentId()
231    {
232       return contentId;
233    }
234
235    /**
236     * Read the root stream.
237     */

238
239    public int read(byte[] b, int off, int len)
240            throws java.io.IOException JavaDoc
241    {
242       if (closed)
243       {
244          throw new java.io.IOException JavaDoc(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 JavaDoc
260    {
261       return read(b, 0, b.length);
262    }
263
264    public int read() throws java.io.IOException JavaDoc
265    {
266       if (closed)
267       {
268          throw new java.io.IOException JavaDoc(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 JavaDoc
284    {
285       closed = true;
286       soapStream.close();
287    }
288 }
289
Popular Tags