KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > marshal > http > HTTPUnMarshaller


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.marshal.http;
8
9 import java.io.BufferedReader JavaDoc;
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.io.StreamCorruptedException JavaDoc;
16 import java.util.Map JavaDoc;
17 import org.jboss.logging.Logger;
18 import org.jboss.remoting.marshal.UnMarshaller;
19 import org.jboss.remoting.marshal.serializable.SerializableUnMarshaller;
20
21 /**
22  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
23  */

24 public class HTTPUnMarshaller extends SerializableUnMarshaller
25 {
26    static final long serialVersionUID = 1085086661310576768L;
27
28    public final static String JavaDoc DATATYPE = "http";
29
30    protected final Logger log = Logger.getLogger(getClass());
31
32    /**
33     * Will try to unmarshall data from inputstream. Will try to convert to either an object
34     * or a string. If there is no data to read, will return null.
35     *
36     * @param inputStream
37     * @return
38     * @throws IOException
39     * @throws ClassNotFoundException
40     */

41    public Object JavaDoc read(InputStream JavaDoc inputStream, Map JavaDoc metadata) throws IOException JavaDoc, ClassNotFoundException JavaDoc
42    {
43       int contentLength = -1;
44       Object JavaDoc ret = null;
45       int bufferSize = 1024;
46       byte[] byteBuffer = new byte[bufferSize];
47       ByteArrayOutputStream JavaDoc byteOutputStream = new ByteArrayOutputStream JavaDoc();
48
49       // check the metadat to see if is entry for content length
50
if(metadata != null)
51       {
52          Object JavaDoc value = metadata.get("Content-Length");
53          if(value == null)
54          {
55             value = metadata.get("content-length");
56          }
57          if(value != null)
58          {
59             if(value instanceof String JavaDoc)
60             {
61                try
62                {
63                   contentLength = Integer.parseInt((String JavaDoc) value);
64                }
65                catch(NumberFormatException JavaDoc e)
66                {
67                   log.warn("Error converting Content-Length value (" + value + ") from metadata into int value.");
68                }
69             }
70             else
71             {
72                //TODO: -TME Need support for multivalue entries
73
}
74          }
75       }
76
77       int pointer = 0;
78       int amtRead = inputStream.read(byteBuffer);
79       while(amtRead > 0)
80       {
81          byteOutputStream.write(byteBuffer, pointer, amtRead);
82          if(amtRead < bufferSize && byteOutputStream.size() >= contentLength)
83          {
84             //done reading, so process
85
break;
86          }
87          amtRead = inputStream.read(byteBuffer);
88       }
89
90       byteOutputStream.flush();
91
92       byte[] totalByteArray = byteOutputStream.toByteArray();
93
94       if(totalByteArray.length == 0)
95       {
96          //nothing to read, so is null
97
return null;
98       }
99
100       try
101       {
102          return super.read(new ByteArrayInputStream JavaDoc(totalByteArray), metadata);
103       }
104       catch(StreamCorruptedException JavaDoc sce)
105       {
106
107          try
108          {
109
110             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new ByteArrayInputStream JavaDoc(totalByteArray)));
111             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
112             String JavaDoc str = null;
113             while((str = reader.readLine()) != null)
114             {
115                buffer.append(str);
116             }
117             reader.close();
118
119             ret = buffer.toString();
120
121          }
122          catch(Exception JavaDoc e)
123          {
124             log.error("Can not unmarshall inputstream. Tried to unmarshall as both an object and string type.", e);
125             throw new IOException JavaDoc("Can not unmarshall inputstream.");
126          }
127       }
128       return ret;
129
130    }
131
132    public UnMarshaller cloneUnMarshaller() throws CloneNotSupportedException JavaDoc
133    {
134       HTTPUnMarshaller unmarshaller = new HTTPUnMarshaller();
135       unmarshaller.setClassLoader(this.customClassLoader);
136       return unmarshaller;
137    }
138
139 }
Popular Tags