1- // Copyright (c) 2019, 2021 , Oracle and/or its affiliates.
1+ // Copyright (c) 2019, 2022 , Oracle and/or its affiliates.
22// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
44package com .oracle .weblogic .imagetool .aru ;
55
66import java .io .IOException ;
7- import javax .xml .parsers .ParserConfigurationException ;
87import javax .xml .xpath .XPathExpressionException ;
98
109import com .oracle .weblogic .imagetool .util .HttpUtil ;
1110import com .oracle .weblogic .imagetool .util .XPathUtil ;
1211import org .w3c .dom .Document ;
13- import org .w3c .dom .Element ;
14- import org .w3c .dom .Node ;
1512import org .w3c .dom .NodeList ;
1613
1714/**
2017 */
2118public class AruHttpHelper {
2219 private boolean success ;
23- private Document results ;
24- private String errorMessage ;
2520 private final AruProduct product ;
2621 private final String userId ;
2722 private final String password ;
@@ -38,7 +33,6 @@ public class AruHttpHelper {
3833 this .userId = userId ;
3934 this .password = password ;
4035 this .success = true ;
41- this .results = null ;
4236 }
4337
4438 /**
@@ -50,15 +44,6 @@ public class AruHttpHelper {
5044 AruHttpHelper (String userId , String password ) {
5145 this (null , userId , password );
5246 }
53-
54- /**
55- * Get the error errorMessage.
56- *
57- * @return the search error message
58- */
59- String errorMessage () {
60- return errorMessage ;
61- }
6247
6348 /**
6449 * Returns true if no conflicts ; false if there is conflicts.
@@ -68,15 +53,6 @@ boolean success() {
6853 return success ;
6954 }
7055
71- /**
72- * Get the result object from the search.
73- *
74- * @return dom document detailing about the conflicts
75- */
76- Document results () {
77- return results ;
78- }
79-
8056 /**
8157 * Return the user for the ARU credentials.
8258 *
@@ -114,123 +90,12 @@ AruHttpHelper execSearch(String url) throws IOException {
11490 return this ;
11591 }
11692
117- /**
118- * Call ARU via the HTTPUtil methods and check the result for conflict messages about
119- * patches not compatible.
120- *
121- * @param url to the ARU conflict patch numbers site.
122- * @param payload XML string containing the patch number being validated
123- * @return this instance of encapsulation of ARU search information
124- * @throws IOException if the ARU request is not successful
125- */
126- AruHttpHelper execValidation (String url , String payload ) throws IOException {
127- results = HttpUtil .postCheckConflictRequest (url , payload , userId , password );
128- return this ;
129- }
130-
131- /**
132- * Check the resulting document from the execValidation for conflicts.
133- *
134- * @return this instance of the encapsulation of the validated results
135- * @throws IOException if not able to parse the returned Document for conflict messages
136- */
137- AruHttpHelper validation () throws IOException {
138- NodeList conflictSets ;
139- try {
140- conflictSets = XPathUtil .nodelist (results (),
141- "/conflict_check/conflict_sets/set" );
142- } catch (XPathExpressionException xee ) {
143- throw new IOException (xee );
144- }
145- if (conflictSets .getLength () > 0 ) {
146- try {
147- success = false ;
148- String expression = "/conflict_check/conflict_sets/set/merge_patches" ;
149-
150- NodeList nodeList = XPathUtil .nodelist (results (), expression );
151-
152- createResultDocument (nodeList );
153-
154- errorMessage = parsePatchValidationError ();
155-
156- } catch (XPathExpressionException xpe ) {
157- throw new IOException (xpe );
158- }
159- return this ;
160- }
161- return this ;
162- }
163-
164- /**
165- * Create an XML document from an XML NodeList.
166- * @param nodeList partial XML document
167- * @return a Document based on the NodeList provided
168- * @throws IOException if an XML parser exception occurs trying to build the document
169- */
170- AruHttpHelper createResultDocument (NodeList nodeList ) throws IOException {
171- try {
172- Document doc = HttpUtil .documentBuilder ().newDocument ();
173- Element element = doc .createElement ("results" );
174-
175- for (int i = 0 ; i < nodeList .getLength (); i ++) {
176- Node n = nodeList .item (i );
177- Node copyNode = doc .importNode (n , true );
178-
179- if (n instanceof Element ) {
180- element .appendChild (copyNode );
181- }
182- }
183-
184- doc .appendChild (element );
185-
186- results = doc ;
187- } catch (ParserConfigurationException pce ) {
188- throw new IOException (pce );
189- }
190- return this ;
191- }
192-
193- /**
194- * Parses the patch conflict check result and returns a string of patch conflicts grouped by each conflict.
195- *
196- * @return String
197- */
198- private String parsePatchValidationError () {
199- StringBuilder stringBuilder = new StringBuilder ();
200- Node conflictsResultNode = results ();
201- if (conflictsResultNode != null ) {
202- try {
203- NodeList patchSets = XPathUtil .nodelist (conflictsResultNode , "//merge_patches" );
204- stringBuilder .append ("patch conflicts detected: " );
205- for (int i = 0 ; i < patchSets .getLength (); i ++) {
206- stringBuilder .append ("[" );
207- NodeList bugNumbers = XPathUtil .nodelist (patchSets .item (i ), "patch/bug/number"
208- + "/text()" );
209- for (int j = 0 ; j < bugNumbers .getLength (); j ++) {
210- stringBuilder .append (bugNumbers .item (j ).getNodeValue ());
211- stringBuilder .append ("," );
212- }
213- if (bugNumbers .getLength () > 0 ) {
214- stringBuilder .deleteCharAt (stringBuilder .length () - 1 );
215- }
216- stringBuilder .append ("]" );
217- }
218- } catch (XPathExpressionException e ) {
219- return "Exception occurred in parsePatchValidationError: " + e .getMessage ();
220- }
221- }
222- return stringBuilder .toString ();
223- }
224-
22593 private void searchResult (Document result ) throws IOException {
22694 success = true ;
22795 try {
22896 NodeList nodeList = XPathUtil .nodelist (result , "/results/error" );
22997 if (nodeList .getLength () > 0 ) {
23098 success = false ;
231- errorMessage = XPathUtil .string (result , "/results/error/message" );
232- } else {
233- results = result ;
23499 }
235100 } catch (XPathExpressionException xpe ) {
236101 throw new IOException (xpe );
0 commit comments