Skip to content

Commit 7e7b064

Browse files
committed
Cleanup redundant null checks and fix encoding inconsistencies
- Remove redundant null checks for URLConnection as URL.openConnection() never returns null. - Align TextFile.head() and TextFile.fastTail() to use UTF-8 consistently with the rest of the class. Signed-off-by: shbhmexe <shubhushukla586@gmail.com>
1 parent bde4fe7 commit 7e7b064

File tree

5 files changed

+252
-163
lines changed

5 files changed

+252
-163
lines changed

core/src/main/java/hudson/model/StreamBuildListener.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ public StreamBuildListener(File out, Charset charset) throws IOException {
4848
}
4949

5050
/**
51-
* @deprecated as of TODO
52-
* The caller should use {@link #StreamBuildListener(OutputStream, Charset)} to pass in
53-
* the charset and output stream separately, so that this class can handle encoding correctly.
51+
* @deprecated as of 2.540
52+
* The caller should use
53+
* {@link #StreamBuildListener(OutputStream, Charset)} to pass in
54+
* the charset and output stream separately, so that this class can
55+
* handle encoding correctly.
5456
*/
5557
@Deprecated
5658
public StreamBuildListener(OutputStream w) {
@@ -59,8 +61,10 @@ public StreamBuildListener(OutputStream w) {
5961

6062
/**
6163
* @deprecated as of 1.349
62-
* The caller should use {@link #StreamBuildListener(OutputStream, Charset)} to pass in
63-
* the charset and output stream separately, so that this class can handle encoding correctly.
64+
* The caller should use
65+
* {@link #StreamBuildListener(OutputStream, Charset)} to pass in
66+
* the charset and output stream separately, so that this class can
67+
* handle encoding correctly.
6468
*/
6569
@Deprecated
6670
public StreamBuildListener(PrintStream w) {

core/src/main/java/hudson/util/FormFieldValidator.java

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,19 @@
5151
import org.springframework.security.access.AccessDeniedException;
5252

5353
/**
54-
* Base class that provides the framework for doing on-the-fly form field validation.
54+
* Base class that provides the framework for doing on-the-fly form field
55+
* validation.
5556
*
5657
* <p>
57-
* The {@link #check()} method is to be implemented by derived classes to perform
58-
* the validation. See hudson-behavior.js 'validated' CSS class and 'checkUrl' attribute.
58+
* The {@link #check()} method is to be implemented by derived classes to
59+
* perform
60+
* the validation. See hudson-behavior.js 'validated' CSS class and 'checkUrl'
61+
* attribute.
5962
*
6063
* @author Kohsuke Kawaguchi
6164
* @deprecated as of 1.294
62-
* Use {@link FormValidation} as a return value in your check method.
65+
* Use {@link FormValidation} as a return value in your check
66+
* method.
6367
*/
6468
@Deprecated
6569
public abstract class FormFieldValidator {
@@ -80,18 +84,21 @@ public abstract class FormFieldValidator {
8084

8185
/**
8286
* @param adminOnly
83-
* Pass true to only let admin users to run the check. This is necessary
84-
* for security reason, so that unauthenticated user cannot obtain sensitive
85-
* information or run a process that may have side-effect.
87+
* Pass true to only let admin users to run the check. This is
88+
* necessary
89+
* for security reason, so that unauthenticated user cannot
90+
* obtain sensitive
91+
* information or run a process that may have side-effect.
8692
*/
8793
protected FormFieldValidator(StaplerRequest request, StaplerResponse response, boolean adminOnly) {
8894
this(request, response, adminOnly ? Jenkins.get() : null, adminOnly ? CHECK : null);
8995
}
9096

9197
/**
9298
* @deprecated
93-
* Use {@link #FormFieldValidator(Permission)} and remove {@link StaplerRequest} and {@link StaplerResponse}
94-
* from your "doCheck..." method parameter
99+
* Use {@link #FormFieldValidator(Permission)} and remove
100+
* {@link StaplerRequest} and {@link StaplerResponse}
101+
* from your "doCheck..." method parameter
95102
*/
96103
@Deprecated
97104
protected FormFieldValidator(StaplerRequest request, StaplerResponse response, Permission permission) {
@@ -100,19 +107,22 @@ protected FormFieldValidator(StaplerRequest request, StaplerResponse response, P
100107

101108
/**
102109
* @param permission
103-
* Permission needed to perform this validation, or null if no permission is necessary.
110+
* Permission needed to perform this validation, or null if no
111+
* permission is necessary.
104112
*/
105113
protected FormFieldValidator(Permission permission) {
106114
this(Stapler.getCurrentRequest(), Stapler.getCurrentResponse(), permission);
107115
}
108116

109117
/**
110118
* @deprecated
111-
* Use {@link #FormFieldValidator(AccessControlled,Permission)} and remove {@link StaplerRequest} and {@link StaplerResponse}
112-
* from your "doCheck..." method parameter
119+
* Use {@link #FormFieldValidator(AccessControlled,Permission)} and
120+
* remove {@link StaplerRequest} and {@link StaplerResponse}
121+
* from your "doCheck..." method parameter
113122
*/
114123
@Deprecated
115-
protected FormFieldValidator(StaplerRequest request, StaplerResponse response, AccessControlled subject, Permission permission) {
124+
protected FormFieldValidator(StaplerRequest request, StaplerResponse response, AccessControlled subject,
125+
Permission permission) {
116126
this.request = request;
117127
this.response = response;
118128
this.subject = subject;
@@ -134,7 +144,8 @@ public final void process() throws IOException, ServletException {
134144
subject.checkPermission(permission);
135145
} catch (AccessDeniedException e) {
136146
// if the user has hudson-wide admin permission, all checks are allowed
137-
// this is to protect Hudson administrator from broken ACL/SecurityRealm implementation/configuration.
147+
// this is to protect Hudson administrator from broken ACL/SecurityRealm
148+
// implementation/configuration.
138149
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER))
139150
throw e;
140151
}
@@ -171,8 +182,8 @@ public void respond(String html) throws IOException, ServletException {
171182
* Sends out a string error message that indicates an error.
172183
*
173184
* @param message
174-
* Human readable message to be sent. {@code error(null)}
175-
* can be used as {@code ok()}.
185+
* Human readable message to be sent. {@code error(null)}
186+
* can be used as {@code ok()}.
176187
*/
177188
public void error(String message) throws IOException, ServletException {
178189
errorWithMarkup(message == null ? null : Util.escape(message));
@@ -210,8 +221,8 @@ public void ok(String format, Object... args) throws IOException, ServletExcepti
210221
* attack.
211222
*
212223
* @param message
213-
* Human readable message to be sent. {@code error(null)}
214-
* can be used as {@code ok()}.
224+
* Human readable message to be sent. {@code error(null)}
225+
* can be used as {@code ok()}.
215226
*/
216227
public void errorWithMarkup(String message) throws IOException, ServletException {
217228
_errorWithMarkup(message, "error");
@@ -242,7 +253,7 @@ private void _errorWithMarkup(String message, String cssClass) throws IOExceptio
242253
* Convenient base class for checking the validity of URLs
243254
*
244255
* @deprecated as of 1.294
245-
* Use {@link FormValidation.URLCheck}
256+
* Use {@link FormValidation.URLCheck}
246257
*/
247258
@Deprecated
248259
public abstract static class URLCheck extends FormFieldValidator {
@@ -260,17 +271,15 @@ protected URLCheck(StaplerRequest request, StaplerResponse response) {
260271
protected BufferedReader open(URL url) throws IOException {
261272
// use HTTP content type to find out the charset.
262273
URLConnection con = ProxyConfiguration.open(url);
263-
if (con == null) { // TODO is this even permitted by URL.openConnection?
264-
throw new IOException(url.toExternalForm());
265-
}
266274
return new BufferedReader(
267-
new InputStreamReader(con.getInputStream(), getCharset(con)));
275+
new InputStreamReader(con.getInputStream(), getCharset(con)));
268276
}
269277

270278
/**
271279
* Finds the string literal from the given reader.
280+
*
272281
* @return
273-
* true if found, false otherwise.
282+
* true if found, false otherwise.
274283
*/
275284
protected boolean findText(BufferedReader in, String literal) throws IOException {
276285
String line;
@@ -282,10 +291,11 @@ protected boolean findText(BufferedReader in, String literal) throws IOException
282291

283292
/**
284293
* Calls the {@link #error(String)} method with a reasonable error message.
285-
* Use this method when the {@link #open(URL)} or {@link #findText(BufferedReader, String)} fails.
294+
* Use this method when the {@link #open(URL)} or
295+
* {@link #findText(BufferedReader, String)} fails.
286296
*
287297
* @param url
288-
* Pass in the URL that was connected. Used for error diagnosis.
298+
* Pass in the URL that was connected. Used for error diagnosis.
289299
*/
290300
protected void handleIOException(String url, IOException e) throws IOException, ServletException {
291301
// any invalid URL comes here
@@ -315,6 +325,7 @@ private String getCharset(URLConnection con) {
315325

316326
/**
317327
* Checks if the given value is an URL to some Hudson's top page.
328+
*
318329
* @since 1.192
319330
*/
320331
public static class HudsonURL extends URLCheck {
@@ -330,14 +341,15 @@ protected void check() throws IOException, ServletException {
330341
return;
331342
}
332343

333-
if (!value.endsWith("/")) value += '/';
344+
if (!value.endsWith("/"))
345+
value += '/';
334346

335347
try {
336348
URL url = new URL(value);
337349
HttpURLConnection con = openConnection(url);
338350
con.connect();
339351
if (con.getResponseCode() != 200
340-
|| con.getHeaderField("X-Hudson") == null) {
352+
|| con.getHeaderField("X-Hudson") == null) {
341353
error(value + " is not Hudson (" + con.getResponseMessage() + ")");
342354
return;
343355
}
@@ -357,8 +369,10 @@ private HttpURLConnection openConnection(URL url) throws IOException {
357369
/**
358370
* Checks the file mask (specified in the 'value' query parameter) against
359371
* the current workspace.
372+
*
360373
* @since 1.90.
361-
* @deprecated as of 1.294. Use {@link FilePath#validateFileMask(String, boolean, boolean)}
374+
* @deprecated as of 1.294. Use
375+
* {@link FilePath#validateFileMask(String, boolean, boolean)}
362376
*/
363377
@Deprecated
364378
public static class WorkspaceFileMask extends FormFieldValidator {
@@ -393,8 +407,10 @@ protected void check() throws IOException, ServletException {
393407
}
394408

395409
String msg = ws.validateAntFileMask(value, FilePath.VALIDATE_ANT_FILE_MASK_BOUND);
396-
if (errorIfNotExist) error(msg);
397-
else warning(msg);
410+
if (errorIfNotExist)
411+
error(msg);
412+
else
413+
warning(msg);
398414
} catch (InterruptedException e) {
399415
ok(Messages.FormFieldValidator_did_not_manage_to_validate_may_be_too_sl(value));
400416
}
@@ -409,11 +425,14 @@ protected FilePath getBaseDirectory(AbstractProject<?, ?> p) {
409425
}
410426

411427
/**
412-
* Checks a valid directory name (specified in the 'value' query parameter) against
428+
* Checks a valid directory name (specified in the 'value' query parameter)
429+
* against
413430
* the current workspace.
431+
*
414432
* @since 1.116
415-
* @deprecated as of 1.294. Use {@link FilePath#validateRelativeDirectory(String, boolean)}
416-
* (see javadoc plugin for the example)
433+
* @deprecated as of 1.294. Use
434+
* {@link FilePath#validateRelativeDirectory(String, boolean)}
435+
* (see javadoc plugin for the example)
417436
*/
418437
@Deprecated
419438
public static class WorkspaceDirectory extends WorkspaceFilePath {
@@ -427,17 +446,21 @@ public WorkspaceDirectory(StaplerRequest request, StaplerResponse response) {
427446
}
428447

429448
/**
430-
* Checks a valid file name or directory (specified in the 'value' query parameter) against
449+
* Checks a valid file name or directory (specified in the 'value' query
450+
* parameter) against
431451
* the current workspace.
452+
*
432453
* @since 1.160
433-
* @deprecated as of 1.294. Use {@link FilePath#validateRelativePath(String, boolean, boolean)}
454+
* @deprecated as of 1.294. Use
455+
* {@link FilePath#validateRelativePath(String, boolean, boolean)}
434456
*/
435457
@Deprecated
436458
public static class WorkspaceFilePath extends FormFieldValidator {
437459
private final boolean errorIfNotExist;
438460
private final boolean expectingFile;
439461

440-
public WorkspaceFilePath(StaplerRequest request, StaplerResponse response, boolean errorIfNotExist, boolean expectingFile) {
462+
public WorkspaceFilePath(StaplerRequest request, StaplerResponse response, boolean errorIfNotExist,
463+
boolean expectingFile) {
441464
// Require CONFIGURE permission on this job
442465
super(request, response, request.findAncestorObject(AbstractProject.class), Item.CONFIGURE);
443466
this.errorIfNotExist = errorIfNotExist;
@@ -487,8 +510,10 @@ protected void check() throws IOException, ServletException {
487510
}
488511
} else {
489512
String msg = "No such " + (expectingFile ? "file" : "directory") + ": " + value;
490-
if (errorIfNotExist) error(msg);
491-
else warning(msg);
513+
if (errorIfNotExist)
514+
error(msg);
515+
else
516+
warning(msg);
492517
}
493518
} catch (InterruptedException e) {
494519
ok(); // couldn't check
@@ -515,7 +540,8 @@ protected FilePath getBaseDirectory(AbstractProject<?, ?> p) {
515540
* needed.
516541
*
517542
* @since 1.124
518-
* @deprecated as of 1.294. Use {@link FormValidation#validateExecutable(String)}
543+
* @deprecated as of 1.294. Use
544+
* {@link FormValidation#validateExecutable(String)}
519545
*/
520546
@Deprecated
521547
public static class Executable extends FormFieldValidator {
@@ -529,7 +555,7 @@ public Executable(StaplerRequest request, StaplerResponse response) {
529555
protected void check() throws IOException, ServletException {
530556
String exe = fixEmpty(request.getParameter("value"));
531557
FormFieldValidator.Executable self = this;
532-
Exception[] exceptions = {null};
558+
Exception[] exceptions = { null };
533559
DOSToUnixPathHelper.iteratePath(exe, new DOSToUnixPathHelper.Helper() {
534560
@Override
535561
public void ok() {
@@ -577,7 +603,8 @@ public void validate(File fexe) {
577603
}
578604

579605
/**
580-
* Provides an opportunity for derived classes to do additional checks on the executable.
606+
* Provides an opportunity for derived classes to do additional checks on the
607+
* executable.
581608
*/
582609
protected void checkExecutable(File exe) throws IOException, ServletException {
583610
ok();
@@ -589,15 +616,18 @@ protected void checkExecutable(File exe) throws IOException, ServletException {
589616
*
590617
* @since 1.257
591618
* @deprecated as of 1.305
592-
* Use {@link FormValidation#validateBase64(String, boolean, boolean, String)} instead.
619+
* Use
620+
* {@link FormValidation#validateBase64(String, boolean, boolean, String)}
621+
* instead.
593622
*/
594623
@Deprecated
595624
public static class Base64 extends FormFieldValidator {
596625
private final boolean allowWhitespace;
597626
private final boolean allowEmpty;
598627
private final String errorMessage;
599628

600-
public Base64(StaplerRequest request, StaplerResponse response, boolean allowWhitespace, boolean allowEmpty, String errorMessage) {
629+
public Base64(StaplerRequest request, StaplerResponse response, boolean allowWhitespace, boolean allowEmpty,
630+
String errorMessage) {
601631
super(request, response, false);
602632
this.allowWhitespace = allowWhitespace;
603633
this.allowEmpty = allowEmpty;

0 commit comments

Comments
 (0)