1 /*******************************************************************************
2 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3 *******************************************************************************/
4 package com.att.authz.gui;
6 import java.util.Enumeration;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
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;
16 public class Display {
17 private final Page get;
18 public Display(final AuthGUI gui, final HttpMethods meth, final Page 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]);
28 * We handle all the "Form POST" calls here with a naming convention that allows us to create arrays from strings.
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.
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()) {
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)) {
52 int x = Integer.parseInt(key.substring(field.length()));
54 String[] temp = new String[x+10];
55 System.arraycopy(temp, 0, temp, 0, array.length);
58 array[x]=req.getParameter(key);
59 } catch (NumberFormatException e) {
64 trans.put(slots[i], array);
67 page.replay(context,trans,resp.getOutputStream(),"general");
69 }, "application/x-www-form-urlencoded","*/*");
72 // Transfer whether Page shouldn't be cached to local Final var.
73 final boolean no_cache = page.no_cache;
75 gui.route(gui.env, meth, page.url(),
76 new HttpCode<AuthzTrans,AuthGUI>(gui,page.name()) {
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);
88 for(Enumeration<String> mm = req.getParameterNames();mm.hasMoreElements();) {
89 String key = mm.nextElement();
90 if(key.startsWith(field)) {
92 int x = Integer.parseInt(key.substring(field.length()));
94 String[] temp = new String[x+10];
95 System.arraycopy(temp, 0, temp, 0, array.length);
98 array[x]=req.getParameter(key);
99 } catch (NumberFormatException e) {
100 trans.debug().log(e);
104 trans.put(slots[i], array);
107 page.replay(context,trans,resp.getOutputStream(),"general");
111 public boolean no_cache() {
114 }, "text/html","*/*");