Skip to content

Commit c49e106

Browse files
authored
Fix Apache license fetching. (#512)
My fix in #484 changed the way the Apache license was handled, and all whitespace was stripped from this popular license. Essentially, the original code fetched it directly with the URL class while the other licenses were fetched with Jsoup. This commit restores this behavior. This has the unfortunate side-effect of re-introducing boilerplate code, but it's the most cost-effective solution for now.
1 parent d9ed980 commit c49e106

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

buildSrc/src/main/groovy/com/google/firebase/gradle/plugins/license/RemoteLicenseFetcher.groovy

+28-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.jsoup.nodes.Document
2121

2222
/**
2323
* Parse licenses from remote urls*/
24-
class RemoteLicenseFetcher implements Serializable {
24+
abstract class RemoteLicenseFetcher implements Serializable {
2525
private static final HtmlToPlainText TEXT_FORMATTER = new HtmlToPlainText()
2626

2727
private final String remoteUrl
@@ -51,7 +51,7 @@ class RemoteLicenseFetcher implements Serializable {
5151
Thread.sleep(i * 1000)
5252
}
5353

54-
return processDocument(Jsoup.connect(remoteUrl).get())
54+
return getTextAttempt()
5555
} catch (IOException ex) {
5656
if (storedEx == null) {
5757
storedEx = ex
@@ -64,10 +64,8 @@ class RemoteLicenseFetcher implements Serializable {
6464
throw storedEx
6565
}
6666

67-
/** Extracts the license text from the rest of the document. */
68-
String processDocument(Document doc) {
69-
return TEXT_FORMATTER.getPlainText(doc)
70-
}
67+
/** Attempts to download and extract the license exactly once. */
68+
abstract String getTextAttempt()
7169

7270
static final class AndroidSdkTermsFetcher extends RemoteLicenseFetcher {
7371

@@ -76,10 +74,11 @@ class RemoteLicenseFetcher implements Serializable {
7674
}
7775

7876
@Override
79-
String processDocument(Document doc) {
77+
String getTextAttempt() {
8078
// TODO(vkryachko, allisonbm92): Fix this silent failure.
8179
// This evaluates to an empty string. The HTML for this page must have changed since this
8280
// filter was original written. Interestingly, this is a hard-failure if run from Java.
81+
def doc = Jsoup.connect(getRemoteUrl()).get()
8382
return TEXT_FORMATTER.getPlainText(doc.select("#body-content > div.jd-descr > div")[0])
8483
}
8584
}
@@ -89,6 +88,11 @@ class RemoteLicenseFetcher implements Serializable {
8988
Apache2LicenseFetcher() {
9089
super("http://www.apache.org/licenses/LICENSE-2.0.txt")
9190
}
91+
92+
@Override
93+
String getTextAttempt() {
94+
return getRemoteUrl().toURL().getText()
95+
}
9296
}
9397

9498
static final class AnotherApache2LicenseFetcher extends RemoteLicenseFetcher {
@@ -98,7 +102,8 @@ class RemoteLicenseFetcher implements Serializable {
98102
}
99103

100104
@Override
101-
String processDocument(Document doc) {
105+
String getTextAttempt() {
106+
def doc = Jsoup.connect(getRemoteUrl()).get()
102107
return TEXT_FORMATTER.getPlainText(doc.select("#content-wrapper").get(0))
103108
}
104109
}
@@ -108,6 +113,11 @@ class RemoteLicenseFetcher implements Serializable {
108113
YetAnotherApache2LicenseFetcher() {
109114
super("http://www.apache.org/licenses/LICENSE-2.0")
110115
}
116+
117+
@Override
118+
String getTextAttempt() {
119+
return getRemoteUrl().toURL().getText()
120+
}
111121
}
112122

113123
static final class BSDLicenseFetcher extends RemoteLicenseFetcher {
@@ -117,7 +127,8 @@ class RemoteLicenseFetcher implements Serializable {
117127
}
118128

119129
@Override
120-
String processDocument(Document doc) {
130+
String getTextAttempt() {
131+
def doc = Jsoup.connect(getRemoteUrl()).get()
121132
return TEXT_FORMATTER.getPlainText(doc.select("#content-wrapper").get(0))
122133
}
123134
}
@@ -129,7 +140,8 @@ class RemoteLicenseFetcher implements Serializable {
129140
}
130141

131142
@Override
132-
String processDocument(Document doc) {
143+
String getTextAttempt() {
144+
def doc = Jsoup.connect(getRemoteUrl()).get()
133145
return TEXT_FORMATTER.getPlainText(doc.select("#deed").get(0))
134146
}
135147
}
@@ -141,7 +153,8 @@ class RemoteLicenseFetcher implements Serializable {
141153
}
142154

143155
@Override
144-
String processDocument(Document doc) {
156+
String getTextAttempt() {
157+
def doc = Jsoup.connect(getRemoteUrl()).get()
145158
return TEXT_FORMATTER.getPlainText(doc.select("#content-wrapper").get(0))
146159
}
147160
}
@@ -153,7 +166,8 @@ class RemoteLicenseFetcher implements Serializable {
153166
}
154167

155168
@Override
156-
String processDocument(Document doc) {
169+
String getTextAttempt() {
170+
def doc = Jsoup.connect(getRemoteUrl()).get()
157171
return TEXT_FORMATTER.getPlainText(doc.select("#content-wrapper").get(0))
158172
}
159173
}
@@ -166,7 +180,8 @@ class RemoteLicenseFetcher implements Serializable {
166180
}
167181

168182
@Override
169-
String processDocument(Document doc) {
183+
String getTextAttempt() {
184+
def doc = Jsoup.connect(getRemoteUrl()).get()
170185
return TEXT_FORMATTER.getPlainText(doc.select("body > table > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr:nth-child(3) > td > en > blockquote").get(0))
171186
}
172187
}

0 commit comments

Comments
 (0)