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.protocol.exception.OpenR66ProtocolBusinessException;
38  import org.waarp.openr66.protocol.exception.OpenR66ProtocolNotAuthenticatedException;
39  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
40  import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler;
41  import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler.RESTHANDLERS;
42  import org.waarp.openr66.protocol.localhandler.ServerActions;
43  import org.waarp.openr66.protocol.localhandler.packet.json.JsonPacket;
44  import org.waarp.openr66.protocol.localhandler.packet.json.LogJsonPacket;
45  import org.waarp.openr66.protocol.localhandler.packet.json.LogResponseJsonPacket;
46  
47  import java.sql.Timestamp;
48  import java.util.Date;
49  
50  /**
51   * Log Http REST interface: http://host/log?... + LogJsonPacket as GET
52   */
53  public class HttpRestLogR66Handler extends HttpRestAbstractR66Handler {
54  
55    public static final String BASEURI = "log";
56    /**
57     * Internal Logger
58     */
59    private static final WaarpLogger logger =
60        WaarpLoggerFactory.getLogger(HttpRestLogR66Handler.class);
61  
62    public HttpRestLogR66Handler(final RestConfiguration config,
63                                 final METHOD... methods) {
64      super(BASEURI, config, METHOD.OPTIONS);
65      setIntersectionMethods(methods, METHOD.GET);
66    }
67  
68    @Override
69    public final void endParsingRequest(final HttpRestHandler handler,
70                                        final RestArgument arguments,
71                                        final RestArgument result,
72                                        final Object body)
73        throws HttpIncorrectRequestException, HttpInvalidAuthenticationException {
74      try {
75        HttpRestV1Utils.checkSanity(arguments);
76      } catch (final InvalidArgumentException e) {
77        throw new HttpIncorrectRequestException("Issue on values", e);
78      }
79      logger.debug("debug: {} ### {}", arguments, result);
80      if (body != null) {
81        logger.debug("Obj: {}", body);
82      }
83      handler.setWillClose(false);
84      final ServerActions serverHandler =
85          ((HttpRestR66Handler) handler).getServerHandler();
86      // now action according to body
87      final JsonPacket json = (JsonPacket) body;
88      if (json == null) {
89        result.setDetail("not enough information");
90        setError(handler, result, HttpResponseStatus.BAD_REQUEST);
91        return;
92      }
93      result.getAnswer().put(AbstractDbData.JSON_MODEL, RESTHANDLERS.Log.name());
94      try {
95        if (json instanceof LogJsonPacket) {//
96          result.setCommand(ACTIONS_TYPE.GetLog.name());
97          final LogJsonPacket node = (LogJsonPacket) json;
98          final boolean purge = node.isPurge();
99          final boolean clean = node.isClean();
100         final Timestamp start = node.getStart() == null? null :
101             new Timestamp(node.getStart().getTime());
102         final Timestamp stop = node.getStop() == null? null :
103             new Timestamp(node.getStop().getTime());
104         final String startid = node.getStartid();
105         final String stopid = node.getStopid();
106         final String rule = node.getRule();
107         final String request = node.getRequest();
108         final boolean pending = node.isStatuspending();
109         final boolean transfer = node.isStatustransfer();
110         final boolean done = node.isStatusdone();
111         final boolean error = node.isStatuserror();
112         final String[] sresult =
113             serverHandler.logPurge(purge, clean, start, stop, startid, stopid,
114                                    rule, request, pending, transfer, done,
115                                    error, purge);
116         final LogResponseJsonPacket newjson = new LogResponseJsonPacket();
117         newjson.fromJson(node);
118         // Now answer
119         newjson.setCommand(node.getRequestUserPacket());
120         newjson.setFilename(sresult[0]);
121         newjson.setExported(Long.parseLong(sresult[1]));
122         newjson.setPurged(Long.parseLong(sresult[2]));
123         setOk(handler, result, newjson, HttpResponseStatus.OK);
124       } else {
125         logger.info("Validation is ignored: {}", json);
126         result.setDetail("Unknown command");
127         setError(handler, result, json, HttpResponseStatus.PRECONDITION_FAILED);
128       }
129     } catch (final OpenR66ProtocolNotAuthenticatedException e) {
130       throw new HttpInvalidAuthenticationException(e);
131     } catch (final OpenR66ProtocolBusinessException e) {
132       throw new HttpIncorrectRequestException(e);
133     }
134   }
135 
136   @Override
137   protected final ArrayNode getDetailedAllow() {
138     final ArrayNode node = JsonHandler.createArrayNode();
139 
140     if (methods.contains(METHOD.GET)) {
141       final LogJsonPacket node3 = new LogJsonPacket();
142       node3.setRequestUserPacket();
143       node3.setComment("Log export request (GET)");
144       node3.setRequest("The requester or requested host name");
145       node3.setRule("The rule name");
146       node3.setStart(new Date());
147       node3.setStop(new Date());
148       node3.setStartid("Start id - long -");
149       node3.setStopid("Stop id - long -");
150       final ObjectNode node2;
151       final LogResponseJsonPacket resp = new LogResponseJsonPacket();
152       resp.setComment("Log export response");
153       resp.setFilename("filepath");
154       final ArrayNode node1 = JsonHandler.createArrayNode();
155       try {
156         node1.add(resp.createObjectNode());
157         node2 = RestArgument.fillDetailedAllow(METHOD.GET, path,
158                                                ACTIONS_TYPE.GetLog.name(),
159                                                node3.createObjectNode(), node1);
160         node.add(node2);
161       } catch (final OpenR66ProtocolPacketException ignored) {
162         // ignore
163       }
164     }
165 
166     final ObjectNode node2 =
167         RestArgument.fillDetailedAllow(METHOD.OPTIONS, path,
168                                        COMMAND_TYPE.OPTIONS.name(), null, null);
169     node.add(node2);
170 
171     return node;
172   }
173 }