|
3 | 3 | import java.io.File;
|
4 | 4 | import java.io.IOException;
|
5 | 5 | import java.io.StringReader;
|
| 6 | +import java.io.StringWriter; |
| 7 | +import java.io.Writer; |
6 | 8 |
|
7 | 9 | import javax.xml.parsers.DocumentBuilder;
|
8 | 10 | import javax.xml.parsers.DocumentBuilderFactory;
|
9 | 11 | 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; |
10 | 17 |
|
11 | 18 | import org.apache.http.HttpEntity;
|
12 | 19 | import org.apache.http.client.ClientProtocolException;
|
13 | 20 | import org.apache.http.client.CookieStore;
|
14 | 21 | import org.apache.http.client.config.RequestConfig;
|
15 | 22 | import org.apache.http.client.fluent.Executor;
|
16 | 23 | import org.apache.http.client.fluent.Request;
|
| 24 | +import org.apache.http.entity.mime.FormBodyPartBuilder; |
17 | 25 | import org.apache.http.entity.mime.HttpMultipartMode;
|
18 | 26 | import org.apache.http.entity.mime.MultipartEntityBuilder;
|
| 27 | +import org.apache.http.entity.mime.content.FileBody; |
19 | 28 | import org.apache.http.impl.client.BasicCookieStore;
|
20 | 29 | import org.apache.http.impl.client.CloseableHttpClient;
|
21 | 30 | import org.apache.http.impl.client.HttpClientBuilder;
|
|
26 | 35 |
|
27 | 36 | public class HttpUtil {
|
28 | 37 |
|
29 |
| - |
30 | 38 | public static Document getXMLContent(String url, String username, String password) throws IOException {
|
31 | 39 |
|
32 | 40 | RequestConfig.Builder config = RequestConfig.custom();
|
33 | 41 | config.setCircularRedirectsAllowed(true);
|
34 | 42 |
|
35 |
| - |
36 | 43 | CookieStore cookieStore = new BasicCookieStore();
|
37 | 44 | BasicClientCookie cc = new BasicClientCookie("oraclelicense", "a");
|
38 | 45 | cc.setDomain("edelivery.oracle.com");
|
39 | 46 | cookieStore.addCookie(cc);
|
40 | 47 |
|
41 |
| - CloseableHttpClient client = HttpClientBuilder.create() |
42 |
| - .setDefaultRequestConfig(config.build()) |
43 |
| - .useSystemProperties() |
44 |
| - .build(); |
| 48 | + CloseableHttpClient client = |
| 49 | + HttpClientBuilder.create().setDefaultRequestConfig(config.build()).useSystemProperties().build(); |
45 | 50 |
|
46 | 51 | Executor httpExecutor = Executor.newInstance(client).auth(username, password);
|
47 | 52 | httpExecutor.use(cookieStore);
|
48 | 53 |
|
49 |
| - String xmlString = httpExecutor.execute(Request.Get(url) |
50 |
| - .connectTimeout(30000) |
51 |
| - .socketTimeout(30000)).returnContent().asString(); |
| 54 | + String xmlString = |
| 55 | + httpExecutor.execute(Request.Get(url).connectTimeout(30000).socketTimeout(30000)).returnContent() |
| 56 | + .asString(); |
52 | 57 |
|
53 | 58 | try {
|
54 | 59 | DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
|
55 | 60 | DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
|
56 | 61 | InputSource is = new InputSource(new StringReader(xmlString));
|
57 |
| - return docBuilder.parse(is); |
| 62 | + Document doc = docBuilder.parse(is); |
| 63 | + prettyPrint(doc); |
| 64 | + return doc; |
58 | 65 | } catch (ParserConfigurationException ex) {
|
59 | 66 | throw new IllegalStateException(ex);
|
60 | 67 | } catch (SAXException ex) {
|
61 | 68 | throw new ClientProtocolException("Malformed XML document", ex);
|
| 69 | + } catch (Exception g) { |
| 70 | + throw new IllegalStateException(g); |
| 71 | + |
62 | 72 | }
|
63 | 73 |
|
64 | 74 | }
|
65 | 75 |
|
66 |
| - public static void downloadFile(String url, String destination, String username, String password) throws |
67 |
| - IOException { |
| 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 | + } |
| 84 | + |
| 85 | + public static void downloadFile(String url, String destination, String username, String password) |
| 86 | + throws IOException { |
68 | 87 | RequestConfig.Builder config = RequestConfig.custom();
|
69 | 88 | config.setCircularRedirectsAllowed(true);
|
70 | 89 | // config.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);
|
71 | 90 |
|
72 |
| - |
73 | 91 | CookieStore cookieStore = new BasicCookieStore();
|
74 | 92 | BasicClientCookie cc = new BasicClientCookie("oraclelicense", "a");
|
75 | 93 | cc.setDomain("edelivery.oracle.com");
|
76 | 94 | cookieStore.addCookie(cc);
|
77 | 95 |
|
78 |
| - CloseableHttpClient client = HttpClientBuilder.create() |
79 |
| - .setDefaultRequestConfig(config.build()) |
80 |
| - .useSystemProperties() |
81 |
| - .build(); |
| 96 | + CloseableHttpClient client = |
| 97 | + HttpClientBuilder.create().setDefaultRequestConfig(config.build()).useSystemProperties().build(); |
82 | 98 |
|
83 | 99 | Executor httpExecutor = Executor.newInstance(client).auth(username, password);
|
84 | 100 | httpExecutor.use(cookieStore);
|
85 | 101 |
|
86 |
| - httpExecutor.execute(Request.Get(url) |
87 |
| - .connectTimeout(30000) |
88 |
| - .socketTimeout(30000)).saveContent(new File(destination)); |
| 102 | + httpExecutor.execute(Request.Get(url).connectTimeout(30000).socketTimeout(30000)) |
| 103 | + .saveContent(new File(destination)); |
89 | 104 |
|
90 | 105 | }
|
91 | 106 |
|
92 |
| - public static String checkConflicts(String url, String payload, String username, String password) throws |
93 |
| - IOException { |
| 107 | + public static String checkConflicts(String url, String payload, String username, String password) |
| 108 | + throws IOException { |
94 | 109 | RequestConfig.Builder config = RequestConfig.custom();
|
95 | 110 | config.setCircularRedirectsAllowed(true);
|
96 | 111 |
|
97 | 112 | CookieStore cookieStore = new BasicCookieStore();
|
98 | 113 |
|
99 |
| - CloseableHttpClient client = HttpClientBuilder.create() |
100 |
| - .setDefaultRequestConfig(config.build()) |
101 |
| - .useSystemProperties() |
102 |
| - .build(); |
| 114 | + CloseableHttpClient client = |
| 115 | + HttpClientBuilder.create().setDefaultRequestConfig(config.build()).useSystemProperties().build(); |
103 | 116 |
|
104 | 117 | Executor httpExecutor = Executor.newInstance(client).auth(username, password);
|
105 | 118 | httpExecutor.use(cookieStore);
|
106 | 119 |
|
107 |
| - HttpEntity entity = MultipartEntityBuilder.create() |
108 |
| - .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) |
109 |
| - .addTextBody("request_xml", payload) |
| 120 | + // FormBodyPartBuilder.create( |
| 121 | + // "request_xml", |
| 122 | + // new StringBody(payload, ContentType.APPLICATION_XML) |
| 123 | + // ).build(); |
| 124 | + |
| 125 | + HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE) |
| 126 | + .addPart(FormBodyPartBuilder.create("request_xml", new FileBody(new File("/tmp/chkreq.xml"))).build()) |
110 | 127 | .build();
|
111 | 128 |
|
112 |
| - String result = httpExecutor.execute(Request.Post(url) |
113 |
| - .connectTimeout(30000) |
114 |
| - .socketTimeout(30000) |
115 |
| - .body(entity) |
116 |
| - ).returnContent().asString(); |
| 129 | + String response = |
| 130 | + httpExecutor.execute(Request.Post(url).connectTimeout(30000).socketTimeout(30000).body(entity)) |
| 131 | + .returnContent().asString(); |
117 | 132 |
|
118 |
| - System.out.println(result); |
119 |
| - return result; |
| 133 | + System.out.println(response); |
| 134 | + return ""; |
120 | 135 | }
|
121 | 136 |
|
122 |
| - |
123 |
| - public static void main(String args[]) throws IOException { |
124 |
| - |
125 |
| - |
126 |
| - } |
127 | 137 | }
|
0 commit comments