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;
21  
22  import io.netty.handler.codec.http.HttpResponseStatus;
23  import org.waarp.common.database.DbSession;
24  import org.waarp.gateway.kernel.database.DbConstantGateway;
25  import org.waarp.gateway.kernel.database.WaarpActionLogger;
26  import org.waarp.gateway.kernel.exception.HttpIncorrectRequestException;
27  import org.waarp.gateway.kernel.session.HttpSession;
28  
29  import java.util.Map;
30  
31  /**
32   *
33   */
34  public class HttpPageHandler {
35    /*
36     * Need as default error pages: 400, 401, 403, 404, 406, 500
37     */
38  
39    private static final String INCORRECT_PAGE = "Incorrect Page: ";
40  
41    public static String hostid;
42  
43    private Map<String, HttpPage> hashmap;
44  
45    /**
46     * @param hashmap
47     */
48    public HttpPageHandler(final Map<String, HttpPage> hashmap) {
49      setHashmap(hashmap);
50    }
51  
52    /**
53     * @param code
54     *
55     * @return an HttpPage according to the error code (400, 404, 500, ...)
56     */
57    public final HttpPage getHttpPage(final int code) {
58      final String scode = Integer.toString(code);
59      return getHashmap().get(scode);
60    }
61  
62    /**
63     * @param uri
64     * @param method
65     * @param session
66     *
67     * @return the associated HttpPage if any
68     *
69     * @throws HttpIncorrectRequestException
70     */
71    public final HttpPage getHttpPage(final String uri, final String method,
72                                      final HttpSession session)
73        throws HttpIncorrectRequestException {
74      HttpPage page = getHashmap().get(uri);
75      if (page == null) {
76        return null;
77      }
78      if ("HEAD".equalsIgnoreCase(method)) {
79        return page;
80      }
81      switch (page.getPagerole()) {
82        case DELETE:
83          if (!"DELETE".equalsIgnoreCase(method)) {
84            page = getHttpPageError(session, page);
85          }
86          break;
87        case HTML:
88        case MENU:
89          // no check
90          break;
91        case GETDOWNLOAD:
92          if (!"GET".equalsIgnoreCase(method)) {
93            // error
94            page = getHttpPageError(session, page);
95          }
96          break;
97        case POST:
98        case POSTUPLOAD:
99          if (!"POST".equalsIgnoreCase(method)) {
100           // error
101           page = getHttpPageError(session, page);
102         }
103         break;
104       case PUT:
105         if (!"PUT".equalsIgnoreCase(method)) {
106           // error
107           page = getHttpPageError(session, page);
108         }
109         break;
110       case ERROR:
111         break;
112       default:
113         // error
114         page = getHttpPageError(session, page);
115     }
116     if (page == null) {
117       throw new HttpIncorrectRequestException("No Page found");
118     }
119     return page;
120   }
121 
122   private HttpPage getHttpPageError(final HttpSession session, HttpPage page) {
123     // error
124     final DbSession dbSession =
125         DbConstantGateway.admin != null? DbConstantGateway.admin.getSession() :
126             null;
127     WaarpActionLogger.logErrorAction(dbSession, session,
128                                      INCORRECT_PAGE + page.getPagerole() + ":" +
129                                      page.getPagename(),
130                                      HttpResponseStatus.BAD_REQUEST);
131     if (page.getErrorpage() != null && page.getErrorpage().length() > 1) {
132       page = getHashmap().get(page.getErrorpage());
133     } else {
134       page = null;
135     }
136     return page;
137   }
138 
139   /**
140    * @return the hashmap
141    */
142   public final Map<String, HttpPage> getHashmap() {
143     return hashmap;
144   }
145 
146   /**
147    * @param hashmap the hashmap to set
148    */
149   private void setHashmap(final Map<String, HttpPage> hashmap) {
150     this.hashmap = hashmap;
151   }
152 }