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 java.io.IOException; |
22 | |
23 | import org.jclouds.javax.annotation.Nullable; |
24 | |
25 | import org.jclouds.io.payloads.StringPayload; |
26 | import org.jclouds.util.Strings2; |
27 | |
28 | /** |
29 | * Represents an error obtained from an HttpResponse. |
30 | * |
31 | * @author Adrian Cole |
32 | * |
33 | */ |
34 | public class HttpResponseException extends RuntimeException { |
35 | |
36 | private static final long serialVersionUID = 1L; |
37 | protected final HttpCommand command; |
38 | protected final HttpResponse response; |
39 | private String content; |
40 | |
41 | public HttpResponseException(String message, HttpCommand command, @Nullable HttpResponse response, Throwable cause) { |
42 | super(message, cause); |
43 | this.command = command; |
44 | this.response = response; |
45 | } |
46 | |
47 | public HttpResponseException(String message, HttpCommand command, @Nullable HttpResponse response, |
48 | String content, Throwable cause) { |
49 | super(message, cause); |
50 | this.command = command; |
51 | this.response = response; |
52 | this.content = content; |
53 | } |
54 | |
55 | public HttpResponseException(HttpCommand command, HttpResponse response, Throwable cause) { |
56 | this(String.format("command: %1$s failed with response: %2$s", command.getCurrentRequest().getRequestLine(), |
57 | response.getStatusLine()), command, response, cause); |
58 | } |
59 | |
60 | public HttpResponseException(HttpCommand command, HttpResponse response, String content, Throwable cause) { |
61 | this(String.format("command: %1$s failed with response: %2$s; content: [%3$s]", command.getCurrentRequest() |
62 | .getRequestLine(), response.getStatusLine()), command, response, content, cause); |
63 | } |
64 | |
65 | public HttpResponseException(String message, HttpCommand command, @Nullable HttpResponse response) { |
66 | super(message); |
67 | this.command = command; |
68 | this.response = response; |
69 | } |
70 | |
71 | public HttpResponseException(String message, HttpCommand command, @Nullable HttpResponse response, String content) { |
72 | super(message); |
73 | this.command = command; |
74 | this.response = response; |
75 | this.content = content; |
76 | } |
77 | |
78 | public HttpResponseException(HttpCommand command, HttpResponse response) { |
79 | this(String.format("request: %s %sfailed with response: %s", command.getCurrentRequest().getRequestLine(), |
80 | requestPayloadIfStringOrFormIfNotReturnEmptyString((HttpRequest) command.getCurrentRequest()), |
81 | response.getStatusLine()), command, response); |
82 | } |
83 | |
84 | static String requestPayloadIfStringOrFormIfNotReturnEmptyString(HttpRequest request) { |
85 | if (request.getPayload() != null |
86 | && ("application/x-www-form-urlencoded".equals(request.getPayload().getContentMetadata().getContentType()) || request |
87 | .getPayload() instanceof StringPayload) |
88 | && request.getPayload().getContentMetadata().getContentLength() != null |
89 | && request.getPayload().getContentMetadata().getContentLength() < 1024) { |
90 | try { |
91 | return String.format(" [%s] ", request.getPayload() instanceof StringPayload ? request.getPayload() |
92 | .getRawContent() : Strings2.toStringAndClose(request.getPayload().getInput())); |
93 | } catch (IOException e) { |
94 | } |
95 | } |
96 | return ""; |
97 | } |
98 | |
99 | public HttpResponseException(HttpCommand command, HttpResponse response, String content) { |
100 | this(String.format("command: %s failed with response: %s; content: [%s]", command.getCurrentRequest() |
101 | .getRequestLine(), response.getStatusLine(), content), command, response, content); |
102 | } |
103 | |
104 | public HttpCommand getCommand() { |
105 | return command; |
106 | } |
107 | |
108 | public HttpResponse getResponse() { |
109 | return response; |
110 | } |
111 | |
112 | public void setContent(String content) { |
113 | this.content = content; |
114 | } |
115 | |
116 | public String getContent() { |
117 | return content; |
118 | } |
119 | |
120 | } |