2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.gui;
24 import java.util.Enumeration;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
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;
34 public class Display {
35 private final Page get;
36 public Display(final AAF_GUI gui, final HttpMethods meth, final Page 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]);
46 * We handle all the "Form POST" calls here with a naming convention that allows us to create arrays from strings.
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.
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()) {
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)) {
70 int x = Integer.parseInt(key.substring(field.length()));
72 String[] temp = new String[x+10];
73 System.arraycopy(temp, 0, temp, 0, array.length);
76 array[x]=req.getParameter(key);
77 } catch (NumberFormatException e) {
82 trans.put(slots[i], array);
85 page.replay(context,trans,resp.getOutputStream(),"general");
87 }, "application/x-www-form-urlencoded","*/*");
90 // Transfer whether Page shouldn't be cached to local Final var.
91 final boolean no_cache = page.no_cache;
93 gui.route(gui.env, meth, page.url(),
94 new HttpCode<AuthzTrans,AAF_GUI>(gui,page.name()) {
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);
106 for(Enumeration<String> mm = req.getParameterNames();mm.hasMoreElements();) {
107 String key = mm.nextElement();
108 if(key.startsWith(field)) {
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);
116 array[x]=req.getParameter(key);
117 } catch (NumberFormatException e) {
118 trans.debug().log(e);
122 trans.put(slots[i], array);
125 page.replay(context,trans,resp.getOutputStream(),"general");
129 public boolean no_cache() {
132 }, "text/html","*/*");