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.multipart.FileUpload;
23  import org.waarp.gateway.kernel.exception.HttpIncorrectRequestException;
24  
25  /**
26   *
27   */
28  public abstract class AbstractHttpField implements Cloneable {
29  
30    public enum FieldRole {
31      BUSINESS_INPUT_TEXT, BUSINESS_INPUT_CHECKBOX, BUSINESS_INPUT_RADIO,
32      BUSINESS_INPUT_HIDDEN, BUSINESS_INPUT_FILE, BUSINESS_INPUT_IMAGE,
33      BUSINESS_INPUT_PWD, BUSINESS_TEXTAREA, BUSINESS_SELECT, SUBMIT,
34      BUSINESS_COOKIE
35    }
36  
37    public enum FieldPosition {
38      URL, HEADER, COOKIE, BODY, ANY
39    }
40  
41    /**
42     * Special field name for Error page
43     */
44    public static final String ERRORINFO = "ERRORINFO";
45    /*
46     * fieldname, fieldtype, fieldinfo, fieldvalue, fieldvisibility, fieldmandatory, fieldcookieset,
47     * fieldtovalidate, fieldposition, fieldrank
48     */
49    private String fieldname;
50    private FieldRole fieldtype;
51    private String fieldinfo;
52    public String fieldvalue;
53    private boolean fieldvisibility;
54    private boolean fieldmandatory;
55    private boolean fieldcookieset;
56    private boolean fieldtovalidate;
57    private FieldPosition fieldposition;
58    private int fieldrank;
59    private boolean present;
60    FileUpload fileUpload;
61  
62    /**
63     * @param fieldname
64     * @param fieldtype
65     * @param fieldinfo
66     * @param fieldvalue
67     * @param fieldvisibility
68     * @param fieldmandatory
69     * @param fieldcookieset
70     * @param fieldtovalidate
71     * @param fieldposition
72     * @param fieldrank
73     */
74    protected AbstractHttpField(final String fieldname, final FieldRole fieldtype,
75                                final String fieldinfo, final String fieldvalue,
76                                final boolean fieldvisibility,
77                                final boolean fieldmandatory,
78                                final boolean fieldcookieset,
79                                final boolean fieldtovalidate,
80                                final FieldPosition fieldposition,
81                                final int fieldrank) {
82      setFieldname(fieldname);
83      setFieldtype(fieldtype);
84      setFieldinfo(fieldinfo);
85      this.fieldvalue = fieldvalue;
86      setFieldvisibility(fieldvisibility);
87      setFieldmandatory(fieldmandatory);
88      setFieldcookieset(fieldcookieset);
89      setFieldtovalidate(fieldtovalidate);
90      setFieldposition(fieldposition);
91      setFieldrank(fieldrank);
92    }
93  
94    /**
95     * @param page source HttpPage
96     *
97     * @return the html form of a field according to its type and value
98     */
99    public abstract String getHtmlFormField(HttpPage page)
100       throws HttpIncorrectRequestException;
101 
102   /**
103    * @param page source HttpPage
104    *
105    * @return the html tab of a field according to its type and value
106    */
107   public abstract String getHtmlTabField(HttpPage page)
108       throws HttpIncorrectRequestException;
109 
110   @Override
111   public abstract AbstractHttpField clone();
112 
113   /**
114    * Set the value
115    *
116    * @param value
117    *
118    * @throws HttpIncorrectRequestException if the value was already
119    *     set
120    */
121   public abstract void setStringValue(String value)
122       throws HttpIncorrectRequestException;
123 
124   /**
125    * Set the fileUpload
126    *
127    * @param fileUpload
128    *
129    * @throws HttpIncorrectRequestException if the value was already
130    *     set
131    */
132   public abstract void setFileUpload(FileUpload fileUpload)
133       throws HttpIncorrectRequestException;
134 
135   /**
136    * Clean method
137    */
138   public final void clean() {
139     setFieldname(null);
140     setFieldinfo(null);
141     fieldvalue = null;
142     setPresent(false);
143     if (getFileUpload() != null) {
144       getFileUpload().delete();
145       fileUpload = null;
146     }
147   }
148 
149   /**
150    * @return the fieldname
151    */
152   public final String getFieldname() {
153     return fieldname;
154   }
155 
156   /**
157    * @param fieldname the fieldname to set
158    */
159   private void setFieldname(final String fieldname) {
160     this.fieldname = fieldname;
161   }
162 
163   /**
164    * @return the fieldtype
165    */
166   public final FieldRole getFieldtype() {
167     return fieldtype;
168   }
169 
170   /**
171    * @param fieldtype the fieldtype to set
172    */
173   private void setFieldtype(final FieldRole fieldtype) {
174     this.fieldtype = fieldtype;
175   }
176 
177   /**
178    * @return the fieldinfo
179    */
180   public final String getFieldinfo() {
181     return fieldinfo;
182   }
183 
184   /**
185    * @param fieldinfo the fieldinfo to set
186    */
187   private void setFieldinfo(final String fieldinfo) {
188     this.fieldinfo = fieldinfo;
189   }
190 
191   /**
192    * @return the fieldvisibility
193    */
194   public final boolean isFieldvisibility() {
195     return fieldvisibility;
196   }
197 
198   /**
199    * @param fieldvisibility the fieldvisibility to set
200    */
201   public final void setFieldvisibility(final boolean fieldvisibility) {
202     this.fieldvisibility = fieldvisibility;
203   }
204 
205   /**
206    * @return the fieldmandatory
207    */
208   public final boolean isFieldmandatory() {
209     return fieldmandatory;
210   }
211 
212   /**
213    * @param fieldmandatory the fieldmandatory to set
214    */
215   private void setFieldmandatory(final boolean fieldmandatory) {
216     this.fieldmandatory = fieldmandatory;
217   }
218 
219   /**
220    * @return the fieldcookieset
221    */
222   public final boolean isFieldcookieset() {
223     return fieldcookieset;
224   }
225 
226   /**
227    * @param fieldcookieset the fieldcookieset to set
228    */
229   private void setFieldcookieset(final boolean fieldcookieset) {
230     this.fieldcookieset = fieldcookieset;
231   }
232 
233   /**
234    * @return the fieldtovalidate
235    */
236   public final boolean isFieldtovalidate() {
237     return fieldtovalidate;
238   }
239 
240   /**
241    * @param fieldtovalidate the fieldtovalidate to set
242    */
243   private void setFieldtovalidate(final boolean fieldtovalidate) {
244     this.fieldtovalidate = fieldtovalidate;
245   }
246 
247   /**
248    * @return the fieldposition
249    */
250   public final FieldPosition getFieldposition() {
251     return fieldposition;
252   }
253 
254   /**
255    * @param fieldposition the fieldposition to set
256    */
257   private void setFieldposition(final FieldPosition fieldposition) {
258     this.fieldposition = fieldposition;
259   }
260 
261   /**
262    * @return the fieldrank
263    */
264   public final int getFieldrank() {
265     return fieldrank;
266   }
267 
268   /**
269    * @param fieldrank the fieldrank to set
270    */
271   private void setFieldrank(final int fieldrank) {
272     this.fieldrank = fieldrank;
273   }
274 
275   /**
276    * @return the present
277    */
278   public final boolean isPresent() {
279     return present;
280   }
281 
282   /**
283    * @param present the present to set
284    */
285   void setPresent(final boolean present) {
286     this.present = present;
287   }
288 
289   /**
290    * @return the fileUpload
291    */
292   public final FileUpload getFileUpload() {
293     return fileUpload;
294   }
295 }