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.rest;
21
22 import org.waarp.common.crypto.HmacSha256;
23 import org.waarp.common.exception.CryptoException;
24 import org.waarp.common.utility.WaarpStringUtils;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.HashMap;
29 import java.util.Map.Entry;
30
31
32
33
34 public class RestConfiguration {
35 public enum CRUD {
36 CREATE(0x01), READ(0x02), UPDATE(0x04), DELETE(0x08), ALL(0x0F);
37
38 public final byte mask;
39
40 CRUD(final int mask) {
41 this.mask = (byte) mask;
42 }
43
44 public final boolean isValid(final byte tocheck) {
45 return (tocheck & mask) != 0;
46 }
47 }
48
49
50
51
52 private String restAddress;
53
54
55
56 private int restPort = -1;
57
58
59
60 private boolean restSsl;
61
62
63
64 private long restTimeLimit = -1;
65
66
67
68 private boolean restAuthenticated;
69
70
71
72 private boolean restSignature = true;
73
74
75
76 private HmacSha256 hmacSha256;
77
78
79
80
81
82
83 private byte[] resthandlersCrud;
84
85
86
87
88 public final HashMap<String, RestMethodHandler> restHashMap =
89 new HashMap<String, RestMethodHandler>();
90
91
92
93
94
95
96 public final void initializeKey(final String authentKey) {
97 setHmacSha256(new HmacSha256());
98 getHmacSha256().setSecretKey(authentKey.getBytes(WaarpStringUtils.UTF8));
99 }
100
101
102
103
104
105
106
107
108
109 public final void initializeKey(final File authentKey)
110 throws CryptoException, IOException {
111 setHmacSha256(new HmacSha256());
112 getHmacSha256().setSecretKey(authentKey);
113 }
114
115 @Override
116 public String toString() {
117 final StringBuilder result = new StringBuilder(
118 "{address: " + getRestAddress() + ", port: " + getRestPort() +
119 ", ssl: " + isRestSsl() + ", time: " + getRestTimeLimit() +
120 ", authent:" + isRestAuthenticated() + ", signature: " +
121 isRestSignature() + ", handlers: [");
122 for (final Entry<String, RestMethodHandler> elt : restHashMap.entrySet()) {
123 result.append(elt.getKey()).append('=').append(elt.getValue().methods)
124 .append(", ");
125 }
126 result.append("], crud: [");
127 for (final byte crud : getResthandlersCrud()) {
128 result.append(crud).append(", ");
129 }
130 result.append("] }");
131 return result.toString();
132 }
133
134
135
136
137 public final String getRestAddress() {
138 return restAddress;
139 }
140
141
142
143
144 public final void setRestAddress(final String restAddress) {
145 this.restAddress = restAddress;
146 }
147
148
149
150
151 public final int getRestPort() {
152 return restPort;
153 }
154
155
156
157
158 public final void setRestPort(final int restPort) {
159 this.restPort = restPort;
160 }
161
162
163
164
165 public final boolean isRestSsl() {
166 return restSsl;
167 }
168
169
170
171
172 public final void setRestSsl(final boolean restSsl) {
173 this.restSsl = restSsl;
174 }
175
176
177
178
179 public final long getRestTimeLimit() {
180 return restTimeLimit;
181 }
182
183
184
185
186 public final void setRestTimeLimit(final long restTimeLimit) {
187 this.restTimeLimit = restTimeLimit;
188 }
189
190
191
192
193 public final boolean isRestAuthenticated() {
194 return restAuthenticated;
195 }
196
197
198
199
200 public final void setRestAuthenticated(final boolean restAuthenticated) {
201 this.restAuthenticated = restAuthenticated;
202 }
203
204
205
206
207 public final boolean isRestSignature() {
208 return restSignature;
209 }
210
211
212
213
214 public final void setRestSignature(final boolean restSignature) {
215 this.restSignature = restSignature;
216 }
217
218
219
220
221 public final HmacSha256 getHmacSha256() {
222 return hmacSha256;
223 }
224
225
226
227
228 public final void setHmacSha256(final HmacSha256 hmacSha256) {
229 this.hmacSha256 = hmacSha256;
230 }
231
232
233
234
235 public final byte[] getResthandlersCrud() {
236 return resthandlersCrud;
237 }
238
239
240
241
242 public final void setResthandlersCrud(final byte[] resthandlersCrud) {
243 this.resthandlersCrud = resthandlersCrud;
244 }
245 }