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  package org.waarp.openr66.protocol.http.rest.handler;
21  
22  import com.fasterxml.jackson.databind.JsonNode;
23  import com.fasterxml.jackson.databind.node.ArrayNode;
24  import com.fasterxml.jackson.databind.node.ObjectNode;
25  import org.waarp.common.database.DbPreparedStatement;
26  import org.waarp.common.database.data.AbstractDbData;
27  import org.waarp.common.database.exception.WaarpDatabaseException;
28  import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
29  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
30  import org.waarp.common.exception.InvalidArgumentException;
31  import org.waarp.common.json.JsonHandler;
32  import org.waarp.common.role.RoleDefault.ROLE;
33  import org.waarp.common.utility.ParametersChecker;
34  import org.waarp.gateway.kernel.exception.HttpForbiddenRequestException;
35  import org.waarp.gateway.kernel.exception.HttpIncorrectRequestException;
36  import org.waarp.gateway.kernel.exception.HttpNotFoundRequestException;
37  import org.waarp.gateway.kernel.rest.DataModelRestMethodHandler;
38  import org.waarp.gateway.kernel.rest.HttpRestHandler;
39  import org.waarp.gateway.kernel.rest.HttpRestHandler.METHOD;
40  import org.waarp.gateway.kernel.rest.RestArgument;
41  import org.waarp.gateway.kernel.rest.RestConfiguration;
42  import org.waarp.openr66.context.R66Session;
43  import org.waarp.openr66.database.data.DbRule;
44  import org.waarp.openr66.database.data.DbRule.Columns;
45  import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler;
46  
47  /**
48   * DbRule Rest handler
49   */
50  public class DbRuleR66RestMethodHandler
51      extends DataModelRestMethodHandler<DbRule> {
52    private static final String RULE_ID_IN_URI_AS_VARCHAR_AS =
53        "RuleId in URI as VARCHAR as ";
54    public static final String BASEURI = "rules";
55  
56    public enum FILTER_ARGS {
57      IDRULE("rule name"), MODETRANS("MODETRANS value");
58  
59      public final String type;
60  
61      FILTER_ARGS(final String type) {
62        this.type = type;
63      }
64    }
65  
66    /**
67     * @param config
68     * @param method
69     */
70    public DbRuleR66RestMethodHandler(final RestConfiguration config,
71                                      final METHOD... method) {
72      super(BASEURI, config, method);
73    }
74  
75    @Override
76    protected final DbRule getItem(final HttpRestHandler handler,
77                                   final RestArgument arguments,
78                                   final RestArgument result, final Object body)
79        throws HttpNotFoundRequestException {
80      try {
81        HttpRestV1Utils.checkSanity(arguments);
82      } catch (final InvalidArgumentException e) {
83        throw new HttpNotFoundRequestException("Issue on values", e);
84      }
85      final ObjectNode arg = arguments.getUriArgs().deepCopy();
86      arg.setAll(arguments.getBody());
87      try {
88        final JsonNode node = RestArgument.getId(arg);
89        final String id;
90        if (node.isMissingNode()) {
91          // shall not be but continue however
92          id = arg.path(DbRule.Columns.IDRULE.name()).asText();
93        } else {
94          id = node.asText();
95        }
96        return new DbRule(id);
97      } catch (final WaarpDatabaseException e) {
98        throw new HttpNotFoundRequestException(
99            "Issue while reading from database " + arg, e);
100     }
101   }
102 
103   @Override
104   protected final DbRule createItem(final HttpRestHandler handler,
105                                     final RestArgument arguments,
106                                     final RestArgument result,
107                                     final Object body)
108       throws HttpIncorrectRequestException {
109     try {
110       HttpRestV1Utils.checkSanity(arguments);
111     } catch (final InvalidArgumentException e) {
112       throw new HttpIncorrectRequestException("Issue on values", e);
113     }
114     final ObjectNode arg = arguments.getUriArgs().deepCopy();
115     arg.setAll(arguments.getBody());
116     try {
117       return new DbRule(arg);
118     } catch (final WaarpDatabaseException e) {
119       throw new HttpIncorrectRequestException(
120           "Issue while inserting into database", e);
121     }
122   }
123 
124   @Override
125   protected final DbPreparedStatement getPreparedStatement(
126       final HttpRestHandler handler, final RestArgument arguments,
127       final RestArgument result, final Object body)
128       throws HttpIncorrectRequestException {
129     try {
130       HttpRestV1Utils.checkSanity(arguments);
131     } catch (final InvalidArgumentException e) {
132       throw new HttpIncorrectRequestException("Issue on values", e);
133     }
134     final ObjectNode arg = arguments.getUriArgs().deepCopy();
135     arg.setAll(arguments.getBody());
136     String rule = arg.path(FILTER_ARGS.IDRULE.name()).asText();
137     if (ParametersChecker.isEmpty(rule)) {
138       rule = null;
139     }
140     final int mode = arg.path(FILTER_ARGS.MODETRANS.name()).asInt();
141     try {
142       return DbRule.getFilterPrepareStament(handler.getDbSession(), rule, mode);
143     } catch (final WaarpDatabaseNoConnectionException e) {
144       throw new HttpIncorrectRequestException(
145           "Issue while reading from database", e);
146     } catch (final WaarpDatabaseSqlException e) {
147       throw new HttpIncorrectRequestException(
148           "Issue while reading from database", e);
149     }
150   }
151 
152   @Override
153   protected final DbRule getItemPreparedStatement(
154       final DbPreparedStatement statement)
155       throws HttpIncorrectRequestException, HttpNotFoundRequestException {
156     try {
157       return DbRule.getFromStatement(statement);
158     } catch (final WaarpDatabaseNoConnectionException e) {
159       throw new HttpIncorrectRequestException(
160           "Issue while selecting from database", e);
161     } catch (final WaarpDatabaseSqlException e) {
162       throw new HttpNotFoundRequestException(
163           "Issue while selecting from database", e);
164     }
165   }
166 
167   @Override
168   protected final ArrayNode getDetailedAllow() {
169     final ArrayNode node = JsonHandler.createArrayNode();
170 
171     final ObjectNode node1 = JsonHandler.createObjectNode();
172     node1.put(AbstractDbData.JSON_MODEL, DbRule.class.getSimpleName());
173     for (final DbRule.Columns column : DbRule.Columns.values()) {
174       node1.put(column.name(), DbRule.dbTypes[column.ordinal()]);
175     }
176 
177     ObjectNode node2;
178     ObjectNode node3;
179     if (methods.contains(METHOD.GET)) {
180       node2 = RestArgument.fillDetailedAllow(METHOD.GET, path + "/id",
181                                              COMMAND_TYPE.GET.name(),
182                                              JsonHandler.createObjectNode().put(
183                                                  DbRule.Columns.IDRULE.name(),
184                                                  RULE_ID_IN_URI_AS_VARCHAR_AS +
185                                                  path + "/id"), node1);
186       node.add(node2);
187 
188       node3 = JsonHandler.createObjectNode();
189       for (final FILTER_ARGS arg : FILTER_ARGS.values()) {
190         node3.put(arg.name(), arg.type);
191       }
192       node2 = RestArgument.fillDetailedAllow(METHOD.GET, path,
193                                              COMMAND_TYPE.MULTIGET.name(),
194                                              node3,
195                                              JsonHandler.createArrayNode()
196                                                         .add(node1));
197       node.add(node2);
198     }
199     if (methods.contains(METHOD.PUT)) {
200       node3 = JsonHandler.createObjectNode();
201       node3.put(DbRule.Columns.IDRULE.name(),
202                 RULE_ID_IN_URI_AS_VARCHAR_AS + path + "/id");
203       for (final DbRule.Columns column : DbRule.Columns.values()) {
204         if (column.name().equalsIgnoreCase(DbRule.Columns.IDRULE.name())) {
205           continue;
206         }
207         node3.put(column.name(), DbRule.dbTypes[column.ordinal()]);
208       }
209       node2 = RestArgument.fillDetailedAllow(METHOD.PUT, path + "/id",
210                                              COMMAND_TYPE.UPDATE.name(), node3,
211                                              node1);
212       node.add(node2);
213     }
214     if (methods.contains(METHOD.DELETE)) {
215       node3 = JsonHandler.createObjectNode();
216       node3.put(DbRule.Columns.IDRULE.name(),
217                 RULE_ID_IN_URI_AS_VARCHAR_AS + path + "/id");
218       node2 = RestArgument.fillDetailedAllow(METHOD.DELETE, path + "/id",
219                                              COMMAND_TYPE.DELETE.name(), node3,
220                                              node1);
221       node.add(node2);
222     }
223     if (methods.contains(METHOD.POST)) {
224       node3 = JsonHandler.createObjectNode();
225       for (final DbRule.Columns column : DbRule.Columns.values()) {
226         node3.put(column.name(), DbRule.dbTypes[column.ordinal()]);
227       }
228       node2 = RestArgument.fillDetailedAllow(METHOD.POST, path,
229                                              COMMAND_TYPE.CREATE.name(), node3,
230                                              node1);
231       node.add(node2);
232     }
233     node2 = RestArgument.fillDetailedAllow(METHOD.OPTIONS, path,
234                                            COMMAND_TYPE.OPTIONS.name(), null,
235                                            null);
236     node.add(node2);
237 
238     return node;
239   }
240 
241   @Override
242   public final String getPrimaryPropertyName() {
243     return Columns.IDRULE.name();
244   }
245 
246   @Override
247   protected final void checkAuthorization(final HttpRestHandler handler,
248                                           final RestArgument arguments,
249                                           final RestArgument result,
250                                           final METHOD method)
251       throws HttpForbiddenRequestException {
252     try {
253       HttpRestV1Utils.checkSanity(arguments);
254     } catch (final InvalidArgumentException e) {
255       throw new HttpForbiddenRequestException("Issue on values", e);
256     }
257     final HttpRestR66Handler r66handler = (HttpRestR66Handler) handler;
258     final R66Session session = r66handler.getServerHandler().getSession();
259     if (!session.getAuth().isValidRole(ROLE.RULE)) {
260       throw new HttpForbiddenRequestException(
261           "Partner must have ConfigAdmin role");
262     }
263   }
264 
265 }