1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
12 * * http://www.apache.org/licenses/LICENSE-2.0
\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
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cadi.config;
\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
36 import com.att.cadi.AbsUserCache;
\r
37 import com.att.cadi.lur.LocalLur;
\r
39 public class UsersDump {
\r
44 public static boolean write(OutputStream os, AbsUserCache<?> lur) {
\r
46 if(os instanceof PrintStream) {
\r
47 ps = (PrintStream)os;
\r
49 ps = new PrintStream(os);
\r
52 ps.println("<?xml version='1.0' encoding='utf-8'?>");
\r
54 ps.print( " Code Generated Tomcat Users and Roles from AT&T LUR on ");
\r
55 ps.println(new Date());
\r
57 ps.println("<tomcat-users>");
\r
59 // We loop through Users, but want to write Groups first... therefore, save off print
\r
60 StringBuilder sb = new StringBuilder();
\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
67 sb.append("\" roles=\"");
\r
68 boolean first = true;
\r
69 for(String role : di.perms) {
\r
71 if(first)first = false;
\r
72 else sb.append(',');
\r
80 for(String group : groups) {
\r
81 ps.print(" <role rolename=\"");
\r
88 ps.println("</tomcat-users>");
\r
90 } catch (Throwable t) {
\r
91 t.printStackTrace(ps);
\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
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
117 if(file.exists()) {
\r
119 FileInputStream fis = new FileInputStream(file);
\r
120 byte[] orig = new byte[(int)file.length()];
\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
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
135 } catch (Exception e) {
\r
144 FileOutputStream fos = new FileOutputStream(file);
\r
146 fos.write(postulate);
\r
150 } catch (IOException e) {
\r
151 return e.getMessage();
\r
156 return null; // no message means ok.
\r