19
19
20
20
public class FileDownloaderCache {
21
21
private static Logger log = LoggerFactory .getLogger (FileDownloaderCache .class );
22
- private final URL remoteURL ;
23
22
private final Path cacheFilePath ;
24
- // Will be initialized by call the checkIfTheFileIsChanged function
25
- private String eTag ;
23
+ private final String remoteETag ;
24
+ private final String preferencesDataKey ;
26
25
27
26
// BaseNoGui.getSettingsFolder()
28
- public FileDownloaderCache (String cacheFolder , URL remoteURL ) {
29
- this .remoteURL = remoteURL ;
30
- String [] splitPath = remoteURL .getPath ().split ("/" );
27
+ private FileDownloaderCache (Path cacheFilePath , String remoteETag , String preferencesDataKey ) {
28
+ this .cacheFilePath = cacheFilePath ;
29
+ this .remoteETag = remoteETag ;
30
+ this .preferencesDataKey = preferencesDataKey ;
31
+ }
32
+
33
+ public static FileDownloaderCache getFileCached (String cacheFolder , URL remoteURL )
34
+ throws IOException , NoSuchMethodException , ScriptException , URISyntaxException {
35
+
36
+ final String [] splitPath = remoteURL .getPath ().split ("/" );
37
+ final String preferencesDataKey = "cache.file." + remoteURL .getPath ();
38
+ final Path cacheFilePath ;
31
39
if (splitPath .length > 0 ) {
32
- this . cacheFilePath = Paths .get (cacheFolder , splitPath );
40
+ cacheFilePath = Paths .get (cacheFolder , splitPath );
33
41
} else {
34
- this . cacheFilePath = null ;
42
+ cacheFilePath = null ;
35
43
}
36
- }
37
-
38
- public boolean checkIfTheFileIsChanged ()
39
- throws NoSuchMethodException , ScriptException , IOException ,
40
- URISyntaxException {
41
44
42
45
final HttpURLConnection headRequest = new HttpConnectionManager (remoteURL )
43
46
.makeConnection ((connection ) -> {
@@ -52,20 +55,30 @@ public boolean checkIfTheFileIsChanged()
52
55
// Something bad is happening return a conservative true to try to download the file
53
56
if (responseCode < 200 || responseCode >= 300 ) {
54
57
log .warn ("The head request return a bad response code " + responseCode );
55
- return true ;
58
+ // if something bad happend
59
+ return new FileDownloaderCache (cacheFilePath , null , preferencesDataKey );
56
60
}
57
61
58
62
final String remoteETag = headRequest .getHeaderField ("ETag" );
59
- final String localETag = PreferencesData .get (getPreferencesDataKey ());
63
+ String remoteETagClean = null ;
64
+ if (remoteETag != null ) {
65
+ remoteETagClean = remoteETag .trim ().replace ("\" " , "" );
66
+ }
67
+
68
+ return new FileDownloaderCache (cacheFilePath , remoteETagClean , preferencesDataKey );
69
+ }
70
+
71
+ public boolean isChange () {
72
+
73
+ final String localETag = PreferencesData .get (preferencesDataKey );
60
74
61
75
// If the header doesn't exist or the local cache doesn't exist you need to download the file
62
- if (remoteETag == null || localETag == null ) {
76
+ if (cacheFilePath == null || remoteETag == null || localETag == null ) {
63
77
return true ;
64
78
}
65
- eTag = remoteETag .trim ().replace ("\" " , "" );
66
79
67
80
// If are different means that the file is change
68
- return !eTag .equals (localETag );
81
+ return !remoteETag .equals (localETag );
69
82
}
70
83
71
84
public Optional <File > getFileFromCache () {
@@ -77,10 +90,10 @@ public Optional<File> getFileFromCache() {
77
90
}
78
91
79
92
public void fillCache (File fileToCache ) throws Exception {
80
- if (Optional .ofNullable (eTag ).isPresent () &&
93
+ if (Optional .ofNullable (remoteETag ).isPresent () &&
81
94
Optional .ofNullable (cacheFilePath ).isPresent ()) {
82
95
83
- PreferencesData .set (getPreferencesDataKey (), eTag );
96
+ PreferencesData .set (preferencesDataKey , remoteETag );
84
97
// If the cache directory does not exist create it
85
98
if (!Files .exists (cacheFilePath .getParent ())) {
86
99
Files .createDirectories (cacheFilePath .getParent ());
@@ -89,7 +102,4 @@ public void fillCache(File fileToCache) throws Exception {
89
102
}
90
103
}
91
104
92
- private String getPreferencesDataKey () {
93
- return "cache.file." + remoteURL .getPath ();
94
- }
95
105
}
0 commit comments