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     protected void onResponseHeader(Buffer name, Buffer value) throws IOException
112     {
113         super.onResponseHeader(name,value);
114         int header = HttpHeaders.CACHE.getOrdinal(value);
115         switch (header)
116         {
117             case HttpHeaders.CONTENT_LENGTH_ORDINAL:
118                 _contentLength = BufferUtil.toInt(value);
119                 break;
120             case HttpHeaders.CONTENT_TYPE_ORDINAL:
121 
122                 String mime = StringUtil.asciiToLowerCase(value.toString());
123                 int i = mime.indexOf("charset=");
124                 if (i > 0)
125                 {
126                     mime = mime.substring(i + 8);
127                     i = mime.indexOf(';');
128                     if (i > 0)
129                         mime = mime.substring(0,i);
130                 }
131                 if (mime != null && mime.length() > 0)
132                     _encoding = mime;
133                 break;
134         }
135     }
136 
137     /* ------------------------------------------------------------ */
138     protected void onResponseContent(Buffer content) throws IOException
139     {
140         super.onResponseContent( content );
141         if (_responseContent == null)
142             _responseContent = (_contentLength>=0)?new ByteArrayOutputStream2(_contentLength):new ByteArrayOutputStream2();
143         
144         content.writeTo(_responseContent);
145     }
146 
147     /* ------------------------------------------------------------ */
148     protected void onRetry() throws IOException
149     {
150         if ( _fileForUpload != null  )
151         {
152             _requestContent = null;
153             _requestContentSource =  getInputStream();
154         }
155         else if ( _requestContentSource != null )
156         {
157             throw new IOException("Unsupported Retry attempt, no registered file for upload.");
158         }
159 
160         super.onRetry();
161     }
162 
163     /* ------------------------------------------------------------ */
164     private InputStream getInputStream() throws IOException
165     {
166         return new FileInputStream( _fileForUpload );
167     }
168 
169     /* ------------------------------------------------------------ */
170     public File getFileForUpload()
171     {
172         return _fileForUpload;
173     }
174 
175     /* ------------------------------------------------------------ */
176     public void setFileForUpload(File fileForUpload) throws IOException
177     {
178         this._fileForUpload = fileForUpload;
179         _requestContentSource = getInputStream();
180     }
181 }