Skip to content

Commit 1497e10

Browse files
Merge branch 'java-poc' of gitlab-odx.oracledx.com:weblogic/image-builder into java-poc
Former-commit-id: 0a6c6df88b7ba8c8a9a7aad96acddb63264b4b5f
2 parents 75eab85 + 209b149 commit 1497e10

File tree

3 files changed

+227
-37
lines changed

3 files changed

+227
-37
lines changed

src/main/java/com/oracle/weblogicx/imagebuilder/builder/util/ARUUtil.java

Lines changed: 155 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,127 @@
1010

1111
import org.w3c.dom.Document;
1212
import org.w3c.dom.Element;
13+
import org.w3c.dom.Node;
1314
import org.w3c.dom.NodeList;
1415

1516
public class ARUUtil {
1617

18+
/**
19+
* Return All WLS releases information
20+
*
21+
* @param userId userid for support account
22+
* @param password password for support account
23+
* @throws IOException when failed to access the aru api
24+
*/
1725

18-
public void getAllWLSReleases(String userId, String password) throws IOException {
19-
getAllReleases("wls", userId, password);
26+
public static Document getAllWLSReleases(String userId, String password) throws IOException {
27+
return getAllReleases("wls", userId, password);
2028
}
2129

22-
public void getLatestWLSPatches(String release, String userId, String password) throws IOException {
30+
/**
31+
* Return release number of a WLS release by version
32+
*
33+
* @param version wls version 12.2.1.3.0 etc ...
34+
* @param userId user id for support account
35+
* @param password password for support account
36+
* @return release number or empty string if not found
37+
* @throws IOException when failed to access the aru api
38+
*/
39+
private static String getWLSReleaseNumber(String version, String userId, String password) throws
40+
IOException {
41+
return getReleaseNumber("wls", version, userId, password);
42+
}
43+
44+
/**
45+
* Return release number of a FMW release by version
46+
*
47+
* @param version wls version 12.2.1.3.0 etc ...
48+
* @param userId user id for support account
49+
* @param password password for support account
50+
* @return release number or empty string if not found
51+
* @throws IOException when failed to access the aru api
52+
*/
53+
private static String getFMWReleaseNumber(String version, String userId, String password) throws
54+
IOException {
55+
return getReleaseNumber("fmw", version, userId, password);
56+
}
57+
58+
59+
/**
60+
* Return All FMW releases information
61+
*
62+
* @param userId userid for support account
63+
* @param password password for support account
64+
* @throws IOException when failed to access the aru api
65+
*/
66+
public static void getAllFMWReleases(String userId, String password) throws IOException {
67+
getAllReleases("fmw", userId, password);
68+
}
69+
70+
/**
71+
* Download the latest WLS patches(PSU) for the release
72+
*
73+
* @param release release number
74+
* @param userId userid for support account
75+
* @param password password for support account
76+
* @throws IOException when failed to access the aru api
77+
*/
78+
public static void getLatestWLSPatches(String release, String userId, String password) throws IOException {
2379
getLatestPSU("wls", release, userId, password);
2480
}
2581

26-
public void getPatches(String category, List<String> patches, String userId, String password) throws IOException {
82+
/**
83+
* Download the latest FMW patches(PSU) for the release
84+
*
85+
* @param release release number
86+
* @param userId userid for support account
87+
* @param password password for support account
88+
* @throws IOException when failed to access the aru api
89+
*/
90+
public static void getLatestFMWPatches(String release, String userId, String password) throws IOException {
91+
getLatestPSU("fmw", release, userId, password);
92+
}
93+
94+
/**
95+
* Download a list of WLS patches
96+
*
97+
* @param patches A list of patches number
98+
* @param userId userid for support account
99+
* @param password password for support account
100+
* @throws IOException when failed to access the aru api
101+
*/
102+
public static void getWLSPatches(List<String> patches, String userId, String password) throws
103+
IOException {
27104
for (String patch : patches)
28105
getPatch("wls", patch, userId, password);
29106
}
30107

31-
public void validatePatches(List<String> patches, String category, String version) {
108+
/**
109+
* Download a list of FMW patches
110+
*
111+
* @param patches A list of patches number
112+
* @param userId userid for support account
113+
* @param password password for support account
114+
* @throws IOException when failed to access the aru api
115+
*/
116+
public static void getFMWPatches(String category, List<String> patches, String userId, String password) throws
117+
IOException {
118+
for (String patch : patches)
119+
getPatch("fmw", patch, userId, password);
120+
}
121+
122+
/**
123+
*
124+
* @param patches A list of patches number
125+
* @param category
126+
* @param version
127+
* @param userId userid for support account
128+
* @param password password for support account
129+
* @throws IOException when failed to access the aru api
130+
*/
131+
132+
public static void validatePatches(List<String> patches, String category, String version, String userId, String
133+
password) throws IOException {
32134

33135
// TODO
34136

@@ -52,7 +154,7 @@ public void validatePatches(List<String> patches, String category, String versio
52154

53155
}
54156

55-
private Document getAllReleases(String category, String userId, String password) throws IOException {
157+
private static Document getAllReleases(String category, String userId, String password) throws IOException {
56158

57159
//HTTP_STATUS=$(curl -v -w "%{http_code}" -b cookies.txt -L --header 'Authorization: Basic ${basicauth}'
58160
// "https://updates.oracle.com/Orion/Services/metadata?table=aru_releases" -o allarus.xml)
@@ -64,23 +166,28 @@ private Document getAllReleases(String category, String userId, String password)
64166

65167
String expression;
66168

67-
if ("wls".equalsIgnoreCase("wls")) {
169+
if ("wls".equalsIgnoreCase(category)) {
68170
expression = "/results/release[starts-with(text(), 'Oracle WebLogic Server')]";
69171
} else {
70-
expression = "";
172+
expression = "/results/release[starts-with(text(), 'Fusion Middleware Upgrade')]";
71173
}
72174
NodeList nodeList = XPathUtil.applyXPathReturnNodeList(allReleases, expression);
73175

74176
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
75177
DocumentBuilder builder = dbf.newDocumentBuilder();
76178
Document doc = builder.newDocument();
77179
Element element = doc.createElement("results");
78-
doc.appendChild(element);
79180

80181
for (int i = 0; i < nodeList.getLength(); i++) {
81-
element.appendChild(nodeList.item(0));
182+
Node n = nodeList.item(i);
183+
Node copyNode = doc.importNode(n, true);
184+
185+
if (n instanceof Element )
186+
element.appendChild(copyNode);
82187
}
83188

189+
doc.appendChild(element);
190+
XPathUtil.prettyPrint(doc);
84191

85192
return doc;
86193

@@ -91,32 +198,44 @@ private Document getAllReleases(String category, String userId, String password)
91198

92199
}
93200

94-
private void getLatestPSU(String category, String release, String userId, String password) throws IOException {
201+
private static void getLatestPSU(String category, String release, String userId, String password) throws IOException {
95202

96-
// HTTP_STATUS=$(curl -v -w "%{http_code}" -b cookies.txt -L --header 'Authorization: Basic ${basicauth}' "https://updates.oracle.com/Orion/Services/search?product=15991&release=$releaseid&include_prereqs=true" -o latestpsu.xml)
203+
String xpath = "https://updates.oracle"
204+
+ ".com/Orion/Services/search?product=%s&release=%s";
205+
String expression;
206+
if ("wls".equalsIgnoreCase(category))
207+
expression = String.format(xpath, "15991", release);
208+
else
209+
expression = String.format(xpath, "27638", release);
97210

98-
99-
Document allPatches = HttpUtil.getXMLContent("https://updates.oracle"
100-
+ ".com/Orion/Services/search?product=15991&release=" + release, userId, password);
211+
Document allPatches = HttpUtil.getXMLContent(expression, userId, password);
101212

102213
savepatch(allPatches, userId, password);
103214
}
104215

105-
private void getPatch(String category, String patchNumber, String userId, String password) throws IOException {
216+
private static void getPatch(String category, String patchNumber, String userId, String password) throws
217+
IOException {
106218

107219
// HTTP_STATUS=$(curl -v -w "%{http_code}" -b cookies.txt -L --header 'Authorization: Basic ${basicauth}' "https://updates.oracle.com/Orion/Services/search?product=15991&release=$releaseid&include_prereqs=true" -o latestpsu.xml)
108220

221+
String urlFormat = "https://updates.oracle"
222+
+ ".com/Orion/Services/search?product=%s&bug=%s";
223+
String url;
224+
225+
if ("wls".equalsIgnoreCase(category))
226+
url = String.format(urlFormat, "15991", patchNumber);
227+
else
228+
url = String.format(urlFormat, "27638", patchNumber);
109229

110-
Document allPatches = HttpUtil.getXMLContent("https://updates.oracle"
111-
+ ".com/Orion/Services/search?product=15991&bug=" + patchNumber, userId, password);
230+
Document allPatches = HttpUtil.getXMLContent(url, userId, password);
112231

113232
savepatch(allPatches, userId, password);
114233

115234

116235

117236
}
118237

119-
private void savepatch(Document allPatches, String userId, String password) throws IOException {
238+
private static void savepatch(Document allPatches, String userId, String password) throws IOException {
120239
try {
121240

122241
// TODO: needs to make sure there is one and some filtering if not sorting
@@ -144,6 +263,23 @@ private void savepatch(Document allPatches, String userId, String password) thro
144263

145264
}
146265

266+
private static String getReleaseNumber(String category, String version, String userId, String password) throws
267+
IOException {
268+
Document allReleases = getAllReleases(category, userId, password);
269+
270+
String expression = String.format("string(/results/release[@name = '%s']/@id)", version);
271+
try {
272+
return XPathUtil.applyXPathReturnString(allReleases, expression);
273+
} catch (XPathExpressionException xpe) {
274+
throw new IOException(xpe);
275+
}
276+
277+
278+
}
279+
280+
public static void main(String args[]) throws Exception {
281+
String release = ARUUtil.getWLSReleaseNumber("121.2.1.3.0","[email protected]", "iJCPiUah7jdmLk1E");
282+
}
147283

148284

149285
}

src/main/java/com/oracle/weblogicx/imagebuilder/builder/util/HttpUtil.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.StringReader;
6-
import java.io.StringWriter;
7-
import java.io.Writer;
86

97
import javax.xml.parsers.DocumentBuilder;
108
import javax.xml.parsers.DocumentBuilderFactory;
119
import javax.xml.parsers.ParserConfigurationException;
12-
import javax.xml.transform.OutputKeys;
13-
import javax.xml.transform.Transformer;
14-
import javax.xml.transform.TransformerFactory;
15-
import javax.xml.transform.dom.DOMSource;
16-
import javax.xml.transform.stream.StreamResult;
1710

1811
import org.apache.http.HttpEntity;
1912
import org.apache.http.client.ClientProtocolException;
@@ -35,6 +28,15 @@
3528

3629
public class HttpUtil {
3730

31+
/**
32+
* Return the xml result of a GET from the url
33+
*
34+
* @param url url of the aru server
35+
* @param username userid for support account
36+
* @param password password for support account
37+
* @return xml dom document
38+
* @throws IOException when it fails to access the url
39+
*/
3840
public static Document getXMLContent(String url, String username, String password) throws IOException {
3941

4042
RequestConfig.Builder config = RequestConfig.custom();
@@ -60,7 +62,7 @@ public static Document getXMLContent(String url, String username, String passwor
6062
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
6163
InputSource is = new InputSource(new StringReader(xmlString));
6264
Document doc = docBuilder.parse(is);
63-
prettyPrint(doc);
65+
// XPathUtil.prettyPrint(doc);
6466
return doc;
6567
} catch (ParserConfigurationException ex) {
6668
throw new IllegalStateException(ex);
@@ -73,14 +75,15 @@ public static Document getXMLContent(String url, String username, String passwor
7375

7476
}
7577

76-
public static final void prettyPrint(Document xml) throws Exception {
77-
Transformer tf = TransformerFactory.newInstance().newTransformer();
78-
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
79-
tf.setOutputProperty(OutputKeys.INDENT, "yes");
80-
Writer out = new StringWriter();
81-
tf.transform(new DOMSource(xml), new StreamResult(out));
82-
System.out.println(out.toString());
83-
}
78+
/**
79+
* Downlod a file from the url
80+
*
81+
* @param url url of the aru server
82+
* @param destination full path to save the file
83+
* @param username userid for support account
84+
* @param password password for support account
85+
* @throws IOException when it fails to access the url
86+
*/
8487

8588
public static void downloadFile(String url, String destination, String username, String password)
8689
throws IOException {
@@ -104,6 +107,17 @@ public static void downloadFile(String url, String destination, String username,
104107

105108
}
106109

110+
/**
111+
* Check conflicts
112+
*
113+
* @param url
114+
* @param payload
115+
* @param username
116+
* @param password
117+
* @return
118+
* @throws IOException
119+
*/
120+
107121
public static String checkConflicts(String url, String payload, String username, String password)
108122
throws IOException {
109123
RequestConfig.Builder config = RequestConfig.custom();

0 commit comments

Comments
 (0)