[AAF-21] Initial code import
[aaf/cadi.git] / core / src / main / java / com / att / cadi / config / UsersDump.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.config;\r
25 \r
26 import java.io.ByteArrayOutputStream;\r
27 import java.io.File;\r
28 import java.io.FileInputStream;\r
29 import java.io.FileOutputStream;\r
30 import java.io.IOException;\r
31 import java.io.OutputStream;\r
32 import java.io.PrintStream;\r
33 import java.util.Date;\r
34 import java.util.HashSet;\r
35 \r
36 import com.att.cadi.AbsUserCache;\r
37 import com.att.cadi.lur.LocalLur;\r
38 \r
39 public class UsersDump {\r
40 \r
41         /**\r
42          * @param args\r
43          */\r
44         public static boolean write(OutputStream os, AbsUserCache<?> lur) {\r
45                 PrintStream ps;\r
46                 if(os instanceof PrintStream) {\r
47                         ps = (PrintStream)os;\r
48                 } else {\r
49                         ps = new PrintStream(os);\r
50                 }\r
51                 try {\r
52                         ps.println("<?xml version='1.0' encoding='utf-8'?>");\r
53                         ps.println("<!--");\r
54                         ps.print(  "     Code Generated Tomcat Users and Roles from AT&T LUR on ");\r
55                         ps.println(new Date());\r
56                         ps.println(  "-->");\r
57                         ps.println("<tomcat-users>");\r
58 \r
59                         // We loop through Users, but want to write Groups first... therefore, save off print\r
60                         StringBuilder sb = new StringBuilder();\r
61                         \r
62                         // Obtain all unique role names\r
63                         HashSet<String> groups = new HashSet<String>();\r
64                         for(AbsUserCache<?>.DumpInfo di : lur.dumpInfo()) {\r
65                                 sb.append("\n  <user username=\"");\r
66                                 sb.append(di.user);\r
67                                 sb.append("\" roles=\"");\r
68                                 boolean first = true;\r
69                                 for(String role : di.perms) {\r
70                                         groups.add(role);\r
71                                         if(first)first = false;\r
72                                         else sb.append(',');\r
73                                         sb.append(role);\r
74                                 }\r
75                                 sb.append("\"/>");\r
76 \r
77                         }\r
78 \r
79                         // Print roles\r
80                         for(String group : groups) {\r
81                                 ps.print("  <role rolename=\"");\r
82                                 ps.print(group);\r
83                                 ps.println("\"/>");\r
84                         }\r
85         \r
86                         ps.println(sb);\r
87 \r
88                         ps.println("</tomcat-users>");\r
89                         ps.flush();\r
90                 } catch (Throwable t) {\r
91                         t.printStackTrace(ps);\r
92                         return false;\r
93                 }\r
94                 return true;\r
95         }\r
96         \r
97         /**\r
98          * \r
99          * Note: This method returns a String if there's an error, or null if ok.\r
100          * This unusual style is necessitated by the fact that any Exceptions thrown are likely to \r
101          * be unlogged and hidden from view, making debugging almost impossible.\r
102          * \r
103          * @param writeto\r
104          * @param up\r
105          * @return\r
106          */\r
107         public static String updateUsers(String writeto, LocalLur up) {\r
108                 // Dump a Tomcat-user.xml lookalike (anywhere)\r
109                 if(writeto!=null) {\r
110                         // First read content\r
111                         ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
112                         if(UsersDump.write(baos, up)) {\r
113                                 byte[] postulate = baos.toByteArray();\r
114                                 // now get contents of file\r
115                                 File file = new File(writeto);\r
116                                 boolean writeIt;\r
117                                 if(file.exists()) {\r
118                                         try {\r
119                                                 FileInputStream fis = new FileInputStream(file);\r
120                                                 byte[] orig = new byte[(int)file.length()];\r
121                                                 try {\r
122                                                         fis.read(orig);\r
123                                                 } finally {\r
124                                                         fis.close();\r
125                                                 }\r
126                                                 // Starting at third "<" (<tomcat-users> line)\r
127                                                 int startA=0, startB=0;\r
128                                                 for(int i=0;startA<orig.length && i<3;++startA) if(orig[startA]=='<')++i;\r
129                                                 for(int i=0;startB<orig.length && i<3;++startB) if(postulate[startB]=='<')++i;\r
130                                                 \r
131                                                 writeIt=orig.length-startA!=postulate.length-startB; // first, check if remaining length is the same\r
132                                                 while(!writeIt && startA<orig.length && startB<postulate.length) {\r
133                                                         if(orig[startA++]!=postulate[startB++])writeIt = true;\r
134                                                 }\r
135                                         } catch (Exception e) {\r
136                                                 writeIt = true;\r
137                                         }\r
138                                 } else {\r
139                                         writeIt = true;\r
140                                 }\r
141                                 \r
142                                 if(writeIt) {\r
143                                         try {\r
144                                                 FileOutputStream fos = new FileOutputStream(file);\r
145                                                 try {\r
146                                                         fos.write(postulate);\r
147                                                 } finally {\r
148                                                         fos.close();\r
149                                                 }\r
150                                         } catch (IOException e) {\r
151                                                 return e.getMessage();\r
152                                         }\r
153                                 }\r
154                         }\r
155                 }\r
156                 return null; // no message means ok.\r
157         }\r
158 \r
159 }\r