Skip to content
This repository was archived by the owner on Sep 19, 2021. It is now read-only.

Commit 5390a69

Browse files
FindBugs process
1 parent e84e08c commit 5390a69

File tree

6 files changed

+65
-56
lines changed

6 files changed

+65
-56
lines changed

src/main/java/com/qwazr/webapps/RewriteFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final public void doFilter(final ServletRequest req, final ServletResponse rep,
4545
matcher = pattern.matcher(reqUrl);
4646
}
4747
final String newUrl = matcher.replaceAll(replace);
48-
if (reqUrl.equals(newUrl))
48+
if (reqUrl.toString().equals(newUrl))
4949
chain.doFilter(req, rep);
5050
else
5151
request.getRequestDispatcher(newUrl).forward(request, rep);

src/main/java/com/qwazr/webapps/WebappDefinition.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2017 Emmanuel Keller / QWAZR
2+
* Copyright 2015-2018 Emmanuel Keller / QWAZR
33
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@
2020
import com.fasterxml.jackson.annotation.JsonInclude;
2121
import com.qwazr.utils.ObjectMappers;
2222

23-
import java.io.File;
2423
import java.io.IOException;
24+
import java.nio.file.Path;
2525
import java.util.Collection;
2626
import java.util.LinkedHashMap;
2727
import java.util.LinkedHashSet;
@@ -146,15 +146,15 @@ public WebappDefinition build() {
146146
}
147147
}
148148

149-
public static WebappDefinition load(final File jsonFile) throws IOException {
150-
return jsonFile == null ? EMPTY : ObjectMappers.JSON.readValue(jsonFile, WebappDefinition.class);
149+
public static WebappDefinition load(final Path jsonFile) throws IOException {
150+
return jsonFile == null ? EMPTY : ObjectMappers.JSON.readValue(jsonFile.toFile(), WebappDefinition.class);
151151
}
152152

153-
public static WebappDefinition load(final Collection<File> configurationFiles) {
153+
public static WebappDefinition load(final Collection<Path> configurationFiles) {
154154
if (configurationFiles == null || configurationFiles.isEmpty())
155155
return EMPTY;
156156
final WebappDefinition.Builder builder = new WebappDefinition.Builder();
157-
configurationFiles.stream().filter(f -> f.getName().endsWith(".json")).forEach(f -> {
157+
configurationFiles.stream().filter(f -> f.getFileName().toString().endsWith(".json")).forEach(f -> {
158158
try {
159159
builder.add(load(f));
160160
} catch (IOException e) {

src/main/java/com/qwazr/webapps/WebappServer.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import javax.management.JMException;
3232
import javax.servlet.ServletException;
3333
import java.io.IOException;
34-
import java.net.URISyntaxException;
3534
import java.util.HashSet;
3635
import java.util.Set;
3736
import java.util.concurrent.ExecutorService;
@@ -73,9 +72,8 @@ public WebappServer(final ServerConfiguration configuration, final PreBuild preb
7372
final WebappManager.Builder webappManagerBuilder = WebappManager.of(builder, builder.getWebAppContext())
7473
.libraryService(libraryManager.getService())
7574
.registerDefaultFaviconServlet()
76-
.persistSessions(configuration.tempDirectory.toPath().resolve(WebappManager.SESSIONS_PERSISTENCE_DIR))
77-
.webappDefinition(configuration.dataDirectory.toPath(),
78-
WebappDefinition.load(configuration.getEtcFiles()));
75+
.persistSessions(configuration.tempDirectory.resolve(WebappManager.SESSIONS_PERSISTENCE_DIR))
76+
.webappDefinition(configuration.dataDirectory, WebappDefinition.load(configuration.getEtcFiles()));
7977

8078
if (prebuild != null)
8179
prebuild.accept(webappManagerBuilder, builder);
@@ -104,7 +102,7 @@ public WebappServiceInterface getService() {
104102
}
105103

106104
public static synchronized void main(final String... args)
107-
throws IOException, ReflectiveOperationException, ServletException, JMException, URISyntaxException {
105+
throws IOException, ReflectiveOperationException, ServletException, JMException {
108106
if (INSTANCE != null)
109107
shutdown();
110108
INSTANCE = new WebappServer(new ServerConfiguration(args), null);

src/main/java/com/qwazr/webapps/example/DocumentationServlet.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright 2015-2016 Emmanuel Keller / QWAZR
1+
/*
2+
* Copyright 2015-2018 Emmanuel Keller / QWAZR
33
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
**/
15+
*/
1616
package com.qwazr.webapps.example;
1717

1818
import com.qwazr.library.annotations.Library;
@@ -127,11 +127,8 @@ public void doGet(final HttpServletRequest request, final HttpServletResponse re
127127
response.setDateHeader("Last-Modified", file.lastModified());
128128
response.setHeader("Cache-Control", "max-age=86400");
129129
response.setDateHeader("Expires", System.currentTimeMillis() + 86400 * 1000);
130-
InputStream inputStream = new FileInputStream(file);
131-
try {
130+
try (final InputStream inputStream = new FileInputStream(file)) {
132131
IOUtils.copy(inputStream, response.getOutputStream());
133-
} finally {
134-
IOUtils.closeQuietly(inputStream);
135132
}
136133
return;
137134
} else if (file.isDirectory()) {
@@ -146,23 +143,31 @@ public void doGet(final HttpServletRequest request, final HttpServletResponse re
146143
}
147144
}
148145

149-
protected Pair<String, String[]> getRemoteLink(String remotePath, String path) {
146+
protected Pair<String, String[]> getRemoteLink(final String remotePath, final String path) {
150147
if (StringUtils.isEmpty(path))
151148
return null;
152-
String[] parts = StringUtils.split(path, '/');
149+
final StringBuilder pathBuilder = new StringBuilder(path);
150+
final String[] parts = StringUtils.split(path, '/');
153151
if (parts.length > 0) {
154-
path = remotePath + parts[0];
152+
pathBuilder.append(remotePath);
153+
pathBuilder.append(parts[0]);
155154
int i = 0;
156-
for (String part : parts)
157-
if (i++ > 0)
158-
path += '/' + part;
155+
for (String part : parts) {
156+
if (i++ > 0) {
157+
pathBuilder.append('/');
158+
pathBuilder.append(part);
159+
}
160+
}
159161
}
160-
return Pair.of(path, parts);
162+
return Pair.of(pathBuilder.toString(), parts);
161163
}
162164

163165
protected List<File> getBuildList(File parentFile) {
164-
List<File> list = new ArrayList();
165-
for (File file : parentFile.listFiles()) {
166+
final List<File> list = new ArrayList<>();
167+
final File[] files = parentFile.listFiles();
168+
if (files == null)
169+
return list;
170+
for (final File file : files) {
166171
if (file.isDirectory()) {
167172
if (isDocFile(file))
168173
list.add(file);
@@ -175,7 +180,10 @@ protected List<File> getBuildList(File parentFile) {
175180
}
176181

177182
protected boolean isDocFile(File parentFile) {
178-
for (File file : parentFile.listFiles()) {
183+
final File[] files = parentFile.listFiles();
184+
if (files == null)
185+
return false;
186+
for (final File file : files) {
179187
if (file.isFile()) {
180188
if (isDocFile(file.getName()))
181189
return true;

src/main/java/com/qwazr/webapps/exception/WebappException.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public enum Title {
4444

4545
}
4646

47-
public class Error {
47+
public static class Error {
4848

4949
public final int status;
5050
public final Title title;
@@ -71,28 +71,33 @@ public WebappException(Status status, Title title, Throwable e) {
7171
this(status, title, e == null ? null : e.getMessage());
7272
}
7373

74-
private void sendQuietlyHTML(HttpServletResponse response) throws IOException {
75-
PrintWriter printWriter;
74+
private void sendQuietlyHTML(final HttpServletResponse response) throws IOException {
75+
PrintWriter printWriter = null;
7676
try {
77-
printWriter = response.getWriter();
78-
} catch (IllegalStateException e) {
79-
printWriter = new PrintWriter(response.getOutputStream());
77+
try {
78+
printWriter = response.getWriter();
79+
} catch (IllegalStateException e) {
80+
printWriter = new PrintWriter(response.getOutputStream());
81+
}
82+
String message = StringEscapeUtils.escapeHtml4(error.message);
83+
response.setStatus(error.status);
84+
response.setContentType("text/html");
85+
printWriter.print("<html><head><title>");
86+
printWriter.print(error.title.title);
87+
printWriter.println("</title></head>");
88+
printWriter.print("<body><h3>");
89+
printWriter.print(error.title.title);
90+
printWriter.println("</h3>");
91+
printWriter.print("<pre>");
92+
printWriter.print(message);
93+
printWriter.println("</pre></body></html>");
94+
} finally {
95+
if (printWriter != null)
96+
printWriter.close();
8097
}
81-
String message = StringEscapeUtils.escapeHtml4(error.message);
82-
response.setStatus(error.status);
83-
response.setContentType("text/html");
84-
printWriter.print("<html><head><title>");
85-
printWriter.print(error.title.title);
86-
printWriter.println("</title></head>");
87-
printWriter.print("<body><h3>");
88-
printWriter.print(error.title.title);
89-
printWriter.println("</h3>");
90-
printWriter.print("<pre>");
91-
printWriter.print(message);
92-
printWriter.println("</pre></body></html>");
9398
}
9499

95-
private void sendQuietlyXML(HttpServletResponse response) throws IOException {
100+
private void sendQuietlyXML(final HttpServletResponse response) throws IOException {
96101
XMLBuilder2 xml = XMLBuilder2.create("error").a("code", Integer.toString(error.status));
97102
if (error.title != null)
98103
xml.e("title").t(error.title.title).up();
@@ -101,12 +106,12 @@ private void sendQuietlyXML(HttpServletResponse response) throws IOException {
101106
xml.toWriter(true, response.getWriter(), null);
102107
}
103108

104-
private void sendQuietlyJSON(HttpServletResponse response) throws IOException {
109+
private void sendQuietlyJSON(final HttpServletResponse response) throws IOException {
105110
ObjectMappers.JSON.writeValue(response.getWriter(), error);
106111
}
107112

108113
@Override
109-
public void sendQuietly(HttpServletResponse response) {
114+
public void sendQuietly(final HttpServletResponse response) {
110115
try {
111116
String contentType = response.getContentType();
112117
response.setStatus(error.status);

src/test/java/com/qwazr/webapps/test/ServletTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Emmanuel Keller / QWAZR
2+
* Copyright 2017-2018 Emmanuel Keller / QWAZR
33
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package com.qwazr.webapps.test;
1717

18-
import com.google.common.io.Files;
1918
import com.qwazr.server.configuration.ServerConfiguration;
2019
import com.qwazr.utils.RandomUtils;
2120
import com.qwazr.webapps.WebappServer;
@@ -35,7 +34,7 @@
3534
import javax.ws.rs.core.MediaType;
3635
import javax.ws.rs.core.Response;
3736
import java.io.IOException;
38-
import java.net.URISyntaxException;
37+
import java.nio.file.Files;
3938

4039
public class ServletTest implements TestChecker {
4140

@@ -46,12 +45,11 @@ public class ServletTest implements TestChecker {
4645
public static String randomString2;
4746

4847
@BeforeClass
49-
public static void before()
50-
throws IOException, URISyntaxException, ReflectiveOperationException, JMException, ServletException {
48+
public static void before() throws IOException, ReflectiveOperationException, JMException, ServletException {
5149
randomString1 = RandomUtils.alphanumeric(10);
5250
randomString2 = RandomUtils.alphanumeric(10);
5351
server = new WebappServer(ServerConfiguration.of()
54-
.data(Files.createTempDir())
52+
.data(Files.createTempDirectory("servletTest"))
5553
.publicAddress("localhost")
5654
.listenAddress("localhost")
5755
.etcFilter("*.json")

0 commit comments

Comments
 (0)