1 16 package org.jahia.services.webapps_deployer.tomcat; 17 18 import org.jahia.data.xml.JahiaXmlDocument; 19 import org.jahia.exceptions.JahiaException; 20 import org.jahia.utils.xml.XMLParser; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 24 import java.util.Vector ; 25 26 39 public class Tomcat_Users_Xml extends JahiaXmlDocument { 40 41 private static org.apache.log4j.Logger logger = 42 org.apache.log4j.Logger.getLogger (Tomcat_Users_Xml.class); 43 44 45 private Vector m_UserNodes = new Vector (); 46 47 52 public Tomcat_Users_Xml (String docPath) 53 throws JahiaException { 54 super (docPath); 55 extractDocumentData (); 56 57 } 58 59 63 public void extractDocumentData () 64 throws JahiaException { 65 66 logger.debug ("started "); 67 68 if (m_XMLDocument == null) { 69 70 throw new JahiaException ("Tomcat_Users_Xml", 71 "Parsed Tomcat-users.xml document is null", 72 JahiaException.ERROR_SEVERITY, 73 JahiaException.SERVICE_ERROR); 74 } 75 76 if (!m_XMLDocument.hasChildNodes ()) { 77 78 throw new JahiaException ("Tomcat_Users_Xml", 79 "Main document node has no children", 80 JahiaException.ERROR_SEVERITY, 81 JahiaException.SERVICE_ERROR); 82 } 83 84 86 Element docElNode = (Element ) m_XMLDocument.getDocumentElement (); 87 88 if (!docElNode.getNodeName ().equalsIgnoreCase ("tomcat-users")) { 89 90 throw new JahiaException ("Invalid XML format", 91 "tomcat-users tag is not present as starting tag in file", 92 JahiaException.ERROR_SEVERITY, 93 JahiaException.SERVICE_ERROR); 94 } 95 96 logger.debug ("Tomcat-users.xml file has tomcat-users element"); 97 98 m_UserNodes = XMLParser.getChildNodes (docElNode, "user"); 100 101 logger.debug ("done"); 102 } 103 104 109 public Vector getUserNodes () { 110 111 return m_UserNodes; 112 113 } 114 115 123 public void addUser ( 124 String name, 125 String password, 126 String roles 127 ) { 128 129 Element docElNode = (Element ) m_XMLDocument.getDocumentElement (); 130 131 Element newNode = (Element ) m_XMLDocument.createElement ("user"); 132 133 XMLParser.setAttribute (newNode, "name", name); 134 XMLParser.setAttribute (newNode, "password", password); 135 XMLParser.setAttribute (newNode, "roles", roles); 136 137 if (m_UserNodes.size () > 0) { 138 Node lastUserNode = (Node ) m_UserNodes.get ((m_UserNodes.size () - 1)); 139 docElNode.insertBefore (newNode, lastUserNode); 140 } else { 141 docElNode.appendChild (newNode); 142 } 143 144 m_UserNodes.add (newNode); 145 146 } 147 148 156 public boolean updateUser ( 157 String name, 158 String password, 159 String role 160 ) { 161 162 int size = m_UserNodes.size (); 163 for (int i = 0; i < size; i++) { 164 Element user = (Element ) m_UserNodes.get (i); 165 166 if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user, "name")) 167 && 168 role.equalsIgnoreCase (XMLParser.getAttributeValue (user, "roles"))) { 169 170 XMLParser.setAttribute (user, "password", password); 171 return true; 172 } else if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user, 173 "username")) 174 && 175 role.equalsIgnoreCase (XMLParser.getAttributeValue (user, 176 "roles"))) { 177 XMLParser.setAttribute (user, "password", password); 178 return true; 179 180 } 181 } 182 183 return false; 184 } 185 186 192 public boolean checkUser ( 193 String name, 194 String password 195 ) { 196 197 int size = m_UserNodes.size (); 198 for (int i = 0; i < size; i++) { 199 Element user = (Element ) m_UserNodes.get (i); 200 201 if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user, "name")) 202 && 203 password.equalsIgnoreCase (XMLParser.getAttributeValue (user, "password"))) { 204 return true; 205 } else if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user, 206 "username")) 207 && 208 password.equalsIgnoreCase (XMLParser.getAttributeValue (user, 209 "password"))) { 210 return true; 211 } 212 } 213 214 return false; 215 } 216 217 223 public String getUserPassword (String name, String role) { 224 225 int size = m_UserNodes.size (); 226 for (int i = 0; i < size; i++) { 227 Element user = (Element ) m_UserNodes.get (i); 228 229 if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user, "name")) && 230 (XMLParser.getAttributeValue (user, "roles") != null) && 231 (XMLParser.getAttributeValue (user, "roles").indexOf(role) != -1)) { 232 return XMLParser.getAttributeValue (user, "password"); 233 234 } else if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user, 235 "username")) && 236 (XMLParser.getAttributeValue (user, "roles") != null) && 237 (XMLParser.getAttributeValue (user, "roles").indexOf(role) != -1)) { 238 return XMLParser.getAttributeValue (user, "password"); 239 } 240 } 241 242 return null; 243 } 244 245 } | Popular Tags |