55package com .oracle .weblogic .imagetool .cli .cache ;
66
77import com .oracle .weblogic .imagetool .api .meta .CacheStore ;
8+ import com .oracle .weblogic .imagetool .api .model .AbstractFile ;
89import com .oracle .weblogic .imagetool .api .model .CommandResponse ;
910import com .oracle .weblogic .imagetool .api .model .WLSInstallerType ;
1011import com .oracle .weblogic .imagetool .util .ARUUtil ;
2122import java .io .IOException ;
2223import java .nio .file .Files ;
2324import java .nio .file .Path ;
25+ import java .util .logging .Logger ;
2426
2527@ Command (
2628 name = "addPatch" ,
2931)
3032public class AddPatchEntry extends CacheOperation {
3133
32- String password ;
34+ private final Logger logger = Logger . getLogger ( AddPatchEntry . class . getName ()) ;
3335
3436 public AddPatchEntry () {
3537 }
@@ -40,40 +42,77 @@ public AddPatchEntry(boolean isCLIMode) {
4042
4143 @ Override
4244 public CommandResponse call () throws Exception {
43- password = handlePasswordOptions ();
45+ String password = handlePasswordOptions ();
46+
4447 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+
4851 String patchNumber ;
4952 if (patchId .matches (Constants .PATCH_ID_REGEX )) {
5053 patchNumber = patchId .substring (1 );
5154 } else {
5255 return new CommandResponse (-1 , "Invalid patch id format: " + patchId );
5356 }
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 );
7262 }
73- } else {
74- return new CommandResponse (-1 , "Invalid arguments" );
7563 }
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 );
77116 }
78117
79118 /**
@@ -119,7 +158,6 @@ private String handlePasswordOptions() throws IOException {
119158 @ Option (
120159 names = {"--user" },
121160 paramLabel = "<support email>" ,
122- required = true ,
123161 description = "Oracle Support email id"
124162 )
125163 private String userId ;
0 commit comments