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 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   * General RestConfiguration model
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     * SERVER REST interface using explicit address (null means all available)
51     */
52    private String restAddress;
53    /**
54     * Http REST port (SSL or not SSL)
55     */
56    private int restPort = -1;
57    /**
58     * SERVER REST interface using SSL
59     */
60    private boolean restSsl;
61    /**
62     * SERVER REST interface using time limit (default: no limit <= 0)
63     */
64    private long restTimeLimit = -1;
65    /**
66     * SERVER REST interface using authentication
67     */
68    private boolean restAuthenticated;
69    /**
70     * SERVER REST interface using signature
71     */
72    private boolean restSignature = true;
73    /**
74     * Key for signature in SHA-256
75     */
76    private HmacSha256 hmacSha256;
77    /**
78     * SERVER REST interface allowing one Handler and associated CRUD (or
79     * equivalent POST, GET, PUT, DELETE)
80     * methods (2^0 for active, 2^1 as Create/POST, 2^2 as Read/GET, 2^3 as
81     * Update/PUT, 2^4 as Delete/DELETE)
82     */
83    private byte[] resthandlersCrud;
84  
85    /**
86     * Associated RestMethod Handlers
87     */
88    public final HashMap<String, RestMethodHandler> restHashMap =
89        new HashMap<String, RestMethodHandler>();
90  
91    /**
92     * Set Key from String directly
93     *
94     * @param authentKey
95     */
96    public final void initializeKey(final String authentKey) {
97      setHmacSha256(new HmacSha256());
98      getHmacSha256().setSecretKey(authentKey.getBytes(WaarpStringUtils.UTF8));
99    }
100 
101   /**
102    * Set Key from file
103    *
104    * @param authentKey
105    *
106    * @throws CryptoException
107    * @throws IOException
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    * @return the restAddress
136    */
137   public final String getRestAddress() {
138     return restAddress;
139   }
140 
141   /**
142    * @param restAddress the restAddress to set
143    */
144   public final void setRestAddress(final String restAddress) {
145     this.restAddress = restAddress;
146   }
147 
148   /**
149    * @return the restPort
150    */
151   public final int getRestPort() {
152     return restPort;
153   }
154 
155   /**
156    * @param restPort the restPort to set
157    */
158   public final void setRestPort(final int restPort) {
159     this.restPort = restPort;
160   }
161 
162   /**
163    * @return the restSsl
164    */
165   public final boolean isRestSsl() {
166     return restSsl;
167   }
168 
169   /**
170    * @param restSsl the restSsl to set
171    */
172   public final void setRestSsl(final boolean restSsl) {
173     this.restSsl = restSsl;
174   }
175 
176   /**
177    * @return the restTimeLimit
178    */
179   public final long getRestTimeLimit() {
180     return restTimeLimit;
181   }
182 
183   /**
184    * @param restTimeLimit the restTimeLimit to set
185    */
186   public final void setRestTimeLimit(final long restTimeLimit) {
187     this.restTimeLimit = restTimeLimit;
188   }
189 
190   /**
191    * @return the restAuthenticated
192    */
193   public final boolean isRestAuthenticated() {
194     return restAuthenticated;
195   }
196 
197   /**
198    * @param restAuthenticated the restAuthenticated to set
199    */
200   public final void setRestAuthenticated(final boolean restAuthenticated) {
201     this.restAuthenticated = restAuthenticated;
202   }
203 
204   /**
205    * @return the restSignature
206    */
207   public final boolean isRestSignature() {
208     return restSignature;
209   }
210 
211   /**
212    * @param restSignature the restSignature to set
213    */
214   public final void setRestSignature(final boolean restSignature) {
215     this.restSignature = restSignature;
216   }
217 
218   /**
219    * @return the hmacSha256
220    */
221   public final HmacSha256 getHmacSha256() {
222     return hmacSha256;
223   }
224 
225   /**
226    * @param hmacSha256 the hmacSha256 to set
227    */
228   public final void setHmacSha256(final HmacSha256 hmacSha256) {
229     this.hmacSha256 = hmacSha256;
230   }
231 
232   /**
233    * @return the resthandlersCrud
234    */
235   public final byte[] getResthandlersCrud() {
236     return resthandlersCrud;
237   }
238 
239   /**
240    * @param resthandlersCrud the resthandlersCrud to set
241    */
242   public final void setResthandlersCrud(final byte[] resthandlersCrud) {
243     this.resthandlersCrud = resthandlersCrud;
244   }
245 }