View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client.util;
20  
21  import java.io.UnsupportedEncodingException;
22  import java.nio.ByteBuffer;
23  import java.nio.charset.UnsupportedCharsetException;
24  import java.util.Locale;
25  
26  import org.eclipse.jetty.client.api.Response;
27  import org.eclipse.jetty.client.api.Result;
28  import org.eclipse.jetty.http.HttpFields;
29  import org.eclipse.jetty.http.HttpHeader;
30  
31  /**
32   * <p>Implementation of {@link Response.Listener} that buffers the content up to a maximum length
33   * specified to the constructors.</p>
34   * <p>The content may be retrieved from {@link #onSuccess(Response)} or {@link #onComplete(Result)}
35   * via {@link #getContent()} or {@link #getContentAsString()}.</p>
36   */
37  public abstract class BufferingResponseListener extends Response.Listener.Empty
38  {
39      private final int maxLength;
40      private volatile byte[] buffer = new byte[0];
41      private volatile String encoding;
42  
43      /**
44       * Creates an instance with a default maximum length of 2 MiB.
45       */
46      public BufferingResponseListener()
47      {
48          this(2 * 1024 * 1024);
49      }
50  
51      /**
52       * Creates an instance with the given maximum length
53       *
54       * @param maxLength the maximum length of the content
55       */
56      public BufferingResponseListener(int maxLength)
57      {
58          this.maxLength = maxLength;
59      }
60  
61      @Override
62      public void onHeaders(Response response)
63      {
64          HttpFields headers = response.getHeaders();
65          long length = headers.getLongField(HttpHeader.CONTENT_LENGTH.asString());
66          if (length > maxLength)
67              response.abort(new IllegalArgumentException("Buffering capacity exceeded"));
68  
69          String contentType = headers.get(HttpHeader.CONTENT_TYPE);
70          if (contentType != null)
71          {
72              String charset = "charset=";
73              int index = contentType.toLowerCase(Locale.ENGLISH).indexOf(charset);
74              if (index > 0)
75              {
76                  String encoding = contentType.substring(index + charset.length());
77                  // Sometimes charsets arrive with an ending semicolon
78                  index = encoding.indexOf(';');
79                  if (index > 0)
80                      encoding = encoding.substring(0, index);
81                  this.encoding = encoding;
82              }
83          }
84      }
85  
86      @Override
87      public void onContent(Response response, ByteBuffer content)
88      {
89          long newLength = buffer.length + content.remaining();
90          if (newLength > maxLength)
91              response.abort(new IllegalArgumentException("Buffering capacity exceeded"));
92  
93          byte[] newBuffer = new byte[(int)newLength];
94          System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
95          content.get(newBuffer, buffer.length, content.remaining());
96          buffer = newBuffer;
97      }
98  
99      @Override
100     public abstract void onComplete(Result result);
101 
102     public String getEncoding()
103     {
104         return encoding;
105     }
106 
107     /**
108      * @return the content as bytes
109      * @see #getContentAsString()
110      */
111     public byte[] getContent()
112     {
113         return buffer;
114     }
115 
116     /**
117      * @return the content as a string, using the "Content-Type" header to detect the encoding
118      *         or defaulting to UTF-8 if the encoding could not be detected.
119      * @see #getContentAsString(String)
120      */
121     public String getContentAsString()
122     {
123         String encoding = this.encoding;
124         if (encoding == null)
125             encoding = "UTF-8";
126         return getContentAsString(encoding);
127     }
128 
129     /**
130      * @param encoding the encoding of the content bytes
131      * @return the content as a string, with the specified encoding
132      * @see #getContentAsString()
133      */
134     public String getContentAsString(String encoding)
135     {
136         try
137         {
138             return new String(getContent(), encoding);
139         }
140         catch (UnsupportedEncodingException x)
141         {
142             throw new UnsupportedCharsetException(encoding);
143         }
144     }
145 }