All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.rest.client.RestCallException Maven / Gradle / Ivy

// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you 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.                                              *
// ***************************************************************************************************************************
package org.apache.juneau.rest.client;

import static org.apache.juneau.common.internal.StringUtils.*;

import java.text.*;

import org.apache.http.*;
import org.apache.juneau.common.internal.*;
import org.apache.juneau.http.header.*;

/**
 * Exception representing a 400+ HTTP response code against a remote resource or other exception.
 *
 * 
See Also:
* * @serial exclude */ public final class RestCallException extends HttpException { private static final long serialVersionUID = 1L; private final RestResponse response; private final Thrown thrown; /** * Constructor. * * @param response The HTTP response. Can be null. * @param cause The cause of this exception. * @param message The {@link MessageFormat}-style message. * @param args Optional {@link MessageFormat}-style arguments. */ public RestCallException(RestResponse response, Throwable cause, String message, Object...args) { super(format(message,args),cause); this.response = response; this.thrown = response == null ? Thrown.EMPTY : response.getHeader("Thrown").asHeader(Thrown.class); } /** * Returns the value of the "Thrown" header on the response. * * @return The value of the "Thrown" header on the response, never null. */ public Thrown getThrown() { return thrown; } /** * Returns the HTTP response object that caused this exception. * * @return * The HTTP response object that caused this exception, or null if no response was created yet when the * exception was thrown. */ public RestResponse getResponse() { return this.response; } /** * Returns the HTTP response status code. * * @return The response status code. If a connection could not be made at all, returns 0. */ public int getResponseCode() { return response == null ? 0 : response.getStatusCode(); } /** * Similar to {@link #getCause()} but searches until it finds the throwable of the specified type. * * @param The throwable type. * @param c The throwable type. * @return The cause of the specified type, or null of not found. */ public T getCause(Class c) { return ThrowableUtils.getCause(c, this); } //------------------------------------------------------------------------------------------------------------------ // Helper methods //------------------------------------------------------------------------------------------------------------------ private static String format(String msg, Object...args) { if (args.length == 0) return clean(msg); return clean(StringUtils.format(msg, args)); } // HttpException has a bug involving ASCII control characters so just replace them with spaces. private static String clean(String message) { message = emptyIfNull(message); boolean needsCleaning = false; for (int i = 0; i < message.length() && !needsCleaning; i++) if (message.charAt(i) < 32) needsCleaning = true; if (!needsCleaning) return message; StringBuilder sb = new StringBuilder(message.length()); for (int i = 0; i < message.length(); i++) { char c = message.charAt(i); sb.append(c < 32 ? ' ' : c); } return sb.toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy