View Javadoc

1   /*
2    * Copyright (c) 2013 Maxim Yunusov
3    *    Licensed under the Apache License, Version 2.0 (the "License");
4    *    you may not use this file except in compliance with the License.
5    *    You may obtain a copy of the License at
6    *
7    *        http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *    Unless required by applicable law or agreed to in writing, software
10   *    distributed under the License is distributed on an "AS IS" BASIS,
11   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *    See the License for the specific language governing permissions and
13   *    limitations under the License.
14   */
15  
16  package org.maxur.perfmodel.backend.rest;
17  
18  import org.maxur.perfmodel.backend.domain.Incident;
19  
20  import javax.ws.rs.WebApplicationException;
21  import javax.ws.rs.core.GenericEntity;
22  import javax.ws.rs.core.Response;
23  import java.util.List;
24  
25  import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
26  import static javax.ws.rs.core.Response.Status.*;
27  import static org.maxur.perfmodel.backend.domain.Incident.incidents;
28  
29  /**
30   * @author Maxim Yunusov
31   * @version 1.0 06.11.13
32   */
33  public final class WebException extends WebApplicationException {
34  
35      private static final long serialVersionUID = -2826609919565709334L;
36  
37      private WebException(final Response.Status status, final String... messages) {
38          super(Response
39                  .status(status)
40                  .type(APPLICATION_JSON)
41                  .entity(new GenericEntity<List<Incident>>(incidents(messages)) {
42                  })
43                  .build());
44      }
45  
46      public static WebException notFoundException(final String... errors) {
47          return new WebException(NOT_FOUND, errors);
48      }
49  
50      public static WebException badRequestException(final String... errors) {
51          return new WebException(BAD_REQUEST, errors);
52      }
53  
54      public static WebException conflictException(final String... errors) {
55          return new WebException(CONFLICT, errors);
56      }
57  
58  
59  }