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.converters;
22  
23  import com.fasterxml.jackson.databind.JsonNode;
24  import com.fasterxml.jackson.databind.node.ObjectNode;
25  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
26  import org.waarp.common.json.JsonHandler;
27  import org.waarp.common.logging.SysErrLogger;
28  import org.waarp.openr66.pojo.Limit;
29  import org.waarp.openr66.protocol.http.restv2.errors.RestError;
30  import org.waarp.openr66.protocol.http.restv2.errors.RestErrorException;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  import java.util.Map;
35  
36  import static org.waarp.openr66.protocol.http.restv2.RestConstants.LimitsFields.*;
37  import static org.waarp.openr66.protocol.http.restv2.RestConstants.*;
38  import static org.waarp.openr66.protocol.http.restv2.errors.RestErrors.*;
39  
40  /**
41   * A collection of utility methods to convert {@link Limit} objects to their
42   * corresponding {@link ObjectNode}
43   * and vice-versa.
44   */
45  public final class LimitsConverter {
46  
47    /**
48     * Makes the default constructor of this utility class inaccessible.
49     */
50    private LimitsConverter() throws InstantiationException {
51      throw new InstantiationException(
52          getClass().getName() + " cannot be instantiated.");
53    }
54  
55    // ########################### PUBLIC METHODS ###############################
56  
57    /**
58     * Converts the given {@link Limit} object into an {@link ObjectNode}.
59     *
60     * @param limits the limits object to convert
61     *
62     * @return the converted ObjectNode
63     */
64    public static ObjectNode limitToNode(final Limit limits) {
65      final ObjectNode node = JsonHandler.createObjectNode();
66      node.put(READ_GLOBAL_LIMIT, limits.getReadGlobalLimit());
67      node.put(WRITE_GLOBAL_LIMIT, limits.getWriteGlobalLimit());
68      node.put(READ_SESSION_LIMIT, limits.getReadSessionLimit());
69      node.put(WRITE_SESSION_LIMIT, limits.getWriteSessionLimit());
70      node.put(DELAY_LIMIT, limits.getDelayLimit());
71  
72      return node;
73    }
74  
75    /**
76     * Converts the given {@link ObjectNode} into a {@link Limit} object.
77     *
78     * @param object the ObjectNode to convert
79     *
80     * @return the corresponding Limit object
81     *
82     * @throws RestErrorException if the given ObjectNode does not
83     *     represent a Limit object
84     */
85    public static Limit nodeToNewLimit(final ObjectNode object) {
86      Limit emptyLimits = null;
87      try {
88        emptyLimits = new Limit(serverName(), 0, 0, 0, 0, 0);
89      } catch (final WaarpDatabaseSqlException e) {
90        SysErrLogger.FAKE_LOGGER.syserr(e);
91      }
92      return nodeToUpdatedLimit(object, emptyLimits);
93    }
94  
95    /**
96     * Returns the given {@link Limit} object updated with the values defined in
97     * the {@link ObjectNode} parameter.
98     * All fields missing in the JSON object will stay unchanged in the updated
99     * limit object.
100    *
101    * @param object the ObjectNode to convert
102    * @param oldLimits the Limit object to update
103    *
104    * @return the updated Limit object
105    *
106    * @throws RestErrorException if the given ObjectNode does not
107    *     represent a Limit object
108    */
109   public static Limit nodeToUpdatedLimit(final ObjectNode object,
110                                          final Limit oldLimits) {
111     final List<RestError> errors = new ArrayList<RestError>();
112 
113     while (object.fields().hasNext()) {
114       final Map.Entry<String, JsonNode> field = object.fields().next();
115       final String name = field.getKey();
116       final JsonNode value = field.getValue();
117 
118       if (name.equalsIgnoreCase(READ_GLOBAL_LIMIT)) {
119         if (value.canConvertToLong() && value.asLong() >= 0) {
120           oldLimits.setReadGlobalLimit(value.asLong());
121         } else {
122           errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
123         }
124       } else if (name.equalsIgnoreCase(WRITE_GLOBAL_LIMIT)) {
125         if (value.canConvertToLong() && value.asLong() >= 0) {
126           oldLimits.setWriteGlobalLimit(value.asLong());
127         } else {
128           errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
129         }
130       } else if (name.equalsIgnoreCase(READ_SESSION_LIMIT)) {
131         if (value.canConvertToLong() && value.asLong() >= 0) {
132           oldLimits.setReadSessionLimit(value.asLong());
133         } else {
134           errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
135         }
136       } else if (name.equalsIgnoreCase(WRITE_SESSION_LIMIT)) {
137         if (value.canConvertToLong() && value.asLong() >= 0) {
138           oldLimits.setWriteSessionLimit(value.asLong());
139         } else {
140           errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
141         }
142       } else if (name.equalsIgnoreCase(DELAY_LIMIT)) {
143         if (value.canConvertToLong() && value.asLong() >= 0) {
144           oldLimits.setDelayLimit(value.asLong());
145         } else {
146           errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
147         }
148       } else {
149         errors.add(UNKNOWN_FIELD(name));
150       }
151     }
152 
153     if (errors.isEmpty()) {
154       return oldLimits;
155     } else {
156       throw new RestErrorException(errors);
157     }
158   }
159 }