FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/build/jre/src/Downloader.java at master · processing/processing · GitHub
processing
processing
Repository navigation
Code
Issues
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
build
/
jre
/
src
/
Downloader.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
201 lines (158 loc) · 5.51 KB
Breadcrumbs
processing
/
build
/
jre
/
src
/
Downloader.java
Copy path
File metadata and controls
201 lines (158 loc) · 5.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import
java
.
io
.*;
import
java
.
net
.*;
import
java
.
util
.*;
import
org
.
apache
.
tools
.
ant
.
BuildException
;
import
org
.
apache
.
tools
.
ant
.
Task
;
/**
* Ant Task for downloading the latest JRE or JDK from Oracle.
* This was used to set a cookie properly to retrieve a JRE.
* Nowadays the older versions have been removed from Oracle's site,
* so this is hard wired it to use download.processing.org instead.
*/
public
class
Downloader
extends
Task
{
static
final
boolean
ORACLE_SUCKS
=
true
;
// that's final
static
final
String
COOKIE
=
"oraclelicense=accept-securebackup-cookie"
;
private
int
version
;
// Java 8
private
int
update
;
// Update 131
private
int
build
;
// Build 11
// https://gist.github.com/P7h/9741922
// http://stackoverflow.com/q/10268583
private
String
hash
;
// d54c1d3a095b4ff2b6607d096fa80163
private
boolean
jdk
;
// false if JRE
private
String
flavor
;
private
String
path
;
// target path
public
Downloader
() { }
public
void
setVersion
(
int
version
) {
this
.
version
=
version
;
}
public
void
setUpdate
(
int
update
) {
this
.
update
=
update
;
}
public
void
setBuild
(
int
build
) {
this
.
build
=
build
;
}
public
void
setHash
(
String
hash
) {
this
.
hash
=
hash
;
}
public
void
setJDK
(
boolean
jdk
) {
this
.
jdk
=
jdk
;
}
public
void
setFlavor
(
String
flavor
) {
this
.
flavor
=
flavor
;
}
public
void
setPath
(
String
path
) {
this
.
path
=
path
;
}
public
void
execute
()
throws
BuildException
{
if
(
version
==
0
) {
throw
new
BuildException
(
"Version (i.e. 7 or 8) must be set"
);
}
if
(
build
==
0
) {
throw
new
BuildException
(
"Build number must be set"
);
}
if
(
flavor
==
null
) {
throw
new
BuildException
(
"You've gotta choose a flavor (macosx-x64.dmg, windows-x64.exe...)"
);
}
if
(
update
>=
121
&&
hash
==
null
) {
throw
new
BuildException
(
"Starting with 8u121, a hash is required, see https://gist.github.com/P7h/9741922"
);
}
try
{
download
();
}
catch
(
IOException
e
) {
throw
new
BuildException
(
e
);
}
}
void
download
()
throws
IOException
{
String
filename
= (
jdk
?
"jdk"
:
"jre"
) +
(
update
==
0
?
String
.
format
(
"-%d-%s"
,
version
,
flavor
) :
String
.
format
(
"-%du%d-%s"
,
version
,
update
,
flavor
));
if
(
path
==
null
) {
path
=
filename
;
}
String
url
=
"http://download.oracle.com/otn-pub/java/jdk/"
+
(
update
==
0
?
String
.
format
(
"%d-b%02d/"
,
version
,
build
) :
String
.
format
(
"%du%d-b%02d/"
,
version
,
update
,
build
));
// URL format changed starting with 8u121
if
(
update
>=
121
) {
url
+=
hash
+
"/"
;
}
if
(
ORACLE_SUCKS
) {
url
=
"https://download.processing.org/java/"
;
}
// Finally, add the filename to the end
url
+=
filename
;
HttpURLConnection
conn
=
(
HttpURLConnection
)
new
URL
(
url
).
openConnection
();
conn
.
setRequestProperty
(
"Cookie"
,
COOKIE
);
//printHeaders(conn);
//conn.connect();
while
(
conn
.
getResponseCode
() ==
302
||
conn
.
getResponseCode
() ==
301
) {
Map
<
String
,
List
<
String
>>
headers
=
conn
.
getHeaderFields
();
List
<
String
>
location
=
headers
.
get
(
"Location"
);
if
(
location
.
size
() ==
1
) {
url
=
location
.
get
(
0
);
System
.
out
.
println
(
"Redirecting to "
+
url
);
}
else
{
throw
new
BuildException
(
"Got "
+
location
.
size
() +
" locations."
);
}
List
<
String
>
cookies
=
headers
.
get
(
"Set-Cookie"
);
conn
= (
HttpURLConnection
)
new
URL
(
url
).
openConnection
();
if
(
cookies
!=
null
) {
for
(
String
cookie
:
cookies
) {
conn
.
setRequestProperty
(
"Cookie"
,
cookie
);
}
}
conn
.
setRequestProperty
(
"Cookie"
,
COOKIE
);
conn
.
connect
();
}
if
(
conn
.
getResponseCode
() ==
200
) {
InputStream
input
=
conn
.
getInputStream
();
BufferedInputStream
bis
=
new
BufferedInputStream
(
input
);
File
outputFile
=
new
File
(
path
);
//folder, filename);
System
.
out
.
format
(
"Downloading %s from %s%n"
,
outputFile
.
getAbsolutePath
(),
url
);
// Write to a temp file so that we don't have an incomplete download
// masquerading as a good archive.
File
tempFile
=
File
.
createTempFile
(
"download"
,
""
,
outputFile
.
getParentFile
());
BufferedOutputStream
output
=
new
BufferedOutputStream
(
new
FileOutputStream
(
tempFile
));
int
c
=
bis
.
read
();
while
(
c
!= -
1
) {
output
.
write
(
c
);
c
=
bis
.
read
();
}
bis
.
close
();
output
.
flush
();
output
.
close
();
if
(
outputFile
.
exists
()) {
if
(!
outputFile
.
delete
()) {
throw
new
BuildException
(
"Could not delete old download: "
+
outputFile
.
getAbsolutePath
());
}
}
if
(!
tempFile
.
renameTo
(
outputFile
)) {
throw
new
BuildException
(
String
.
format
(
"Could not rename %s to %s"
,
tempFile
.
getAbsolutePath
(),
outputFile
.
getAbsolutePath
()));
}
}
else
{
printHeaders
(
conn
);
System
.
exit
(
1
);
}
}
static
void
printHeaders
(
URLConnection
conn
) {
Map
<
String
,
List
<
String
>>
headers
=
conn
.
getHeaderFields
();
Set
<
Map
.
Entry
<
String
,
List
<
String
>>>
entrySet
=
headers
.
entrySet
();
for
(
Map
.
Entry
<
String
,
List
<
String
>>
entry
:
entrySet
) {
String
headerName
=
entry
.
getKey
();
System
.
out
.
println
(
"Header Name:"
+
headerName
);
List
<
String
>
headerValues
=
entry
.
getValue
();
for
(
String
value
:
headerValues
) {
System
.
out
.
print
(
"Header value:"
+
value
);
}
System
.
out
.
println
();
System
.
out
.
println
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL