001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.client;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016
017import java.text.*;
018
019import org.apache.http.*;
020import org.apache.juneau.common.internal.*;
021import org.apache.juneau.http.header.*;
022
023/**
024 * Exception representing a <c>400+</c> HTTP response code against a remote resource or other exception.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-client">juneau-rest-client</a>
028 * </ul>
029 *
030 * @serial exclude
031 */
032public final class RestCallException extends HttpException {
033
034   private static final long serialVersionUID = 1L;
035
036   private final RestResponse response;
037   private final Thrown thrown;
038
039   /**
040    * Constructor.
041    *
042    * @param response The HTTP response.  Can be <jk>null</jk>.
043    * @param cause The cause of this exception.
044    * @param message The {@link MessageFormat}-style message.
045    * @param args Optional {@link MessageFormat}-style arguments.
046    */
047   public RestCallException(RestResponse response, Throwable cause, String message, Object...args) {
048      super(format(message,args),cause);
049      this.response = response;
050      this.thrown = response == null ? Thrown.EMPTY : response.getHeader("Thrown").asHeader(Thrown.class);
051   }
052
053   /**
054    * Returns the value of the <js>"Thrown"</js> header on the response.
055    *
056    * @return The value of the <js>"Thrown"</js> header on the response, never <jk>null</jk>.
057    */
058   public Thrown getThrown() {
059      return thrown;
060   }
061
062   /**
063    * Returns the HTTP response object that caused this exception.
064    *
065    * @return
066    *    The HTTP response object that caused this exception, or <jk>null</jk> if no response was created yet when the
067    *    exception was thrown.
068    */
069   public RestResponse getResponse() {
070      return this.response;
071   }
072
073   /**
074    * Returns the HTTP response status code.
075    *
076    * @return The response status code.  If a connection could not be made at all, returns <c>0</c>.
077    */
078   public int getResponseCode() {
079      return response == null ? 0 : response.getStatusCode();
080   }
081
082   /**
083    * Similar to {@link #getCause()} but searches until it finds the throwable of the specified type.
084    *
085    * @param <T> The throwable type.
086    * @param c The throwable type.
087    * @return The cause of the specified type, or <jk>null</jk> of not found.
088    */
089   public <T extends Throwable> T getCause(Class<T> c) {
090      return ThrowableUtils.getCause(c, this);
091   }
092
093   //------------------------------------------------------------------------------------------------------------------
094   // Helper methods
095   //------------------------------------------------------------------------------------------------------------------
096
097   private static String format(String msg, Object...args) {
098      if (args.length == 0)
099         return clean(msg);
100      return clean(StringUtils.format(msg, args));
101   }
102
103   // HttpException has a bug involving ASCII control characters so just replace them with spaces.
104   private static String clean(String message) {
105      message = emptyIfNull(message);
106
107      boolean needsCleaning = false;
108      for (int i = 0; i < message.length() && !needsCleaning; i++)
109         if (message.charAt(i) < 32)
110            needsCleaning = true;
111
112      if (!needsCleaning)
113         return message;
114
115      StringBuilder sb = new StringBuilder(message.length());
116      for (int i = 0; i < message.length(); i++) {
117         char c = message.charAt(i);
118         sb.append(c < 32 ? ' ' : c);
119      }
120
121      return sb.toString();
122   }
123}