1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.common.InfoException; 4 5 import java.util.HashMap ; 6 import java.util.StringTokenizer ; 7 import java.util.Iterator ; 8 import java.io.*; 9 10 import com.calipso.reportgenerator.common.LanguageTraslator; 11 import com.calipso.reportgenerator.common.VersionProperties; 12 13 17 18 public class UsersRepository { 19 20 private HashMap users; 21 private String repositoryPath; 22 23 27 public UsersRepository(String repositoryPath) throws InfoException { 28 this.repositoryPath = repositoryPath; 29 initialize(new File(repositoryPath)); 30 } 31 32 38 private void initialize(File repositoryFile) throws InfoException { 39 if(repositoryFile.exists()) { 40 fillUsersMapFrom(repositoryFile); 41 } 42 } 43 44 51 private void fillUsersMapFrom(File repositoryFile) throws InfoException { 52 FileReader fileReader = null; 53 try { 54 fileReader = new FileReader(repositoryFile); 55 } catch (FileNotFoundException e) { 56 throw new InfoException(LanguageTraslator.traslate("479"),e); 57 } 58 BufferedReader bufferedReader = new BufferedReader(fileReader); 59 String line = null; 60 try { 61 line = bufferedReader.readLine(); 62 while(line != null) { 63 if(!line.equals("")) { 64 StringTokenizer tokenizer = new StringTokenizer (line, "="); 65 String user = tokenizer.nextToken(); 66 String password = tokenizer.nextToken(); 67 getUsers().put(user, password); 68 } 69 line = bufferedReader.readLine(); 70 } 71 bufferedReader.close(); 72 fileReader.close(); 73 } catch (IOException e) { 74 throw new InfoException(LanguageTraslator.traslate("480"),e); 75 } 76 77 } 78 79 88 public boolean validate(String userName, String password) throws InfoException { 89 boolean returnVal = false; 90 if(getUsers().containsKey(userName)) { 91 String encryptPassword = PasswordEncryptor.getEncryptedPassword(password); 92 String storedPassword = getUsers().get(userName).toString(); 93 if(storedPassword.trim().equalsIgnoreCase(encryptPassword.trim())) { 94 returnVal = true; 95 } 96 } 97 return returnVal; 98 } 99 100 105 public void changePasswd(String userName, String newPasswd) throws InfoException { 106 getUsers().remove(userName); 107 getUsers().put(userName, newPasswd); 108 updateChanges(); 109 } 110 111 115 public void deleteUser(String userName) throws InfoException { 116 getUsers().remove(userName); 117 updateChanges(); 118 } 119 120 123 private void updateChanges() throws InfoException { 124 try{ 125 File outputFile = new File(repositoryPath); 126 FileWriter fileWriter = null; 127 fileWriter = new FileWriter(outputFile.getPath(), true); 128 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 129 Iterator iterator = getUsers().keySet().iterator(); 130 while(iterator.hasNext()) { 131 String currentUserName = iterator.next().toString(); 132 String hashedPasswd = getUsers().get(currentUserName).toString(); 133 bufferedWriter.write(currentUserName + "=" + hashedPasswd + '\n'); 134 } 135 bufferedWriter.close(); 136 fileWriter.close(); 137 }catch(Exception e){ 138 throw new InfoException(LanguageTraslator.traslate("481"),e); 139 } 140 } 141 142 147 public void addNewUser(String userName, String password) throws InfoException { 148 if(getUsers().containsKey(userName)){ 149 throw new InfoException(LanguageTraslator.traslate("394")); 150 } 151 File outputFile = new File(repositoryPath); 152 FileWriter fileWriter = null; 153 try { 154 fileWriter = new FileWriter(outputFile.getPath(), true); 155 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 156 bufferedWriter.write(userName + "=" + PasswordEncryptor.getEncryptedPassword(password) + '\n'); 157 bufferedWriter.close(); 158 fileWriter.close(); 159 }catch(Exception e){ 160 throw new InfoException(LanguageTraslator.traslate("482"),e); 161 } 162 } 163 164 168 private HashMap getUsers() { 169 if(users == null) { 170 users = new HashMap (); 171 } 172 return users; 173 } 174 } 175 | Popular Tags |