View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.protocol.http.restv2.errors;
22  
23  /**
24   * Factory class used to create instances of {@link RestError} representing
25   * input errors in an HTTP request
26   * made to the REST API.
27   */
28  public final class RestErrors {
29  
30    private static final String[] STRING_0_LENGTH = new String[0];
31  
32    /**
33     * Prevent the default constructor from being called.
34     */
35    private RestErrors() throws InstantiationException {
36      throw new InstantiationException(
37          getClass().getName() + " cannot be instantiated.");
38    }
39  
40    /**
41     * Creates an error saying that the request is missing a body when one was
42     * required.
43     *
44     * @return the corresponding new RestError object
45     */
46    public static RestError MISSING_BODY() {
47      return new RestError("BadRequest.MissingBody", STRING_0_LENGTH, 1);
48    }
49  
50    /**
51     * Creates an error saying that the request content is not a valid JSON
52     * object.
53     *
54     * @return the corresponding new RestError object
55     */
56    public static RestError MALFORMED_JSON(final int line, final int column,
57                                           final String cause) {
58      final String[] args =
59          { Integer.toString(line), Integer.toString(column), cause };
60      return new RestError("BadRequest.MalformedJSON", args, 2);
61    }
62  
63    /**
64     * Creates an error saying that JSON object given has a duplicate field.
65     *
66     * @param field the missing parameter's name
67     *
68     * @return the corresponding new RestError object
69     */
70    public static RestError DUPLICATE_KEY(final String field) {
71      final String[] args = { field };
72      return new RestError("BadRequest.DuplicateKey", args, 3);
73    }
74  
75    /**
76     * Creates an error saying that one of the request's parameters has an
77     * illegal value. This includes numbers
78     * out of their expected range, or invalid enum values.
79     *
80     * @param parameter the incorrect parameter's name
81     *
82     * @return the corresponding new RestError object
83     */
84    public static RestError ILLEGAL_PARAMETER_VALUE(final String parameter,
85                                                    final String value) {
86      final String[] args = { value, parameter };
87      return new RestError("BadRequest.IllegalParameterValue", args, 4);
88    }
89  
90    /**
91     * Creates an error saying that the JSON object sent had an extra unknown
92     * field named with the given name.
93     *
94     * @param field the extra field encountered
95     *
96     * @return the corresponding new RestError object
97     */
98    public static RestError UNKNOWN_FIELD(final String field) {
99      final String[] args = { field };
100     return new RestError("BadRequest.UnknownField", args, 5);
101   }
102 
103   /**
104    * Creates an error saying that one of the JSON object's field is missing
105    * when it was required. This error can
106    * also be sent if the field is present but is missing its' value.
107    *
108    * @param field the field whose value is missing
109    *
110    * @return the corresponding new RestError object
111    */
112   public static RestError MISSING_FIELD(final String field) {
113     final String[] args = { field };
114     return new RestError("BadRequest.MissingFieldValue", args, 6);
115   }
116 
117   /**
118    * Creates an error saying that the given field has been assigned an illegal
119    * value. This includes values of
120    * the wrong type, or values outside the allowed bounds of the field.
121    *
122    * @param field the name of the field whose value is incorrect
123    * @param value the textual representation of the incorrect value
124    *
125    * @return the corresponding new RestError object
126    */
127   public static RestError ILLEGAL_FIELD_VALUE(final String field,
128                                               final String value) {
129     final String[] args = { value, field };
130     return new RestError("BadRequest.IllegalFieldValue", args, 7);
131   }
132 
133   /**
134    * Creates an error saying that the database already contains an entry with
135    * the same ID as the one in the
136    * entry the user tried to create. Since the database cannot contain entries
137    * with duplicate IDs, the requested
138    * entry cannot be created.
139    *
140    * @param id the duplicate ID in the requested collection
141    *
142    * @return the corresponding new RestError object
143    */
144   public static RestError ALREADY_EXISTING(final String id) {
145     final String[] args = { id };
146     return new RestError("BadRequest.AlreadyExisting", args, 8);
147   }
148 
149   /**
150    * Creates an error saying that the file requested for import at the given
151    * location does not exist on the
152    * server.
153    *
154    * @param path the incorrect path
155    *
156    * @return the corresponding new RestError object
157    */
158   public static RestError FILE_NOT_FOUND(final String path) {
159     final String[] args = { path };
160     return new RestError("BadRequest.FileNotFound", args, 9);
161   }
162 
163   /**
164    * Creates an error saying that the transfer rule entered alongside the
165    * newly created transfer does not exist.
166    *
167    * @param rule the unknown rule's name
168    *
169    * @return the corresponding new RestError object
170    */
171   public static RestError UNKNOWN_RULE(final String rule) {
172     final String[] args = { rule };
173     return new RestError("BadRequest.UnknownRule", args, 10);
174   }
175 
176   /**
177    * Creates an error saying that the requested host name entered alongside
178    * the newly created transfer does not
179    * exist.
180    *
181    * @param host the unknown host name
182    *
183    * @return the corresponding new RestError object
184    */
185   public static RestError UNKNOWN_HOST(final String host) {
186     final String[] args = { host };
187     return new RestError("BadRequest.UnknownHost", args, 11);
188   }
189 
190   /**
191    * Creates an error saying that the given host is not allowed to use the
192    * given rule for its transfers.
193    *
194    * @param host the host processing the transfer
195    * @param rule the rule which the host is not allowed to use
196    *
197    * @return the corresponding new RestError object
198    */
199   public static RestError RULE_NOT_ALLOWED(final String host,
200                                            final String rule) {
201     final String[] args = { host, rule };
202     return new RestError("BadRequest.RuleNotAllowed", args, 12);
203   }
204 
205   /**
206    * Creates an error saying that the given field is a primary key, and thus
207    * cannot be changed when updating an
208    * entry.
209    *
210    * @param field the name of the field which was illegally modified
211    *
212    * @return the corresponding new RestError object
213    */
214   public static RestError FIELD_NOT_ALLOWED(final String field) {
215     final String[] args = { field };
216     return new RestError("BadRequest.UnauthorizedField", args, 13);
217   }
218 }