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.http.saplink;
21  
22  import org.waarp.common.logging.SysErrLogger;
23  import org.waarp.gateway.kernel.AbstractHttpBusinessRequest;
24  import org.waarp.gateway.kernel.AbstractHttpField;
25  import org.waarp.gateway.kernel.AbstractHttpField.FieldPosition;
26  import org.waarp.gateway.kernel.AbstractHttpField.FieldRole;
27  import org.waarp.gateway.kernel.DefaultHttpField;
28  import org.waarp.gateway.kernel.HttpBusinessFactory;
29  import org.waarp.gateway.kernel.HttpPage;
30  import org.waarp.gateway.kernel.HttpPage.PageRole;
31  import org.waarp.gateway.kernel.HttpPageHandler;
32  
33  import java.net.SocketAddress;
34  import java.util.HashMap;
35  import java.util.LinkedHashMap;
36  import java.util.Map;
37  
38  /**
39   *
40   */
41  public class HttpSapBusinessFactory extends HttpBusinessFactory {
42  
43    public static final String SAPLINK = "/saplink";
44  
45    @Override
46    public AbstractHttpBusinessRequest getNewHttpBusinessRequest(
47        final SocketAddress remoteAddress,
48        final Map<String, AbstractHttpField> fields, final HttpPage page) {
49      return null;
50    }
51  
52    /**
53     * All functions for SapArg: some could be not implemented. Note that create
54     * and update exist in two modes
55     * (Post and Put)
56     */
57    public enum SapFunction {
58      info, get, docGet, createPost, createPut, mCreate, append, updatePost,
59      updatePut, delete
60    }
61  
62    /**
63     * All fields for SapArg
64     */
65    public enum SapField {
66      contRep, docId, compId, pVersion, resultAs, accessMode, authId, expiration,
67      secKey, fromOffset, toOffset, Content_Type("Content-Type"), charset,
68      version, Content_Length("Content-Length"), docProt, pattern, caseSensitive,
69      numResults, X_compId("X-compId"), X_docId("X-docId"),
70      X_docStatus("X-docStatus"), X_contentRep("X-contentRep"),
71      X_pVersion("X-pVersion"), X_dateC("X-dateC"), X_timeC("X-timeC"),
72      X_dateM("X-dateM"), X_timeM("X-timeM"), X_numberComps("X-numberComps"),
73      X_Content_Type("X-Content-Type"), X_Content_Length("X-Content-Length"),
74      X_compStatus("X-compStatus"), X_compDateC("X-compDateC"),
75      X_compTimeC("X-compTimeC"), X_compDateM("X-compDateM"),
76      X_compTimeM("X-compTimeM"), X_numComps("X-numComps"),
77      X_contRep("X-contRep"), Filename, last_none;
78  
79      /**
80       * True String as Field
81       */
82      public final String value;
83  
84      SapField(final String value) {
85        this.value = value;
86      }
87  
88      SapField() {
89        value = name();
90      }
91  
92      @Override
93      public String toString() {
94        return value;
95      }
96    }
97  
98    /**
99     * Array of status: one entry by SapFunction, each entry has in that order
100    * UrlMandatory, UrlOptional,
101    * HeaderMandatory, HeaderOptional,BodyMandatory, BodyOptional,
102    * SecurityOptional. If one SapArg is in the
103    * array, it has the status associated with the rank. One SapArg can appears
104    * in multiple rank.
105    */
106   private static final SapField[][][] allStatus = {
107       // info
108       {
109           { SapField.contRep, SapField.docId, SapField.pVersion },
110           { SapField.compId, SapField.resultAs }, {}, {}, {}, {}, {
111           SapField.secKey, SapField.accessMode, SapField.authId,
112           SapField.expiration
113       }
114       },
115       // get
116       {
117           { SapField.contRep, SapField.docId, SapField.pVersion },
118           { SapField.compId, SapField.fromOffset, SapField.toOffset }, {}, {},
119           {}, {}, {
120           SapField.secKey, SapField.accessMode, SapField.authId,
121           SapField.expiration
122       }
123       },
124       // docGet (potential multiple get)
125       {
126           { SapField.contRep, SapField.docId, SapField.pVersion }, {}, {}, {},
127           {}, {}, {
128           SapField.secKey, SapField.accessMode, SapField.authId,
129           SapField.expiration
130       }
131       },
132       // createPost
133       {
134           { SapField.contRep, SapField.docId, SapField.pVersion },
135           { SapField.docProt }, { SapField.Content_Length }, {},
136           { SapField.compId, SapField.Content_Length }, {
137           SapField.Content_Type, SapField.charset, SapField.version
138       }, {
139           SapField.secKey, SapField.accessMode, SapField.authId,
140           SapField.expiration
141       }
142       },
143       // createPut
144       {
145           {
146               SapField.contRep, SapField.docId, SapField.compId,
147               SapField.pVersion
148           }, { SapField.docProt }, { SapField.Content_Length }, {}, {}, {
149           SapField.Content_Type, SapField.charset, SapField.version
150       }, {
151           SapField.secKey, SapField.accessMode, SapField.authId,
152           SapField.expiration
153       }
154       },
155       // mCreate
156       {
157           { SapField.contRep, SapField.docId, SapField.pVersion },
158           { SapField.docProt }, {}, {}, {
159           SapField.X_compId, SapField.X_docId, SapField.Content_Length
160       }, {
161           SapField.Content_Type, SapField.charset, SapField.version
162       }, {
163           SapField.secKey, SapField.accessMode, SapField.authId,
164           SapField.expiration
165       }
166       },
167       // append
168       {
169           {
170               SapField.contRep, SapField.docId, SapField.compId,
171               SapField.pVersion
172           }, {}, {}, {}, {}, {}, {
173           SapField.secKey, SapField.accessMode, SapField.authId,
174           SapField.expiration
175       }
176       },
177       // updatePost
178       {
179           { SapField.contRep, SapField.docId, SapField.pVersion }, {}, {}, {},
180           { SapField.compId, SapField.Content_Length }, {
181           SapField.Content_Type, SapField.charset, SapField.version
182       }, {
183           SapField.secKey, SapField.accessMode, SapField.authId,
184           SapField.expiration
185       }
186       },
187       // updatePut
188       {
189           {
190               SapField.contRep, SapField.docId, SapField.compId,
191               SapField.pVersion
192           }, {}, { SapField.Content_Length }, {}, {}, {
193           SapField.Content_Type, SapField.charset, SapField.version
194       }, {
195           SapField.secKey, SapField.accessMode, SapField.authId,
196           SapField.expiration
197       }
198       },
199       // delete
200       {
201           { SapField.contRep, SapField.docId, SapField.pVersion },
202           { SapField.compId }, {}, {}, {}, {}, {
203           SapField.secKey, SapField.accessMode, SapField.authId,
204           SapField.expiration
205       }
206       },
207   };
208 
209   public HttpSapBusinessFactory() {
210     // nothing
211   }
212 
213   public static HttpPageHandler initializeHttpPageHandler() {
214     // manual creation
215     final HashMap<String, HttpPage> pages = new HashMap<String, HttpPage>();
216     String pagename;
217     String header;
218     String footer;
219     String beginform;
220     String endform;
221     String nextinform;
222     String uri;
223     String errorpage;
224     final String classname;
225     PageRole pageRole;
226     LinkedHashMap<String, AbstractHttpField> linkedHashMap;
227 
228     try {
229       // Need as default error pages: 400, 401, 403, 404, 406, 500
230       final HttpPageHandler pageHandler = new HttpPageHandler(pages);
231       if (!HttpBusinessFactory.addDefaultErrorPages(pageHandler, "SAP ERROR",
232                                                     HttpSapBusinessFactory.class)) {
233         throw new IllegalAccessException("Cannot build default error pages");
234       }
235 
236       classname = HttpSapBusinessFactory.class.getName();
237 
238       // info
239       pageRole = PageRole.GETDOWNLOAD;
240       pagename = SapFunction.info.name();
241       uri = SAPLINK;
242       header = null;
243       footer = null;
244       beginform = null;
245       endform = null;
246       nextinform = null;
247       errorpage = "404";
248       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
249       addDefaultFields(linkedHashMap, allStatus[SapFunction.info.ordinal()]);
250       pages.put(uri,
251                 new HttpPage(pagename, null, header, footer, beginform, endform,
252                              nextinform, uri, pageRole, errorpage, classname,
253                              linkedHashMap));
254       // get
255       pageRole = PageRole.GETDOWNLOAD;
256       pagename = SapFunction.get.name();
257       uri = SAPLINK;
258       header = null;
259       footer = null;
260       beginform = null;
261       endform = null;
262       nextinform = null;
263       errorpage = "404";
264       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
265       addDefaultFields(linkedHashMap, allStatus[SapFunction.get.ordinal()]);
266       pages.put(uri,
267                 new HttpPage(pagename, null, header, footer, beginform, endform,
268                              nextinform, uri, pageRole, errorpage, classname,
269                              linkedHashMap));
270       // docGet
271       pageRole = PageRole.GETDOWNLOAD;
272       pagename = SapFunction.docGet.name();
273       uri = SAPLINK;
274       header = null;
275       footer = null;
276       beginform = null;
277       endform = null;
278       nextinform = null;
279       errorpage = "404";
280       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
281       addDefaultFields(linkedHashMap, allStatus[SapFunction.docGet.ordinal()]);
282       pages.put(uri,
283                 new HttpPage(pagename, null, header, footer, beginform, endform,
284                              nextinform, uri, pageRole, errorpage, classname,
285                              linkedHashMap));
286       // createPost
287       pageRole = PageRole.POSTUPLOAD;
288       pagename = SapFunction.createPost.name();
289       uri = SAPLINK;
290       header = null;
291       footer = null;
292       beginform = null;
293       endform = null;
294       nextinform = null;
295       errorpage = "404";
296       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
297       addDefaultFields(linkedHashMap,
298                        allStatus[SapFunction.createPost.ordinal()]);
299       pages.put(uri,
300                 new HttpPage(pagename, null, header, footer, beginform, endform,
301                              nextinform, uri, pageRole, errorpage, classname,
302                              linkedHashMap));
303       // createPut
304       pageRole = PageRole.PUT;
305       pagename = SapFunction.createPut.name();
306       uri = SAPLINK;
307       header = null;
308       footer = null;
309       beginform = null;
310       endform = null;
311       nextinform = null;
312       errorpage = "404";
313       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
314       addDefaultFields(linkedHashMap,
315                        allStatus[SapFunction.createPut.ordinal()]);
316       pages.put(uri,
317                 new HttpPage(pagename, null, header, footer, beginform, endform,
318                              nextinform, uri, pageRole, errorpage, classname,
319                              linkedHashMap));
320       // mCreate
321       pageRole = PageRole.POSTUPLOAD;
322       pagename = SapFunction.mCreate.name();
323       uri = SAPLINK;
324       header = null;
325       footer = null;
326       beginform = null;
327       endform = null;
328       nextinform = null;
329       errorpage = "404";
330       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
331       addDefaultFields(linkedHashMap, allStatus[SapFunction.mCreate.ordinal()]);
332       pages.put(uri,
333                 new HttpPage(pagename, null, header, footer, beginform, endform,
334                              nextinform, uri, pageRole, errorpage, classname,
335                              linkedHashMap));
336       // append
337       pageRole = PageRole.PUT;
338       pagename = SapFunction.append.name();
339       uri = SAPLINK;
340       header = null;
341       footer = null;
342       beginform = null;
343       endform = null;
344       nextinform = null;
345       errorpage = "404";
346       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
347       addDefaultFields(linkedHashMap, allStatus[SapFunction.append.ordinal()]);
348       pages.put(uri,
349                 new HttpPage(pagename, null, header, footer, beginform, endform,
350                              nextinform, uri, pageRole, errorpage, classname,
351                              linkedHashMap));
352       // updatePost
353       pageRole = PageRole.POSTUPLOAD;
354       pagename = SapFunction.updatePost.name();
355       uri = SAPLINK;
356       header = null;
357       footer = null;
358       beginform = null;
359       endform = null;
360       nextinform = null;
361       errorpage = "404";
362       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
363       addDefaultFields(linkedHashMap,
364                        allStatus[SapFunction.updatePost.ordinal()]);
365       pages.put(uri,
366                 new HttpPage(pagename, null, header, footer, beginform, endform,
367                              nextinform, uri, pageRole, errorpage, classname,
368                              linkedHashMap));
369       // updatePut
370       pageRole = PageRole.PUT;
371       pagename = SapFunction.updatePut.name();
372       uri = SAPLINK;
373       header = null;
374       footer = null;
375       beginform = null;
376       endform = null;
377       nextinform = null;
378       errorpage = "404";
379       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
380       addDefaultFields(linkedHashMap,
381                        allStatus[SapFunction.updatePut.ordinal()]);
382       pages.put(uri,
383                 new HttpPage(pagename, null, header, footer, beginform, endform,
384                              nextinform, uri, pageRole, errorpage, classname,
385                              linkedHashMap));
386       // delete
387       pageRole = PageRole.DELETE;
388       pagename = SapFunction.delete.name();
389       uri = SAPLINK;
390       header = null;
391       footer = null;
392       beginform = null;
393       endform = null;
394       nextinform = null;
395       errorpage = "404";
396       linkedHashMap = new LinkedHashMap<String, AbstractHttpField>();
397       addDefaultFields(linkedHashMap, allStatus[SapFunction.delete.ordinal()]);
398       pages.put(uri,
399                 new HttpPage(pagename, null, header, footer, beginform, endform,
400                              nextinform, uri, pageRole, errorpage, classname,
401                              linkedHashMap));
402     } catch (final ClassNotFoundException e) {
403       SysErrLogger.FAKE_LOGGER.syserr(e);
404     } catch (final InstantiationException e) {
405       SysErrLogger.FAKE_LOGGER.syserr(e);
406     } catch (final IllegalAccessException e) {
407       SysErrLogger.FAKE_LOGGER.syserr(e);
408     }
409     return new HttpPageHandler(pages);
410   }
411 
412   private static void addDefaultFields(
413       final LinkedHashMap<String, AbstractHttpField> linkedHashMap,
414       final SapField[][] fields) {
415     String fieldname;
416     String fieldinfo;
417     String fieldvalue;
418     FieldRole fieldRole;
419     boolean fieldvisibility;
420     boolean fieldmandatory;
421     boolean fieldcookieset;
422     int fieldrank;
423     /*
424      * UrlMandatory, UrlOptional, HeaderMandatory, HeaderOptional,BodyMandatory, BodyOptional, SecurityOptional.
425      */
426     int nb = 0;
427     int rank = 0;
428     for (int j = 0; j < fields[rank].length; j++) {
429       final SapField field = fields[rank][j];
430       fieldname = field.name();
431       fieldinfo = field.toString();
432       fieldvalue = null;
433       fieldRole = FieldRole.BUSINESS_INPUT_TEXT;
434       fieldvisibility = true;
435       fieldmandatory = true;
436       fieldcookieset = false;
437       fieldrank = ++nb;
438       linkedHashMap.put(fieldname,
439                         new DefaultHttpField(fieldname, fieldRole, fieldinfo,
440                                              fieldvalue, fieldvisibility,
441                                              fieldmandatory, fieldcookieset,
442                                              true, FieldPosition.URL,
443                                              fieldrank));
444     }
445     rank++;
446     for (int j = 0; j < fields[rank].length; j++) {
447       final SapField field = fields[rank][j];
448       fieldname = field.name();
449       fieldinfo = field.toString();
450       fieldvalue = null;
451       fieldRole = FieldRole.BUSINESS_INPUT_TEXT;
452       fieldvisibility = true;
453       fieldmandatory = false;
454       fieldcookieset = false;
455       fieldrank = ++nb;
456       linkedHashMap.put(fieldname,
457                         new DefaultHttpField(fieldname, fieldRole, fieldinfo,
458                                              fieldvalue, fieldvisibility,
459                                              fieldmandatory, fieldcookieset,
460                                              true, FieldPosition.URL,
461                                              fieldrank));
462     }
463     rank++;
464     for (int j = 0; j < fields[rank].length; j++) {
465       final SapField field = fields[rank][j];
466       fieldname = field.name();
467       fieldinfo = field.toString();
468       fieldvalue = null;
469       fieldRole = FieldRole.BUSINESS_INPUT_TEXT;
470       fieldvisibility = true;
471       fieldmandatory = true;
472       fieldcookieset = false;
473       fieldrank = ++nb;
474       linkedHashMap.put(fieldname,
475                         new DefaultHttpField(fieldname, fieldRole, fieldinfo,
476                                              fieldvalue, fieldvisibility,
477                                              fieldmandatory, fieldcookieset,
478                                              true, FieldPosition.HEADER,
479                                              fieldrank));
480     }
481     rank++;
482     for (int j = 0; j < fields[rank].length; j++) {
483       final SapField field = fields[rank][j];
484       fieldname = field.name();
485       fieldinfo = field.toString();
486       fieldvalue = null;
487       fieldRole = FieldRole.BUSINESS_INPUT_TEXT;
488       fieldvisibility = true;
489       fieldmandatory = false;
490       fieldcookieset = false;
491       fieldrank = ++nb;
492       linkedHashMap.put(fieldname,
493                         new DefaultHttpField(fieldname, fieldRole, fieldinfo,
494                                              fieldvalue, fieldvisibility,
495                                              fieldmandatory, fieldcookieset,
496                                              true, FieldPosition.HEADER,
497                                              fieldrank));
498     }
499     rank++;
500     for (int j = 0; j < fields[rank].length; j++) {
501       final SapField field = fields[rank][j];
502       fieldname = field.name();
503       fieldinfo = field.toString();
504       fieldvalue = null;
505       fieldRole = FieldRole.BUSINESS_INPUT_TEXT;
506       fieldvisibility = true;
507       fieldmandatory = true;
508       fieldcookieset = false;
509       fieldrank = ++nb;
510       linkedHashMap.put(fieldname,
511                         new DefaultHttpField(fieldname, fieldRole, fieldinfo,
512                                              fieldvalue, fieldvisibility,
513                                              fieldmandatory, fieldcookieset,
514                                              true, FieldPosition.BODY,
515                                              fieldrank));
516     }
517     rank++;
518     for (int j = 0; j < fields[rank].length; j++) {
519       final SapField field = fields[rank][j];
520       fieldname = field.name();
521       fieldinfo = field.toString();
522       fieldvalue = null;
523       fieldRole = FieldRole.BUSINESS_INPUT_TEXT;
524       fieldvisibility = true;
525       fieldmandatory = false;
526       fieldcookieset = false;
527       fieldrank = ++nb;
528       linkedHashMap.put(fieldname,
529                         new DefaultHttpField(fieldname, fieldRole, fieldinfo,
530                                              fieldvalue, fieldvisibility,
531                                              fieldmandatory, fieldcookieset,
532                                              true, FieldPosition.BODY,
533                                              fieldrank));
534     }
535     rank++;
536     for (int j = 0; j < fields[rank].length; j++) {
537       final SapField field = fields[rank][j];
538       fieldname = field.name();
539       fieldinfo = field.toString();
540       fieldvalue = null;
541       fieldRole = FieldRole.BUSINESS_INPUT_TEXT;
542       fieldvisibility = true;
543       fieldmandatory = false;
544       fieldcookieset = false;
545       fieldrank = ++nb;
546       linkedHashMap.put(fieldname,
547                         new DefaultHttpField(fieldname, fieldRole, fieldinfo,
548                                              fieldvalue, fieldvisibility,
549                                              fieldmandatory, fieldcookieset,
550                                              true, FieldPosition.ANY,
551                                              fieldrank));
552     }
553   }
554 
555 }