|
1 |
| -// Copyright (c) 2019, Oracle Corporation and/or its affiliates. All rights reserved. |
| 1 | +// Copyright (c) 2019, 2020, Oracle Corporation and/or its affiliates. All rights reserved. |
2 | 2 | // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
|
3 | 3 |
|
4 | 4 | package com.oracle.weblogic.imagetool.util;
|
5 | 5 |
|
6 | 6 | import java.io.File;
|
7 | 7 | import java.io.IOException;
|
| 8 | +import java.io.InterruptedIOException; |
8 | 9 | import java.io.StringReader;
|
| 10 | +import java.net.UnknownHostException; |
9 | 11 | import java.util.ArrayList;
|
10 | 12 | import java.util.List;
|
| 13 | +import javax.net.ssl.SSLException; |
11 | 14 | import javax.xml.XMLConstants;
|
12 | 15 | import javax.xml.parsers.DocumentBuilder;
|
13 | 16 | import javax.xml.parsers.DocumentBuilderFactory;
|
14 | 17 |
|
15 | 18 | import com.oracle.weblogic.imagetool.logging.LoggingFacade;
|
16 | 19 | import com.oracle.weblogic.imagetool.logging.LoggingFactory;
|
17 | 20 | import org.apache.http.HttpEntity;
|
| 21 | +import org.apache.http.HttpEntityEnclosingRequest; |
| 22 | +import org.apache.http.HttpRequest; |
18 | 23 | import org.apache.http.auth.AuthScope;
|
19 | 24 | import org.apache.http.auth.UsernamePasswordCredentials;
|
20 | 25 | import org.apache.http.client.ClientProtocolException;
|
21 | 26 | import org.apache.http.client.CookieStore;
|
22 | 27 | import org.apache.http.client.CredentialsProvider;
|
23 | 28 | import org.apache.http.client.HttpClient;
|
| 29 | +import org.apache.http.client.HttpRequestRetryHandler; |
24 | 30 | import org.apache.http.client.config.CookieSpecs;
|
25 | 31 | import org.apache.http.client.config.RequestConfig;
|
26 | 32 | import org.apache.http.client.fluent.Executor;
|
27 | 33 | import org.apache.http.client.fluent.Request;
|
| 34 | +import org.apache.http.client.protocol.HttpClientContext; |
28 | 35 | import org.apache.http.entity.mime.HttpMultipartMode;
|
29 | 36 | import org.apache.http.entity.mime.MultipartEntityBuilder;
|
30 | 37 | import org.apache.http.impl.client.BasicCookieStore;
|
@@ -107,15 +114,54 @@ private static HttpClient getOraClient(String userId, String password) {
|
107 | 114 | credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
|
108 | 115 | userId, password));
|
109 | 116 | }
|
110 |
| - HttpClient result = HttpClientBuilder.create().setDefaultRequestConfig(config.build()) |
| 117 | + HttpClient result = HttpClientBuilder.create() |
| 118 | + .setDefaultRequestConfig(config.build()) |
| 119 | + .setRetryHandler(retryHandler()) |
111 | 120 | .setDefaultCookieStore(cookieStore).useSystemProperties()
|
112 | 121 | .setDefaultCredentialsProvider(credentialsProvider).build();
|
113 | 122 | logger.exiting();
|
114 | 123 | return result;
|
115 | 124 | }
|
116 | 125 |
|
| 126 | + private static HttpRequestRetryHandler retryHandler() { |
| 127 | + return (exception, executionCount, context) -> { |
| 128 | + |
| 129 | + if (executionCount > 3) { |
| 130 | + // Do not retry if over max retries |
| 131 | + return false; |
| 132 | + } |
| 133 | + if (exception instanceof InterruptedIOException) { |
| 134 | + // Timeout |
| 135 | + return false; |
| 136 | + } |
| 137 | + if (exception instanceof UnknownHostException) { |
| 138 | + // Unknown host |
| 139 | + return false; |
| 140 | + } |
| 141 | + if (exception instanceof SSLException) { |
| 142 | + // SSL handshake failed |
| 143 | + return false; |
| 144 | + } |
| 145 | + HttpClientContext clientContext = HttpClientContext.adapt(context); |
| 146 | + HttpRequest request = clientContext.getRequest(); |
| 147 | + // return true if it is okay to retry this request type |
| 148 | + boolean retriable = !(request instanceof HttpEntityEnclosingRequest); |
| 149 | + if (retriable) { |
| 150 | + try { |
| 151 | + logger.warning("Connect failed, retrying in 10 seconds, attempts={0} ", executionCount); |
| 152 | + Thread.sleep(10000); |
| 153 | + } catch (InterruptedException e) { |
| 154 | + //continue |
| 155 | + } |
| 156 | + return true; |
| 157 | + } else { |
| 158 | + return false; |
| 159 | + } |
| 160 | + }; |
| 161 | + } |
| 162 | + |
117 | 163 | /**
|
118 |
| - * Downlod a file from the url. |
| 164 | + * Download a file from the url. |
119 | 165 | *
|
120 | 166 | * @param url url of the aru server
|
121 | 167 | * @param fileName full path to save the file
|
|
0 commit comments