1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 package org.apache.hc.client5.http; 28 29 import org.apache.hc.core5.http.ContentType; 30 import org.apache.hc.core5.util.TextUtils; 31 32 /** 33 * Signals a non 2xx HTTP response. 34 * 35 * @since 4.0 36 */ 37 public class HttpResponseException extends ClientProtocolException { 38 39 private static final long serialVersionUID = -7186627969477257933L; 40 41 private final int statusCode; 42 private final String reasonPhrase; 43 private final byte[] contentBytes; 44 private final ContentType contentType; 45 46 /** 47 * Constructs a new instance of {@code HttpResponseException} with the given 48 * status code and reason phrase, and no content bytes or content type. 49 * 50 * @param statusCode the HTTP status code 51 * @param reasonPhrase the reason phrase associated with the HTTP status code 52 */ 53 public HttpResponseException(final int statusCode, final String reasonPhrase) { 54 this(statusCode, reasonPhrase, null, null); 55 } 56 57 /** 58 * Constructs a new instance of {@code HttpResponseException} with the given 59 * status code, reason phrase, content bytes, and content type. 60 * 61 * @param statusCode the HTTP status code 62 * @param reasonPhrase the reason phrase associated with the HTTP status code 63 * @param contentBytes the content bytes of the HTTP response 64 * @param contentType the content type of the HTTP response 65 */ 66 public HttpResponseException(final int statusCode, final String reasonPhrase, final byte[] contentBytes, final ContentType contentType) { 67 super(String.format("status code: %d" + 68 (TextUtils.isBlank(reasonPhrase) ? "" : ", reason phrase: %s") + 69 (contentBytes == null ? "" : ", content: %s"), 70 statusCode, reasonPhrase, 71 contentBytes == null || contentType == null || contentType.getCharset() == null ? 72 null : 73 new String(contentBytes, contentType.getCharset()))); 74 75 this.statusCode = statusCode; 76 this.reasonPhrase = reasonPhrase; 77 this.contentBytes = contentBytes; 78 this.contentType = contentType; 79 } 80 81 82 public int getStatusCode() { 83 return this.statusCode; 84 } 85 86 public String getReasonPhrase() { 87 return this.reasonPhrase; 88 } 89 90 public byte[] getContentBytes() { 91 return contentBytes; 92 } 93 94 public ContentType getContentType() { 95 return contentType; 96 } 97 }