1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
47
48 public HttpPageHandler(final Map<String, HttpPage> hashmap) {
49 setHashmap(hashmap);
50 }
51
52
53
54
55
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
64
65
66
67
68
69
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
90 break;
91 case GETDOWNLOAD:
92 if (!"GET".equalsIgnoreCase(method)) {
93
94 page = getHttpPageError(session, page);
95 }
96 break;
97 case POST:
98 case POSTUPLOAD:
99 if (!"POST".equalsIgnoreCase(method)) {
100
101 page = getHttpPageError(session, page);
102 }
103 break;
104 case PUT:
105 if (!"PUT".equalsIgnoreCase(method)) {
106
107 page = getHttpPageError(session, page);
108 }
109 break;
110 case ERROR:
111 break;
112 default:
113
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
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
141
142 public final Map<String, HttpPage> getHashmap() {
143 return hashmap;
144 }
145
146
147
148
149 private void setHashmap(final Map<String, HttpPage> hashmap) {
150 this.hashmap = hashmap;
151 }
152 }