Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable whitespace-related Checkstyle checks #6149

Merged
merged 2 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
38 changes: 22 additions & 16 deletions cli/src/main/java/hudson/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.cli;

import static java.util.logging.Level.FINE;
Expand Down Expand Up @@ -80,9 +81,10 @@ private CLI() {}
* @throws NotTalkingToJenkinsException when connection is not made to Jenkins service.
*/
/*package*/ static void verifyJenkinsConnection(URLConnection c) throws IOException {
if (c.getHeaderField("X-Hudson")==null && c.getHeaderField("X-Jenkins")==null)
if (c.getHeaderField("X-Hudson") == null && c.getHeaderField("X-Jenkins") == null)
throw new NotTalkingToJenkinsException(c);
}

/*package*/ static final class NotTalkingToJenkinsException extends IOException {
NotTalkingToJenkinsException(String s) {
super(s);
Expand All @@ -106,14 +108,15 @@ public static void main(final String[] _args) throws Exception {
}
}

private enum Mode {HTTP, SSH, WEB_SOCKET}
private enum Mode { HTTP, SSH, WEB_SOCKET }

public static int _main(String[] _args) throws Exception {
List<String> args = Arrays.asList(_args);
PrivateKeyProvider provider = new PrivateKeyProvider();

String url = System.getenv("JENKINS_URL");

if (url==null)
if (url == null)
url = System.getenv("HUDSON_URL");

boolean noKeyAuth = false;
Expand All @@ -130,10 +133,10 @@ public static int _main(String[] _args) throws Exception {

boolean strictHostKey = false;

while(!args.isEmpty()) {
while (!args.isEmpty()) {
String head = args.get(0);
if (head.equals("-version")) {
System.out.println("Version: "+computeVersion());
System.out.println("Version: " + computeVersion());
return 0;
}
if (head.equals("-http")) {
Expand Down Expand Up @@ -167,9 +170,9 @@ public static int _main(String[] _args) throws Exception {
printUsage("-remoting mode is no longer supported");
return -1;
}
if(head.equals("-s") && args.size()>=2) {
if (head.equals("-s") && args.size() >= 2) {
url = args.get(1);
args = args.subList(2,args.size());
args = args.subList(2, args.size());
continue;
}
if (head.equals("-noCertificateCheck")) {
Expand All @@ -185,15 +188,15 @@ public boolean verify(String s, SSLSession sslSession) {
return true;
}
});
args = args.subList(1,args.size());
args = args.subList(1, args.size());
continue;
}
if (head.equals("-noKeyAuth")) {
noKeyAuth = true;
args = args.subList(1,args.size());
args = args.subList(1, args.size());
continue;
}
if(head.equals("-i") && args.size()>=2) {
if (head.equals("-i") && args.size() >= 2) {
File f = getFileFromArguments(args);
if (!f.exists()) {
printUsage(Messages.CLI_NoSuchFileExists(f));
Expand All @@ -202,7 +205,7 @@ public boolean verify(String s, SSLSession sslSession) {

provider.readFrom(f);

args = args.subList(2,args.size());
args = args.subList(2, args.size());
continue;
}
if (head.equals("-strictHostKey")) {
Expand Down Expand Up @@ -239,7 +242,7 @@ public boolean verify(String s, SSLSession sslSession) {
break;
}

if(url==null) {
if (url == null) {
printUsage(Messages.CLI_NoURL());
return -1;
}
Expand All @@ -263,7 +266,7 @@ public boolean verify(String s, SSLSession sslSession) {
url += '/';
}

if(args.isEmpty())
if (args.isEmpty())
args = Collections.singletonList("help"); // default to help

if (mode == null) {
Expand Down Expand Up @@ -338,6 +341,7 @@ class CLIEndpoint extends Endpoint {
@Override
public void onOpen(Session session, EndpointConfig config) {}
}

class Authenticator extends ClientEndpointConfig.Configurator {
@Override
public void beforeRequest(Map<String, List<String>> headers) {
Expand All @@ -346,6 +350,7 @@ public void beforeRequest(Map<String, List<String>> headers) {
}
}
}

ClientManager client = ClientManager.createClient(JdkClientContainer.class.getName()); // ~ ContainerProvider.getWebSocketContainer()
client.getProperties().put(ClientProperties.REDIRECT_ENABLED, true); // https://tyrus-project.github.io/documentation/1.13.1/index/tyrus-proprietary-config.html#d0e1775
Session session = client.connectToServer(new CLIEndpoint(), ClientEndpointConfig.Builder.create().configurator(new Authenticator()).build(), URI.create(url.replaceFirst("^http", "ws") + "cli/ws"));
Expand All @@ -354,6 +359,7 @@ public void beforeRequest(Map<String, List<String>> headers) {
public void send(byte[] data) throws IOException {
session.getBasicRemote().sendBinary(ByteBuffer.wrap(data));
}

@Override
public void close() throws IOException {
session.close();
Expand Down Expand Up @@ -479,7 +485,7 @@ private static String computeVersion() {
Properties props = new Properties();
try {
InputStream is = CLI.class.getResourceAsStream("/jenkins/cli/jenkins-cli-version.properties");
if(is!=null) {
if (is != null) {
try {
props.load(is);
} finally {
Expand All @@ -489,7 +495,7 @@ private static String computeVersion() {
} catch (IOException e) {
e.printStackTrace(); // if the version properties is missing, that's OK.
}
return props.getProperty("version","?");
return props.getProperty("version", "?");
}

/**
Expand Down Expand Up @@ -520,7 +526,7 @@ static String usage() {
}

private static void printUsage(String msg) {
if(msg!=null) System.out.println(msg);
if (msg != null) System.out.println(msg);
System.err.println(usage());
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/java/hudson/cli/CLIConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* Fluent-API to instantiate {@link CLI}.
*
*
* @author Kohsuke Kawaguchi
*/
public class CLIConnectionFactory {
Expand All @@ -26,7 +26,7 @@ public CLIConnectionFactory authorization(String value) {
* Currently unused.
*/
public CLIConnectionFactory basicAuth(String username, String password) {
return basicAuth(username+':'+password);
return basicAuth(username + ':' + password);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public String toString() {
buf.append(super.toString()).append("\n");
buf.append("Read back: ").append(HexDump.toHex(readBack)).append('\n');
buf.append("Read ahead: ").append(HexDump.toHex(readAhead));
if (diagnoseFailure!=null) {
if (diagnoseFailure != null) {
StringWriter w = new StringWriter();
PrintWriter p = new PrintWriter(w);
diagnoseFailure.printStackTrace(p);
p.flush();

buf.append("\nDiagnosis problem:\n ");
buf.append(w.toString().trim().replace("\n","\n "));
buf.append(w.toString().trim().replace("\n", "\n "));
}
return buf.toString();
}
Expand Down
18 changes: 9 additions & 9 deletions cli/src/main/java/hudson/cli/FlightRecorderInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public DiagnosedStreamCorruptionException analyzeCrash(Exception problem, String
final ByteArrayOutputStream readAhead = new ByteArrayOutputStream();
final IOException[] error = new IOException[1];

Thread diagnosisThread = new Thread(diagnosisName+" stream corruption diagnosis thread") {
Thread diagnosisThread = new Thread(diagnosisName + " stream corruption diagnosis thread") {
@Override
public void run() {
int b;
try {
// not all InputStream will look for the thread interrupt flag, so check that explicitly to be defensive
while (!Thread.interrupted() && (b=source.read())!=-1) {
while (!Thread.interrupted() && (b = source.read()) != -1) {
readAhead.write(b);
}
} catch (IOException e) {
Expand All @@ -81,23 +81,23 @@ public void run() {
if (diagnosisThread.isAlive())
diagnosisThread.interrupt(); // if it's not dead, kill

return new DiagnosedStreamCorruptionException(problem,diagnosisProblem,getRecord(),readAhead.toByteArray());
return new DiagnosedStreamCorruptionException(problem, diagnosisProblem, getRecord(), readAhead.toByteArray());

}

@Override
public int read() throws IOException {
int i = source.read();
if (i>=0)
if (i >= 0)
recorder.write(i);
return i;
}

@Override
public int read(@NonNull byte[] b, int off, int len) throws IOException {
len = source.read(b, off, len);
if (len>0)
recorder.write(b,off,len);
if (len > 0)
recorder.write(b, off, len);
return len;
}

Expand All @@ -106,8 +106,8 @@ public int read(@NonNull byte[] b, int off, int len) throws IOException {
*/
@Override
public long skip(long n) throws IOException {
byte[] buf = new byte[(int)Math.min(n,64*1024)];
return read(buf,0,buf.length);
byte[] buf = new byte[(int) Math.min(n, 64 * 1024)];
return read(buf, 0, buf.length);
}

@Override
Expand Down Expand Up @@ -157,7 +157,7 @@ public synchronized byte[] toByteArray() {
System.arraycopy(data, 0, ret, capacity - pos, pos);
return ret;
}

/** @author @roadrunner2 */
@Override public synchronized void write(@NonNull byte[] buf, int off, int len) {
// no point in trying to copy more than capacity; this also simplifies logic below
Expand Down
12 changes: 6 additions & 6 deletions cli/src/main/java/hudson/cli/FullDuplexHttpStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public class FullDuplexHttpStream {
private final URL base;

private final OutputStream output;
private final InputStream input;

Expand Down Expand Up @@ -65,7 +65,7 @@ public FullDuplexHttpStream(URL base, String relativeTarget, String authorizatio
con.setDoOutput(true); // request POST to avoid caching
con.setRequestMethod("POST");
con.addRequestProperty("Session", uuid.toString());
con.addRequestProperty("Side","download");
con.addRequestProperty("Side", "download");
if (authorization != null) {
con.addRequestProperty("Authorization", authorization);
}
Expand All @@ -83,11 +83,11 @@ public FullDuplexHttpStream(URL base, String relativeTarget, String authorizatio
con.setDoOutput(true); // request POST
con.setRequestMethod("POST");
con.setChunkedStreamingMode(0);
con.setRequestProperty("Content-type","application/octet-stream");
con.setRequestProperty("Content-type", "application/octet-stream");
con.addRequestProperty("Session", uuid.toString());
con.addRequestProperty("Side","upload");
con.addRequestProperty("Side", "upload");
if (authorization != null) {
con.addRequestProperty ("Authorization", authorization);
con.addRequestProperty("Authorization", authorization);
}
output = con.getOutputStream();
LOGGER.fine("established upload side");
Expand Down Expand Up @@ -118,5 +118,5 @@ private URL tryToResolveRedirects(URL base, String authorization) {

static final int BLOCK_SIZE = 1024;
static final Logger LOGGER = Logger.getLogger(FullDuplexHttpStream.class.getName());

}
13 changes: 7 additions & 6 deletions cli/src/main/java/hudson/cli/HexDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ class HexDump {
private static final String CODE = "0123456789abcdef";

public static String toHex(byte[] buf) {
return toHex(buf,0,buf.length);
return toHex(buf, 0, buf.length);
}

public static String toHex(byte[] buf, int start, int len) {
StringBuilder r = new StringBuilder(len*2);
StringBuilder r = new StringBuilder(len * 2);
boolean inText = false;
for (int i=0; i<len; i++) {
byte b = buf[start+i];
for (int i = 0; i < len; i++) {
byte b = buf[start + i];
if (b >= 0x20 && b <= 0x7e) {
if (!inText) {
inText = true;
Expand All @@ -28,8 +29,8 @@ public static String toHex(byte[] buf, int start, int len) {
inText = false;
}
r.append("0x");
r.append(CODE.charAt((b>>4)&15));
r.append(CODE.charAt(b&15));
r.append(CODE.charAt((b >> 4) & 15));
r.append(CODE.charAt(b & 15));
if (i < len - 1) {
if (b == 10) {
r.append('\n');
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main/java/hudson/cli/PlainCLIProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,12 @@ protected final OutputStream stream(final Op op) {
public void write(int b) throws IOException {
send(op, new byte[] {(byte) b});
}

@Override
public void write(@NonNull byte[] b, int off, int len) throws IOException {
send(op, b, off, len);
}

@Override
public void write(@NonNull byte[] b) throws IOException {
send(op, b);
Expand Down
9 changes: 5 additions & 4 deletions cli/src/main/java/hudson/cli/PrivateKeyProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.cli;

import static java.util.logging.Level.FINE;
Expand Down Expand Up @@ -110,15 +111,15 @@ public void readFrom(File keyFile) throws IOException, GeneralSecurityException
privateKeys.add(loadKey(keyFile, password));
}

private static boolean isPemEncrypted(File f) throws IOException{
private static boolean isPemEncrypted(File f) throws IOException {
//simple check if the file is encrypted
return readPemFile(f).contains("4,ENCRYPTED");
}

private static String askForPasswd(String filePath){
private static String askForPasswd(String filePath) {
Console cons = System.console();
String passwd = null;
if (cons != null){
if (cons != null) {
char[] p = cons.readPassword("%s", "Enter passphrase for " + filePath + ":");
passwd = String.valueOf(p);
}
Expand All @@ -129,7 +130,7 @@ public static KeyPair loadKey(File f, String passwd) throws IOException, General
return loadKey(readPemFile(f), passwd);
}

private static String readPemFile(File f) throws IOException{
private static String readPemFile(File f) throws IOException {
try (InputStream is = Files.newInputStream(f.toPath());
DataInputStream dis = new DataInputStream(is)) {
byte[] bytes = new byte[(int) f.length()];
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/java/hudson/cli/SSHCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static int sshConnection(String jenkinsUrl, String user, List<String> args, Priv
command.append(' ');
}

try(SshClient client = SshClient.setUpDefaultClient()) {
try (SshClient client = SshClient.setUpDefaultClient()) {

KnownHostsServerKeyVerifier verifier = new DefaultKnownHostsServerKeyVerifier(new ServerKeyVerifier() {
@Override
Expand Down Expand Up @@ -116,7 +116,7 @@ public boolean verifyServerKey(ClientSession clientSession, SocketAddress remote

Set<ClientChannelEvent> waitMask = channel.waitFor(Collections.singletonList(ClientChannelEvent.CLOSED), 0L);

if(waitMask.contains(ClientChannelEvent.TIMEOUT)) {
if (waitMask.contains(ClientChannelEvent.TIMEOUT)) {
throw new SocketTimeoutException("Failed to retrieve command result in time: " + command);
}

Expand Down
Loading