Sonar Fixes
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / BreadCrumbs.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 static org.onap.aaf.misc.xgen.html.HTMLGen.A;
25 import static org.onap.aaf.misc.xgen.html.HTMLGen.LI;
26 import static org.onap.aaf.misc.xgen.html.HTMLGen.UL;
27
28 import java.io.IOException;
29
30 import javax.servlet.http.HttpServletRequest;
31
32 import org.onap.aaf.misc.env.APIException;
33 import org.onap.aaf.misc.env.TransStore;
34 import org.onap.aaf.misc.xgen.Cache;
35 import org.onap.aaf.misc.xgen.DynamicCode;
36 import org.onap.aaf.misc.xgen.Mark;
37 import org.onap.aaf.misc.xgen.html.HTMLGen;
38
39 public class BreadCrumbs extends NamedCode {
40     Page[] breadcrumbs;
41
42     public BreadCrumbs(Page ... pages) {
43         super(false,"breadcrumbs");
44         breadcrumbs = pages;
45     }
46
47     @Override
48     public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
49         // BreadCrumbs
50         Mark mark = new Mark();
51         hgen.incr(mark, UL);
52             cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, TransStore>() {
53                 @Override
54                 public void code(AAF_GUI gui, TransStore trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
55                     HttpServletRequest req = trans.get(gui.slotHttpServletRequest, null);
56                     StringBuilder key = new StringBuilder();
57                     String value;
58                     String hidden;
59                     for (Page p : breadcrumbs) {
60                         hidden="";
61                         // Add keys for page from commandline, where possible.
62                         if (p.fields().length>0) {
63                             boolean first = true;
64                             key.setLength(0);
65                             for (String field : p.fields()) {
66                                 if ((value=req.getParameter(field))==null) {
67                                     hidden="style=display:none;";
68                                     break;
69                                 }
70                                 if (first) {
71                                     first = false;
72                                     key.append('?');
73                                 } else {
74                                     key.append("&amp;");
75                                 }
76                                 key.append(field);
77                                 key.append('=');
78                                 key.append(value);
79                             }
80                             hgen.incr(LI,true,hidden);
81                             hgen.leaf(A,"href="+p.url()+key.toString(),hidden).text(p.name()).end(2);
82                         } else {
83                             hgen.incr(LI,true);
84                             hgen.leaf(A,"href="+p.url(),hidden).text(p.name()).end(2);
85                         }
86                     }
87                 }
88             });
89         hgen.end(mark);
90     }
91 }