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.gateway.kernel.rest;
21  
22  import com.fasterxml.jackson.databind.node.ArrayNode;
23  import com.fasterxml.jackson.databind.node.ObjectNode;
24  import io.netty.buffer.ByteBuf;
25  import io.netty.channel.ChannelFuture;
26  import io.netty.channel.ChannelHandlerContext;
27  import io.netty.handler.codec.http.HttpResponseStatus;
28  import io.netty.handler.codec.http.multipart.FileUpload;
29  import org.waarp.common.json.JsonHandler;
30  import org.waarp.gateway.kernel.rest.DataModelRestMethodHandler.COMMAND_TYPE;
31  import org.waarp.gateway.kernel.rest.HttpRestHandler.METHOD;
32  
33  /**
34   * RestMethod handler to implement Root Options handler
35   */
36  public class RootOptionsRestMethodHandler extends RestMethodHandler {
37  
38    public static final String ROOT = "root";
39  
40    public RootOptionsRestMethodHandler(final RestConfiguration config) {
41      super(ROOT, "/", true, config, METHOD.OPTIONS);
42    }
43  
44    @Override
45    public final void checkHandlerSessionCorrectness(
46        final HttpRestHandler handler, final RestArgument arguments,
47        final RestArgument result) {
48      // nothing
49    }
50  
51    @Override
52    public final void getFileUpload(final HttpRestHandler handler,
53                                    final FileUpload data,
54                                    final RestArgument arguments,
55                                    final RestArgument result) {
56      // nothing
57    }
58  
59    @Override
60    public final Object getBody(final HttpRestHandler handler, final ByteBuf body,
61                                final RestArgument arguments,
62                                final RestArgument result) {
63      return null;
64    }
65  
66    @Override
67    public final void endParsingRequest(final HttpRestHandler handler,
68                                        final RestArgument arguments,
69                                        final RestArgument result,
70                                        final Object body) {
71      // nothing
72    }
73  
74    @Override
75    public final ChannelFuture sendResponse(final HttpRestHandler handler,
76                                            final ChannelHandlerContext ctx,
77                                            final RestArgument arguments,
78                                            final RestArgument result,
79                                            final Object body,
80                                            final HttpResponseStatus status) {
81      return sendOptionsResponse(handler, ctx, result, status);
82    }
83  
84    @Override
85    public final void optionsCommand(final HttpRestHandler handler,
86                                     final RestArgument arguments,
87                                     final RestArgument result) {
88      result.setCommand(COMMAND_TYPE.OPTIONS);
89      final METHOD[] realmethods = METHOD.values();
90      final boolean[] allMethods = new boolean[realmethods.length];
91      for (final RestMethodHandler method : handler.restHashMap.values()) {
92        for (final METHOD methoditem : method.methods) {
93          allMethods[methoditem.ordinal()] = true;
94        }
95      }
96      StringBuilder allow = null;
97      for (int i = 0; i < allMethods.length; i++) {
98        if (allMethods[i]) {
99          if (allow == null) {
100           allow = new StringBuilder(realmethods[i].name());
101         } else {
102           allow.append(',').append(realmethods[i].name());
103         }
104       }
105     }
106     StringBuilder allowUri = null;
107     for (final RestMethodHandler method : handler.restHashMap.values()) {
108       if (allowUri == null) {
109         allowUri = new StringBuilder(method.path);
110       } else {
111         allowUri.append(',').append(method.path);
112       }
113     }
114     final ArrayNode array = JsonHandler.createArrayNode();
115     for (final RestMethodHandler method : handler.restHashMap.values()) {
116       final ArrayNode array2 = method.getDetailedAllow();
117       if (method != this) {
118         array.addObject().putArray(method.path).addAll(array2);
119       } else {
120         array.addObject().putArray(ROOT).addAll(array2);
121       }
122     }
123     if (allow != null && allowUri != null) {
124       result.addOptions(allow.toString(), allowUri.toString(), array);
125     }
126   }
127 
128   @Override
129   protected final ArrayNode getDetailedAllow() {
130     final ArrayNode node = JsonHandler.createArrayNode();
131 
132     final ObjectNode node2 = node.addObject().putObject(METHOD.OPTIONS.name());
133     node2.put(RestArgument.REST_FIELD.JSON_PATH.field, path);
134     node2.put(RestArgument.REST_ROOT_FIELD.JSON_COMMAND.field,
135               COMMAND_TYPE.OPTIONS.name());
136 
137     return node;
138   }
139 
140 }