FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
javaxt-express/src/javaxt/express/ConfigFile.java at master · javaxt-project/javaxt-express · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
javaxt-project
/
javaxt-express
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
javaxt-express
/
src
/
javaxt
/
express
/
ConfigFile.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
319 lines (265 loc) · 10.8 KB
Breadcrumbs
javaxt-express
/
src
/
javaxt
/
express
/
ConfigFile.java
Copy path
File metadata and controls
319 lines (265 loc) · 10.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package
javaxt
.
express
;
import
javaxt
.
json
.
JSONObject
;
import
javaxt
.
encryption
.
AES256
;
//******************************************************************************
//** ConfigFile
//******************************************************************************
/**
* Used to access and save configuration information stored in a JSON file.
* Provides an option to encrypt the JSON file.
*
******************************************************************************/
public
class
ConfigFile
{
private
javaxt
.
io
.
File
file
;
private
JSONObject
config
;
//**************************************************************************
//** Constructor
//**************************************************************************
public
ConfigFile
(
javaxt
.
io
.
File
file
){
this
.
file
=
file
;
}
//**************************************************************************
//** Constructor
//**************************************************************************
public
ConfigFile
(
String
path
){
this
(
new
javaxt
.
io
.
File
(
path
));
}
//**************************************************************************
//** exists
//**************************************************************************
/** Returns true if the file exists.
*/
public
boolean
exists
(){
return
file
.
exists
();
}
//**************************************************************************
//** getDirectory
//**************************************************************************
public
javaxt
.
io
.
Directory
getDirectory
(){
return
file
.
getDirectory
();
}
//**************************************************************************
//** delete
//**************************************************************************
/** Used to delete the config file, if it exists.
*/
public
void
delete
(){
file
.
delete
();
}
//**************************************************************************
//** getConfig
//**************************************************************************
/** Returns the current config (json document).
*/
public
JSONObject
getConfig
(){
if
(
config
==
null
)
config
=
file
.
getJSONObject
();
return
config
;
}
//**************************************************************************
//** getConfig
//**************************************************************************
/** Used to decrypt and parse a config file (json document).
*/
public
JSONObject
getConfig
(
String
username
,
String
password
)
throws
java
.
security
.
InvalidKeyException
,
Exception
{
if
(
config
==
null
)
config
=
new
JSONObject
(
AES256
.
decrypt
(
file
.
getBytes
().
toByteArray
(),
generatePassword
(
username
,
password
)
)
);
return
config
;
}
//**************************************************************************
//** save
//**************************************************************************
/** Used to save the config file.
*/
public
void
save
(){
file
.
write
(
getConfig
());
}
//**************************************************************************
//** save
//**************************************************************************
/** Used to encrypt and save the config file.
*/
public
void
save
(
String
username
,
String
password
)
throws
java
.
security
.
InvalidKeyException
,
Exception
{
save
(
getConfig
(),
username
,
password
);
}
//**************************************************************************
//** save
//**************************************************************************
/** Used to encrypt and save the config file.
*/
public
void
save
(
JSONObject
config
,
String
username
,
String
password
)
throws
java
.
security
.
InvalidKeyException
,
Exception
{
file
.
write
(
AES256
.
encrypt
(
config
.
toString
(),
generatePassword
(
username
,
password
)
)
);
}
//**************************************************************************
//** generatePassword
//**************************************************************************
/** Used to generate a password/key used to encrypt/decrypt a config file.
*/
private
static
String
generatePassword
(
String
username
,
String
password
)
throws
Exception
{
return
(
javaxt
.
utils
.
Base64
.
encode
(
(
username
+
"/"
+
password
).
getBytes
(
"UTF-8"
)
));
}
//**************************************************************************
//** updateDir
//**************************************************************************
/** Used to update a path to a directory defined in the config file.
* Resolves both canonical and relative paths (relative to the configFile).
*/
public
void
updateDir
(
String
key
,
boolean
create
){
updateDir
(
key
,
getConfig
(),
file
,
create
);
}
//**************************************************************************
//** updateDir
//**************************************************************************
/** Used to update a path to a directory defined in a given config. Resolves
* both canonical and relative paths (relative to the configFile).
*/
public
void
updateDir
(
String
key
,
JSONObject
config
,
boolean
create
){
updateDir
(
key
,
config
,
file
,
create
);
}
//**************************************************************************
//** updateDir
//**************************************************************************
/** Used to update a path to a directory defined in a given config. Resolves
* both canonical and relative paths (relative to the configFile).
*/
public
static
void
updateDir
(
String
key
,
JSONObject
config
,
javaxt
.
io
.
File
configFile
,
boolean
create
){
if
(
config
!=
null
&&
config
.
has
(
key
)){
String
path
=
config
.
get
(
key
).
toString
();
if
(
path
==
null
){
config
.
remove
(
key
);
}
else
{
path
=
path
.
trim
();
if
(
path
.
length
()==
0
){
config
.
remove
(
key
);
}
else
{
//Get directory
javaxt
.
io
.
Directory
dir
;
if
(
isRelativePath
(
path
)){
path
=
configFile
.
MapPath
(
path
);
dir
=
new
javaxt
.
io
.
Directory
(
new
java
.
io
.
File
(
path
));
}
else
{
dir
=
new
javaxt
.
io
.
Directory
(
path
);
}
//Get canonical path
if
(
dir
.
exists
()){
try
{
javaxt
.
io
.
Directory
d
=
new
javaxt
.
io
.
Directory
(
dir
.
toFile
().
getCanonicalFile
());
if
(!
dir
.
toString
().
equals
(
d
.
toString
())){
dir
=
d
;
}
}
catch
(
Exception
e
){}
}
//Create directory as needed
if
(!
dir
.
exists
() &&
create
)
dir
.
create
();
//Update config
if
(
dir
.
exists
()){
config
.
set
(
key
,
dir
.
toString
());
}
else
{
config
.
remove
(
key
);
}
}
}
}
}
//**************************************************************************
//** updateFile
//**************************************************************************
/** Used to update a path to a file defined in the config file. Resolves
* both canonical and relative paths (relative to the configFile).
*/
public
void
updateFile
(
String
key
){
updateFile
(
key
,
getConfig
(),
file
);
}
//**************************************************************************
//** updateFile
//**************************************************************************
/** Used to update a path to a file defined a given config. Resolves both
* canonical and relative paths (relative to the configFile).
*/
public
void
updateFile
(
String
key
,
JSONObject
config
){
updateFile
(
key
,
config
,
file
);
}
//**************************************************************************
//** updateFile
//**************************************************************************
/** Used to update a path to a file defined in a config. Resolves both
* canonical and relative paths (relative to a configFile).
*/
public
static
void
updateFile
(
String
key
,
JSONObject
config
,
javaxt
.
io
.
File
configFile
){
if
(
config
.
has
(
key
)){
String
path
=
config
.
get
(
key
).
toString
();
if
(
path
==
null
){
config
.
remove
(
key
);
}
else
{
path
=
path
.
trim
();
if
(
path
.
length
()==
0
){
config
.
remove
(
key
);
}
else
{
//Get file
javaxt
.
io
.
File
file
=
getFile
(
path
,
configFile
);
//Get canonical path
if
(
file
.
exists
()){
try
{
javaxt
.
io
.
File
f
=
new
javaxt
.
io
.
File
(
file
.
toFile
().
getCanonicalFile
());
if
(!
file
.
toString
().
equals
(
f
.
toString
())){
file
=
f
;
}
}
catch
(
Exception
e
){}
}
//Update config
config
.
set
(
key
,
file
.
toString
());
}
}
}
}
//**************************************************************************
//** getFile
//**************************************************************************
/** Returns a File for a given path. Resolves both canonical and relative
* paths (relative to the given file).
* @param path Full canonical path or a relative path
* @param file If a relative path is given, the path is resolved to the
* given file
*/
public
static
javaxt
.
io
.
File
getFile
(
String
path
,
javaxt
.
io
.
File
file
){
if
(
isRelativePath
(
path
)){
return
new
javaxt
.
io
.
File
(
file
.
MapPath
(
path
));
}
else
{
return
new
javaxt
.
io
.
File
(
path
);
}
}
//**************************************************************************
//** isRelativePath
//**************************************************************************
/** Returns true is the given path appears to be relative
*/
private
static
boolean
isRelativePath
(
String
path
){
path
=
path
.
replace
(
"
\\
"
,
"/"
);
if
(
path
.
startsWith
(
"/"
))
return
false
;
if
(
System
.
getProperty
(
"os.name"
).
toLowerCase
().
startsWith
(
"windows"
)){
if
(
path
.
indexOf
(
":"
)==
1
)
return
false
;
}
return
true
;
}
}
Back
|
FazBrowse Home
|
New Git URL