[FIX] correct many data-access element that does not close elements
This commit is contained in:
parent
7d4b246d4a
commit
4236dc38bd
@ -5,6 +5,7 @@ import java.awt.image.BufferedImage;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -26,6 +27,7 @@ import org.kar.archidata.annotation.security.PermitTokenInURI;
|
|||||||
import org.kar.archidata.dataAccess.DataAccess;
|
import org.kar.archidata.dataAccess.DataAccess;
|
||||||
import org.kar.archidata.dataAccess.QueryCondition;
|
import org.kar.archidata.dataAccess.QueryCondition;
|
||||||
import org.kar.archidata.dataAccess.options.Condition;
|
import org.kar.archidata.dataAccess.options.Condition;
|
||||||
|
import org.kar.archidata.exception.FailException;
|
||||||
import org.kar.archidata.filter.GenericContext;
|
import org.kar.archidata.filter.GenericContext;
|
||||||
import org.kar.archidata.model.Data;
|
import org.kar.archidata.model.Data;
|
||||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||||
@ -191,7 +193,8 @@ public class DataResource {
|
|||||||
LOGGER.info("Move done");
|
LOGGER.info("Move done");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String saveTemporaryFile(final InputStream uploadedInputStream, final long idData) {
|
public static String saveTemporaryFile(final InputStream uploadedInputStream, final long idData)
|
||||||
|
throws FailException {
|
||||||
return saveFile(uploadedInputStream, DataResource.getTmpFileInData(idData));
|
return saveFile(uploadedInputStream, DataResource.getTmpFileInData(idData));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,35 +211,35 @@ public class DataResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// save uploaded file to a defined location on the server
|
// save uploaded file to a defined location on the server
|
||||||
static String saveFile(final InputStream uploadedInputStream, final String serverLocation) {
|
static String saveFile(final InputStream uploadedInputStream, final String serverLocation) throws FailException {
|
||||||
String out = "";
|
String out = "";
|
||||||
try {
|
MessageDigest md = null;
|
||||||
OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
|
try (OutputStream outpuStream = new FileOutputStream(new File(serverLocation))) {
|
||||||
|
md = MessageDigest.getInstance("SHA-512");
|
||||||
|
} catch (final IOException ex) {
|
||||||
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR, "Can not write in temporary file", ex);
|
||||||
|
} catch (final NoSuchAlgorithmException ex) {
|
||||||
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR, "Can not find sha512 algorithms", ex);
|
||||||
|
}
|
||||||
|
if (md != null) {
|
||||||
|
try (OutputStream outpuStream = new FileOutputStream(new File(serverLocation))) {
|
||||||
int read = 0;
|
int read = 0;
|
||||||
final byte[] bytes = new byte[CHUNK_SIZE_IN];
|
final byte[] bytes = new byte[CHUNK_SIZE_IN];
|
||||||
final MessageDigest md = MessageDigest.getInstance("SHA-512");
|
|
||||||
|
|
||||||
outpuStream = new FileOutputStream(new File(serverLocation));
|
|
||||||
while ((read = uploadedInputStream.read(bytes)) != -1) {
|
while ((read = uploadedInputStream.read(bytes)) != -1) {
|
||||||
// logger.info("write {}", read);
|
// logger.info("write {}", read);
|
||||||
md.update(bytes, 0, read);
|
md.update(bytes, 0, read);
|
||||||
outpuStream.write(bytes, 0, read);
|
outpuStream.write(bytes, 0, read);
|
||||||
}
|
}
|
||||||
LOGGER.info("Flush input stream ... {}", serverLocation);
|
LOGGER.info("Flush input stream ... {}", serverLocation);
|
||||||
System.out.flush();
|
|
||||||
outpuStream.flush();
|
outpuStream.flush();
|
||||||
outpuStream.close();
|
|
||||||
// create the end of sha512
|
// create the end of sha512
|
||||||
final byte[] sha512Digest = md.digest();
|
final byte[] sha512Digest = md.digest();
|
||||||
// convert in hexadecimal
|
// convert in hexadecimal
|
||||||
out = bytesToHex(sha512Digest);
|
out = bytesToHex(sha512Digest);
|
||||||
uploadedInputStream.close();
|
uploadedInputStream.close();
|
||||||
} catch (final IOException ex) {
|
} catch (final IOException ex) {
|
||||||
LOGGER.info("Can not write in temporary file ... ");
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR, "Can not write in temporary file", ex);
|
||||||
ex.printStackTrace();
|
}
|
||||||
} catch (final NoSuchAlgorithmException ex) {
|
|
||||||
LOGGER.info("Can not find sha512 algorithms");
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
@ -267,7 +270,7 @@ public class DataResource {
|
|||||||
public void uploadFile(
|
public void uploadFile(
|
||||||
@Context final SecurityContext sc,
|
@Context final SecurityContext sc,
|
||||||
@FormDataParam("file") final InputStream fileInputStream,
|
@FormDataParam("file") final InputStream fileInputStream,
|
||||||
@FormDataParam("file") final FormDataContentDisposition fileMetaData) {
|
@FormDataParam("file") final FormDataContentDisposition fileMetaData) throws FailException {
|
||||||
final GenericContext gc = (GenericContext) sc.getUserPrincipal();
|
final GenericContext gc = (GenericContext) sc.getUserPrincipal();
|
||||||
LOGGER.info("===================================================");
|
LOGGER.info("===================================================");
|
||||||
LOGGER.info("== DATA uploadFile {}", (gc == null ? "null" : gc.userByToken));
|
LOGGER.info("== DATA uploadFile {}", (gc == null ? "null" : gc.userByToken));
|
||||||
@ -277,8 +280,9 @@ public class DataResource {
|
|||||||
final String filePath = ConfigBaseVariable.getTmpDataFolder() + File.separator + tmpFolderId++;
|
final String filePath = ConfigBaseVariable.getTmpDataFolder() + File.separator + tmpFolderId++;
|
||||||
try {
|
try {
|
||||||
createFolder(ConfigBaseVariable.getTmpDataFolder() + File.separator);
|
createFolder(ConfigBaseVariable.getTmpDataFolder() + File.separator);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException ex) {
|
||||||
e.printStackTrace();
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR,
|
||||||
|
"Impossible to create the folder in the server", ex);
|
||||||
}
|
}
|
||||||
saveFile(fileInputStream, filePath);
|
saveFile(fileInputStream, filePath);
|
||||||
}
|
}
|
||||||
@ -293,7 +297,7 @@ public class DataResource {
|
|||||||
@Context final SecurityContext sc,
|
@Context final SecurityContext sc,
|
||||||
@QueryParam(HttpHeaders.AUTHORIZATION) final String token,
|
@QueryParam(HttpHeaders.AUTHORIZATION) final String token,
|
||||||
@HeaderParam("Range") final String range,
|
@HeaderParam("Range") final String range,
|
||||||
@PathParam("uuid") final UUID uuid) throws Exception {
|
@PathParam("uuid") final UUID uuid) throws FailException {
|
||||||
final GenericContext gc = (GenericContext) sc.getUserPrincipal();
|
final GenericContext gc = (GenericContext) sc.getUserPrincipal();
|
||||||
// logger.info("===================================================");
|
// logger.info("===================================================");
|
||||||
LOGGER.info("== DATA retrieveDataId ? id={} user={}", uuid, (gc == null ? "null" : gc.userByToken));
|
LOGGER.info("== DATA retrieveDataId ? id={} user={}", uuid, (gc == null ? "null" : gc.userByToken));
|
||||||
@ -302,8 +306,12 @@ public class DataResource {
|
|||||||
if (value == null) {
|
if (value == null) {
|
||||||
return Response.status(404).entity("media NOT FOUND: " + uuid).type("text/plain").build();
|
return Response.status(404).entity("media NOT FOUND: " + uuid).type("text/plain").build();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
return buildStream(getFileData(uuid), range,
|
return buildStream(getFileData(uuid), range,
|
||||||
value.mimeType == null ? "application/octet-stream" : value.mimeType);
|
value.mimeType == null ? "application/octet-stream" : value.mimeType);
|
||||||
|
} catch (final Exception ex) {
|
||||||
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR, "Fail to build output stream", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@ -317,7 +325,7 @@ public class DataResource {
|
|||||||
@Context final SecurityContext sc,
|
@Context final SecurityContext sc,
|
||||||
@QueryParam(HttpHeaders.AUTHORIZATION) final String token,
|
@QueryParam(HttpHeaders.AUTHORIZATION) final String token,
|
||||||
@HeaderParam("Range") final String range,
|
@HeaderParam("Range") final String range,
|
||||||
@PathParam("uuid") final UUID uuid) throws Exception {
|
@PathParam("uuid") final UUID uuid) throws FailException {
|
||||||
// GenericContext gc = (GenericContext) sc.getUserPrincipal();
|
// GenericContext gc = (GenericContext) sc.getUserPrincipal();
|
||||||
// logger.info("===================================================");
|
// logger.info("===================================================");
|
||||||
// logger.info("== DATA retrieveDataThumbnailId ? {}", (gc==null?"null":gc.user));
|
// logger.info("== DATA retrieveDataThumbnailId ? {}", (gc==null?"null":gc.user));
|
||||||
@ -336,7 +344,12 @@ public class DataResource {
|
|||||||
// || value.mimeType.contentEquals("image/webp")
|
// || value.mimeType.contentEquals("image/webp")
|
||||||
) {
|
) {
|
||||||
// reads input image
|
// reads input image
|
||||||
final BufferedImage inputImage = ImageIO.read(inputFile);
|
BufferedImage inputImage;
|
||||||
|
try {
|
||||||
|
inputImage = ImageIO.read(inputFile);
|
||||||
|
} catch (final IOException ex) {
|
||||||
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR, "Fail to READ the image", ex);
|
||||||
|
}
|
||||||
final int scaledWidth = 250;
|
final int scaledWidth = 250;
|
||||||
final int scaledHeight = (int) ((float) inputImage.getHeight() / (float) inputImage.getWidth()
|
final int scaledHeight = (int) ((float) inputImage.getHeight() / (float) inputImage.getWidth()
|
||||||
* scaledWidth);
|
* scaledWidth);
|
||||||
@ -369,7 +382,11 @@ public class DataResource {
|
|||||||
out.cacheControl(cc);
|
out.cacheControl(cc);
|
||||||
return out.build();
|
return out.build();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
return buildStream(filePathName, range, value.mimeType);
|
return buildStream(filePathName, range, value.mimeType);
|
||||||
|
} catch (final Exception ex) {
|
||||||
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR, "Fail to build output stream", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@ -400,8 +417,10 @@ public class DataResource {
|
|||||||
*
|
*
|
||||||
* @param range range header
|
* @param range range header
|
||||||
* @return Streaming output
|
* @return Streaming output
|
||||||
|
* @throws FileNotFoundException
|
||||||
* @throws Exception IOException if an error occurs in streaming. */
|
* @throws Exception IOException if an error occurs in streaming. */
|
||||||
private Response buildStream(final String filename, final String range, final String mimeType) throws Exception {
|
private Response buildStream(final String filename, final String range, final String mimeType)
|
||||||
|
throws FailException {
|
||||||
final File file = new File(filename);
|
final File file = new File(filename);
|
||||||
// logger.info("request range : {}", range);
|
// logger.info("request range : {}", range);
|
||||||
// range not requested : Firefox does not send range headers
|
// range not requested : Firefox does not send range headers
|
||||||
@ -448,7 +467,7 @@ public class DataResource {
|
|||||||
}
|
}
|
||||||
final String responseRange = String.format("bytes %d-%d/%d", from, to, file.length());
|
final String responseRange = String.format("bytes %d-%d/%d", from, to, file.length());
|
||||||
// logger.info("responseRange: {}", responseRange);
|
// logger.info("responseRange: {}", responseRange);
|
||||||
final RandomAccessFile raf = new RandomAccessFile(file, "r");
|
try (final RandomAccessFile raf = new RandomAccessFile(file, "r")) {
|
||||||
raf.seek(from);
|
raf.seek(from);
|
||||||
|
|
||||||
final long len = to - from + 1;
|
final long len = to - from + 1;
|
||||||
@ -461,6 +480,11 @@ public class DataResource {
|
|||||||
out.type(mimeType);
|
out.type(mimeType);
|
||||||
}
|
}
|
||||||
return out.build();
|
return out.build();
|
||||||
|
} catch (final FileNotFoundException ex) {
|
||||||
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR, "Fail to find the required file.", ex);
|
||||||
|
} catch (final IOException ex) {
|
||||||
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR, "Fail to access to the required file.", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void undelete(final Long id) throws Exception {
|
public static void undelete(final Long id) throws Exception {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user