7da77607676d62045f143e395dce88b14fed238d
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / ApiExample.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.pages;
23
24 import java.io.IOException;
25
26 import org.onap.aaf.auth.env.AuthzTrans;
27 import org.onap.aaf.auth.gui.AAF_GUI;
28 import org.onap.aaf.auth.gui.BreadCrumbs;
29 import org.onap.aaf.auth.gui.NamedCode;
30 import org.onap.aaf.auth.gui.Page;
31 import org.onap.aaf.cadi.Symm;
32 import org.onap.aaf.cadi.client.Future;
33 import org.onap.aaf.misc.env.APIException;
34 import org.onap.aaf.misc.env.Env;
35 import org.onap.aaf.misc.env.TimeTaken;
36 import org.onap.aaf.misc.xgen.Cache;
37 import org.onap.aaf.misc.xgen.DynamicCode;
38 import org.onap.aaf.misc.xgen.Mark;
39 import org.onap.aaf.misc.xgen.html.HTMLGen;
40
41 /**
42  * Detail Page for Permissions
43  * 
44  * @author Jonathan
45  *
46  */
47 public class ApiExample extends Page {
48     public static final String HREF = "/gui/example/:tc";
49     public static final String NAME = "APIExample";
50
51     public ApiExample(final AAF_GUI gui, Page ... breadcrumbs) throws APIException, IOException {
52         super(gui.env, NAME, HREF, 2/*backdots*/, new String[] {"API Code Example"},
53                 new BreadCrumbs(breadcrumbs),
54                 new Model(NAME)
55                 );
56     }
57     
58     private static class Model extends NamedCode {
59         private static final String WITH_OPTIONAL_PARAMETERS = "\n\n////////////\n  Data with Optional Parameters \n////////////\n\n";
60
61         public Model(String name) {
62             super(false,name);
63         }
64
65         @Override
66         public void code(Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
67             Mark inner = xgen.divID("inner");
68             xgen.divID("example","class=std");
69             cache.dynamic(xgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
70                 @Override
71                 public void code(final AAF_GUI gui, final AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
72                     TimeTaken tt = trans.start("Code Example",Env.REMOTE);
73                     try {
74                         final String typecode;
75                         int prefix = trans.path().lastIndexOf('/')+1;
76                         String encoded = trans.path().substring(prefix);
77                         typecode = Symm.base64noSplit.decode(encoded);
78                         Future<String> fp = gui.client().read("/api/example/" + encoded,
79                                 "application/Void+json"
80                                 );
81                         Future<String> fs2;
82                         if (typecode.contains("Request+")) {
83                             fs2 = gui.client().read("/api/example/" + encoded+"?optional=true",
84                                     "application/Void+json"
85                                     );
86                         } else {
87                             fs2=null;
88                         }
89                         
90                         
91                         if (fp.get(5000)) {
92                                 xgen.incr(HTMLGen.H1).text("Sample Code").end()
93                                 .incr(HTMLGen.H5).text(typecode).end();
94                                 xgen.incr("pre");
95                                 if (typecode.contains("+xml")) {
96                                     xgen.xml(fp.body());
97                                     if (fs2!=null && fs2.get(5000)) {
98                                         xgen.text(WITH_OPTIONAL_PARAMETERS);
99                                         xgen.xml(fs2.body());
100                                     }
101                                 } else {
102                                     xgen.text(fp.body());
103                                     if (fs2!=null && fs2.get(5000)) {
104                                         xgen.text(WITH_OPTIONAL_PARAMETERS);
105                                         xgen.text(fs2.body());
106                                     }
107                                 }
108                                 xgen.end();
109                         } else {
110                             xgen.incr(HTMLGen.H3)
111                                 .textCR(2,"Error from AAF Service")
112                                 .end();
113                             gui.writeError(trans, fp, xgen, 0);
114                         }
115
116                     } catch (APIException e) {
117                         throw e;
118                     } catch (IOException e) {
119                         throw e;
120                     } catch (Exception e) {
121                         throw new APIException(e);
122                     }finally {
123                         tt.done();
124                     }
125                 }
126                     
127             });
128             xgen.end(inner);
129         }
130     }
131
132 }        
133