1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.http; |
20 | |
21 | import org.jclouds.javax.annotation.Nullable; |
22 | |
23 | import org.jclouds.io.Payload; |
24 | |
25 | import com.google.common.collect.ImmutableMultimap; |
26 | import com.google.common.collect.Multimap; |
27 | |
28 | /** |
29 | * Represents a response produced from {@link HttpCommandExecutorService} |
30 | * |
31 | * @author Adrian Cole |
32 | */ |
33 | public class HttpResponse extends HttpMessage { |
34 | public static Builder builder() { |
35 | return new Builder(); |
36 | } |
37 | |
38 | public static class Builder extends HttpMessage.Builder<HttpResponse> { |
39 | private int statusCode; |
40 | private String message; |
41 | |
42 | public Builder message(String message) { |
43 | this.message = message; |
44 | return this; |
45 | } |
46 | |
47 | public Builder statusCode(int statusCode) { |
48 | this.statusCode = statusCode; |
49 | return this; |
50 | } |
51 | |
52 | @Override |
53 | public Builder payload(Payload payload) { |
54 | return (Builder) super.payload(payload); |
55 | } |
56 | |
57 | @Override |
58 | public Builder headers(Multimap<String, String> headers) { |
59 | return (Builder) super.headers(headers); |
60 | } |
61 | |
62 | public HttpResponse build() { |
63 | return new HttpResponse(statusCode, message, payload, headers); |
64 | } |
65 | |
66 | public static Builder from(HttpResponse input) { |
67 | return new Builder().message(input.getMessage()).statusCode(input.getStatusCode()).payload(input.getPayload()) |
68 | .headers(input.getHeaders()); |
69 | } |
70 | |
71 | } |
72 | |
73 | private final int statusCode; |
74 | private final String message; |
75 | |
76 | public HttpResponse(int statusCode, String message, @Nullable Payload payload) { |
77 | this(statusCode, message, payload, ImmutableMultimap.<String, String> of()); |
78 | } |
79 | |
80 | public HttpResponse(int statusCode, String message, @Nullable Payload payload, Multimap<String, String> headers) { |
81 | super(payload, headers); |
82 | this.statusCode = statusCode; |
83 | this.message = message; |
84 | } |
85 | |
86 | public int getStatusCode() { |
87 | return statusCode; |
88 | } |
89 | |
90 | public String getMessage() { |
91 | return message; |
92 | } |
93 | |
94 | @Override |
95 | public String toString() { |
96 | return "[message=" + message + ", statusCode=" + statusCode + ", headers=" + headers + ", payload=" + payload |
97 | + "]"; |
98 | } |
99 | |
100 | public String getStatusLine() { |
101 | return String.format("HTTP/1.1 %d %s", getStatusCode(), getMessage()); |
102 | } |
103 | |
104 | @Override |
105 | public Builder toBuilder() { |
106 | return Builder.from(this); |
107 | } |
108 | |
109 | @Override |
110 | public int hashCode() { |
111 | final int prime = 31; |
112 | int result = super.hashCode(); |
113 | result = prime * result + ((payload == null) ? 0 : payload.hashCode()); |
114 | result = prime * result + ((headers == null) ? 0 : headers.hashCode()); |
115 | result = prime * result + ((message == null) ? 0 : message.hashCode()); |
116 | result = prime * result + statusCode; |
117 | return result; |
118 | } |
119 | |
120 | @Override |
121 | public boolean equals(Object obj) { |
122 | if (this == obj) |
123 | return true; |
124 | if (!super.equals(obj)) |
125 | return false; |
126 | if (getClass() != obj.getClass()) |
127 | return false; |
128 | HttpResponse other = (HttpResponse) obj; |
129 | if (payload == null) { |
130 | if (other.payload != null) |
131 | return false; |
132 | } else if (!payload.equals(other.payload)) |
133 | return false; |
134 | if (headers == null) { |
135 | if (other.headers != null) |
136 | return false; |
137 | } else if (!headers.equals(other.headers)) |
138 | return false; |
139 | if (message == null) { |
140 | if (other.message != null) |
141 | return false; |
142 | } else if (!message.equals(other.message)) |
143 | return false; |
144 | if (statusCode != other.statusCode) |
145 | return false; |
146 | return true; |
147 | } |
148 | |
149 | } |