Skip to content

Commit e8d6f95

Browse files
authored
Correctly handle proxy servers that require authentication (#278)
1 parent 7fb578b commit e8d6f95

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/aru/AruUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.oracle.weblogic.imagetool.util.XPathUtil;
1919
import org.apache.http.HttpStatus;
2020
import org.apache.http.client.HttpResponseException;
21-
import org.apache.http.client.fluent.Executor;
2221
import org.apache.http.client.fluent.Request;
2322
import org.w3c.dom.Document;
2423
import org.w3c.dom.NamedNodeMap;
@@ -437,8 +436,9 @@ public String downloadAruPatch(AruPatch aruPatch, String targetDir, String usern
437436
String filename = targetDir + File.separator + aruPatch.fileName();
438437
logger.info("IMG-0018", aruPatch.patchId());
439438
try {
440-
Executor.newInstance(HttpUtil.getOraClient(username, password))
441-
.execute(Request.Get(aruPatch.downloadUrl()).connectTimeout(30000).socketTimeout(30000))
439+
HttpUtil.getHttpExecutor(username, password)
440+
.execute(Request.Get(aruPatch.downloadUrl()).connectTimeout(30000)
441+
.socketTimeout(30000))
442442
.saveContent(new File(filename));
443443
} catch (Exception ex) {
444444
String message = String.format("Failed to download and save file %s from %s: %s", filename,

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/HttpUtil.java

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
1818
import org.apache.http.HttpEntity;
1919
import org.apache.http.HttpEntityEnclosingRequest;
20+
import org.apache.http.HttpHost;
2021
import org.apache.http.HttpRequest;
21-
import org.apache.http.auth.AuthScope;
22-
import org.apache.http.auth.UsernamePasswordCredentials;
2322
import org.apache.http.client.ClientProtocolException;
2423
import org.apache.http.client.CookieStore;
25-
import org.apache.http.client.CredentialsProvider;
2624
import org.apache.http.client.HttpClient;
2725
import org.apache.http.client.HttpRequestRetryHandler;
2826
import org.apache.http.client.config.CookieSpecs;
@@ -33,10 +31,8 @@
3331
import org.apache.http.entity.mime.HttpMultipartMode;
3432
import org.apache.http.entity.mime.MultipartEntityBuilder;
3533
import org.apache.http.impl.client.BasicCookieStore;
36-
import org.apache.http.impl.client.BasicCredentialsProvider;
3734
import org.apache.http.impl.client.CloseableHttpClient;
3835
import org.apache.http.impl.client.HttpClientBuilder;
39-
import org.apache.http.impl.cookie.BasicClientCookie;
4036
import org.w3c.dom.Document;
4137
import org.xml.sax.InputSource;
4238
import org.xml.sax.SAXException;
@@ -100,8 +96,8 @@ public static Document parseXmlString(String xmlString) throws ClientProtocolExc
10096
*/
10197
public static Document getXMLContent(String url, String username, String password) throws IOException {
10298
logger.entering(url);
103-
String xmlString = Executor.newInstance(getOraClient(username, password))
104-
.execute(Request.Get(url).connectTimeout(30000).socketTimeout(30000))
99+
String xmlString = getHttpExecutor(username,password).execute(Request.Get(url).connectTimeout(30000)
100+
.socketTimeout(30000))
105101
.returnContent().asString();
106102
logger.exiting(xmlString);
107103
return parseXmlString(xmlString);
@@ -120,25 +116,51 @@ public static HttpClient getOraClient(String userId, String password) {
120116
config.setCookieSpec(CookieSpecs.STANDARD);
121117

122118
CookieStore cookieStore = new BasicCookieStore();
123-
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
124-
125-
if (userId != null && password != null) {
126-
BasicClientCookie cc = new BasicClientCookie("oraclelicense", "a");
127-
cc.setDomain("edelivery.oracle.com");
128-
cookieStore.addCookie(cc);
129-
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
130-
userId, password));
131-
}
132-
HttpClient result = HttpClientBuilder.create()
119+
120+
String proxyHost = System.getProperty("https.proxyHost");
121+
String proxyPort = System.getProperty("https.proxyPort");
122+
HttpClient result;
123+
result = HttpClientBuilder.create()
133124
.setDefaultRequestConfig(config.build())
134125
.setRetryHandler(retryHandler())
126+
.setProxy(proxyHost != null ? new HttpHost(proxyHost, Integer.parseInt(proxyPort)) : null)
135127
.setUserAgent("Wget/1.10")
136128
.setDefaultCookieStore(cookieStore).useSystemProperties()
137-
.setDefaultCredentialsProvider(credentialsProvider).build();
129+
.build();
130+
138131
logger.exiting();
139132
return result;
140133
}
141134

135+
/**
136+
* Return a Executor for http access.
137+
* @param supportUserName oracle support username
138+
* @param supportPassword oracle support password
139+
* @return Executor
140+
*/
141+
142+
public static Executor getHttpExecutor(String supportUserName, String supportPassword) {
143+
144+
String proxyUser = System.getProperty("https.proxyUser");
145+
String proxyPassword = System.getProperty("https.proxyPassword");
146+
String proxyHost = System.getProperty("https.proxyHost");
147+
String proxyPort = System.getProperty("https.proxyPort");
148+
Executor executor = Executor.newInstance(getOraClient(supportUserName, supportPassword));
149+
150+
151+
if (proxyHost != null) {
152+
if (proxyPassword != null) {
153+
executor.auth(new HttpHost(proxyHost, Integer.parseInt(proxyPort)), proxyUser, proxyPassword);
154+
}
155+
156+
executor
157+
.auth(new HttpHost("login.oracle.com", 443), supportUserName, supportPassword)
158+
.auth(new HttpHost("updates.oracle.com", 443), supportUserName, supportPassword)
159+
.authPreemptiveProxy(new HttpHost(proxyHost, Integer.parseInt(proxyPort)));
160+
}
161+
return executor;
162+
}
163+
142164
private static HttpRequestRetryHandler retryHandler() {
143165
return (exception, executionCount, context) -> {
144166

@@ -202,16 +224,18 @@ public static Document postCheckConflictRequest(String url, String payload, Stri
202224
.setUserAgent("Wget/1.10")
203225
.useSystemProperties().build();
204226

205-
Executor httpExecutor = Executor.newInstance(client).auth(username, password);
227+
Executor httpExecutor = HttpUtil.getHttpExecutor(username, password);
206228
httpExecutor.use(cookieStore);
207229

230+
208231
// Has to do search first, otherwise results in 302
209232
// MUST use the same httpExecutor to maintain session
210233

211234

212235
boolean complete = false;
213236
int count = 0;
214237
String xmlString = null;
238+
215239
while (!complete) {
216240
try {
217241
httpExecutor
@@ -223,7 +247,9 @@ public static Document postCheckConflictRequest(String url, String payload, Stri
223247
.build();
224248

225249
xmlString =
226-
httpExecutor.execute(Request.Post(url).connectTimeout(30000).socketTimeout(30000).body(entity))
250+
httpExecutor.execute(Request.Post(url).connectTimeout(30000)
251+
.socketTimeout(30000)
252+
.body(entity))
227253
.returnContent().asString();
228254
complete = true;
229255
} catch (IOException ioe) {

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ private static void setSystemProxy(String proxyUrl, String protocolToSet) throws
172172
int port = url.getPort() == -1 ? url.getDefaultPort() : url.getPort();
173173
String userInfo = url.getUserInfo();
174174
String protocol = protocolToSet == null ? url.getProtocol() : protocolToSet;
175-
176175
if (host != null && port != -1) {
177176
System.setProperty(String.format("%s.proxyHost", protocol), host);
178177
System.setProperty(String.format("%s.proxyPort", protocol), String.valueOf(port));

0 commit comments

Comments
 (0)