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