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;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.eclipse.jetty.client.api.Request;
25  import org.eclipse.jetty.client.api.Response;
26  import org.eclipse.jetty.http.HttpFields;
27  import org.eclipse.jetty.http.HttpVersion;
28  
29  public class HttpResponse implements Response
30  {
31      private final HttpFields headers = new HttpFields();
32      private final Request request;
33      private final List<ResponseListener> listeners;
34      private HttpVersion version;
35      private int status;
36      private String reason;
37  
38      public HttpResponse(Request request, List<ResponseListener> listeners)
39      {
40          this.request = request;
41          this.listeners = listeners;
42      }
43  
44      public HttpVersion getVersion()
45      {
46          return version;
47      }
48  
49      public HttpResponse version(HttpVersion version)
50      {
51          this.version = version;
52          return this;
53      }
54  
55      @Override
56      public int getStatus()
57      {
58          return status;
59      }
60  
61      public HttpResponse status(int status)
62      {
63          this.status = status;
64          return this;
65      }
66  
67      public String getReason()
68      {
69          return reason;
70      }
71  
72      public HttpResponse reason(String reason)
73      {
74          this.reason = reason;
75          return this;
76      }
77  
78      @Override
79      public HttpFields getHeaders()
80      {
81          return headers;
82      }
83  
84      @Override
85      public long getConversationID()
86      {
87          return request.getConversationID();
88      }
89  
90      @Override
91      public <T extends ResponseListener> List<T> getListeners(Class<T> type)
92      {
93          ArrayList<T> result = new ArrayList<>();
94          for (ResponseListener listener : listeners)
95              if (type == null || type.isInstance(listener))
96                  result.add((T)listener);
97          return result;
98      }
99  
100     @Override
101     public boolean abort(Throwable cause)
102     {
103         return request.abort(cause);
104     }
105 
106     @Override
107     public String toString()
108     {
109         return String.format("%s[%s %d %s]", HttpResponse.class.getSimpleName(), getVersion(), getStatus(), getReason());
110     }
111 }