1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi.config;
\r
25 import java.io.ByteArrayOutputStream;
\r
26 import java.io.File;
\r
27 import java.io.FileInputStream;
\r
28 import java.io.FileOutputStream;
\r
29 import java.io.IOException;
\r
30 import java.io.OutputStream;
\r
31 import java.io.PrintStream;
\r
32 import java.util.Date;
\r
33 import java.util.HashSet;
\r
35 import org.onap.aaf.cadi.AbsUserCache;
\r
36 import org.onap.aaf.cadi.lur.LocalLur;
\r
38 public class UsersDump {
\r
43 public static boolean write(OutputStream os, AbsUserCache<?> lur) {
\r
45 if(os instanceof PrintStream) {
\r
46 ps = (PrintStream)os;
\r
48 ps = new PrintStream(os);
\r
51 ps.println("<?xml version='1.0' encoding='utf-8'?>");
\r
53 ps.print( " Code Generated Tomcat Users and Roles from AT&T LUR on ");
\r
54 ps.println(new Date());
\r
56 ps.println("<tomcat-users>");
\r
58 // We loop through Users, but want to write Groups first... therefore, save off print
\r
59 StringBuilder sb = new StringBuilder();
\r
61 // Obtain all unique role names
\r
62 HashSet<String> groups = new HashSet<String>();
\r
63 for(AbsUserCache<?>.DumpInfo di : lur.dumpInfo()) {
\r
64 sb.append("\n <user username=\"");
\r
66 sb.append("\" roles=\"");
\r
67 boolean first = true;
\r
68 for(String role : di.perms) {
\r
70 if(first)first = false;
\r
71 else sb.append(',');
\r
79 for(String group : groups) {
\r
80 ps.print(" <role rolename=\"");
\r
87 ps.println("</tomcat-users>");
\r
89 } catch (Throwable t) {
\r
90 t.printStackTrace(ps);
\r
98 * Note: This method returns a String if there's an error, or null if ok.
\r
99 * This unusual style is necessitated by the fact that any Exceptions thrown are likely to
\r
100 * be unlogged and hidden from view, making debugging almost impossible.
\r
106 public static String updateUsers(String writeto, LocalLur up) {
\r
107 // Dump a Tomcat-user.xml lookalike (anywhere)
\r
108 if(writeto!=null) {
\r
109 // First read content
\r
110 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
111 if(UsersDump.write(baos, up)) {
\r
112 byte[] postulate = baos.toByteArray();
\r
113 // now get contents of file
\r
114 File file = new File(writeto);
\r
116 if(file.exists()) {
\r
118 FileInputStream fis = new FileInputStream(file);
\r
119 byte[] orig = new byte[(int)file.length()];
\r
125 // Starting at third "<" (<tomcat-users> line)
\r
126 int startA=0, startB=0;
\r
127 for(int i=0;startA<orig.length && i<3;++startA) if(orig[startA]=='<')++i;
\r
128 for(int i=0;startB<orig.length && i<3;++startB) if(postulate[startB]=='<')++i;
\r
130 writeIt=orig.length-startA!=postulate.length-startB; // first, check if remaining length is the same
\r
131 while(!writeIt && startA<orig.length && startB<postulate.length) {
\r
132 if(orig[startA++]!=postulate[startB++])writeIt = true;
\r
134 } catch (Exception e) {
\r
143 FileOutputStream fos = new FileOutputStream(file);
\r
145 fos.write(postulate);
\r
149 } catch (IOException e) {
\r
150 return e.getMessage();
\r
155 return null; // no message means ok.
\r