a3e6a64ff64c772b9fa035bd11d1f5327210450a
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / Display.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui;
5
6 import java.util.Enumeration;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import com.att.authz.env.AuthzTrans;
12 import com.att.cssa.rserv.HttpCode;
13 import com.att.cssa.rserv.HttpMethods;
14 import org.onap.aaf.inno.env.Slot;
15
16 public class Display {
17         private final Page get;
18         public Display(final AuthGUI gui, final HttpMethods meth, final Page page) {
19                 get = page;
20                 final String[] fields = page.fields();
21                 final Slot slots[] = new Slot[fields.length];
22                 String prefix = page.name() + '.';
23                 for(int i=0;i<slots.length;++i) {
24                         slots[i] = gui.env.slot(prefix + fields[i]);
25                 }
26
27                 /*
28                  * We handle all the "Form POST" calls here with a naming convention that allows us to create arrays from strings.
29                  * 
30                  * On the HTTP side, elements concatenate their name with their Index number (if multiple).  In this code, 
31                  * we turn such names into arrays with same index number.  Then, we place them in the Transaction "Properties" so that 
32                  * it can be transferred to subclasses easily.
33                  */ 
34                 if(meth.equals(HttpMethods.POST)) {
35                         // Here, we'll expect FORM URL Encoded Data, which we need to get from the body
36                         gui.route(gui.env, meth, page.url(), 
37                                 new HttpCode<AuthzTrans,AuthGUI>(gui,page.name()) {
38                                         @Override
39                                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
40                                                 trans.put(gui.slot_httpServletRequest, req);
41                                                 for(int i=0; i<fields.length;++i) {
42                                                         int idx = fields[i].indexOf("[]");
43                                                         if(idx<0) { // single value
44                                                                 trans.put(slots[i], req.getParameter(fields[i])); // assume first value
45                                                         } else { // multi value
46                                                                 String field=fields[i].substring(0, idx);
47                                                                 String[] array = new String[30];
48                                                                 for(Enumeration<String> names = req.getParameterNames(); names.hasMoreElements();) {
49                                                                         String key = names.nextElement();
50                                                                         if(key.subSequence(0, idx).equals(field)) {
51                                                                                 try {
52                                                                                         int x = Integer.parseInt(key.substring(field.length()));
53                                                                                         if(x>=array.length) {
54                                                                                                 String[] temp = new String[x+10];
55                                                                                                 System.arraycopy(temp, 0, temp, 0, array.length);
56                                                                                                 array = temp;
57                                                                                         }
58                                                                                         array[x]=req.getParameter(key);
59                                                                                 } catch (NumberFormatException e) {
60                                                                                         trans.debug().log(e);
61                                                                                 }
62                                                                         }
63                                                                 }
64                                                                 trans.put(slots[i], array);
65                                                         }
66                                                 }
67                                                 page.replay(context,trans,resp.getOutputStream(),"general");
68                                         }
69                                 }, "application/x-www-form-urlencoded","*/*");
70
71                 } else {
72                         // Transfer whether Page shouldn't be cached to local Final var.
73                         final boolean no_cache = page.no_cache;
74                         
75                         gui.route(gui.env, meth, page.url(), 
76                                 new HttpCode<AuthzTrans,AuthGUI>(gui,page.name()) {
77                                         @Override
78                                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
79                                                 trans.put(gui.slot_httpServletRequest, req);
80                                                 for(int i=0; i<slots.length;++i) {
81                                                         int idx = fields[i].indexOf("[]");
82                                                         if(idx<0) { // single value
83                                                                 trans.put(slots[i], req.getParameter(fields[i]));
84                                                         } else { // multi value
85                                                                 String[] array = new String[30];
86                                                                 String field=fields[i].substring(0, idx);
87                                                                 
88                                                                 for(Enumeration<String> mm = req.getParameterNames();mm.hasMoreElements();) {
89                                                                         String key = mm.nextElement();
90                                                                         if(key.startsWith(field)) {
91                                                                                 try {
92                                                                                         int x = Integer.parseInt(key.substring(field.length()));
93                                                                                         if(x>=array.length) {
94                                                                                                 String[] temp = new String[x+10];
95                                                                                                 System.arraycopy(temp, 0, temp, 0, array.length);
96                                                                                                 array = temp;
97                                                                                         }
98                                                                                         array[x]=req.getParameter(key);
99                                                                                 } catch (NumberFormatException e) {
100                                                                                         trans.debug().log(e);
101                                                                                 }
102                                                                         }
103                                                                 }
104                                                                 trans.put(slots[i], array);
105                                                         }
106                                                 }
107                                                 page.replay(context,trans,resp.getOutputStream(),"general");
108                                         }
109                                         
110                                         @Override
111                                         public boolean no_cache() {
112                                                 return no_cache;
113                                         }
114                                 }, "text/html","*/*");
115                 }
116
117         }
118         
119         public Page page() { 
120                 return get;
121         }
122 }