001 /*
002 * The contents of this file are subject to the terms
003 * of the Common Development and Distribution License
004 * (the "License"). You may not use this file except
005 * in compliance with the License.
006 *
007 * You can obtain a copy of the license at
008 * http://www.opensource.org/licenses/cddl1.php
009 * See the License for the specific language governing
010 * permissions and limitations under the License.
011 */
012
013 package javax.ws.rs;
014
015 import javax.ws.rs.core.Response;
016
017 /**
018 * Runtime exception for applications.
019 * <p>
020 * This exception may be thrown by a resource method, provider or
021 * {@link javax.ws.rs.core.StreamingOutput} implementation if a specific
022 * HTTP error response needs to be produced. Only effective if thrown prior to
023 * the response being committed.
024 *
025 * @author Paul.Sandoz@Sun.Com
026 */
027 public class WebApplicationException extends RuntimeException {
028
029 private static final long serialVersionUID = 11660101L;
030
031 private Response response;
032
033 /**
034 * Construct a new instance with a blank message and default HTTP status code of 500
035 */
036 public WebApplicationException() {
037 this(null, Response.Status.INTERNAL_SERVER_ERROR);
038 }
039
040 /**
041 * Construct a new instance using the supplied response
042 * @param response the response that will be returned to the client, a value
043 * of null will be replaced with an internal server error response (status
044 * code 500)
045 */
046 public WebApplicationException(Response response) {
047 this(null,response);
048 }
049
050 /**
051 * Construct a new instance with a blank message and specified HTTP status code
052 * @param status the HTTP status code that will be returned to the client
053 */
054 public WebApplicationException(int status) {
055 this(null, status);
056 }
057
058 /**
059 * Construct a new instance with a blank message and specified HTTP status code
060 * @param status the HTTP status code that will be returned to the client
061 * @throws IllegalArgumentException if status is null
062 */
063 public WebApplicationException(Response.Status status) {
064 this(null, status);
065 }
066
067 /**
068 * Construct a new instance with a blank message and default HTTP status code of 500
069 * @param cause the underlying cause of the exception
070 */
071 public WebApplicationException(Throwable cause) {
072 this(cause,Response.Status.INTERNAL_SERVER_ERROR);
073 }
074
075 /**
076 * Construct a new instance using the supplied response
077 * @param response the response that will be returned to the client, a value
078 * of null will be replaced with an internal server error response (status
079 * code 500)
080 * @param cause the underlying cause of the exception
081 */
082 public WebApplicationException(Throwable cause, Response response) {
083 super(cause);
084 if (response==null)
085 this.response = Response.serverError().build();
086 else
087 this.response = response;
088 }
089
090 /**
091 * Construct a new instance with a blank message and specified HTTP status code
092 * @param status the HTTP status code that will be returned to the client
093 * @param cause the underlying cause of the exception
094 */
095 public WebApplicationException(Throwable cause, int status) {
096 this(cause, Response.status(status).build());
097 }
098
099 /**
100 * Construct a new instance with a blank message and specified HTTP status code
101 * @param status the HTTP status code that will be returned to the client
102 * @param cause the underlying cause of the exception
103 * @throws IllegalArgumentException if status is null
104 */
105 public WebApplicationException(Throwable cause, Response.Status status) {
106 this(cause, Response.status(status).build());
107 }
108
109 /**
110 * Get the HTTP response.
111 *
112 * @return the HTTP response.
113 */
114 public Response getResponse() {
115 return response;
116 }
117 }