13
13
14
14
package org .codehaus .plexus .components .secdispatcher .internal ;
15
15
16
- import javax .inject .Inject ;
17
- import javax .inject .Named ;
18
- import javax .inject .Singleton ;
19
-
20
16
import java .io .IOException ;
17
+ import java .nio .file .Files ;
21
18
import java .nio .file .Path ;
22
- import java .nio .file .Paths ;
23
19
import java .util .Collection ;
24
20
import java .util .HashMap ;
25
21
import java .util .List ;
40
36
import static java .util .Objects .requireNonNull ;
41
37
42
38
/**
39
+ * Note: this implementation is NOT a JSR330 component. Integrating apps anyway want to customize it (at least
40
+ * the name and location of configuration file), so instead as before (providing "bad" configuration file just
41
+ * to have one), it is the duty of integrator to wrap and "finish" the implementation in a way it suits the
42
+ * integrator. Also, using "globals" like Java System Properties are bad thing, and it is integrator who knows
43
+ * what is needed anyway.
44
+ * <p>
45
+ * Recommended way for integration is to create JSR330 {@link javax.inject.Provider}.
46
+ *
43
47
* @author Oleg Gusakov
44
48
*/
45
- @ Singleton
46
- @ Named
47
49
public class DefaultSecDispatcher implements SecDispatcher {
48
50
public static final String ATTR_START = "[" ;
49
51
public static final String ATTR_STOP = "]" ;
50
52
51
53
protected final PlexusCipher cipher ;
52
54
protected final Map <String , Dispatcher > dispatchers ;
53
- protected final String configurationFile ;
55
+ protected final Path configurationFile ;
54
56
55
- @ Inject
56
- public DefaultSecDispatcher (
57
- PlexusCipher cipher ,
58
- Map <String , Dispatcher > dispatchers ,
59
- @ Named ("${configurationFile:-" + DEFAULT_CONFIGURATION + "}" ) final String configurationFile ) {
57
+ public DefaultSecDispatcher (PlexusCipher cipher , Map <String , Dispatcher > dispatchers , Path configurationFile ) {
60
58
this .cipher = requireNonNull (cipher );
61
59
this .dispatchers = requireNonNull (dispatchers );
62
60
this .configurationFile = requireNonNull (configurationFile );
61
+
62
+ // file may or may not exist, but one thing is certain: it cannot be an exiting directory
63
+ if (Files .isDirectory (configurationFile )) {
64
+ throw new IllegalArgumentException ("configurationFile cannot be a directory" );
65
+ }
63
66
}
64
67
65
68
@ Override
@@ -94,7 +97,7 @@ public Collection<Field> fields() {
94
97
}
95
98
96
99
@ Override
97
- public String encrypt (String str , Map <String , String > attr ) throws SecDispatcherException {
100
+ public String encrypt (String str , Map <String , String > attr ) throws SecDispatcherException , IOException {
98
101
if (isEncryptedString (str )) return str ;
99
102
100
103
try {
@@ -130,7 +133,7 @@ public String encrypt(String str, Map<String, String> attr) throws SecDispatcher
130
133
}
131
134
132
135
@ Override
133
- public String decrypt (String str ) throws SecDispatcherException {
136
+ public String decrypt (String str ) throws SecDispatcherException , IOException {
134
137
if (!isEncryptedString (str )) return str ;
135
138
try {
136
139
String bare = cipher .unDecorate (str );
@@ -149,7 +152,7 @@ public String decrypt(String str) throws SecDispatcherException {
149
152
150
153
@ Override
151
154
public SettingsSecurity readConfiguration (boolean createIfMissing ) throws IOException {
152
- SettingsSecurity configuration = SecUtil .read (getConfigurationPath () );
155
+ SettingsSecurity configuration = SecUtil .read (configurationFile );
153
156
if (configuration == null && createIfMissing ) {
154
157
configuration = new SettingsSecurity ();
155
158
}
@@ -159,10 +162,10 @@ public SettingsSecurity readConfiguration(boolean createIfMissing) throws IOExce
159
162
@ Override
160
163
public void writeConfiguration (SettingsSecurity configuration ) throws IOException {
161
164
requireNonNull (configuration , "configuration is null" );
162
- SecUtil .write (getConfigurationPath () , configuration , true );
165
+ SecUtil .write (configurationFile , configuration , true );
163
166
}
164
167
165
- private Map <String , String > prepareDispatcherConfig (String type ) {
168
+ protected Map <String , String > prepareDispatcherConfig (String type ) throws IOException {
166
169
HashMap <String , String > dispatcherConf = new HashMap <>();
167
170
Map <String , String > conf = SecUtil .getConfig (getConfiguration (), type );
168
171
if (conf != null ) {
@@ -171,7 +174,7 @@ private Map<String, String> prepareDispatcherConfig(String type) {
171
174
return dispatcherConf ;
172
175
}
173
176
174
- private String strip (String str ) {
177
+ protected String strip (String str ) {
175
178
int start = str .indexOf (ATTR_START );
176
179
int stop = str .indexOf (ATTR_STOP );
177
180
if (start != -1 && stop != -1 && stop > start ) {
@@ -180,7 +183,7 @@ private String strip(String str) {
180
183
return str ;
181
184
}
182
185
183
- private Map <String , String > stripAttributes (String str ) {
186
+ protected Map <String , String > stripAttributes (String str ) {
184
187
HashMap <String , String > result = new HashMap <>();
185
188
int start = str .indexOf (ATTR_START );
186
189
int stop = str .indexOf (ATTR_STOP );
@@ -202,30 +205,12 @@ private Map<String, String> stripAttributes(String str) {
202
205
return result ;
203
206
}
204
207
205
- private boolean isEncryptedString (String str ) {
208
+ protected boolean isEncryptedString (String str ) {
206
209
if (str == null ) return false ;
207
210
return cipher .isEncryptedString (str );
208
211
}
209
212
210
- private Path getConfigurationPath () {
211
- String location = System .getProperty (SYSTEM_PROPERTY_CONFIGURATION_LOCATION , getConfigurationFile ());
212
- location = location .charAt (0 ) == '~' ? System .getProperty ("user.home" ) + location .substring (1 ) : location ;
213
- return Paths .get (location );
214
- }
215
-
216
- private SettingsSecurity getConfiguration () throws SecDispatcherException {
217
- Path path = getConfigurationPath ();
218
- try {
219
- SettingsSecurity sec = SecUtil .read (path );
220
- if (sec == null )
221
- throw new SecDispatcherException ("Please check that configuration file on path " + path + " exists" );
222
- return sec ;
223
- } catch (IOException e ) {
224
- throw new SecDispatcherException (e .getMessage (), e );
225
- }
226
- }
227
-
228
- public String getConfigurationFile () {
229
- return configurationFile ;
213
+ protected SettingsSecurity getConfiguration () throws SecDispatcherException , IOException {
214
+ return SecUtil .read (configurationFile );
230
215
}
231
216
}
0 commit comments