11
11
12
12
import org .elasticsearch .ElasticsearchStatusException ;
13
13
import org .elasticsearch .ResourceNotFoundException ;
14
- import org .elasticsearch .SpecialPermission ;
15
- import org .elasticsearch .common .CheckedSupplier ;
16
14
import org .elasticsearch .core .SuppressForbidden ;
17
15
import org .elasticsearch .rest .RestStatus ;
18
16
22
20
import java .net .HttpURLConnection ;
23
21
import java .net .PasswordAuthentication ;
24
22
import java .net .URL ;
25
- import java .security .AccessController ;
26
- import java .security .PrivilegedActionException ;
27
- import java .security .PrivilegedExceptionAction ;
28
23
import java .util .Arrays ;
29
24
import java .util .Objects ;
30
25
@@ -88,46 +83,44 @@ InputStream get(final PasswordAuthentication auth, final String url) throws IOEx
88
83
89
84
final String originalAuthority = new URL (url ).getAuthority ();
90
85
91
- return doPrivileged (() -> {
92
- String innerUrl = url ;
93
- HttpURLConnection conn = createConnection (auth , innerUrl );
94
-
95
- int redirectsCount = 0 ;
96
- while (true ) {
97
- switch (conn .getResponseCode ()) {
98
- case HTTP_OK :
99
- return getInputStream (conn );
100
- case HTTP_MOVED_PERM :
101
- case HTTP_MOVED_TEMP :
102
- case HTTP_SEE_OTHER :
103
- if (redirectsCount ++ > 50 ) {
104
- throw new IllegalStateException ("too many redirects connection to [" + url + "]" );
105
- }
106
-
107
- // deal with redirections (including relative urls)
108
- final String location = conn .getHeaderField ("Location" );
109
- final URL base = new URL (innerUrl );
110
- final URL next = new URL (base , location );
111
- innerUrl = next .toExternalForm ();
112
-
113
- // compare the *original* authority and the next authority to determine whether to include auth details.
114
- // this means that the host and port (if it is provided explicitly) are considered. it also means that if we
115
- // were to ping-pong back to the original authority, then we'd start including the auth details again.
116
- final String nextAuthority = next .getAuthority ();
117
- if (originalAuthority .equals (nextAuthority )) {
118
- conn = createConnection (auth , innerUrl );
119
- } else {
120
- conn = createConnection (NO_AUTH , innerUrl );
121
- }
122
- break ;
123
- case HTTP_NOT_FOUND :
124
- throw new ResourceNotFoundException ("{} not found" , url );
125
- default :
126
- int responseCode = conn .getResponseCode ();
127
- throw new ElasticsearchStatusException ("error during downloading {}" , RestStatus .fromCode (responseCode ), url );
128
- }
86
+ String innerUrl = url ;
87
+ HttpURLConnection conn = createConnection (auth , innerUrl );
88
+
89
+ int redirectsCount = 0 ;
90
+ while (true ) {
91
+ switch (conn .getResponseCode ()) {
92
+ case HTTP_OK :
93
+ return getInputStream (conn );
94
+ case HTTP_MOVED_PERM :
95
+ case HTTP_MOVED_TEMP :
96
+ case HTTP_SEE_OTHER :
97
+ if (redirectsCount ++ > 50 ) {
98
+ throw new IllegalStateException ("too many redirects connection to [" + url + "]" );
99
+ }
100
+
101
+ // deal with redirections (including relative urls)
102
+ final String location = conn .getHeaderField ("Location" );
103
+ final URL base = new URL (innerUrl );
104
+ final URL next = new URL (base , location );
105
+ innerUrl = next .toExternalForm ();
106
+
107
+ // compare the *original* authority and the next authority to determine whether to include auth details.
108
+ // this means that the host and port (if it is provided explicitly) are considered. it also means that if we
109
+ // were to ping-pong back to the original authority, then we'd start including the auth details again.
110
+ final String nextAuthority = next .getAuthority ();
111
+ if (originalAuthority .equals (nextAuthority )) {
112
+ conn = createConnection (auth , innerUrl );
113
+ } else {
114
+ conn = createConnection (NO_AUTH , innerUrl );
115
+ }
116
+ break ;
117
+ case HTTP_NOT_FOUND :
118
+ throw new ResourceNotFoundException ("{} not found" , url );
119
+ default :
120
+ int responseCode = conn .getResponseCode ();
121
+ throw new ElasticsearchStatusException ("error during downloading {}" , RestStatus .fromCode (responseCode ), url );
129
122
}
130
- });
123
+ }
131
124
}
132
125
133
126
@ SuppressForbidden (reason = "we need socket connection to download data from internet" )
@@ -150,13 +143,4 @@ protected PasswordAuthentication getPasswordAuthentication() {
150
143
conn .setInstanceFollowRedirects (false );
151
144
return conn ;
152
145
}
153
-
154
- private static <R > R doPrivileged (final CheckedSupplier <R , IOException > supplier ) throws IOException {
155
- SpecialPermission .check ();
156
- try {
157
- return AccessController .doPrivileged ((PrivilegedExceptionAction <R >) supplier ::get );
158
- } catch (PrivilegedActionException e ) {
159
- throw (IOException ) e .getCause ();
160
- }
161
- }
162
146
}
0 commit comments