1 package org.mortbay.jetty.client;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.UnsupportedEncodingException;
6
7 import org.mortbay.io.Buffer;
8 import org.mortbay.io.BufferUtil;
9 import org.mortbay.jetty.HttpHeaders;
10 import org.mortbay.util.StringUtil;
11
12
13
14
15
16 public class ContentExchange extends CachedExchange
17 {
18 int _contentLength = 1024;
19 String _encoding = "utf-8";
20 ByteArrayOutputStream _responseContent;
21
22 public ContentExchange()
23 {
24 super(false);
25 }
26
27
28 public String getResponseContent() throws UnsupportedEncodingException
29 {
30 if (_responseContent != null)
31 {
32 return _responseContent.toString(_encoding);
33 }
34 return null;
35 }
36
37
38 protected void onResponseHeader(Buffer name, Buffer value) throws IOException
39 {
40 super.onResponseHeader(name,value);
41 int header = HttpHeaders.CACHE.getOrdinal(value);
42 switch (header)
43 {
44 case HttpHeaders.CONTENT_LANGUAGE_ORDINAL:
45 _contentLength = BufferUtil.toInt(value);
46 break;
47 case HttpHeaders.CONTENT_TYPE_ORDINAL:
48
49 String mime = StringUtil.asciiToLowerCase(value.toString());
50 int i = mime.indexOf("charset=");
51 if (i > 0)
52 {
53 mime = mime.substring(i + 8);
54 i = mime.indexOf(';');
55 if (i > 0)
56 mime = mime.substring(0,i);
57 }
58 if (mime != null && mime.length() > 0)
59 _encoding = mime;
60 break;
61 }
62 }
63
64 protected void onResponseContent(Buffer content) throws IOException
65 {
66 super.onResponseContent( content );
67 if (_responseContent == null)
68 _responseContent = new ByteArrayOutputStream(_contentLength);
69 content.writeTo(_responseContent);
70 }
71 }