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  
21  /*
22   * Copyright 2009 Red Hat, Inc.
23   *
24   * Red Hat licenses this file to you under the Apache License, version 2.0 (the
25   * "License"); you may not use this file except in compliance with the License.
26   * You may obtain a copy of the License at:
27   *
28   * http://www.apache.org/licenses/LICENSE-2.0
29   *
30   * Unless required by applicable law or agreed to in writing, software
31   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
32   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
33   * License for the specific language governing permissions and limitations under
34   * the License.
35   */
36  package org.waarp.openr66.protocol.http.rest.client;
37  
38  import com.fasterxml.jackson.databind.JsonNode;
39  import com.fasterxml.jackson.databind.node.ObjectNode;
40  import io.netty.buffer.ByteBuf;
41  import io.netty.buffer.Unpooled;
42  import io.netty.channel.Channel;
43  import io.netty.channel.ChannelHandlerContext;
44  import io.netty.channel.SimpleChannelInboundHandler;
45  import io.netty.handler.codec.http.FullHttpResponse;
46  import io.netty.handler.codec.http.HttpContent;
47  import io.netty.handler.codec.http.HttpHeaderNames;
48  import io.netty.handler.codec.http.HttpObject;
49  import io.netty.handler.codec.http.HttpResponse;
50  import io.netty.handler.codec.http.HttpResponseStatus;
51  import io.netty.handler.codec.http.LastHttpContent;
52  import org.waarp.common.crypto.ssl.WaarpSslUtility;
53  import org.waarp.common.json.JsonHandler;
54  import org.waarp.common.logging.WaarpLogger;
55  import org.waarp.common.logging.WaarpLoggerFactory;
56  import org.waarp.common.utility.WaarpNettyUtil;
57  import org.waarp.common.utility.WaarpStringUtils;
58  import org.waarp.gateway.kernel.exception.HttpIncorrectRequestException;
59  import org.waarp.gateway.kernel.exception.HttpInvalidAuthenticationException;
60  import org.waarp.gateway.kernel.rest.DataModelRestMethodHandler.COMMAND_TYPE;
61  import org.waarp.gateway.kernel.rest.RestArgument;
62  import org.waarp.gateway.kernel.rest.client.HttpRestClientSimpleResponseHandler;
63  import org.waarp.gateway.kernel.rest.client.RestFuture;
64  import org.waarp.openr66.protocol.http.rest.handler.HttpRestAbstractR66Handler.ACTIONS_TYPE;
65  
66  import java.net.ConnectException;
67  import java.nio.channels.ClosedChannelException;
68  import java.nio.charset.UnsupportedCharsetException;
69  
70  /**
71   * Rest client response handler.
72   * <p>
73   * Note: by default, no connection are closed except in case of error or if in
74   * HTTP 1.0 or explicitly to be
75   * closed.
76   */
77  public abstract class HttpRestR66ClientResponseHandler
78      extends SimpleChannelInboundHandler<HttpObject> {
79    /**
80     * Internal Logger
81     */
82    private static final WaarpLogger logger =
83        WaarpLoggerFactory.getLogger(HttpRestR66ClientResponseHandler.class);
84  
85    private ByteBuf cumulativeBody;
86    protected JsonNode jsonObject;
87  
88    protected final void addContent(final FullHttpResponse response)
89        throws HttpIncorrectRequestException {
90      final ByteBuf content = response.content();
91      if (content != null && content.isReadable()) {
92        WaarpNettyUtil.retain(content);
93        if (cumulativeBody != null) {
94          cumulativeBody = Unpooled.wrappedBuffer(cumulativeBody, content);
95        } else {
96          cumulativeBody = content;
97        }
98        // get the Json equivalent of the Body
99        try {
100         final String json = cumulativeBody.toString(WaarpStringUtils.UTF8);
101         jsonObject = JsonHandler.getFromString(json);
102       } catch (final UnsupportedCharsetException e2) {
103         logger.warn("Error" + " : {}", e2.getMessage());
104         throw new HttpIncorrectRequestException(e2);
105       }
106       cumulativeBody = null;
107     }
108   }
109 
110   /**
111    * Setting the RestArgument to the RestFuture and validating RestFuture.
112    *
113    * @param channel
114    *
115    * @throws HttpInvalidAuthenticationException
116    */
117   protected final void actionFromResponse(final Channel channel)
118       throws HttpInvalidAuthenticationException {
119     boolean includeValidation = false;
120     final RestArgument ra = new RestArgument((ObjectNode) jsonObject);
121     if (jsonObject == null) {
122       logger.debug("Recv: EMPTY");
123     }
124     final RestFuture restFuture =
125         channel.attr(HttpRestClientSimpleResponseHandler.RESTARGUMENT).get();
126     restFuture.setRestArgument(ra);
127     switch (ra.getMethod()) {
128       case DELETE:
129         includeValidation = delete(channel, ra);
130         break;
131       case GET:
132         includeValidation = get(channel, ra);
133         break;
134       case OPTIONS:
135         includeValidation = options(channel, ra);
136         break;
137       case POST:
138         includeValidation = post(channel, ra);
139         break;
140       case PUT:
141         includeValidation = put(channel, ra);
142         break;
143       default:
144         break;
145     }
146     if (!includeValidation) {
147       // finalize the future
148       restFuture.setSuccess();
149     }
150   }
151 
152   /**
153    * Method calls when a action REST command is raised as answer
154    *
155    * @param channel
156    * @param ra
157    * @param act
158    *
159    * @return if validation is done (or suppose to be)
160    *
161    * @throws HttpInvalidAuthenticationException
162    */
163   protected abstract boolean action(Channel channel, RestArgument ra,
164                                     ACTIONS_TYPE act)
165       throws HttpInvalidAuthenticationException;
166 
167   /**
168    * Method calls when a REST Get command is raised as answer
169    *
170    * @param channel
171    * @param ra
172    *
173    * @return if validation is done (or suppose to be)
174    *
175    * @throws HttpInvalidAuthenticationException
176    */
177   protected abstract boolean afterDbGet(Channel channel, RestArgument ra)
178       throws HttpInvalidAuthenticationException;
179 
180   /**
181    * Method calls when a REST Post command is raised as answer
182    *
183    * @param channel
184    * @param ra
185    *
186    * @return if validation is done (or suppose to be)
187    *
188    * @throws HttpInvalidAuthenticationException
189    */
190   protected abstract boolean afterDbPost(Channel channel, RestArgument ra)
191       throws HttpInvalidAuthenticationException;
192 
193   /**
194    * Method calls when a REST Put command is raised as answer
195    *
196    * @param channel
197    * @param ra
198    *
199    * @return if validation is done (or suppose to be)
200    *
201    * @throws HttpInvalidAuthenticationException
202    */
203   protected abstract boolean afterDbPut(Channel channel, RestArgument ra)
204       throws HttpInvalidAuthenticationException;
205 
206   /**
207    * Method calls when a REST Delete command is raised as answer
208    *
209    * @param channel
210    * @param ra
211    *
212    * @return if validation is done (or suppose to be)
213    *
214    * @throws HttpInvalidAuthenticationException
215    */
216   protected abstract boolean afterDbDelete(Channel channel, RestArgument ra)
217       throws HttpInvalidAuthenticationException;
218 
219   /**
220    * Method calls when a REST GetMultiple command is raised as answer
221    *
222    * @param channel
223    * @param ra
224    *
225    * @return if validation is done (or suppose to be)
226    *
227    * @throws HttpInvalidAuthenticationException
228    */
229   protected abstract boolean afterDbGetMultiple(Channel channel,
230                                                 RestArgument ra)
231       throws HttpInvalidAuthenticationException;
232 
233   /**
234    * Method calls when a REST Options command is raised as answer
235    *
236    * @param channel
237    * @param ra
238    *
239    * @return if validation is done (or suppose to be)
240    *
241    * @throws HttpInvalidAuthenticationException
242    */
243   protected abstract boolean afterDbOptions(Channel channel, RestArgument ra)
244       throws HttpInvalidAuthenticationException;
245 
246   /**
247    * Method calls when a REST command is in error
248    *
249    * @param channel
250    * @param ra (might be null)
251    *
252    * @return if validation is done (or suppose to be)
253    *
254    * @throws HttpInvalidAuthenticationException
255    */
256   protected abstract boolean afterError(Channel channel, RestArgument ra)
257       throws HttpInvalidAuthenticationException;
258 
259   protected final boolean get(final Channel channel, final RestArgument ra)
260       throws HttpInvalidAuthenticationException {
261     if (logger.isDebugEnabled()) {
262       logger.debug(ra.prettyPrint());
263     }
264     if (ra.getCommand() == COMMAND_TYPE.GET) {
265       return afterDbGet(channel, ra);
266     } else if (ra.getCommand() == COMMAND_TYPE.MULTIGET) {
267       return afterDbGetMultiple(channel, ra);
268     } else {
269       final String cmd = ra.getCommandField();
270       try {
271         final ACTIONS_TYPE act = ACTIONS_TYPE.valueOf(cmd);
272         return action(channel, ra, act);
273       } catch (final Exception e) {
274         return false;
275       }
276     }
277   }
278 
279   protected final boolean put(final Channel channel, final RestArgument ra)
280       throws HttpInvalidAuthenticationException {
281     if (logger.isDebugEnabled()) {
282       logger.debug(ra.prettyPrint());
283     }
284     if (ra.getCommand() == COMMAND_TYPE.UPDATE) {
285       return afterDbPut(channel, ra);
286     } else {
287       final String cmd = ra.getCommandField();
288       try {
289         final ACTIONS_TYPE act = ACTIONS_TYPE.valueOf(cmd);
290         return action(channel, ra, act);
291       } catch (final Exception e) {
292         return false;
293       }
294     }
295   }
296 
297   protected final boolean post(final Channel channel, final RestArgument ra)
298       throws HttpInvalidAuthenticationException {
299     if (logger.isDebugEnabled()) {
300       logger.debug(ra.prettyPrint());
301     }
302     if (ra.getCommand() == COMMAND_TYPE.CREATE) {
303       return afterDbPost(channel, ra);
304     } else {
305       final String cmd = ra.getCommandField();
306       try {
307         final ACTIONS_TYPE act = ACTIONS_TYPE.valueOf(cmd);
308         return action(channel, ra, act);
309       } catch (final Exception e) {
310         return false;
311       }
312     }
313   }
314 
315   protected final boolean delete(final Channel channel, final RestArgument ra)
316       throws HttpInvalidAuthenticationException {
317     if (logger.isDebugEnabled()) {
318       logger.debug(ra.prettyPrint());
319     }
320     if (ra.getCommand() == COMMAND_TYPE.DELETE) {
321       return afterDbDelete(channel, ra);
322     } else {
323       final String cmd = ra.getCommandField();
324       try {
325         final ACTIONS_TYPE act = ACTIONS_TYPE.valueOf(cmd);
326         return action(channel, ra, act);
327       } catch (final Exception e) {
328         return false;
329       }
330     }
331   }
332 
333   protected final boolean options(final Channel channel, final RestArgument ra)
334       throws HttpInvalidAuthenticationException {
335     if (logger.isDebugEnabled()) {
336       logger.debug(ra.prettyPrint());
337     }
338     if (ra.getCommand() == COMMAND_TYPE.OPTIONS) {
339       return afterDbOptions(channel, ra);
340     } else {
341       final String cmd = ra.getCommandField();
342       try {
343         final ACTIONS_TYPE act = ACTIONS_TYPE.valueOf(cmd);
344         return action(channel, ra, act);
345       } catch (final Exception e) {
346         return false;
347       }
348     }
349   }
350 
351   @Override
352   protected void channelRead0(final ChannelHandlerContext ctx,
353                               final HttpObject msg) throws Exception {
354     if (msg instanceof HttpResponse) {
355       final HttpResponse response = (HttpResponse) msg;
356       final HttpResponseStatus status = response.status();
357       logger.debug("{}: {} STATUS: {}", HttpHeaderNames.REFERER,
358                    response.headers().get(HttpHeaderNames.REFERER), status);
359       if (response.status().code() != 200) {
360         if (response instanceof FullHttpResponse) {
361           addContent((FullHttpResponse) response);
362         }
363         RestArgument ra = null;
364         if (jsonObject != null) {
365           ra = new RestArgument((ObjectNode) jsonObject);
366           final RestFuture restFuture = ctx.channel().attr(
367               HttpRestClientSimpleResponseHandler.RESTARGUMENT).get();
368           restFuture.setRestArgument(ra);
369           logger.error("Error: " + response.status().code() + ' ' +
370                        response.status().reasonPhrase() + '\n' +
371                        ra.prettyPrint());
372         } else {
373           logger.error("Error: " + response.status().code() + ' ' +
374                        response.status().reasonPhrase());
375         }
376         if (!afterError(ctx.channel(), ra)) {
377           final RestFuture restFuture = ctx.channel().attr(
378               HttpRestClientSimpleResponseHandler.RESTARGUMENT).get();
379           restFuture.cancel();
380         }
381         if (ctx.channel().isActive()) {
382           logger.debug("Will close");
383           WaarpSslUtility.closingSslChannel(ctx.channel());
384         }
385       } else {
386         if (response instanceof FullHttpResponse) {
387           addContent((FullHttpResponse) response);
388           actionFromResponse(ctx.channel());
389         }
390       }
391     } else {
392       final HttpContent chunk = (HttpContent) msg;
393       if (chunk instanceof LastHttpContent) {
394         final ByteBuf content = chunk.content();
395         if (content != null && content.isReadable()) {
396           WaarpNettyUtil.retain(content);
397           if (cumulativeBody != null) {
398             cumulativeBody = Unpooled.wrappedBuffer(cumulativeBody, content);
399           } else {
400             cumulativeBody = content;
401           }
402         }
403         // get the Json equivalent of the Body
404         if (cumulativeBody == null) {
405           jsonObject = JsonHandler.createObjectNode();
406         } else {
407           try {
408             final String json = cumulativeBody.toString(WaarpStringUtils.UTF8);
409             jsonObject = JsonHandler.getFromString(json);
410           } catch (final Throwable e2) {
411             logger.warn("Error" + " : {}", e2.getMessage());
412             throw new HttpIncorrectRequestException(e2);
413           }
414           WaarpNettyUtil.release(cumulativeBody);
415           cumulativeBody = null;
416         }
417         actionFromResponse(ctx.channel());
418       } else {
419         final ByteBuf content = chunk.content();
420         if (content != null && content.isReadable()) {
421           WaarpNettyUtil.retain(content);
422           if (cumulativeBody != null) {
423             cumulativeBody = Unpooled.wrappedBuffer(cumulativeBody, content);
424           } else {
425             cumulativeBody = content;
426           }
427         }
428       }
429     }
430   }
431 
432   @Override
433   public void exceptionCaught(final ChannelHandlerContext ctx,
434                               final Throwable cause) {
435     final RestFuture restFuture =
436         ctx.channel().attr(HttpRestClientSimpleResponseHandler.RESTARGUMENT)
437            .get();
438     if (cause instanceof ClosedChannelException) {
439       logger.debug("Close before ending");
440       restFuture.setFailure(cause);
441       return;
442     } else if (cause instanceof ConnectException) {
443       if (ctx.channel().isActive()) {
444         logger.debug("Will close");
445         restFuture.setFailure(cause);
446         WaarpSslUtility.closingSslChannel(ctx.channel());
447       }
448       return;
449     }
450     logger.warn("Error: {}", cause.getMessage());
451     if (ctx.channel() != null && restFuture != null) {
452       restFuture.setFailure(cause);
453     }
454     logger.debug("Will close");
455     WaarpSslUtility.closingSslChannel(ctx.channel());
456   }
457 
458 }