Mass whitespace changes (Style Warnings)
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / Display.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.gui;
23
24 import java.util.Enumeration;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.onap.aaf.auth.env.AuthzTrans;
30 import org.onap.aaf.auth.rserv.HttpCode;
31 import org.onap.aaf.auth.rserv.HttpMethods;
32 import org.onap.aaf.misc.env.Slot;
33
34 public class Display {
35     private final Page get;
36     public Display(final AAF_GUI gui, final HttpMethods meth, final Page page) {
37         get = page;
38         final String[] fields = page.fields();
39         final Slot slots[] = new Slot[fields.length];
40         String prefix = page.name() + '.';
41         for (int i=0;i<slots.length;++i) {
42             slots[i] = gui.env.slot(prefix + fields[i]);
43         }
44
45         /*
46          * We handle all the "Form POST" calls here with a naming convention that allows us to create arrays from strings.
47          * 
48          * On the HTTP side, elements concatenate their name with their Index number (if multiple).  In this code, 
49          * we turn such names into arrays with same index number.  Then, we place them in the Transaction "Properties" so that 
50          * it can be transferred to subclasses easily.
51          */ 
52         if (meth.equals(HttpMethods.POST)) {
53             // Here, we'll expect FORM URL Encoded Data, which we need to get from the body
54             gui.route(gui.env, meth, page.url(), 
55                 new HttpCode<AuthzTrans,AAF_GUI>(gui,page.name()) {
56                     @Override
57                     public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
58                         trans.put(gui.slot_httpServletRequest, req);
59                         for (int i=0; i<fields.length;++i) {
60                             int idx = fields[i].indexOf("[]");
61                             if (idx<0) { // single value
62                                 trans.put(slots[i], req.getParameter(fields[i])); // assume first value
63                             } else { // multi value - Expect Values to be set with Field root name "field.<int>" corresponding to an array of types
64                                 String field=fields[i].substring(0, idx)+'.';
65                                 String[] array = new String[16];
66                                 for (Enumeration<String> names = req.getParameterNames(); names.hasMoreElements();) {
67                                     String key = names.nextElement();
68                                     if (key.startsWith(field)) {
69                                         try {
70                                             int x = Integer.parseInt(key.substring(field.length()));
71                                             if (x>=array.length) {
72                                                 String[] temp = new String[x+10];
73                                                 System.arraycopy(temp, 0, temp, 0, array.length);
74                                                 array = temp;
75                                             }
76                                             array[x]=req.getParameter(key);
77                                         } catch (NumberFormatException e) {
78                                             trans.debug().log(e);
79                                         }
80                                     }
81                                 }
82                                 trans.put(slots[i], array);
83                             }
84                         }
85                         page.replay(context,trans,resp.getOutputStream(),"general");
86                     }
87                 }, "application/x-www-form-urlencoded","*/*");
88
89         } else {
90             // Transfer whether Page shouldn't be cached to local Final var.
91             final boolean no_cache = page.no_cache;
92             
93             gui.route(gui.env, meth, page.url(), 
94                 new HttpCode<AuthzTrans,AAF_GUI>(gui,page.name()) {
95                     @Override
96                     public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
97                         trans.put(gui.slot_httpServletRequest, req);
98                         for (int i=0; i<slots.length;++i) {
99                             int idx = fields[i].indexOf("[]");
100                             if (idx<0) { // single value
101                                 trans.put(slots[i], req.getParameter(fields[i]));
102                             } else { // multi value
103                                 String[] array = new String[30];
104                                 String field=fields[i].substring(0, idx);
105                                 
106                                 for (Enumeration<String> mm = req.getParameterNames();mm.hasMoreElements();) {
107                                     String key = mm.nextElement();
108                                     if (key.startsWith(field)) {
109                                         try {
110                                             int x = Integer.parseInt(key.substring(field.length()));
111                                             if (x>=array.length) {
112                                                 String[] temp = new String[x+10];
113                                                 System.arraycopy(temp, 0, temp, 0, array.length);
114                                                 array = temp;
115                                             }
116                                             array[x]=req.getParameter(key);
117                                         } catch (NumberFormatException e) {
118                                             trans.debug().log(e);
119                                         }
120                                     }
121                                 }
122                                 trans.put(slots[i], array);
123                             }
124                         }
125                         page.replay(context,trans,resp.getOutputStream(),"general");
126                     }
127                     
128                     @Override
129                     public boolean no_cache() {
130                         return no_cache;
131                     }
132                 }, "text/html","*/*");
133         }
134
135     }
136     
137     public Page page() { 
138         return get;
139     }
140 }