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