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.HttpInvalidAuthenticationException;
31  import org.waarp.gateway.kernel.rest.DataModelRestMethodHandler.COMMAND_TYPE;
32  import org.waarp.gateway.kernel.rest.HttpRestHandler;
33  import org.waarp.gateway.kernel.rest.HttpRestHandler.METHOD;
34  import org.waarp.gateway.kernel.rest.RestArgument;
35  import org.waarp.gateway.kernel.rest.RestConfiguration;
36  import org.waarp.openr66.protocol.exception.OpenR66ProtocolNoCorrectAuthenticationException;
37  import org.waarp.openr66.protocol.exception.OpenR66ProtocolNotAuthenticatedException;
38  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
39  import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler;
40  import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler.RESTHANDLERS;
41  import org.waarp.openr66.protocol.localhandler.ServerActions;
42  import org.waarp.openr66.protocol.localhandler.packet.json.BandwidthJsonPacket;
43  import org.waarp.openr66.protocol.localhandler.packet.json.JsonPacket;
44  
45  /**
46   * Bandwidth Http REST interface: http://host/bandwidth?... +
47   * BandwidthJsonPacket as GET or PUT
48   */
49  public class HttpRestBandwidthR66Handler extends HttpRestAbstractR66Handler {
50  
51    public static final String BASEURI = "bandwidth";
52    /**
53     * Internal Logger
54     */
55    private static final WaarpLogger logger =
56        WaarpLoggerFactory.getLogger(HttpRestBandwidthR66Handler.class);
57  
58    public HttpRestBandwidthR66Handler(final RestConfiguration config,
59                                       final METHOD... methods) {
60      super(BASEURI, config, METHOD.OPTIONS);
61      setIntersectionMethods(methods, METHOD.GET, METHOD.PUT);
62    }
63  
64    @Override
65    public final void endParsingRequest(final HttpRestHandler handler,
66                                        final RestArgument arguments,
67                                        final RestArgument result,
68                                        final Object body)
69        throws HttpInvalidAuthenticationException {
70      try {
71        HttpRestV1Utils.checkSanity(arguments);
72      } catch (final InvalidArgumentException e) {
73        throw new HttpInvalidAuthenticationException("Issue on values", e);
74      }
75      logger.debug("debug: {} ### {}", arguments, result);
76      if (body != null) {
77        logger.debug("Obj: {}", body);
78      }
79      handler.setWillClose(false);
80      final ServerActions serverHandler =
81          ((HttpRestR66Handler) handler).getServerHandler();
82      // now action according to body
83      final JsonPacket json = (JsonPacket) body;
84      if (json != null && !(json instanceof BandwidthJsonPacket)) {
85        logger.info("Validation is ignored: {}", json);
86        result.setDetail("Unknown command");
87        setError(handler, result, json, HttpResponseStatus.PRECONDITION_FAILED);
88        return;
89      }
90      result.getAnswer()
91            .put(AbstractDbData.JSON_MODEL, RESTHANDLERS.Bandwidth.name());
92      try {
93        final long[] lresult;
94        final boolean setter;
95        final BandwidthJsonPacket node;
96        if (json != null && json instanceof BandwidthJsonPacket) {//
97          // setter, writeglobal, readglobal, writesession, readsession
98          node = (BandwidthJsonPacket) json;
99          setter = node.isSetter();
100         if (setter && arguments.getMethod() != METHOD.PUT) {
101           // wrong
102           result.setDetail("Setter should be requested with a PUT method");
103           setError(handler, result, HttpResponseStatus.CONFLICT);
104           return;
105         } else if (!setter && arguments.getMethod() != METHOD.GET) {
106           // wrong
107           result.setDetail("Getter should not be requested with a GET method");
108           setError(handler, result, HttpResponseStatus.CONFLICT);
109           return;
110         }
111       } else {
112         if (json == null && arguments.getMethod() != METHOD.GET) {
113           // wrong
114           result.setDetail("Setter should be requested with a JSON argument");
115           setError(handler, result, HttpResponseStatus.CONFLICT);
116           return;
117         }
118         setter = false;
119         node = new BandwidthJsonPacket();
120       }
121       if (setter) {
122         result.setCommand(ACTIONS_TYPE.SetBandwidth.name());
123       } else {
124         result.setCommand(ACTIONS_TYPE.GetBandwidth.name());
125       }
126       // request of current values or set new values
127       lresult = serverHandler.bandwidth(setter, node.getWriteglobal(),
128                                         node.getReadglobal(),
129                                         node.getWritesession(),
130                                         node.getReadsession());
131       // Now answer
132       node.setWriteglobal(lresult[0]);
133       node.setReadglobal(lresult[1]);
134       node.setWritesession(lresult[2]);
135       node.setReadsession(lresult[3]);
136       setOk(handler, result, node, HttpResponseStatus.OK);
137     } catch (final OpenR66ProtocolNotAuthenticatedException e) {
138       throw new HttpInvalidAuthenticationException(e);
139     } catch (final OpenR66ProtocolNoCorrectAuthenticationException e) {
140       throw new HttpInvalidAuthenticationException(e);
141     }
142   }
143 
144   @Override
145   protected final ArrayNode getDetailedAllow() {
146     final ArrayNode node = JsonHandler.createArrayNode();
147 
148     final BandwidthJsonPacket node3 = new BandwidthJsonPacket();
149     node3.setComment("Bandwidth getter (GET)");
150     node3.setRequestUserPacket();
151     ObjectNode node2;
152     final ArrayNode node1 = JsonHandler.createArrayNode();
153     try {
154       node1.add(node3.createObjectNode());
155       if (methods.contains(METHOD.GET)) {
156         node2 = RestArgument.fillDetailedAllow(METHOD.GET, path,
157                                                ACTIONS_TYPE.GetBandwidth.name(),
158                                                node3.createObjectNode(), node1);
159         node.add(node2);
160       }
161     } catch (final OpenR66ProtocolPacketException ignored) {
162       // ignore
163     }
164 
165     if (methods.contains(METHOD.PUT)) {
166       node3.setComment("Bandwidth setter (PUT)");
167       try {
168         node2 = RestArgument.fillDetailedAllow(METHOD.PUT, path,
169                                                ACTIONS_TYPE.SetBandwidth.name(),
170                                                node3.createObjectNode(), node1);
171         node.add(node2);
172       } catch (final OpenR66ProtocolPacketException ignored) {
173         // ignore
174       }
175     }
176     node2 = RestArgument.fillDetailedAllow(METHOD.OPTIONS, path,
177                                            COMMAND_TYPE.OPTIONS.name(), null,
178                                            null);
179     node.add(node2);
180 
181     return node;
182   }
183 }