- Timestamp:
- 04/28/20 12:56:50 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/geniusweb/profilesserver/AutoUpdatingProfilesFactory.java
r8 r12 2 2 3 3 import java.io.File; 4 import java.io.FileWriter; 4 5 import java.io.IOException; 6 import java.io.Writer; 5 7 import java.net.URISyntaxException; 6 8 import java.nio.file.Path; … … 63 65 } 64 66 67 @Override 68 public synchronized void putProfile(Profile profile) throws IOException { 69 if (!available.containsKey(profile.getDomain())) { 70 throw new IOException("profile's domain does not exist"); 71 } 72 String domainname = profile.getDomain().getName(); 73 String profilename = profile.getName(); 74 if (!profilename.matches("[a-zA-Z0-9-]+")) 75 throw new IOException("illegal profile name " + profilename 76 + ", only letters, numbers and - allowed"); 77 File file = Paths 78 .get(rootdir.toString(), domainname, profilename + JSON) 79 .toFile(); 80 if (file.exists()) { 81 String msg = fileCheck(file, false, true); 82 if (msg != null) 83 throw new IOException(msg); 84 } 85 86 // CHECK utf-8 compatibility? Do we need that anyway? 87 try (Writer out = new FileWriter(file, false)) { 88 out.write(Jackson.instance().writeValueAsString(profile)); 89 } 90 // trigger the update immediately, avoid wait for FileChecker. 91 super.add(profile); 92 93 } 94 65 95 /** 66 96 * Called when some file that may be relevant has changed. Synchronized to … … 267 297 268 298 /** 269 * check and report basic problems with a file. 299 * Silent check and report basic problems with a file. Silent in the sense 300 * that errors are logged but cause no exception. 270 301 * 271 302 * @param file the file to check … … 275 306 */ 276 307 private boolean basicFileCheck(File file, boolean shouldbeDirectory) { 308 String msg = fileCheck(file, shouldbeDirectory, false); 309 if (msg != null) { 310 log.log(Level.WARNING, msg); 311 return false; 312 } 313 return true; 314 } 315 316 /** 317 * 318 * @param file the file to check 319 * @param shouldbeDirectory true if expected a directory 320 * @param shouldbeWritable true if expected to be writable 321 * @return null if file is there, is file/directory, allows reading and 322 * allows writing (if specified). 323 */ 324 private String fileCheck(File file, boolean shouldbeDirectory, 325 boolean shouldbeWritable) { 277 326 if (!file.exists()) { 278 log.log(Level.WARNING, file + " is missing"); 279 return false; 327 return file + " is missing"; 280 328 } 281 329 if (!file.canRead()) { 282 log.log(Level.WARNING, 283 "directory " + file + "does not allow reading "); 284 return false; 330 return "directory " + file + "does not allow reading "; 285 331 } 286 332 if (shouldbeDirectory) { 287 333 if (!file.isDirectory()) { 288 log.log(Level.WARNING, file + " is not a directory"); 289 return false; 334 return file + " is not a directory"; 290 335 } 291 336 } else { 292 337 if (!file.isFile()) { 293 log.log(Level.WARNING, file + " is not a file"); 294 return false; 295 } 296 } 297 return true; 338 return file + " is not a file"; 339 } 340 } 341 if (shouldbeWritable && !file.canWrite()) { 342 return file + "is read-only"; 343 } 344 return null; 345 298 346 } 299 347
Note:
See TracChangeset
for help on using the changeset viewer.