Skip to content

Commit ea54b17

Browse files
Steve BlockAndroid (Google) Code Review
authored andcommitted
Merge "Clean up SslError"
2 parents 85b10b0 + 9e334db commit ea54b17

1 file changed

Lines changed: 49 additions & 59 deletions

File tree

core/java/android/net/http/SslError.java

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import java.security.cert.X509Certificate;
2020

2121
/**
22-
* One or more individual SSL errors and the associated SSL certificate
22+
* This class represents a set of one or more SSL errors and the associated SSL
23+
* certificate.
2324
*/
2425
public class SslError {
2526

@@ -48,16 +49,17 @@ public class SslError {
4849
*/
4950
public static final int SSL_DATE_INVALID = 4;
5051
/**
51-
* The certificate is invalid
52+
* A generic error occurred
5253
*/
5354
public static final int SSL_INVALID = 5;
5455

5556

5657
/**
57-
* The number of different SSL errors (update if you add a new SSL error!!!)
58+
* The number of different SSL errors.
5859
* @deprecated This constant is not necessary for using the SslError API and
5960
* can change from release to release.
6061
*/
62+
// Update if you add a new SSL error!!!
6163
@Deprecated
6264
public static final int SSL_MAX_ERROR = 6;
6365

@@ -78,55 +80,55 @@ public class SslError {
7880
final String mUrl;
7981

8082
/**
81-
* Creates a new SSL error set object
83+
* Creates a new SslError object using the supplied error and certificate.
84+
* The URL will be set to the empty string.
8285
* @param error The SSL error
8386
* @param certificate The associated SSL certificate
8487
* @deprecated Use {@link #SslError(int, SslCertificate, String)}
8588
*/
8689
@Deprecated
8790
public SslError(int error, SslCertificate certificate) {
88-
addError(error);
89-
if (certificate == null) {
90-
throw new NullPointerException("certificate is null.");
91-
}
92-
mCertificate = certificate;
93-
mUrl = "";
91+
this(error, certificate, "");
9492
}
9593

9694
/**
97-
* Creates a new SSL error set object
95+
* Creates a new SslError object using the supplied error and certificate.
96+
* The URL will be set to the empty string.
9897
* @param error The SSL error
9998
* @param certificate The associated SSL certificate
10099
* @deprecated Use {@link #SslError(int, X509Certificate, String)}
101100
*/
102101
@Deprecated
103102
public SslError(int error, X509Certificate certificate) {
104-
addError(error);
105-
if (certificate == null) {
106-
throw new NullPointerException("certificate is null.");
107-
}
108-
mCertificate = new SslCertificate(certificate);
109-
mUrl = "";
103+
this(error, certificate, "");
110104
}
111105

112106
/**
113-
* Creates a new SSL error set object
107+
* Creates a new SslError object using the supplied error, certificate and
108+
* URL.
114109
* @param error The SSL error
115110
* @param certificate The associated SSL certificate
116-
* @param url The associated URL.
111+
* @param url The associated URL
117112
*/
118113
public SslError(int error, SslCertificate certificate, String url) {
114+
assert certificate != null;
115+
assert url != null;
119116
addError(error);
120-
if (certificate == null) {
121-
throw new NullPointerException("certificate is null.");
122-
}
123117
mCertificate = certificate;
124-
if (url == null) {
125-
throw new NullPointerException("url is null.");
126-
}
127118
mUrl = url;
128119
}
129120

121+
/**
122+
* Creates a new SslError object using the supplied error, certificate and
123+
* URL.
124+
* @param error The SSL error
125+
* @param certificate The associated SSL certificate
126+
* @param url The associated URL
127+
*/
128+
public SslError(int error, X509Certificate certificate, String url) {
129+
this(error, new SslCertificate(certificate), url);
130+
}
131+
130132
/**
131133
* Creates an SslError object from a chromium error code.
132134
* @param error The chromium error code
@@ -138,56 +140,42 @@ public static SslError SslErrorFromChromiumErrorCode(
138140
int error, SslCertificate cert, String url) {
139141
// The chromium error codes are in:
140142
// external/chromium/net/base/net_error_list.h
141-
if (error > -200 || error < -299) {
142-
throw new NullPointerException("Not a valid chromium SSL error code.");
143-
}
143+
assert (error >= -299 && error <= -200);
144144
if (error == -200)
145145
return new SslError(SSL_IDMISMATCH, cert, url);
146146
if (error == -201)
147147
return new SslError(SSL_DATE_INVALID, cert, url);
148148
if (error == -202)
149149
return new SslError(SSL_UNTRUSTED, cert, url);
150-
// Map all other errors to SSL_INVALID
150+
// Map all other codes to SSL_INVALID.
151151
return new SslError(SSL_INVALID, cert, url);
152152
}
153153

154154
/**
155-
* Creates a new SSL error set object
156-
* @param error The SSL error
157-
* @param certificate The associated SSL certificate
158-
* @param url The associated URL.
159-
*/
160-
public SslError(int error, X509Certificate certificate, String url) {
161-
addError(error);
162-
if (certificate == null) {
163-
throw new NullPointerException("certificate is null.");
164-
}
165-
mCertificate = new SslCertificate(certificate);
166-
if (url == null) {
167-
throw new NullPointerException("url is null.");
168-
}
169-
mUrl = url;
170-
}
171-
172-
/**
173-
* @return The SSL certificate associated with the error set, non-null.
155+
* Gets the SSL certificate associated with this object.
156+
* @return The SSL certificate, non-null.
174157
*/
175158
public SslCertificate getCertificate() {
176159
return mCertificate;
177160
}
178161

179162
/**
180-
* @return The URL associated with the error set, non-null.
181-
* "" if one of the deprecated constructors is used.
163+
* Gets the URL associated with this object.
164+
* @return The URL, non-null.
182165
*/
166+
// TODO: When the WebView constructs an instance of this object, we
167+
// actually provide only the hostname, not the full URL. We should consider
168+
// deprecating this method, adding a new getHost() method and updating the
169+
// constructor arguments. See http://b/5410252.
183170
public String getUrl() {
184171
return mUrl;
185172
}
186173

187174
/**
188-
* Adds the SSL error to the error set
175+
* Adds the supplied SSL error to the set.
189176
* @param error The SSL error to add
190-
* @return True iff the error being added is a known SSL error
177+
* @return True if the error being added is a known SSL error, otherwise
178+
* false.
191179
*/
192180
public boolean addError(int error) {
193181
boolean rval = (0 <= error && error < SslError.SSL_MAX_ERROR);
@@ -199,8 +187,9 @@ public boolean addError(int error) {
199187
}
200188

201189
/**
202-
* @param error The SSL error to check
203-
* @return True iff the set includes the error
190+
* Determines whether this object includes the supplied error.
191+
* @param error The SSL error to check for
192+
* @return True if this object includes the error, otherwise false.
204193
*/
205194
public boolean hasError(int error) {
206195
boolean rval = (0 <= error && error < SslError.SSL_MAX_ERROR);
@@ -212,7 +201,8 @@ public boolean hasError(int error) {
212201
}
213202

214203
/**
215-
* @return The primary, most severe, SSL error in the set
204+
* Gets the most severe SSL error in this object's set of errors.
205+
* @return The most severe SSL error.
216206
*/
217207
public int getPrimaryError() {
218208
if (mErrors != 0) {
@@ -228,12 +218,12 @@ public int getPrimaryError() {
228218
}
229219

230220
/**
231-
* @return A String representation of this SSL error object
232-
* (used mostly for debugging).
221+
* Returns a string representation of this object.
222+
* @return A String representation of this object.
233223
*/
234224
public String toString() {
235225
return "primary error: " + getPrimaryError() +
236-
" certificate: " + getCertificate() +
237-
" on URL: " + getUrl();
226+
" certificate: " + getCertificate() +
227+
" on URL: " + getUrl();
238228
}
239229
}

0 commit comments

Comments
 (0)