|
1 | 1 | package com.oracle.weblogicx.imagebuilder.builder.util;
|
2 | 2 |
|
3 |
| - |
4 | 3 | import java.io.File;
|
5 | 4 | import java.io.IOException;
|
6 |
| -import java.nio.charset.Charset; |
| 5 | +import java.io.StringReader; |
7 | 6 |
|
8 | 7 | import javax.xml.parsers.DocumentBuilder;
|
9 | 8 | import javax.xml.parsers.DocumentBuilderFactory;
|
10 | 9 | import javax.xml.parsers.ParserConfigurationException;
|
11 | 10 |
|
12 |
| -import org.apache.http.HttpEntity; |
13 |
| -import org.apache.http.HttpResponse; |
14 |
| -import org.apache.http.StatusLine; |
15 | 11 | 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; |
18 | 15 | 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; |
21 | 20 | import org.w3c.dom.Document;
|
| 21 | +import org.xml.sax.InputSource; |
22 | 22 | import org.xml.sax.SAXException;
|
23 | 23 |
|
24 | 24 | public class HttpUtil {
|
25 | 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; |
| 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 | + } |
65 | 60 |
|
66 | 61 | }
|
67 | 62 |
|
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 | + |
73 | 87 | }
|
74 | 88 |
|
| 89 | + |
| 90 | + public static void main(String args[]) throws IOException { |
| 91 | + |
| 92 | + |
| 93 | + } |
75 | 94 | }
|
0 commit comments