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.node.ArrayNode;
23  import com.fasterxml.jackson.databind.node.ObjectNode;
24  import io.netty.handler.codec.http.HttpResponseStatus;
25  import org.waarp.common.database.data.AbstractDbData;
26  import org.waarp.common.exception.InvalidArgumentException;
27  import org.waarp.common.json.JsonHandler;
28  import org.waarp.common.logging.WaarpLogger;
29  import org.waarp.common.logging.WaarpLoggerFactory;
30  import org.waarp.gateway.kernel.exception.HttpIncorrectRequestException;
31  import org.waarp.gateway.kernel.exception.HttpInvalidAuthenticationException;
32  import org.waarp.gateway.kernel.rest.DataModelRestMethodHandler.COMMAND_TYPE;
33  import org.waarp.gateway.kernel.rest.HttpRestHandler;
34  import org.waarp.gateway.kernel.rest.HttpRestHandler.METHOD;
35  import org.waarp.gateway.kernel.rest.RestArgument;
36  import org.waarp.gateway.kernel.rest.RestConfiguration;
37  import org.waarp.openr66.database.data.DbTaskRunner;
38  import org.waarp.openr66.protocol.exception.OpenR66ProtocolNoDataException;
39  import org.waarp.openr66.protocol.exception.OpenR66ProtocolNotAuthenticatedException;
40  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
41  import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler;
42  import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler.RESTHANDLERS;
43  import org.waarp.openr66.protocol.localhandler.ServerActions;
44  import org.waarp.openr66.protocol.localhandler.packet.InformationPacket;
45  import org.waarp.openr66.protocol.localhandler.packet.ValidPacket;
46  import org.waarp.openr66.protocol.localhandler.packet.json.InformationJsonPacket;
47  import org.waarp.openr66.protocol.localhandler.packet.json.JsonPacket;
48  
49  /**
50   * Info Http REST interface: http://host/info?... + InformationJsonPacket as GET
51   */
52  public class HttpRestInformationR66Handler extends HttpRestAbstractR66Handler {
53  
54    public static final String BASEURI = "info";
55    /**
56     * Internal Logger
57     */
58    private static final WaarpLogger logger =
59        WaarpLoggerFactory.getLogger(HttpRestInformationR66Handler.class);
60  
61    public HttpRestInformationR66Handler(final RestConfiguration config,
62                                         final METHOD... methods) {
63      super(BASEURI, config, METHOD.OPTIONS);
64      setIntersectionMethods(methods, METHOD.GET);
65    }
66  
67    @Override
68    public final void endParsingRequest(final HttpRestHandler handler,
69                                        final RestArgument arguments,
70                                        final RestArgument result,
71                                        final Object body)
72        throws HttpIncorrectRequestException, HttpInvalidAuthenticationException {
73      try {
74        HttpRestV1Utils.checkSanity(arguments);
75      } catch (final InvalidArgumentException e) {
76        throw new HttpIncorrectRequestException("Issue on values", e);
77      }
78      logger.debug("debug: {} ### {}", arguments, result);
79      if (body != null) {
80        logger.debug("Obj: {}", body);
81      }
82      handler.setWillClose(false);
83      final ServerActions serverHandler =
84          ((HttpRestR66Handler) handler).getServerHandler();
85      // now action according to body
86      final JsonPacket json = (JsonPacket) body;
87      if (json == null) {
88        result.setDetail("not enough information");
89        setError(handler, result, HttpResponseStatus.BAD_REQUEST);
90        return;
91      }
92      result.getAnswer()
93            .put(AbstractDbData.JSON_MODEL, RESTHANDLERS.Information.name());
94      try {
95        if (json instanceof InformationJsonPacket) {//
96          final InformationJsonPacket node = (InformationJsonPacket) json;
97          if (node.isIdRequest()) {
98            result.setCommand(ACTIONS_TYPE.GetTransferInformation.name());
99          } else {
100           result.setCommand(ACTIONS_TYPE.GetInformation.name());
101         }
102         final ValidPacket validPacket;
103         if (node.isIdRequest()) {
104           validPacket =
105               serverHandler.informationRequest(node.getId(), node.isTo(),
106                                                node.getRulename(), true);
107         } else {
108           validPacket = serverHandler.informationFile(node.getRequest(),
109                                                       node.getRulename(),
110                                                       node.getFilename(), true);
111         }
112         if (validPacket != null) {
113           // will not use default setOk
114           if (node.isIdRequest()) {
115             try {
116               final ObjectNode resp =
117                   JsonHandler.getFromString(validPacket.getSheader());
118               result.setResult(HttpResponseStatus.OK);
119               result.getResults().add(resp);
120             } catch (final Exception e) {
121               logger.warn(validPacket.getSheader() + " : {}", e.getMessage());
122               result.setResult(HttpResponseStatus.OK);
123               result.getResults().add(validPacket.getSheader());
124             }
125           } else {
126             result.setResult(HttpResponseStatus.OK);
127             result.getResults().add(validPacket.getSheader());
128           }
129           handler.setStatus(HttpResponseStatus.OK);
130         } else {
131           result.setDetail("Error during information request");
132           setError(handler, result, HttpResponseStatus.NOT_ACCEPTABLE);
133         }
134       } else {
135         logger.info("Validation is ignored: {}", json);
136         result.setDetail("Unknown command");
137         setError(handler, result, json, HttpResponseStatus.PRECONDITION_FAILED);
138       }
139     } catch (final OpenR66ProtocolNotAuthenticatedException e) {
140       throw new HttpInvalidAuthenticationException(e);
141     } catch (final OpenR66ProtocolNoDataException e) {
142       throw new HttpIncorrectRequestException(e);
143     }
144   }
145 
146   @Override
147   protected final ArrayNode getDetailedAllow() {
148     final ArrayNode node = JsonHandler.createArrayNode();
149 
150     if (methods.contains(METHOD.GET)) {
151       InformationJsonPacket node3 = new InformationJsonPacket(
152           (byte) InformationPacket.ASKENUM.ASKEXIST.ordinal(),
153           "The rule name associated with the remote repository",
154           "The filename to look for if any");
155       node3.setComment("Information request (GET)");
156       ObjectNode node2;
157       ArrayNode node1 = JsonHandler.createArrayNode().add("path");
158       try {
159         node2 = RestArgument.fillDetailedAllow(METHOD.GET, path,
160                                                ACTIONS_TYPE.GetInformation.name(),
161                                                node3.createObjectNode(), node1);
162         node.add(node2);
163       } catch (final OpenR66ProtocolPacketException ignored) {
164         // ignore
165       }
166 
167       node3 = new InformationJsonPacket(Long.MIN_VALUE, false, "remoteHost");
168       node3.setComment("Information on Transfer request (GET)");
169       node1 = JsonHandler.createArrayNode();
170       final ObjectNode node1b = JsonHandler.createObjectNode();
171       node1b.put(AbstractDbData.JSON_MODEL, DbTaskRunner.class.getSimpleName());
172       for (final DbTaskRunner.Columns column : DbTaskRunner.Columns.values()) {
173         node1b.put(column.name(), DbTaskRunner.dbTypes[column.ordinal()]);
174       }
175       node1.add(node1b);
176       try {
177         node2 = RestArgument.fillDetailedAllow(METHOD.GET, path,
178                                                ACTIONS_TYPE.GetInformation.name(),
179                                                node3.createObjectNode(), node1);
180         node.add(node2);
181       } catch (final OpenR66ProtocolPacketException ignored) {
182         // ignore
183       }
184     }
185 
186     final ObjectNode node2 =
187         RestArgument.fillDetailedAllow(METHOD.OPTIONS, path,
188                                        COMMAND_TYPE.OPTIONS.name(), null, null);
189     node.add(node2);
190 
191     return node;
192   }
193 }