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; 014 015import java.lang.reflect.*; 016 017import org.apache.juneau.*; 018import org.apache.juneau.http.annotation.*; 019import org.apache.juneau.http.exception.*; 020import org.apache.juneau.reflect.*; 021 022/** 023 * A {@link RuntimeException} meant to wrap a non-{@link RuntimeException}. 024 */ 025public final class HttpRuntimeException extends RuntimeException { 026 private static final long serialVersionUID = 1L; 027 028 final Throwable t; 029 030 /** 031 * Constructor. 032 * 033 * @param t Wrapped exception. 034 */ 035 public HttpRuntimeException(Throwable t) { 036 this.t = t; 037 } 038 039 /** 040 * Returns the wrapped throwable. 041 * 042 * @return The wrapped throwable. 043 */ 044 public Throwable getInner() { 045 return t; 046 } 047 048 /** 049 * Takes in an arbitrary {@link Throwable} and converts it to an appropriate runtime exception for producing an 050 * HTTP response. 051 * 052 * @param t The throwable to wrap. 053 * @param ec The exception class to create if the specified throwable cannot produce a valid HTTP response. 054 * @return RuntimeException The new exception to throw. 055 */ 056 public static RuntimeException toHttpException(Throwable t, Class<?> ec) { 057 return toHttpException(t, ec, null); 058 } 059 060 /** 061 * Takes in an arbitrary {@link Throwable} and converts it to an appropriate runtime exception for producing an 062 * HTTP response. 063 * 064 * @param t The throwable to wrap. 065 * @param ec The exception class to create if the specified throwable cannot produce a valid HTTP response. 066 * @param msg The message text to pass to the ec class constructor. 067 * @param args The message arguments to pass to the ec class constructor. 068 * @return RuntimeException The new exception to throw. 069 */ 070 @SuppressWarnings("deprecation") 071 public static RuntimeException toHttpException(Throwable t, Class<?> ec, String msg, Object...args) { 072 ClassInfo ci = ClassInfo.ofc(t); 073 074 // If it's a RestException or is any RuntimeException annotated with @Response, it can be rethrown. 075 if (ci.isRuntimeException() && (ci.isChildOf(RestException.class) || ci.hasAnnotation(Response.class))) 076 return (RuntimeException)t; 077 078 // If it's a non-RuntimeException but annotated with @Response, it can be wrapped and rethrown. 079 if (ci.hasAnnotation(Response.class)) 080 return new HttpRuntimeException(t); 081 082 if (ci.is(InvocationTargetException.class)) 083 return new HttpRuntimeException(((InvocationTargetException)t).getCause()); 084 085 if (ec == null) 086 ec = InternalServerError.class; 087 ClassInfo eci = ClassInfo.ofc(ec); 088 089 try { 090 ConstructorInfo cci = eci.getPublicConstructor(Throwable.class, String.class, Object[].class); 091 if (cci != null) 092 return toHttpException((Throwable)cci.invoke(t, msg, args), InternalServerError.class); 093 094 cci = eci.getPublicConstructor(Throwable.class); 095 if (cci != null) 096 return toHttpException((Throwable)cci.invoke(t), InternalServerError.class); 097 098 System.err.println("WARNING: Class '"+ec+"' does not have a public constructor that takes in valid arguments."); 099 return new InternalServerError(t); 100 } catch (ExecutableException e) { 101 throw new InternalServerError(e.getCause()); 102 } 103 } 104}