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