Skip to content

Commit

Permalink
Add missing braces to if statements (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored May 7, 2024
1 parent 26e2fd8 commit d7eb216
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 40 deletions.
3 changes: 2 additions & 1 deletion src/main/java/winstone/AbstractSecuredConnectorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ protected void configureSsl(Map<String, String> args, Server server) throws IOEx

if (keyStore != null) {
// load from default Keystore
if (!keyStore.exists() || !keyStore.isFile())
if (!keyStore.exists() || !keyStore.isFile()) {
throw new WinstoneException(
SSL_RESOURCES.getString("HttpsListener.KeyStoreNotFound", keyStore.getPath()));
}

this.keystorePassword = pwd;

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/winstone/HostConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public HostConfiguration(Server server, String hostname, ClassLoader commonLibCL
while (mappingST.hasMoreTokens()) {
String mapping = mappingST.nextToken();
int delimPos = mapping.indexOf('=');
if (delimPos == -1) continue;
if (delimPos == -1) {
continue;
}
String extension = mapping.substring(0, delimPos);
String mimeType = mapping.substring(delimPos + 1);
this.mimeTypes.addMimeMapping(extension.toLowerCase(), mimeType);
Expand Down Expand Up @@ -222,8 +224,9 @@ protected File getWebRoot(File requestedWebroot, File warfile) throws IOExceptio
Logger.log(Level.INFO, Launcher.RESOURCES, "HostConfig.BeginningWarExtraction");

// open the war file
if (!warfile.exists() || !warfile.isFile())
if (!warfile.exists() || !warfile.isFile()) {
throw new WinstoneException(Launcher.RESOURCES.getString("HostConfig.WarFileInvalid", warfile));
}

// Get the webroot folder (or a temp dir if none supplied)
File unzippedDir;
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/winstone/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ public Launcher(Map<String, String> args) throws IOException {
if (libFolder.exists() && libFolder.isDirectory()) {
Logger.log(Level.FINER, RESOURCES, "Launcher.UsingCommonLib", libFolder.getCanonicalPath());
File[] children = libFolder.listFiles();
if (children != null)
for (File aChildren : children)
if (children != null) {
for (File aChildren : children) {
if (aChildren.getName().endsWith(".jar")
|| aChildren.getName().endsWith(".zip")) {
jars.add(aChildren.toURI().toURL());
Logger.log(Level.FINER, RESOURCES, "Launcher.AddedCommonLibJar", aChildren.getName());
}
}
}
} else {
Logger.log(Level.FINER, RESOURCES, "Launcher.NoCommonLib");
}
Expand All @@ -151,12 +153,14 @@ public Launcher(Map<String, String> args) throws IOException {
if (extraLibFolder != null && extraLibFolder.exists()) {
Logger.log(Level.WARNING, RESOURCES, "Launcher.ExtraLibFolder");
File[] children = extraLibFolder.listFiles();
if (children != null)
for (File aChildren : children)
if (children != null) {
for (File aChildren : children) {
if (aChildren.getName().endsWith(".jar")
|| aChildren.getName().endsWith(".zip")) {
extraJars.add(aChildren.toURI().toURL());
}
}
}
}

ClassLoader commonLibCL =
Expand Down Expand Up @@ -212,7 +216,9 @@ public Launcher(Map<String, String> args) throws IOException {

success = true;
} finally {
if (!success) shutdown();
if (!success) {
shutdown();
}
}

try {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/winstone/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public static void init(Level level, boolean showThrowingThreadArg) {
public static void setCurrentDebugLevel(int level) {
if (!initialised) {
init(level);
} else
} else {
synchronized (semaphore) {
LOGGER.setLevel(Level.parse(String.valueOf(level)));
}
}
}

/**
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/winstone/cmdline/CmdLineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,29 @@ public Map<String, String> parse(String[] argv, String nonSwitchArgName) throws
int equalPos = option.indexOf('=');
String paramName = option.substring(2, equalPos == -1 ? option.length() : equalPos);
Option<?> opt = toOption(paramName);
if (opt == null)
if (opt == null) {
throw new IllegalArgumentException(
Launcher.RESOURCES.getString("CmdLineParser.UnrecognizedOption", option));
}

if (equalPos != -1) {
args.put(paramName, option.substring(equalPos + 1));
} else {
if (opt.type == Boolean.class) args.put(paramName, "true");
else
if (opt.type == Boolean.class) {
args.put(paramName, "true");
} else {
throw new IllegalArgumentException(
Launcher.RESOURCES.getString("CmdLineParser.OperandExpected", option));
}
}
if (paramName.equals(Option.CONFIG.name)) {
configFilename = args.get(paramName);
}
} else {
if (args.containsKey(nonSwitchArgName))
if (args.containsKey(nonSwitchArgName)) {
throw new IllegalArgumentException(
Launcher.RESOURCES.getString("CmdLineParser.MultipleArgs", option));
}
args.put(nonSwitchArgName, option);
}
}
Expand Down Expand Up @@ -97,8 +101,12 @@ private static void loadPropsFromStream(InputStream inConfig, Map<String, String

private Option<?> toOption(String paramName) {
for (Option<?> o : options) {
if (o.isWildcard() && paramName.startsWith(o.name)) return o;
if (!o.isWildcard() && paramName.equals(o.name)) return o;
if (o.isWildcard() && paramName.startsWith(o.name)) {
return o;
}
if (!o.isWildcard() && paramName.equals(o.name)) {
return o;
}
}
return null;
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/winstone/cmdline/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,19 @@ public <T> Class<? extends T> get(Map<String, String> args, Class<T> expectedTyp
public <T> Class<? extends T> get(Map<String, String> args, Class<T> expectedType, ClassLoader cl)
throws ClassNotFoundException {
String v = args.get(name);
if (v == null) return defaultValue;
if (v == null) {
return defaultValue;
}

v = v.trim();
if (v.length() == 0) return defaultValue;
if (v.length() == 0) {
return defaultValue;
}

Class<?> c = Class.forName(v, true, cl);
if (!expectedType.isAssignableFrom(c))
if (!expectedType.isAssignableFrom(c)) {
throw new ClassNotFoundException("Expected a subype of " + expectedType + " but got " + c + " instead");
}

return c.asSubclass(expectedType);
}
Expand All @@ -313,8 +318,11 @@ public <T> Class<? extends T> get(Map<String, String> args, Class<T> expectedTyp

public static boolean booleanArg(Map<String, String> args, String name, boolean defaultTrue) {
String value = args.get(name);
if (defaultTrue) return (value == null) || (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));
else return (value != null) && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));
if (defaultTrue) {
return (value == null) || (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));
} else {
return (value != null) && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));
}
}

public static String stringArg(Map<String, String> args, String name, String defaultValue) {
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/winstone/realm/FileRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ public FileRealm(Map<String, String> args) {
setUserStore(userStore);
// Get the filename and parse the xml doc
File realmFile = Option.FILEREALM_CONFIGFILE.get(args);
if (realmFile == null) realmFile = new File(DEFAULT_FILE_NAME);
if (!realmFile.exists())
if (realmFile == null) {
realmFile = new File(DEFAULT_FILE_NAME);
}
if (!realmFile.exists()) {
throw new WinstoneException(REALM_RESOURCES.getString("FileRealm.FileNotFound", realmFile.getPath()));
}
try (InputStream inFile = new FileInputStream(realmFile)) {
int count = 0;
Document doc = this.parseStreamToXML(inFile);
Expand All @@ -69,14 +72,18 @@ public FileRealm(Map<String, String> args) {
// Loop through for attributes
for (int j = 0; j < child.getAttributes().getLength(); j++) {
Node thisAtt = child.getAttributes().item(j);
if (thisAtt.getNodeName().equals(ATT_USERNAME)) userName = thisAtt.getNodeValue();
else if (thisAtt.getNodeName().equals(ATT_PASSWORD)) password = thisAtt.getNodeValue();
else if (thisAtt.getNodeName().equals(ATT_ROLELIST)) roleList = thisAtt.getNodeValue();
if (thisAtt.getNodeName().equals(ATT_USERNAME)) {
userName = thisAtt.getNodeValue();
} else if (thisAtt.getNodeName().equals(ATT_PASSWORD)) {
password = thisAtt.getNodeValue();
} else if (thisAtt.getNodeName().equals(ATT_ROLELIST)) {
roleList = thisAtt.getNodeValue();
}
}

if ((userName == null) || (password == null) || (roleList == null))
if ((userName == null) || (password == null) || (roleList == null)) {
Logger.log(Level.FINEST, REALM_RESOURCES, "FileRealm.SkippingUser", userName);
else {
} else {
// Parse the role list into an array and sort it
StringTokenizer st = new StringTokenizer(roleList, ",");
List<String> rl = new ArrayList<>();
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/winstone/AbstractWinstoneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class AbstractWinstoneTest {

@After
public void tearDown() {
if (winstone != null) winstone.shutdown();
if (winstone != null) {
winstone.shutdown();
}
}

public String makeRequest(String url) throws Exception {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/winstone/TrustManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public void checkServerTrusted(X509Certificate[] xcs, String string) throws Cert
for (X509Certificate x509Certificate : xcs) {
System.out.println(
"certificate: " + x509Certificate.getIssuerX500Principal().getName());
if (cert.getSubjectX500Principal().equals(x509Certificate.getIssuerX500Principal())) return;
if (cert.getSubjectX500Principal().equals(x509Certificate.getIssuerX500Principal())) {
return;
}
}

throw new CertificateException("Untrusted certificate?");
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/winstone/accesslog/SimpleAccessLoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public void testSimpleConnection() throws Exception {
for (int i = 0; i < 50; ++i) {
Thread.sleep(100);
text = Files.readString(logFile, StandardCharsets.UTF_8);
if (!"".equals(text)) break;
if (!"".equals(text)) {
break;
}
}
assertEquals(String.format("127.0.0.1 - - GET /examples/CountRequestsServlet HTTP/1.1 200%n"), text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ public void destroy() {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (this.dumpRequestParams)
if (this.dumpRequestParams) {
for (Enumeration<String> paramNames = request.getParameterNames(); paramNames.hasMoreElements(); ) {
String name = paramNames.nextElement();
this.context.log("Request parameter: " + name + "=" + request.getParameter(name));
}
}

long startTime = System.currentTimeMillis();
chain.doFilter(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ public class UnavailableServlet extends HttpServlet {
public void init() throws ServletException {
String errorTime = getServletConfig().getInitParameter("errorTime");
this.errorAtInit = ((errorTime == null) || errorTime.equals("init"));
if (this.errorAtInit) throw new UnavailableException("Error thrown deliberately during init");
if (this.errorAtInit) {
throw new UnavailableException("Error thrown deliberately during init");
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (!this.errorAtInit) throw new UnavailableException("Error thrown deliberately during get");
if (!this.errorAtInit) {
throw new UnavailableException("Error thrown deliberately during get");
}

try (Writer out = response.getWriter()) {
out.write("This should not be shown, because we've thrown unavailable exceptions");
Expand Down
11 changes: 8 additions & 3 deletions src/test/java/winstone/testCase/load/LoadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ public void test() throws InterruptedException {

// Loop through in steps
for (int n = this.startThreads; n <= this.endThreads; n += this.stepSize) {
if (this.useKeepAlives) client = HttpClient.newHttpClient();
if (this.useKeepAlives) {
client = HttpClient.newHttpClient();
}

// Spawn the threads
int noOfSeconds = (int) this.stepPeriod / 1000;
List<LoadTestThread> threads = new ArrayList<>();
for (int m = 0; m < n; m++)
for (int m = 0; m < n; m++) {
threads.add(new LoadTestThread(this.url, this, this.resources, client, noOfSeconds - 1));
}

// Sleep for step period
Thread.sleep(this.stepPeriod + gracePeriod);
Expand All @@ -105,7 +108,9 @@ public void test() throws InterruptedException {
averageSuccessTime + "");

// Close threads
for (LoadTestThread thread : threads) thread.destroy();
for (LoadTestThread thread : threads) {
thread.destroy();
}

this.successTimeTotal = 0;
this.successCount = 0;
Expand Down
15 changes: 11 additions & 4 deletions src/test/java/winstone/testCase/load/LoadTestThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ public LoadTestThread(
this.thread.start();

// Launch the next second's getter
if (delayedThreads > 0) this.next = new LoadTestThread(url, loadTest, resources, client, delayedThreads - 1);
if (delayedThreads > 0) {
this.next = new LoadTestThread(url, loadTest, resources, client, delayedThreads - 1);
}
}

@Override
public void run() {
if (this.delayBeforeStarting > 0)
if (this.delayBeforeStarting > 0) {
try {
Thread.sleep(this.delayBeforeStarting);
} catch (InterruptedException err) {
}
}

long startTime = System.currentTimeMillis();

Expand All @@ -68,7 +71,9 @@ public void run() {
HttpRequest.newBuilder(new URI(this.url)).GET().build();
HttpResponse<String> response = this.client.send(request, HttpResponse.BodyHandlers.ofString());
int responseCode = response.statusCode();
if (responseCode >= 400) throw new IOException("Failed with status " + responseCode);
if (responseCode >= 400) {
throw new IOException("Failed with status " + responseCode);
}
if (this.interrupted) {
return;
}
Expand All @@ -82,6 +87,8 @@ public void run() {
public void destroy() {
this.interrupted = true;
this.thread.interrupt();
if (this.next != null) this.next.destroy();
if (this.next != null) {
this.next.destroy();
}
}
}

0 comments on commit d7eb216

Please sign in to comment.