AT&T 2.0.19 Code drop, stage 4
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / ApiExample.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import java.io.IOException;
7
8 import com.att.authz.env.AuthzTrans;
9 import com.att.authz.gui.AuthGUI;
10 import com.att.authz.gui.BreadCrumbs;
11 import com.att.authz.gui.NamedCode;
12 import com.att.authz.gui.Page;
13 import org.onap.aaf.cadi.Symm;
14 import org.onap.aaf.cadi.client.Future;
15 import org.onap.aaf.inno.env.APIException;
16 import org.onap.aaf.inno.env.Data.TYPE;
17 import org.onap.aaf.inno.env.Env;
18 import org.onap.aaf.inno.env.TimeTaken;
19 import com.att.xgen.Cache;
20 import com.att.xgen.DynamicCode;
21 import com.att.xgen.Mark;
22 import com.att.xgen.html.HTMLGen;
23
24 import aaf.v2_0.Error;
25
26 /**
27  * Detail Page for Permissions
28  * 
29  *
30  */
31 public class ApiExample extends Page {
32         public static final String HREF = "/gui/example/:tc";
33         public static final String NAME = "APIExample";
34
35         public ApiExample(final AuthGUI gui, Page ... breadcrumbs) throws APIException, IOException {
36                 super(gui.env, NAME, HREF, 2/*backdots*/, new String[] {"API Code Example"},
37                                 new BreadCrumbs(breadcrumbs),
38                                 new Model()
39                                 );
40         }
41         
42         private static class Model extends NamedCode {
43                 private static final String WITH_OPTIONAL_PARAMETERS = "\n\n////////////\n  Data with Optional Parameters \n////////////\n\n";
44
45                 public Model() {
46                         super(false);
47                 }
48
49                 @Override
50                 public void code(Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
51                         Mark inner = xgen.divID("inner");
52                         xgen.divID("example","class=std");
53                         cache.dynamic(xgen, new DynamicCode<HTMLGen,AuthGUI,AuthzTrans>() {
54                                 @Override
55                                 public void code(final AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
56                                         TimeTaken tt = trans.start("Code Example",Env.REMOTE);
57                                         try {
58                                                 final String typecode;
59                                                 int prefix = trans.path().lastIndexOf('/')+1;
60                                                 String encoded = trans.path().substring(prefix);
61                                                 typecode = Symm.base64noSplit.decode(encoded);
62                                                 Future<String> fp = gui.client().read("/api/example/" + encoded,
63                                                                 "application/Void+json"
64                                                                 );
65                                                 Future<String> fs2;
66                                                 if(typecode.contains("Request+")) {
67                                                         fs2 = gui.client().read("/api/example/" + typecode+"?optional=true",
68                                                                         "application/Void+json"
69                                                                         );
70                                                 } else {
71                                                         fs2=null;
72                                                 }
73                                                 
74                                                 
75                                                 if(fp.get(5000)) {
76                                                                 xgen.incr(HTMLGen.H1).text("Sample Code").end()
77                                                                 .incr(HTMLGen.H5).text(typecode).end();
78                                                                 xgen.incr("pre");
79                                                                 if(typecode.contains("+xml")) {
80                                                                         xgen.xml(fp.body());
81                                                                         if(fs2!=null && fs2.get(5000)) {
82                                                                                 xgen.text(WITH_OPTIONAL_PARAMETERS);
83                                                                                 xgen.xml(fs2.body());
84                                                                         }
85                                                                 } else {
86                                                                         xgen.text(fp.body());
87                                                                         if(fs2!=null && fs2.get(5000)) {
88                                                                                 xgen.text(WITH_OPTIONAL_PARAMETERS);
89                                                                                 xgen.text(fs2.body());
90                                                                         }
91                                                                 }
92                                                                 xgen.end();
93                                                 } else {
94                                                         Error err = gui.errDF.newData().in(TYPE.JSON).load(fp.body()).asObject();
95                                                         xgen.incr(HTMLGen.H3)
96                                                                 .textCR(2,"Error from AAF Service")
97                                                                 .end();
98                                                         
99                                                         xgen.p("Error Code: ",err.getMessageId())
100                                                                 .p(err.getText())
101                                                                 .end();
102                                                                 
103                                                 }
104
105                                         } catch (APIException e) {
106                                                 throw e;
107                                         } catch (IOException e) {
108                                                 throw e;
109                                         } catch (Exception e) {
110                                                 throw new APIException(e);
111                                         }finally {
112                                                 tt.done();
113                                         }
114                                 }
115                                         
116                         });
117                         xgen.end(inner);
118                 }
119         }
120
121 }               
122