Skip to content

Commit eb0b17e

Browse files
committed
add utility classes
Former-commit-id: 0319f2023f9a707869e5db8a5df2f094d9526e08
1 parent 7fcd3d4 commit eb0b17e

File tree

4 files changed

+222
-1
lines changed

4 files changed

+222
-1
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
<dependency>
1818
<groupId>org.apache.httpcomponents</groupId>
1919
<artifactId>httpclient</artifactId>
20-
<version>4.5.5</version>
20+
<version>4.5.6</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.apache.httpcomponents</groupId>
25+
<artifactId>fluent-hc</artifactId>
26+
<version>4.5.6</version>
2127
</dependency>
2228
</dependencies>
2329
</project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.oracle.weblogicx.imagebuilder.builder.util;
2+
3+
import java.io.IOException;
4+
5+
import javax.xml.parsers.DocumentBuilder;
6+
import javax.xml.parsers.DocumentBuilderFactory;
7+
import javax.xml.parsers.ParserConfigurationException;
8+
import javax.xml.xpath.XPathExpressionException;
9+
10+
import org.w3c.dom.Document;
11+
import org.w3c.dom.Element;
12+
import org.w3c.dom.Node;
13+
import org.w3c.dom.NodeList;
14+
15+
public class ARUUtil {
16+
17+
18+
public void getAllWLSReleases(String userId, String password) throws IOException {
19+
getAllReleases("wls", userId, password);
20+
}
21+
22+
public void getLatestWLSPatches(String version, String userId, String password) throws IOException {
23+
getLatestReleases("wls", version, userId, password);
24+
}
25+
26+
private Document getAllReleases(String category, String userId, String password) throws IOException {
27+
28+
//HTTP_STATUS=$(curl -v -w "%{http_code}" -b cookies.txt -L --header 'Authorization: Basic ${basicauth}'
29+
// "https://updates.oracle.com/Orion/Services/metadata?table=aru_releases" -o allarus.xml)
30+
31+
Document allReleases = HttpUtil.getXMLContent("https://updates.oracle"
32+
+ ".com/Orion/Services/metadata?table=aru_releases");
33+
34+
try {
35+
36+
String expression;
37+
38+
if ("wls".equalsIgnoreCase("wls")) {
39+
expression = "/results/release[starts-with(text(), 'Oracle WebLogic Server')]";
40+
} else {
41+
expression = "";
42+
}
43+
NodeList nodeList = XPathUtil.applyXPathReturnNodeList(allReleases, expression);
44+
45+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
46+
DocumentBuilder builder = dbf.newDocumentBuilder();
47+
Document doc = builder.newDocument();
48+
Element element = doc.createElement("results");
49+
doc.appendChild(element);
50+
51+
for (int i = 0; i < nodeList.getLength(); i++) {
52+
element.appendChild(nodeList.item(0));
53+
}
54+
55+
56+
return doc;
57+
58+
} catch (XPathExpressionException | ParserConfigurationException xpe) {
59+
throw new IOException(xpe);
60+
}
61+
62+
63+
}
64+
65+
private void getLatestReleases(String category, String version, String userId, String password) throws IOException {
66+
67+
// 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)
68+
69+
70+
Document allPatches = HttpUtil.getXMLContent("https://updates.oracle"
71+
+ ".com/Orion/Services/search?product=15991&release=" + version);
72+
73+
try {
74+
String downLoadLink = XPathUtil.applyXPathReturnString(allPatches, "string"
75+
+ "(/results/patch[1]/files/file/download_url/text())");
76+
77+
String doloadHost = XPathUtil.applyXPathReturnString(allPatches, "string"
78+
+ "(/results/patch[1]/files/file/download_url/@host)");
79+
80+
81+
HttpUtil.downloadFile(doloadHost+downLoadLink, "todo");
82+
83+
} catch (XPathExpressionException xpe) {
84+
throw new IOException(xpe);
85+
}
86+
87+
88+
89+
}
90+
91+
}
92+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.oracle.weblogicx.imagebuilder.builder.util;
2+
3+
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.nio.charset.Charset;
7+
8+
import javax.xml.parsers.DocumentBuilder;
9+
import javax.xml.parsers.DocumentBuilderFactory;
10+
import javax.xml.parsers.ParserConfigurationException;
11+
12+
import org.apache.http.HttpEntity;
13+
import org.apache.http.HttpResponse;
14+
import org.apache.http.StatusLine;
15+
import org.apache.http.client.ClientProtocolException;
16+
import org.apache.http.client.HttpResponseException;
17+
import org.apache.http.client.ResponseHandler;
18+
import org.apache.http.client.fluent.Request;
19+
import org.apache.http.entity.ContentType;
20+
import org.apache.http.protocol.HTTP;
21+
import org.w3c.dom.Document;
22+
import org.xml.sax.SAXException;
23+
24+
public class HttpUtil {
25+
26+
public static Document getXMLContent(String url) throws IOException {
27+
28+
Document result = Request.Get(url)
29+
.execute().handleResponse(new ResponseHandler<Document>() {
30+
31+
public Document handleResponse(final HttpResponse response) throws IOException {
32+
StatusLine statusLine = response.getStatusLine();
33+
HttpEntity entity = response.getEntity();
34+
if (statusLine.getStatusCode() >= 300) {
35+
throw new HttpResponseException(
36+
statusLine.getStatusCode(),
37+
statusLine.getReasonPhrase());
38+
}
39+
if (entity == null) {
40+
throw new ClientProtocolException("Response contains no content");
41+
}
42+
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
43+
try {
44+
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
45+
ContentType contentType = ContentType.getOrDefault(entity);
46+
if (!contentType.equals(ContentType.APPLICATION_XML)) {
47+
throw new ClientProtocolException("Unexpected content type:" +
48+
contentType);
49+
}
50+
String charset = contentType.getCharset().toString();
51+
if (charset == null) {
52+
charset = HTTP.DEFAULT_CONTENT_CHARSET;
53+
}
54+
return docBuilder.parse(entity.getContent(), charset);
55+
} catch (ParserConfigurationException ex) {
56+
throw new IllegalStateException(ex);
57+
} catch (SAXException ex) {
58+
throw new ClientProtocolException("Malformed XML document", ex);
59+
}
60+
}
61+
62+
});
63+
64+
return result;
65+
66+
}
67+
68+
public static void downloadFile(String url, String destination) throws IOException {
69+
Request.Get(url)
70+
.connectTimeout(1000)
71+
.socketTimeout(1000)
72+
.execute().saveContent(new File(destination));
73+
}
74+
75+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.oracle.weblogicx.imagebuilder.builder.util;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
7+
import javax.xml.parsers.DocumentBuilder;
8+
import javax.xml.parsers.DocumentBuilderFactory;
9+
import javax.xml.parsers.ParserConfigurationException;
10+
import javax.xml.xpath.XPath;
11+
import javax.xml.xpath.XPathConstants;
12+
import javax.xml.xpath.XPathExpressionException;
13+
import javax.xml.xpath.XPathFactory;
14+
15+
import org.w3c.dom.Document;
16+
import org.w3c.dom.Node;
17+
import org.w3c.dom.NodeList;
18+
import org.xml.sax.SAXException;
19+
20+
public class XPathUtil {
21+
22+
public static NodeList applyXPathReturnNodeList(Document doc, String expression) throws XPathExpressionException {
23+
24+
XPathFactory factory = XPathFactory.newInstance();
25+
26+
XPath xpath = factory.newXPath();
27+
28+
NodeList nodeList;
29+
30+
nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
31+
return nodeList;
32+
33+
}
34+
35+
36+
public static String applyXPathReturnString(Document doc, String expression) throws XPathExpressionException {
37+
38+
XPathFactory factory = XPathFactory.newInstance();
39+
40+
XPath xpath = factory.newXPath();
41+
42+
return (String)xpath.evaluate(expression, doc, XPathConstants.NODESET);
43+
44+
}
45+
46+
47+
48+
}

0 commit comments

Comments
 (0)