View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *     http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.gbif.ws;
15  
16  import org.springframework.http.HttpStatus;
17  import org.springframework.http.MediaType;
18  
19  /**
20   * Analogue of JAX-RS' WebApplicationException.
21   */
22  public class WebApplicationException extends RuntimeException {
23  
24    private static final long serialVersionUID = 11660101L;
25  
26    /**
27     * HTTP status code of the response. Required.
28     */
29    private final Integer status;
30  
31    /**
32     * Content media type of the response. Optional.
33     */
34    private final MediaType contentType;
35  
36    /**
37     * Construct a new instance with a message, and an HTTP status code.
38     */
39    public WebApplicationException(String message, Integer status) {
40      super(message);
41      this.status = status;
42      this.contentType = MediaType.TEXT_PLAIN;
43    }
44  
45    /**
46     * Construct a new instance with a message, and an HTTP status code.
47     */
48    public WebApplicationException(String message, HttpStatus status) {
49      super(message);
50      this.status = status.value();
51      this.contentType = MediaType.TEXT_PLAIN;
52    }
53  
54    /**
55     * Construct a new instance with a message, an HTTP status code, and content media type.
56     */
57    public WebApplicationException(String message, HttpStatus status, MediaType contentType) {
58      super(message);
59      this.status = status.value();
60      this.contentType = contentType;
61    }
62  
63    public Integer getStatus() {
64      return status;
65    }
66  
67    public MediaType getContentType() {
68      return contentType;
69    }
70  }