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.ByteArrayOutputStream;
    18  import java.io.File;
    19  import java.io.FileInputStream;
    20  import java.io.IOException;
    21  import java.io.InputStream;
    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.StringUtil;
    28
    29  /**
    30   * A CachedExchange that retains all content for later use.
    31   *
    32   */
    33  public class ContentExchange extends CachedExchange
    34  {
    35      int _contentLength = 1024;
    36      String _encoding = "utf-8";
    37      ByteArrayOutputStream _responseContent;
    38
    39      File _fileForUpload;
    40
    41      public ContentExchange()
    42      {
    43          super(false);
    44      }
    45
    46      /* ------------------------------------------------------------ */
    47      public ContentExchange(boolean cacheFields)
    48      {
    49          super(cacheFields);
    50      }
    51
    52      /* ------------------------------------------------------------ */
    53      public String getResponseContent() throws UnsupportedEncodingException
    54      {
    55          if (_responseContent != null)
    56          {
    57              return _responseContent.toString(_encoding);
    58          }
    59          return null;
    60      }
    61
    62      /* ------------------------------------------------------------ */
    63      protected void onResponseHeader(Buffer name, Buffer value) throws IOException
    64      {
    65          super.onResponseHeader(name,value);
    66          int header = HttpHeaders.CACHE.getOrdinal(value);
    67          switch (header)
    68          {
    69              case HttpHeaders.CONTENT_LENGTH_ORDINAL:
    70                  _contentLength = BufferUtil.toInt(value);
    71                  break;
    72              case HttpHeaders.CONTENT_TYPE_ORDINAL:
    73
    74                  String mime = StringUtil.asciiToLowerCase(value.toString());
    75                  int i = mime.indexOf("charset=");
    76                  if (i > 0)
    77                  {
    78                      mime = mime.substring(i + 8);
    79                      i = mime.indexOf(';');
    80                      if (i > 0)
    81                          mime = mime.substring(0,i);
    82                  }
    83                  if (mime != null && mime.length() > 0)
    84                      _encoding = mime;
    85                  break;
    86          }
    87      }
    88
    89      protected void onResponseContent(Buffer content) throws IOException
    90      {
    91          super.onResponseContent( content );
    92          if (_responseContent == null)
    93              _responseContent = new ByteArrayOutputStream(_contentLength);
    94          content.writeTo(_responseContent);
    95      }
    96
    97      protected void onRetry() throws IOException
    98      {
    99          if ( _fileForUpload != null  )
    100         {
    101             _requestContent = null;
    102             _requestContentSource =  getInputStream();
    103         }
    104         else if ( _requestContentSource != null )
    105         {
    106             throw new IOException("Unsupported Retry attempt, no registered file for upload.");
    107         }
    108
    109         super.onRetry();
    110     }
    111
    112     private InputStream getInputStream() throws IOException
    113     {
    114         return new FileInputStream( _fileForUpload );
    115     }
    116
    117     public File getFileForUpload()
    118     {
    119         return _fileForUpload;
    120     }
    121
    122     public void setFileForUpload(File fileForUpload) throws IOException
    123     {
    124         this._fileForUpload = fileForUpload;
    125         _requestContentSource = getInputStream();
    126     }
    127 }