View Javadoc

1   //========================================================================
2   //Copyright 2006-2007 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.mortbay.jetty.client;
16  
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.io.UnsupportedEncodingException;
23  
24  import org.mortbay.io.Buffer;
25  import org.mortbay.io.BufferUtil;
26  import org.mortbay.jetty.HttpHeaders;
27  import org.mortbay.util.ByteArrayOutputStream2;
28  import org.mortbay.util.StringUtil;
29  
30  /* ------------------------------------------------------------ */
31  /**
32   * A CachedExchange that retains response content for later use.
33   *
34   */
35  public class ContentExchange extends CachedExchange
36  {
37      protected int _responseStatus;
38      protected int _contentLength = -1;
39      protected String _encoding = "utf-8";
40      protected ByteArrayOutputStream2 _responseContent;
41  
42      protected File _fileForUpload;
43  
44      /* ------------------------------------------------------------ */
45      public ContentExchange()
46      {
47          super(false);
48      }
49  
50      /* ------------------------------------------------------------ */
51      public ContentExchange(boolean keepHeaders)
52      {
53          super(keepHeaders);
54      }
55  
56      /* ------------------------------------------------------------ */
57      public int getResponseStatus()
58      {
59          if (getStatus() < HttpExchange.STATUS_PARSING_HEADERS)
60              throw new IllegalStateException("Response not received");
61          return _responseStatus;
62      }
63  
64      
65      /* ------------------------------------------------------------ */
66      /**
67       * @return The response content as a String
68       * @throws UnsupportedEncodingException
69       */
70      public String getResponseContent() throws UnsupportedEncodingException
71      {
72          if (_responseContent != null)
73              return _responseContent.toString(_encoding);
74          return null;
75      }
76      
77      /* ------------------------------------------------------------ */
78      /**
79       * @return The response content as a byte array;
80       */
81      public byte[] getResponseBytes()
82      {
83          if (_responseContent != null)
84          {
85              if (_contentLength>=0 && _responseContent.getBuf().length==_contentLength)
86                  return _responseContent.getBuf();
87              return _responseContent.toByteArray();
88          }
89          return null;
90      }
91      
92      /* ------------------------------------------------------------ */
93      /**
94       * @param out An output stream to write the content to.
95       * @throws IOException
96       */
97      public void writeResponseBytesTo(OutputStream out) throws IOException
98      {
99          if (_responseContent != null)
100             out.write(_responseContent.getBuf(),0,_responseContent.getCount());
101     }
102 
103     /* ------------------------------------------------------------ */
104     protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
105     {
106         _responseStatus = status;
107         super.onResponseStatus(version,status,reason);
108     }
109 
110 
111     /* ------------------------------------------------------------ */
112     protected void onResponseHeader(Buffer name, Buffer value) throws IOException
113     {
114         super.onResponseHeader(name,value);
115         int header = HttpHeaders.CACHE.getOrdinal(name);
116         switch (header)
117         {
118             case HttpHeaders.CONTENT_LENGTH_ORDINAL:
119                 _contentLength = BufferUtil.toInt(value);
120                 break;
121             case HttpHeaders.CONTENT_TYPE_ORDINAL:
122                 String mime = StringUtil.asciiToLowerCase(value.toString());
123                 int i = mime.indexOf("charset=");
124                 if (i > 0)
125                     _encoding = mime.substring(i + 8);
126                 break;
127         }
128     }
129 
130     /* ------------------------------------------------------------ */
131     protected void onResponseContent(Buffer content) throws IOException
132     {
133         super.onResponseContent( content );
134         if (_responseContent == null)
135             _responseContent = (_contentLength>=0)?new ByteArrayOutputStream2(_contentLength):new ByteArrayOutputStream2();
136         
137         content.writeTo(_responseContent);
138     }
139 
140     /* ------------------------------------------------------------ */
141     protected void onRetry() throws IOException
142     {
143         if ( _fileForUpload != null  )
144         {
145             _requestContent = null;
146             _requestContentSource =  getInputStream();
147         }
148         else if (_requestContentSource != null) 
149         {
150 			if (_requestContentSource.markSupported()) 
151 			{
152 				_requestContent = null;
153 				_requestContentSource.reset();
154 			} 
155 			else 
156 			{
157 				throw new IOException("Unsupported retry attempt");
158 			}
159 		}
160 
161         super.onRetry();
162     }
163 
164     /* ------------------------------------------------------------ */
165     private InputStream getInputStream() throws IOException
166     {
167         return new FileInputStream( _fileForUpload );
168     }
169 
170     /* ------------------------------------------------------------ */
171     public File getFileForUpload()
172     {
173         return _fileForUpload;
174     }
175 
176     /* ------------------------------------------------------------ */
177     public void setFileForUpload(File fileForUpload) throws IOException
178     {
179         this._fileForUpload = fileForUpload;
180         _requestContentSource = getInputStream();
181     }
182 }