Collection syntax change because of Sonar
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / config / UsersDump.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.cadi.config;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.io.PrintStream;
31 import java.util.Date;
32 import java.util.HashSet;
33
34 import org.onap.aaf.cadi.AbsUserCache;
35 import org.onap.aaf.cadi.lur.LocalLur;
36
37 public class UsersDump {
38
39         /**
40          * @param args
41          */
42         public static boolean write(OutputStream os, AbsUserCache<?> lur) {
43                 PrintStream ps;
44                 if(os instanceof PrintStream) {
45                         ps = (PrintStream)os;
46                 } else {
47                         ps = new PrintStream(os);
48                 }
49                 try {
50                         ps.println("<?xml version='1.0' encoding='utf-8'?>");
51                         ps.println("<!--");
52                         ps.print(  "     Code Generated Tomcat Users and Roles from AT&T LUR on ");
53                         ps.println(new Date());
54                         ps.println(  "-->");
55                         ps.println("<tomcat-users>");
56
57                         // We loop through Users, but want to write Groups first... therefore, save off print
58                         StringBuilder sb = new StringBuilder();
59                         
60                         // Obtain all unique role names
61                         HashSet<String> groups = new HashSet<>();
62                         for(AbsUserCache<?>.DumpInfo di : lur.dumpInfo()) {
63                                 sb.append("\n  <user username=\"");
64                                 sb.append(di.user);
65                                 sb.append("\" roles=\"");
66                                 boolean first = true;
67                                 for(String role : di.perms) {
68                                         groups.add(role);
69                                         if(first)first = false;
70                                         else sb.append(',');
71                                         sb.append(role);
72                                 }
73                                 sb.append("\"/>");
74
75                         }
76
77                         // Print roles
78                         for(String group : groups) {
79                                 ps.print("  <role rolename=\"");
80                                 ps.print(group);
81                                 ps.println("\"/>");
82                         }
83         
84                         ps.println(sb);
85
86                         ps.println("</tomcat-users>");
87                         ps.flush();
88                 } catch (Throwable t) {
89                         t.printStackTrace(ps);
90                         return false;
91                 }
92                 return true;
93         }
94         
95         /**
96          * 
97          * Note: This method returns a String if there's an error, or null if ok.
98          * This unusual style is necessitated by the fact that any Exceptions thrown are likely to 
99          * be unlogged and hidden from view, making debugging almost impossible.
100          * 
101          * @param writeto
102          * @param up
103          * @return
104          */
105         public static String updateUsers(String writeto, LocalLur up) {
106                 // Dump a Tomcat-user.xml lookalike (anywhere)
107                 if(writeto!=null) {
108                         // First read content
109                         ByteArrayOutputStream baos = new ByteArrayOutputStream();
110                         if(UsersDump.write(baos, up)) {
111                                 byte[] postulate = baos.toByteArray();
112                                 // now get contents of file
113                                 File file = new File(writeto);
114                                 boolean writeIt;
115                                 if(file.exists()) {
116                                         try {
117                                                 FileInputStream fis = new FileInputStream(file);
118                                                 byte[] orig = new byte[(int)file.length()];
119                                                 int read;
120                                                 try {
121                                                         read = fis.read(orig);
122                                                 } finally {
123                                                         fis.close();
124                                                 }
125                                                 if(read<=0) {
126                                                         writeIt = false;
127                                                 } else {
128                                                         // Starting at third "<" (<tomcat-users> line)
129                                                         int startA=0, startB=0;
130                                                         for(int i=0;startA<orig.length && i<3;++startA) if(orig[startA]=='<')++i;
131                                                         for(int i=0;startB<orig.length && i<3;++startB) if(postulate[startB]=='<')++i;
132                                                         
133                                                         writeIt=orig.length-startA!=postulate.length-startB; // first, check if remaining length is the same
134                                                         while(!writeIt && startA<orig.length && startB<postulate.length) {
135                                                                 if(orig[startA++]!=postulate[startB++])writeIt = true;
136                                                         }
137                                                 }
138                                         } catch (Exception e) {
139                                                 writeIt = true;
140                                         }
141                                 } else {
142                                         writeIt = true;
143                                 }
144                                 
145                                 if(writeIt) {
146                                         try {
147                                                 FileOutputStream fos = new FileOutputStream(file);
148                                                 try {
149                                                         fos.write(postulate);
150                                                 } finally {
151                                                         fos.close();
152                                                 }
153                                         } catch (IOException e) {
154                                                 return e.getMessage();
155                                         }
156                                 }
157                         }
158                 }
159                 return null; // no message means ok.
160         }
161
162 }