View Javadoc

1   /*
2    * Copyright (C) 2017-2019 Centre National d'Etudes Spatiales (CNES).
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 3.0 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17   * MA 02110-1301  USA
18   */
19  package fr.cnes.doi.exception;
20  
21  import fr.cnes.doi.application.AbstractApplication;
22  import fr.cnes.doi.application.DoiMdsApplication.API_MDS;
23  import fr.cnes.doi.client.ClientMDS.DATACITE_API_RESPONSE;
24  import org.restlet.Application;
25  import org.restlet.data.Status;
26  import org.restlet.resource.ResourceException;
27  
28  /**
29   * General Server Exception
30   *
31   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
32   */
33  public class DoiServerException extends ResourceException {
34  
35      /**
36       * Creates an exception based on the app and the error coming from
37       * Doi-server.
38       *
39       * @param app MDS application
40       * @param error MDS error
41       */
42      public DoiServerException(final Application app, final API_MDS error) {
43          super(error.getStatus().getCode(), error.getShortMessage());
44      }
45  
46      /**
47       * Creates an exception based on the app and the error coming from
48       * Doi-server.
49       *
50       * @param app MDS application
51       * @param error MDS error
52       * @param ex Exception
53       */
54      public DoiServerException(final Application app, final API_MDS error, final Exception ex) {
55          super(error.getStatus(), error.getShortMessage(), ex);
56      }
57  
58      /**
59       * Creates an exception based on the app and the error coming from
60       * Doi-server.
61       *
62       * @param app MDS application
63       * @param error MDS error
64       * @param description Description
65       */
66      public DoiServerException(final Application app, final API_MDS error, final String description) {
67          super(error.getStatus().getCode(), error.getShortMessage(), description);
68      }
69  
70      /**
71       * Creates an exception based on the app and the error coming from
72       * Doi-server.
73       *
74       * @param app MDS application
75       * @param error MDS error
76       * @param description Description
77       * @param cause Exception
78       */
79      public DoiServerException(final Application app,
80              final API_MDS error,
81              final String description,
82              final Throwable cause) {
83          super(error.getStatus().getCode(), cause, error.getShortMessage(), description);
84      }
85  
86      /**
87       * Creates an exception based on the app and the error coming from
88       * Doi-server.
89       *
90       * @param app MDS application
91       * @param error Datacite error
92       */
93      public DoiServerException(final Application app, final DATACITE_API_RESPONSE error) {
94          super(error.getStatus().getCode(), error.getShortMessage());
95          if (DATACITE_API_RESPONSE.INTERNAL_SERVER_ERROR.getStatus().getCode() == error.getStatus().
96                  getCode()) {
97              sendAlert(app);
98          }
99      }
100 
101     /**
102      * Creates an exception based on the app and the error coming from
103      * Doi-server.
104      *
105      * @param app MDS application
106      * @param error Datacite error
107      * @param ex Exception
108      */
109     public DoiServerException(final Application app,
110             final DATACITE_API_RESPONSE error,
111             final Exception ex) {
112         super(error.getStatus(), error.getShortMessage(), ex);
113         if (DATACITE_API_RESPONSE.INTERNAL_SERVER_ERROR.getStatus().getCode() == error.getStatus().
114                 getCode()) {
115             sendAlert(app);
116         }
117     }
118 
119     /**
120      * Creates an exception based on the app and the error coming from
121      * Doi-server.
122      *
123      * @param app MDS application
124      * @param error Datacite error
125      * @param description description
126      */
127     public DoiServerException(final Application app,
128             final DATACITE_API_RESPONSE error,
129             final String description) {
130         super(error.getStatus().getCode(), error.getShortMessage(), description);
131         if (DATACITE_API_RESPONSE.INTERNAL_SERVER_ERROR.getStatus().getCode() == error.getStatus().
132                 getCode()) {
133             sendAlert(app);
134         }
135     }
136 
137     /**
138      * Creates an exception based on the app and the error coming from
139      * Doi-server.
140      *
141      * @param app MDS application
142      * @param error Datacite error
143      * @param description description
144      * @param cause Exception
145      */
146     public DoiServerException(final Application app,
147             final DATACITE_API_RESPONSE error,
148             final String description,
149             final Throwable cause) {
150         super(error.getStatus().getCode(), cause, error.getShortMessage(), description);
151         if (DATACITE_API_RESPONSE.INTERNAL_SERVER_ERROR.getStatus().getCode() == error.getStatus().
152                 getCode()) {
153             sendAlert(app);
154         }
155     }
156 
157     /**
158      * Creates an exception based on the app and the error coming from
159      * Doi-server.
160      *
161      * @param app MDS application
162      * @param status Status
163      * @param description description
164      * @param cause Exception
165      */
166     public DoiServerException(final Application app,
167             final Status status,
168             final String description,
169             final Throwable cause) {
170         super(status, description, cause);
171         sendAlert(app);
172     }
173 
174     /**
175      * Creates an exception based on the app and the error coming from
176      * Doi-server.
177      *
178      * @param app MDS application
179      * @param status Status
180      * @param description description
181      */
182     public DoiServerException(final Application app,
183             final Status status,
184             final String description) {
185         super(status, description);
186     }
187 
188     /**
189      * Sends the alert to the administrator.
190      *
191      * @param app Application
192      */
193     private void sendAlert(final Application app) {
194         ((AbstractApplication) app).sendAlertWhenDataCiteFailed(this);
195     }
196 
197 }