5
5
package com .oracle .weblogic .imagetool .cli .cache ;
6
6
7
7
import com .oracle .weblogic .imagetool .api .meta .CacheStore ;
8
+ import com .oracle .weblogic .imagetool .api .model .AbstractFile ;
8
9
import com .oracle .weblogic .imagetool .api .model .CommandResponse ;
9
10
import com .oracle .weblogic .imagetool .api .model .WLSInstallerType ;
10
11
import com .oracle .weblogic .imagetool .util .ARUUtil ;
21
22
import java .io .IOException ;
22
23
import java .nio .file .Files ;
23
24
import java .nio .file .Path ;
25
+ import java .util .logging .Logger ;
24
26
25
27
@ Command (
26
28
name = "addPatch" ,
29
31
)
30
32
public class AddPatchEntry extends CacheOperation {
31
33
32
- String password ;
34
+ private final Logger logger = Logger . getLogger ( AddPatchEntry . class . getName ()) ;
33
35
34
36
public AddPatchEntry () {
35
37
}
@@ -40,40 +42,77 @@ public AddPatchEntry(boolean isCLIMode) {
40
42
41
43
@ Override
42
44
public CommandResponse call () throws Exception {
43
- password = handlePasswordOptions ();
45
+ String password = handlePasswordOptions ();
46
+
44
47
if (patchId != null && !patchId .isEmpty ()
45
- && userId != null && ! userId . isEmpty ( )
46
- && password != null && ! password . isEmpty ()
47
- && location != null && Files . exists ( location ) && Files . isRegularFile ( location )) {
48
+ && location != null && Files . exists ( location )
49
+ && Files . isRegularFile ( location )) {
50
+
48
51
String patchNumber ;
49
52
if (patchId .matches (Constants .PATCH_ID_REGEX )) {
50
53
patchNumber = patchId .substring (1 );
51
54
} else {
52
55
return new CommandResponse (-1 , "Invalid patch id format: " + patchId );
53
56
}
54
- SearchResult result = ARUUtil .getPatchDetail (type .toString (), version , patchNumber , userId , password );
55
- if (result .isSuccess ()) {
56
- Document document = result .getResults ();
57
- String patchDigest = XPathUtil .applyXPathReturnString (document , "string"
58
- + "(/results/patch[1]/files/file/digest[@type='SHA-256']/text())" );
59
- String localDigest = DigestUtils .sha256Hex (new FileInputStream (location .toFile ()));
60
-
61
- if (localDigest .equalsIgnoreCase (patchDigest )) {
62
- String releaseNumber = XPathUtil .applyXPathReturnString (document ,
63
- "string(/results/patch[1]/release/@id)" );
64
- String key = patchNumber + CacheStore .CACHE_KEY_SEPARATOR + releaseNumber ;
65
- cacheStore .addToCache (key , location .toAbsolutePath ().toString ());
66
- return new CommandResponse (0 , String .format (
67
- "Added Patch entry %s=%s for %s" , key , location .toAbsolutePath (), type ));
68
- } else {
69
- return new CommandResponse (-1 , String .format (
70
- "Local file sha-256 digest %s != patch digest %s" , localDigest , patchDigest ));
71
- }
57
+ if (userId != null && !userId .isEmpty () && password != null && !password .isEmpty () ) {
58
+ return validateAndAddToCache (patchNumber , password );
59
+ } else {
60
+ logger .info ("Skipping patch validation, username and password were not provided" );
61
+ return addToCache (patchNumber );
72
62
}
73
- } else {
74
- return new CommandResponse (-1 , "Invalid arguments" );
75
63
}
76
- return null ;
64
+
65
+ String msg = "Invalid arguments" ;
66
+ if (patchId == null || patchId .isEmpty ()) {
67
+ msg += " : --patchId was not supplied" ;
68
+ }
69
+ if (location == null || !Files .exists (location ) || !Files .isRegularFile (location )) {
70
+ msg += " : --path is invalid" ;
71
+ }
72
+
73
+ return new CommandResponse (-1 , msg );
74
+ }
75
+
76
+ /**
77
+ * Validate local patch file's digest against the digest stored in ARU.
78
+ * @param patchNumber the ARU patch number without the 'p'
79
+ * @param password the password to be used for the ARU query (Oracle Support credential)
80
+ * @return true if the local file digest matches the digest stored in Oracle ARU
81
+ * @throws Exception if the ARU call to get patch details failed
82
+ */
83
+ private CommandResponse validateAndAddToCache (String patchNumber , String password ) throws Exception {
84
+ boolean matches = false ;
85
+
86
+ SearchResult searchResult = ARUUtil .getPatchDetail (type .toString (), version , patchNumber , userId , password );
87
+
88
+ if (searchResult .isSuccess ()) {
89
+ Document document = searchResult .getResults ();
90
+ String patchDigest = XPathUtil .applyXPathReturnString (document , "string"
91
+ + "(/results/patch[1]/files/file/digest[@type='SHA-256']/text())" );
92
+ String localDigest = DigestUtils .sha256Hex (new FileInputStream (location .toFile ()));
93
+
94
+ if (localDigest .equalsIgnoreCase (patchDigest )) {
95
+ return addToCache (patchNumber );
96
+ } else {
97
+ return new CommandResponse (-1 , String .format (
98
+ "Local file sha-256 digest %s != patch digest %s" , localDigest , patchDigest ));
99
+ }
100
+ }
101
+
102
+ return new CommandResponse (-1 , String .format ("Unable to find patchId %s on Oracle Support" , patchId ));
103
+ }
104
+
105
+ /**
106
+ * Add patch to the cache.
107
+ * @param patchNumber the patchId (minus the 'p') of the patch to add
108
+ * @return CLI command response
109
+ */
110
+ private CommandResponse addToCache (String patchNumber ) {
111
+ String key = AbstractFile .generateKey (patchNumber , version );
112
+ cacheStore .addToCache (key , location .toAbsolutePath ().toString ());
113
+ String msg = String .format ("Added Patch entry %s=%s for %s" , key , location .toAbsolutePath (), type );
114
+ logger .info (msg );
115
+ return new CommandResponse (0 , msg );
77
116
}
78
117
79
118
/**
@@ -119,7 +158,6 @@ private String handlePasswordOptions() throws IOException {
119
158
@ Option (
120
159
names = {"--user" },
121
160
paramLabel = "<support email>" ,
122
- required = true ,
123
161
description = "Oracle Support email id"
124
162
)
125
163
private String userId ;
0 commit comments