2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.config;
24 import java.io.ByteArrayOutputStream;
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;
34 import org.onap.aaf.cadi.AbsUserCache;
35 import org.onap.aaf.cadi.lur.LocalLur;
37 public class UsersDump {
42 public static boolean write(OutputStream os, AbsUserCache<?> lur) {
44 if (os instanceof PrintStream) {
47 ps = new PrintStream(os);
50 ps.println("<?xml version='1.0' encoding='utf-8'?>");
52 ps.print( " Code Generated Tomcat Users and Roles from AT&T LUR on ");
53 ps.println(new Date());
55 ps.println("<tomcat-users>");
57 // We loop through Users, but want to write Groups first... therefore, save off print
58 StringBuilder sb = new StringBuilder();
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=\"");
65 sb.append("\" roles=\"");
67 for (String role : di.perms) {
69 if (first)first = false;
78 for (String group : groups) {
79 ps.print(" <role rolename=\"");
86 ps.println("</tomcat-users>");
88 } catch (Exception t) {
89 t.printStackTrace(ps);
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.
105 public static String updateUsers(String writeto, LocalLur up) {
106 // Dump a Tomcat-user.xml lookalike (anywhere)
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);
117 FileInputStream fis = new FileInputStream(file);
118 byte[] orig = new byte[(int)file.length()];
121 read = fis.read(orig);
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;
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;
138 } catch (Exception e) {
147 FileOutputStream fos = new FileOutputStream(file);
149 fos.write(postulate);
153 } catch (IOException e) {
154 return e.getMessage();
159 return null; // no message means ok.