1 19 20 package com.sslexplorer.properties.impl.profile; 21 22 import java.io.IOException ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.jdom.JDOMException; 27 28 import com.sslexplorer.boot.AbstractPropertyKey; 29 import com.sslexplorer.boot.AbstractXMLDefinedPropertyClass; 30 import com.sslexplorer.boot.ContextHolder; 31 import com.sslexplorer.boot.PropertyClass; 32 import com.sslexplorer.boot.PropertyDefinition; 33 import com.sslexplorer.core.CoreServlet; 34 import com.sslexplorer.properties.ProfilesFactory; 35 36 42 public class ProfileProperties extends AbstractXMLDefinedPropertyClass { 43 44 final static Log log = LogFactory.getLog(ProfileProperties.class); 45 46 49 public final static String NAME = "profileProperties"; 50 51 57 public ProfileProperties() throws IOException , JDOMException { 58 super(NAME, true); 59 } 60 61 66 public String retrievePropertyImpl(AbstractPropertyKey key) throws IllegalArgumentException { 67 if (!(key instanceof ProfilePropertyKey)) { 68 throw new IllegalArgumentException ("Property key is not an instanceof ProfilePropertyKey"); 69 } 70 PropertyDefinition def = getDefinition(key.getName()); 71 ProfilePropertyKey profilesKey = (ProfilePropertyKey) key; 72 try { 73 String val = ProfilesFactory.getInstance().retrieveGenericProperty(profilesKey.getName(), 74 profilesKey.isUserSpecific() ? profilesKey.getUsername() : "", 75 String.valueOf(profilesKey.getProfile()), 76 String.valueOf(profilesKey.getRealm()), 77 ""); 78 if(val == null && profilesKey.isUserSpecific()) { 80 val = ProfilesFactory.getInstance().retrieveGenericProperty(profilesKey.getName(), 81 "", 82 String.valueOf(profilesKey.getProfile()), 83 String.valueOf(profilesKey.getRealm()), 84 ""); 85 } 86 87 if (val == null) { 89 val = def.getDefaultValue(); 90 } else { 91 if (def.getType() == PropertyDefinition.TYPE_PASSWORD) { 92 try { 93 val = ContextHolder.getContext().deobfuscatePassword(val); 94 } catch (Throwable t) { 95 log.warn("Password property " + def.getName() + " could not be decoded. It has been result to the default.", 96 t); 97 } 98 } 99 } 100 return val; 101 } catch (Exception e) { 102 log.error("Failed to retrieve property.", e); 103 } 104 return null; 105 } 106 107 public String storePropertyImpl(AbstractPropertyKey key, String value) throws IllegalArgumentException { 108 if (!(key instanceof ProfilePropertyKey)) { 109 throw new IllegalArgumentException ("Property key is not an instanceof ProfilePropertyKey"); 110 } 111 PropertyDefinition def = getDefinition(key.getName()); 112 ProfilePropertyKey profilesKey = (ProfilePropertyKey) key; 113 114 String oldValue = retrieveProperty(key); 115 116 if (def.getType() == PropertyDefinition.TYPE_PASSWORD) { 117 try { 118 value = ContextHolder.getContext().obfuscatePassword(value); 119 } catch (Throwable t) { 120 log.warn("Password property " + def.getName() + " could not be encoded.", t); 121 } 122 } 123 124 try { 127 ProfilesFactory.getInstance().storeGenericProperty(profilesKey.getName(), 128 profilesKey.isUserSpecific() ? profilesKey.getUsername() : "", 129 String.valueOf(profilesKey.getProfile()), 130 String.valueOf(profilesKey.getRealm()), 131 "", 132 value); 133 } catch (Exception e) { 134 log.error("Could not store properties in database.", e); 135 } 136 return oldValue; 137 } 138 } 139 | Popular Tags |