Skip to content

Commit 2a7b333

Browse files
author
Emmanuel Hugonnet
committed
Fixing little problem where streams might not be closed properly.
1 parent dc9dae4 commit 2a7b333

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

web-core/src/main/java/com/silverpeas/attachment/web/AttachmentRessource.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.File;
2424
import java.io.FileInputStream;
2525
import java.io.IOException;
26+
import java.io.InputStream;
2627
import java.io.OutputStream;
2728
import java.net.URI;
2829
import java.util.StringTokenizer;
@@ -37,6 +38,7 @@
3738
import javax.ws.rs.core.Response;
3839
import javax.ws.rs.core.Response.Status;
3940
import javax.ws.rs.core.StreamingOutput;
41+
import javax.ws.rs.core.UriBuilderException;
4042

4143
import org.silverpeas.attachment.AttachmentServiceFactory;
4244
import org.silverpeas.attachment.model.SimpleDocument;
@@ -125,7 +127,11 @@ public ZipEntity zipFiles(@PathParam("ids") String attachmentIds) {
125127
token).path("zipcontent").path(zipFile.getName()).build();
126128
long size = ZipManager.compressPathToZip(folderToZip, zipFile);
127129
return new ZipEntity(getUriInfo().getRequestUri(), downloadUri.toString(), size);
128-
} catch (Exception e) {
130+
} catch (IllegalArgumentException e) {
131+
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
132+
} catch (UriBuilderException e) {
133+
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
134+
} catch (IOException e) {
129135
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
130136
} finally {
131137
FileUtils.deleteQuietly(folderToZip);
@@ -142,13 +148,19 @@ public Response getZipContent(@PathParam("name") String zipFileName) {
142148
return Response.ok().build();
143149
} else {
144150
ByteArrayOutputStream output = new ByteArrayOutputStream();
151+
InputStream in = null;
145152
try {
146-
output.write(new FileInputStream(realFile));
147-
} catch (Exception e) {
153+
in = new FileInputStream(realFile);
154+
output.write(in);
155+
return Response.ok().entity(output.toByteArray()).type(MimeTypes.SHORT_ARCHIVE_MIME_TYPE).
156+
build();
157+
} catch (IOException e) {
148158
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
159+
} finally {
160+
IOUtils.closeQuietly(in);
161+
IOUtils.closeQuietly(output);
149162
}
150-
return Response.ok().entity(output.toByteArray()).type(MimeTypes.SHORT_ARCHIVE_MIME_TYPE).
151-
build();
163+
152164
}
153165
}
154166

0 commit comments

Comments
 (0)