Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions core/src/main/java/org/apache/struts2/components/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,11 @@ public void copyParams(Map params) {
* @return the exception as a string.
*/
protected String toString(Throwable t) {
FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
PrintWriter wrt = new PrintWriter(bout);
t.printStackTrace(wrt);
wrt.close();

return bout.toString();
try (FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
PrintWriter wrt = new PrintWriter(bout)) {
t.printStackTrace(wrt);
return bout.toString();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,10 @@ public PropertiesSettings(String name) {
settings = new LocatableProperties(new LocationImpl(null, settingsUrl.toString()));

// Load settings
InputStream in = null;
try {
in = settingsUrl.openStream();
try (InputStream in = settingsUrl.openStream()) {
settings.load(in);
} catch (IOException e) {
throw new StrutsException("Could not load " + name + ".properties: " + e, e);
} finally {
if(in != null) {
try {
in.close();
} catch(IOException io) {
LOG.warn("Unable to close input stream", io);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,11 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro
applyAdditionalHeaders(response);
String location = adjustLocation(finalLocation);

PrintWriter writer = response.getWriter();
InputStreamReader reader = null;
try {
InputStream resourceAsStream = readStream(invocation, location);
try (PrintWriter writer = response.getWriter();
InputStream resourceAsStream = readStream(invocation, location);
InputStreamReader reader = new InputStreamReader(resourceAsStream, charset == null ? Charset.defaultCharset() : charset)) {
logWrongStream(finalLocation, resourceAsStream);
if (charset != null) {
reader = new InputStreamReader(resourceAsStream, charset);
} else {
reader = new InputStreamReader(resourceAsStream);
}
sendStream(writer, reader);
} finally {
if (reader != null)
reader.close();
if (writer != null) {
writer.flush();
writer.close();
}
}
}

Expand Down
16 changes: 3 additions & 13 deletions core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro
// Override any parameters using values on the stack
resolveParamsFromStack(invocation.getStack(), invocation);

OutputStream oOutput = null;

try {
// Find the Response in context
HttpServletResponse oResponse = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);
try (OutputStream oOutput = oResponse.getOutputStream()) {
if (inputStream == null) {
// Find the inputstream from the invocation variable stack
inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
Expand All @@ -236,9 +236,6 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro
throw new IllegalArgumentException(msg);
}

// Find the Response in context
HttpServletResponse oResponse = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);

// Set the content type
if (contentCharSet != null && ! contentCharSet.equals("")) {
oResponse.setContentType(conditionalParse(contentType, invocation)+";charset="+contentCharSet);
Expand Down Expand Up @@ -273,9 +270,6 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro
oResponse.addHeader("Cache-Control", "no-cache");
}

// Get the outputstream
oOutput = oResponse.getOutputStream();

LOG.debug("Streaming result [{}] type=[{}] length=[{}] content-disposition=[{}] charset=[{}]",
inputName, contentType, contentLength, contentDisposition, contentCharSet);

Expand All @@ -291,10 +285,6 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro
// Flush
oOutput.flush();
}
finally {
if (inputStream != null) inputStream.close();
if (oOutput != null) oOutput.close();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,31 +449,14 @@ private File createTemporaryFile(String fileName, String location) throws IOExce
*/
private boolean streamFileToDisk(FileItemStream itemStream, File file) throws IOException {
boolean result = false;
InputStream input = itemStream.openStream();
OutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream(file), bufferSize);
try (InputStream input = itemStream.openStream();
OutputStream output = new BufferedOutputStream(new FileOutputStream(file), bufferSize)) {
byte[] buffer = new byte[bufferSize];
LOG.debug("Streaming file using buffer size {}.", bufferSize);
for (int length = 0; ((length = input.read(buffer)) > 0); ) {
output.write(buffer, 0, length);
}
result = true;
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
LOG.warn("Error occurred during closing of OutputStream.", e);
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
LOG.warn("Error occurred during closing of InputStream.", e);
}
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,9 @@ public void beforeResult(ActionInvocation inv, String actionResult) {
HttpServletResponse res = ServletActionContext.getResponse();
res.setContentType("text/plain");

try {
PrintWriter writer =
ServletActionContext.getResponse().getWriter();
try (PrintWriter writer =
ServletActionContext.getResponse().getWriter()) {
writer.print(stack.findValue(cmd));
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
Expand All @@ -213,8 +211,7 @@ public void beforeResult(ActionInvocation inv, String actionResult) {
ValueStack stack = (ValueStack) ctx.get(ActionContext.VALUE_STACK);
Object rootObject = stack.findValue(rootObjectExpression);

try {
StringWriter writer = new StringWriter();
try (StringWriter writer = new StringWriter()) {
ObjectToHTMLWriter htmlWriter = new ObjectToHTMLWriter(writer);
htmlWriter.write(reflectionProvider, rootObject, rootObjectExpression);
String html = writer.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,10 @@ public void writeTo(JspWriter out, String encoding) throws IOException {
* This method is need only for debug. And needed for tests generated files.
*/
private void writeToFile() {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(File.createTempFile(getClass().getName() + System.currentTimeMillis(), ".log"));
try (FileOutputStream fileOutputStream = new FileOutputStream(File.createTempFile(getClass().getName() + System.currentTimeMillis(), ".log"))){
writeTo(fileOutputStream);
} catch (IOException e) {
// Ignore
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
// Ignore
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,7 @@ protected TemplateLoader createTemplateLoader(ServletContext servletContext, Str
* @see freemarker.template.Configuration#setSettings for the definition of valid settings
*/
protected void loadSettings(ServletContext servletContext) {
InputStream in = null;

try {

in = fileManager.loadFile(ClassLoaderUtil.getResource("freemarker.properties", getClass()));

try (InputStream in = fileManager.loadFile(ClassLoaderUtil.getResource("freemarker.properties", getClass()))){
if (in != null) {
Properties p = new Properties();
p.load(in);
Expand All @@ -465,14 +460,6 @@ protected void loadSettings(ServletContext servletContext) {
LOG.error("Error while loading freemarker settings from /freemarker.properties", e);
} catch (TemplateException e) {
LOG.error("Error while loading freemarker settings from /freemarker.properties", e);
} finally {
if (in != null) {
try {
in.close();
} catch(IOException io) {
LOG.warn("Unable to close input stream", io);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ protected Object findValue(String expr, Class toType) {
}

protected String toString(Throwable t) {
FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
PrintWriter wrt = new PrintWriter(bout);
t.printStackTrace(wrt);
wrt.close();
try (FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
PrintWriter wrt = new PrintWriter(bout)) {
t.printStackTrace(wrt);

return bout.toString();
return bout.toString();
}
}

protected String getBody() {
Expand Down
13 changes: 6 additions & 7 deletions core/src/test/java/org/apache/struts2/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,15 @@ public static String readContent(URL url)
}

StringBuilder buffer = new StringBuilder(128);
InputStream in = url.openStream();
byte[] buf = new byte[4096];
int nbytes;
try (InputStream in = url.openStream()) {
byte[] buf = new byte[4096];
int nbytes;

while((nbytes = in.read(buf)) > 0) {
buffer.append(new String(buf, 0, nbytes));
while ((nbytes = in.read(buf)) > 0) {
buffer.append(new String(buf, 0, nbytes));
}
}

in.close();

return buffer.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ public void testPlainText() throws Exception {

response.setExpectedContentType("text/plain");
response.setExpectedHeader("Content-Disposition", "inline");
InputStream jspResourceInputStream =

try (InputStream jspResourceInputStream =
ClassLoaderUtil.getResourceAsStream(
"org/apache/struts2/dispatcher/someJspFile.jsp",
PlainTextResultTest.class);


try {
PlainTextResultTest.class)) {
servletContext.setResourceAsStream(jspResourceInputStream);
result.execute(invocation);

Expand All @@ -71,9 +69,6 @@ public void testPlainText() throws Exception {
readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
assertEquals(r, e);
}
finally {
jspResourceInputStream.close();
}
}

public void testPlainTextWithoutSlash() throws Exception {
Expand All @@ -82,21 +77,16 @@ public void testPlainTextWithoutSlash() throws Exception {

response.setExpectedContentType("text/plain");
response.setExpectedHeader("Content-Disposition", "inline");
InputStream jspResourceInputStream =
ClassLoaderUtil.getResourceAsStream("org/apache/struts2/dispatcher/someJspFile.jsp", PlainTextResultTest.class);


try {
try (InputStream jspResourceInputStream =
ClassLoaderUtil.getResourceAsStream("org/apache/struts2/dispatcher/someJspFile.jsp", PlainTextResultTest.class)) {
servletContext.setResourceAsStream(jspResourceInputStream);
result.execute(invocation);

String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
String e = AbstractUITagTest.normalize(readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
assertEquals(r, e);
}
finally {
jspResourceInputStream.close();
}
}

public void testPlainTextWithEncoding() throws Exception {
Expand All @@ -106,13 +96,11 @@ public void testPlainTextWithEncoding() throws Exception {

response.setExpectedContentType("text/plain; charset=UTF-8");
response.setExpectedHeader("Content-Disposition", "inline");
InputStream jspResourceInputStream =

try (InputStream jspResourceInputStream =
ClassLoaderUtil.getResourceAsStream(
"org/apache/struts2/dispatcher/someJspFile.jsp",
PlainTextResultTest.class);


try {
PlainTextResultTest.class)) {
servletContext.setResourceAsStream(jspResourceInputStream);
result.execute(invocation);

Expand All @@ -121,15 +109,10 @@ public void testPlainTextWithEncoding() throws Exception {
readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
assertEquals(r, e);
}
finally {
jspResourceInputStream.close();
}
}

protected String readAsString(String resource) throws Exception {
InputStream is = null;
try {
is = ClassLoaderUtil.getResourceAsStream(resource, PlainTextResultTest.class);
try (InputStream is = ClassLoaderUtil.getResourceAsStream(resource, PlainTextResultTest.class)) {
int sizeRead = 0;
byte[] buffer = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
Expand All @@ -138,11 +121,6 @@ protected String readAsString(String resource) throws Exception {
}
return stringBuilder.toString();
}
finally {
if (is != null)
is.close();
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,15 @@ public void verify(URL url) throws Exception {
}

StringBuilder buffer = new StringBuilder(128);
InputStream in = url.openStream();
byte[] buf = new byte[4096];
int nbytes;

while ((nbytes = in.read(buf)) > 0) {
buffer.append(new String(buf, 0, nbytes));
try (InputStream in = url.openStream()) {
byte[] buf = new byte[4096];
int nbytes;

while ((nbytes = in.read(buf)) > 0) {
buffer.append(new String(buf, 0, nbytes));
}
}

in.close();

/**
* compare the trimmed values of each buffer and make sure they're equivalent. however, let's make sure to
* normalize the strings first to account for line termination differences between platforms.
Expand Down
Loading