-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathHttpResponseException.java
More file actions
205 lines (191 loc) · 6.45 KB
/
HttpResponseException.java
File metadata and controls
205 lines (191 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.contrastsecurity.exceptions;
/*-
* #%L
* Contrast Java SDK
* %%
* Copyright (C) 2022 - 2026 Contrast Security, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
/**
* Thrown when the Contrast API returns a response that indicates an error has occurred. Captures
* the HTTP status line and body.
*
* <p>Typically, HTTP status codes in the 4XX and 5XX ranges indicate an error; however, in some
* legacy cases, the Contrast API may return a successful status code (e.g. 200) with an error body.
* In this case, the Contrast Java SDK still throws this exception.
*/
public class HttpResponseException extends ContrastException {
/**
* Static factory method that captures the status line and body of the given {@code
* HttpURLConnection} to create a new exception.
*
* @param connection connection from which to derive status line and body information
* @param message error message provided by the caller
* @return new {@code HttpResponseException}
* @throws IOException when fails to read the status line or body of the response
*/
public static HttpResponseException fromConnection(
final HttpURLConnection connection, final String message) throws IOException {
final String method = connection.getRequestMethod();
final String path = connection.getURL().getPath();
final int code = connection.getResponseCode();
final ExceptionFactory factory = factoryFromResponseCode(code);
final String body = readBody(connection, code);
final String status = connection.getResponseMessage();
return factory.create(message, method, path, code, status, body);
}
/**
* Maps HTTP response codes to the constructor for the appropriate concrete type.
*
* @param code response code
* @return factory for creating a new concrete type of {@code HttpResponseException}
*/
private static ExceptionFactory factoryFromResponseCode(final int code) {
switch (code) {
case 401:
case 403:
return UnauthorizedException::new;
case 404:
return ResourceNotFoundException::new;
default:
return HttpResponseException::new;
}
}
/**
* Reads the response body in its entirety. Error responses should always be relatively small, so
* they should fit into memory.
*
* @return body as a string, or {@code null}
*/
private static String readBody(final HttpURLConnection connection, final int code)
throws IOException {
final String body;
try (InputStream is =
code >= HttpURLConnection.HTTP_BAD_REQUEST
? connection.getErrorStream()
: connection.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
// check if there is a body to read
if (is == null) {
body = null;
} else {
// read the entire body, because error responses should always be small and fit into memory
final byte[] buffer = new byte[4096];
int read;
while ((read = is.read(buffer)) > 0) {
bos.write(buffer, 0, read);
}
body = bos.toString(StandardCharsets.UTF_8.name());
}
}
return body;
}
/** Functional interface that describes the constructor shared by this class and its subclasses */
private interface ExceptionFactory {
HttpResponseException create(
String message, String method, String path, int code, String status, String body);
}
private final String method;
private final String path;
private final int code;
private final String status;
private final String body;
/**
* Constructor.
*
* @param message error message provided by the caller
* @param method HTTP method used in the request
* @param path HTTP path from the request
* @param code code from the status line e.g. 400
* @param status message from the status line e.g. Bad Request
*/
public HttpResponseException(
final String message,
final String method,
final String path,
final int code,
final String status) {
this(message, method, path, code, status, null);
}
/**
* Constructor.
*
* @param message error message provided by the caller
* @param method HTTP method used in the request
* @param path HTTP path from the request
* @param code code from the status line e.g. 400
* @param status message from the status line e.g. Bad Request
* @param body the body of the response, or {@code null} if there is no such body
* @throws NullPointerException when message, method, or path are {@code null}
*/
public HttpResponseException(
final String message,
final String method,
final String path,
final int code,
final String status,
final String body) {
super(message);
this.method = Objects.requireNonNull(method);
this.path = Objects.requireNonNull(path);
this.code = code;
this.status = status;
this.body = body;
}
/**
* @return code from the status line e.g. 400
*/
public int getCode() {
return code;
}
/**
* @return message from the status line e.g. Bad Request, or {@code null} if no such status line
* exists
*/
public String getStatus() {
return status;
}
/**
* @return the body of the response, or {@code null} if there is no such body
*/
public String getBody() {
return body;
}
@Override
public String getMessage() {
final StringBuilder sb =
new StringBuilder()
.append(super.getMessage())
.append("\n")
.append(method)
.append(" ")
.append(path)
.append("\n\n")
.append(code);
if (status != null) {
sb.append(" ").append(status);
}
if (body != null) {
sb.append("\n\n").append(body);
}
return sb.toString();
}
}