File: WebApplicationException.java

package info (click to toggle)
libjsr311-api-java 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 420 kB
  • sloc: java: 1,547; xml: 166; makefile: 9
file content (117 lines) | stat: -rw-r--r-- 3,945 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 * 
 * You can obtain a copy of the license at
 * http://www.opensource.org/licenses/cddl1.php
 * See the License for the specific language governing
 * permissions and limitations under the License.
 */

package javax.ws.rs;

import javax.ws.rs.core.Response;

/**
 * Runtime exception for applications.
 * <p>
 * This exception may be thrown by a resource method, provider or 
 * {@link javax.ws.rs.core.StreamingOutput} implementation if a specific 
 * HTTP error response needs to be produced. Only effective if thrown prior to
 * the response being committed.
 *
 * @author Paul.Sandoz@Sun.Com
 */
public class WebApplicationException extends RuntimeException {

    private static final long serialVersionUID = 11660101L;
    
    private Response response;

    /**
     * Construct a new instance with a blank message and default HTTP status code of 500
     */
    public WebApplicationException() {
        this(null, Response.Status.INTERNAL_SERVER_ERROR);
    }

    /**
     * Construct a new instance using the supplied response
     * @param response the response that will be returned to the client, a value
     * of null will be replaced with an internal server error response (status
     * code 500)
     */
    public WebApplicationException(Response response) {
        this(null,response);        
    }
    
    /**
     * Construct a new instance with a blank message and specified HTTP status code
     * @param status the HTTP status code that will be returned to the client
     */
    public WebApplicationException(int status) {
        this(null, status);
    }
    
    /**
     * Construct a new instance with a blank message and specified HTTP status code
     * @param status the HTTP status code that will be returned to the client
     * @throws IllegalArgumentException if status is null
     */
    public WebApplicationException(Response.Status status) {
        this(null, status);
    }
    
    /**
     * Construct a new instance with a blank message and default HTTP status code of 500
     * @param cause the underlying cause of the exception
     */
    public WebApplicationException(Throwable cause) {
        this(cause,Response.Status.INTERNAL_SERVER_ERROR);
    }
    
    /**
     * Construct a new instance using the supplied response
     * @param response the response that will be returned to the client, a value
     * of null will be replaced with an internal server error response (status
     * code 500)
     * @param cause the underlying cause of the exception
     */
    public WebApplicationException(Throwable cause, Response response) {
        super(cause);
        if (response==null)
            this.response = Response.serverError().build();
        else
            this.response = response;        
    }
    
    /**
     * Construct a new instance with a blank message and specified HTTP status code
     * @param status the HTTP status code that will be returned to the client
     * @param cause the underlying cause of the exception
     */
    public WebApplicationException(Throwable cause, int status) {
        this(cause, Response.status(status).build());
    }
    
    /**
     * Construct a new instance with a blank message and specified HTTP status code
     * @param status the HTTP status code that will be returned to the client
     * @param cause the underlying cause of the exception
     * @throws IllegalArgumentException if status is null
     */
    public WebApplicationException(Throwable cause, Response.Status status) {
        this(cause, Response.status(status).build());
    }
    
    /**
     * Get the HTTP response.
     *
     * @return the HTTP response.
     */
    public Response getResponse() {
        return response;
    }
}