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.client.ClientMDS.DATACITE_API_RESPONSE;
22  import java.io.IOException;
23  import org.restlet.data.Status;
24  import org.restlet.representation.Representation;
25  
26  /**
27   * Exception for Client Cross Cite.
28   *
29   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
30   */
31  public class ClientMdsException extends Exception {
32  
33      /**
34       * Operation successful. 201
35       */
36      public static final int SUCCESS_CREATED = 201;
37  
38      /**
39       * Operation successful. 200
40       */
41      public static final int SUCCESS_OK = 200;
42  
43      /**
44       * no DOIs founds. 204
45       */
46      public static final int SUCCESS_NO_CONTENT = 204;
47  
48      /**
49       * invalid XML, wrong prefix or request body must be exactly two lines: "DOI
50       * and URL; wrong domain, wrong prefix". 400
51       */
52      public static final int CLIENT_BAD_REQUEST = 400;
53  
54      /**
55       * no login. 401
56       */
57      public static final int CLIENT_ERROR_UNAUTHORIZED = 401;
58  
59      /**
60       * no login. 1001
61       */
62      public static final int CONNECTOR_ERROR_COMMUNICATION = 1001;
63  
64      /**
65       * login problem, quota exceeded or dataset belongs to another party. 403
66       */
67      public static final int CLIENT_ERROR_FORBIDDEN = 403;
68  
69      /**
70       * DOI does not exist in our database. 404
71       */
72      public static final int CLIENT_ERROR_NOT_FOUND = 404;
73  
74      /**
75       * DOIServer : Cannot know which role must be applied. 409
76       */
77      public static final int CLIENT_ERROR_CONFLICT = 409;
78  
79      /**
80       * the requested dataset was marked inactive (using DELETE method). 410
81       */
82      public static final int CLIENT_ERROR_GONE = 410;
83  
84      /**
85       * metadata must be uploaded first. 412
86       */
87      public static final int CLIENT_ERROR_PRECONDITION_FAILED = 412;
88  
89      /**
90       * SeralVersionUID
91       */
92      private static final long serialVersionUID = -5061913391706889102L;
93  
94      /**
95       * Detail message.
96       */
97      private final String detailMessage;
98  
99      /**
100      * HTTP status.
101      */
102     private final Status status;
103 
104     /**
105      * Constructs a new exception with HTTP status as its detail message.
106      *
107      * @param status HTTP status
108      */
109     public ClientMdsException(final Status status) {
110         super();
111         this.detailMessage = computeDetailMessage(status);
112         this.status = computeStatus(status);
113     }
114 
115     /**
116      * Constructs a new exception with the specified HTTP status and detail
117      * message.
118      *
119      * @param status HTTP message
120      * @param message message
121      */
122     public ClientMdsException(final Status status,
123             final String message) {
124         super(message);
125         this.detailMessage = computeDetailMessage(status);
126         this.status = computeStatus(status);
127     }
128 
129     /**
130      * Constructs a new exception with the specified detail cause.
131      *
132      * @param status HTTP status
133      * @param cause cause
134      */
135     public ClientMdsException(final Status status,
136             final Throwable cause) {
137         super(cause);
138         this.detailMessage = computeDetailMessage(status);
139         this.status = computeStatus(status);
140     }
141 
142     /**
143      * Constructs a new exception with the specified detail HTTP status, message
144      * and cause.
145      *
146      * @param status HTTP status
147      * @param message message
148      * @param cause cause
149      */
150     public ClientMdsException(final Status status,
151             final String message,
152             final Throwable cause) {
153         super(message, cause);
154         this.detailMessage = computeDetailMessage(status);
155         this.status = computeStatus(status);
156     }
157 
158     /**
159      * Constructs a new exception with the specified detail HTTP status,
160      * response and cause.
161      *
162      * @param status HTTP status
163      * @param message message
164      * @param responseEntity Representation of the response
165      * @param cause cause
166      */
167     public ClientMdsException(final Status status,
168             final String message,
169             final Representation responseEntity,
170             final Throwable cause) {
171         super(message, cause);
172         this.status = computeStatus(status);
173         String txt;
174         try {
175             txt = responseEntity.getText();
176         } catch (IOException ex) {
177             txt = computeDetailMessage(this.status);
178         }
179         this.detailMessage = txt;
180     }
181 
182     /**
183      * Computes status
184      *
185      * @param status status
186      * @return status
187      */
188     private Status computeStatus(final Status status) {
189         return (status.getCode() == CONNECTOR_ERROR_COMMUNICATION) ? new Status(
190                 CLIENT_ERROR_UNAUTHORIZED) : status;
191     }
192 
193     /**
194      * Returns the detail message according to the status.
195      *
196      * @param status HTTP status
197      * @return the detail message
198      */
199     private String computeDetailMessage(final Status status) {
200         return DATACITE_API_RESPONSE.getMessageFromStatus(status);
201     }
202 
203     /**
204      * Returns the status.
205      *
206      * @return the status
207      */
208     public Status getStatus() {
209         return this.status;
210     }
211 
212     /**
213      * Returns detail message;
214      *
215      * @return the detail message
216      */
217     public String getDetailMessage() {
218         return this.detailMessage;
219     }
220 }