Skip to content

Commit 10eb816

Browse files
committed
fix various http client access issues with oracle site
Former-commit-id: 668997084cc87bdb158ac0af46e29b509257cf49
1 parent eb0b17e commit 10eb816

File tree

1 file changed

+72
-53
lines changed
  • src/main/java/com/oracle/weblogicx/imagebuilder/builder/util

1 file changed

+72
-53
lines changed
Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,94 @@
11
package com.oracle.weblogicx.imagebuilder.builder.util;
22

3-
43
import java.io.File;
54
import java.io.IOException;
6-
import java.nio.charset.Charset;
5+
import java.io.StringReader;
76

87
import javax.xml.parsers.DocumentBuilder;
98
import javax.xml.parsers.DocumentBuilderFactory;
109
import javax.xml.parsers.ParserConfigurationException;
1110

12-
import org.apache.http.HttpEntity;
13-
import org.apache.http.HttpResponse;
14-
import org.apache.http.StatusLine;
1511
import org.apache.http.client.ClientProtocolException;
16-
import org.apache.http.client.HttpResponseException;
17-
import org.apache.http.client.ResponseHandler;
12+
import org.apache.http.client.CookieStore;
13+
import org.apache.http.client.config.RequestConfig;
14+
import org.apache.http.client.fluent.Executor;
1815
import org.apache.http.client.fluent.Request;
19-
import org.apache.http.entity.ContentType;
20-
import org.apache.http.protocol.HTTP;
16+
import org.apache.http.impl.client.BasicCookieStore;
17+
import org.apache.http.impl.client.CloseableHttpClient;
18+
import org.apache.http.impl.client.HttpClientBuilder;
19+
import org.apache.http.impl.cookie.BasicClientCookie;
2120
import org.w3c.dom.Document;
21+
import org.xml.sax.InputSource;
2222
import org.xml.sax.SAXException;
2323

2424
public class HttpUtil {
2525

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;
26+
27+
public static Document getXMLContent(String url, String username, String password) throws IOException {
28+
29+
RequestConfig.Builder config = RequestConfig.custom();
30+
config.setCircularRedirectsAllowed(true);
31+
32+
33+
CookieStore cookieStore = new BasicCookieStore();
34+
BasicClientCookie cc = new BasicClientCookie("oraclelicense", "a");
35+
cc.setDomain("edelivery.oracle.com");
36+
cookieStore.addCookie(cc);
37+
38+
CloseableHttpClient client = HttpClientBuilder.create()
39+
.setDefaultRequestConfig(config.build())
40+
.useSystemProperties()
41+
.build();
42+
43+
Executor httpExecutor = Executor.newInstance(client).auth(username, password);
44+
httpExecutor.use(cookieStore);
45+
46+
String xmlString = httpExecutor.execute(Request.Get(url)
47+
.connectTimeout(30000)
48+
.socketTimeout(30000)).returnContent().asString();
49+
50+
try {
51+
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
52+
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
53+
InputSource is = new InputSource(new StringReader(xmlString));
54+
return docBuilder.parse(is);
55+
} catch (ParserConfigurationException ex) {
56+
throw new IllegalStateException(ex);
57+
} catch (SAXException ex) {
58+
throw new ClientProtocolException("Malformed XML document", ex);
59+
}
6560

6661
}
6762

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));
63+
public static void downloadFile(String url, String destination, String username, String password) throws
64+
IOException {
65+
RequestConfig.Builder config = RequestConfig.custom();
66+
config.setCircularRedirectsAllowed(true);
67+
// config.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);
68+
69+
70+
CookieStore cookieStore = new BasicCookieStore();
71+
BasicClientCookie cc = new BasicClientCookie("oraclelicense", "a");
72+
cc.setDomain("edelivery.oracle.com");
73+
cookieStore.addCookie(cc);
74+
75+
CloseableHttpClient client = HttpClientBuilder.create()
76+
.setDefaultRequestConfig(config.build())
77+
.useSystemProperties()
78+
.build();
79+
80+
Executor httpExecutor = Executor.newInstance(client).auth(username, password);
81+
httpExecutor.use(cookieStore);
82+
83+
httpExecutor.execute(Request.Get(url)
84+
.connectTimeout(30000)
85+
.socketTimeout(30000)).saveContent(new File(destination));
86+
7387
}
7488

89+
90+
public static void main(String args[]) throws IOException {
91+
92+
93+
}
7594
}

0 commit comments

Comments
 (0)