Refactoring/Improving source: rest (lombok) (#1613)
Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -18,13 +18,12 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import jakarta.validation.ValidationException;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.eclipse.hawkbit.exception.AbstractServerRtException;
|
||||
import org.eclipse.hawkbit.exception.SpServerError;
|
||||
import org.eclipse.hawkbit.rest.json.model.ExceptionInfo;
|
||||
import org.eclipse.hawkbit.rest.util.FileStreamingFailedException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
@@ -37,10 +36,10 @@ import org.springframework.web.multipart.MultipartException;
|
||||
/**
|
||||
* General controller advice for exception handling.
|
||||
*/
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
public class ResponseExceptionHandler {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ResponseExceptionHandler.class);
|
||||
private static final Map<SpServerError, HttpStatus> ERROR_TO_HTTP_STATUS = new EnumMap<>(SpServerError.class);
|
||||
private static final HttpStatus DEFAULT_RESPONSE_STATUS = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
|
||||
@@ -145,7 +144,7 @@ public class ResponseExceptionHandler {
|
||||
public ResponseEntity<Object> handleFileStreamingFailedException(final HttpServletRequest request,
|
||||
final Exception ex) {
|
||||
logRequest(request, ex);
|
||||
LOG.warn("File streaming failed: {}", ex.getMessage());
|
||||
log.warn("File streaming failed: {}", ex.getMessage());
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@@ -248,7 +247,7 @@ public class ResponseExceptionHandler {
|
||||
final Throwable responseCause = throwables.get(throwables.size() - 1);
|
||||
|
||||
if (responseCause.getMessage().isEmpty()) {
|
||||
LOG.warn("Request {} lead to MultipartException without root cause message:\n{}", request.getRequestURL(),
|
||||
log.warn("Request {} lead to MultipartException without root cause message:\n{}", request.getRequestURL(),
|
||||
ex.getStackTrace());
|
||||
}
|
||||
|
||||
@@ -257,7 +256,7 @@ public class ResponseExceptionHandler {
|
||||
}
|
||||
|
||||
private void logRequest(final HttpServletRequest request, final Exception ex) {
|
||||
LOG.debug("Handling exception {} of request {}", ex.getClass().getName(), request.getRequestURL());
|
||||
log.debug("Handling exception {} of request {}", ex.getClass().getName(), request.getRequestURL());
|
||||
}
|
||||
|
||||
private ExceptionInfo createExceptionInfo(final Exception ex) {
|
||||
|
||||
@@ -24,10 +24,11 @@ import jakarta.servlet.ServletOutputStream;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.eclipse.hawkbit.artifact.repository.model.DbArtifact;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -36,21 +37,16 @@ import org.springframework.http.ResponseEntity;
|
||||
/**
|
||||
* Utility class for artifact file streaming.
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@Slf4j
|
||||
public final class FileStreamingUtil {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FileStreamingUtil.class);
|
||||
|
||||
/**
|
||||
* File suffix for MDH hash download (see Linux md5sum).
|
||||
*/
|
||||
public static final String ARTIFACT_MD5_DWNL_SUFFIX = ".MD5SUM";
|
||||
|
||||
private static final int BUFFER_SIZE = 0x2000; // 8k
|
||||
|
||||
private FileStreamingUtil() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write a md5 file response.
|
||||
*
|
||||
@@ -150,12 +146,12 @@ public final class FileStreamingUtil {
|
||||
// Validate and process Range and If-Range headers.
|
||||
final String range = request.getHeader("Range");
|
||||
if (lastModified > 0 && range != null) {
|
||||
LOG.debug("range header for filename ({}) is: {}", filename, range);
|
||||
log.debug("range header for filename ({}) is: {}", filename, range);
|
||||
|
||||
// Range header matches"bytes=n-n,n-n,n-n..."
|
||||
if (!range.matches("^bytes=\\d*-\\d*(,\\d*-\\d*)*+$")) {
|
||||
response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + length);
|
||||
LOG.debug("range header for filename ({}) is not satisfiable: ", filename);
|
||||
log.debug("range header for filename ({}) is not satisfiable: ", filename);
|
||||
return new ResponseEntity<>(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE);
|
||||
}
|
||||
|
||||
@@ -174,17 +170,17 @@ public final class FileStreamingUtil {
|
||||
|
||||
// full request - no range
|
||||
if (ranges.isEmpty() || ranges.get(0).equals(full)) {
|
||||
LOG.debug("filename ({}) results into a full request: ", filename);
|
||||
log.debug("filename ({}) results into a full request: ", filename);
|
||||
result = handleFullFileRequest(artifact, filename, response, progressListener, full);
|
||||
}
|
||||
// standard range request
|
||||
else if (ranges.size() == 1) {
|
||||
LOG.debug("filename ({}) results into a standard range request: ", filename);
|
||||
log.debug("filename ({}) results into a standard range request: ", filename);
|
||||
result = handleStandardRangeRequest(artifact, filename, response, progressListener, ranges);
|
||||
}
|
||||
// multipart range request
|
||||
else {
|
||||
LOG.debug("filename ({}) results into a multipart range request: ", filename);
|
||||
log.debug("filename ({}) results into a multipart range request: ", filename);
|
||||
result = handleMultipartRangeRequest(artifact, filename, response, progressListener, ranges);
|
||||
}
|
||||
|
||||
@@ -266,7 +262,7 @@ public final class FileStreamingUtil {
|
||||
ranges.add(full);
|
||||
}
|
||||
} catch (final IllegalArgumentException ignore) {
|
||||
LOG.info("Invalid if-range header field", ignore);
|
||||
log.info("Invalid if-range header field", ignore);
|
||||
ranges.add(full);
|
||||
}
|
||||
}
|
||||
@@ -318,7 +314,7 @@ public final class FileStreamingUtil {
|
||||
final ServletOutputStream to = response.getOutputStream();
|
||||
copyStreams(from, to, progressListener, r.getStart(), r.getLength(), filename);
|
||||
} catch (final IOException e) {
|
||||
LOG.error("standardRangeRequest of file ({}) failed!", filename, e);
|
||||
log.error("standardRangeRequest of file ({}) failed!", filename, e);
|
||||
throw new FileStreamingFailedException(filename);
|
||||
}
|
||||
|
||||
@@ -330,7 +326,7 @@ public final class FileStreamingUtil {
|
||||
final String filename) throws IOException {
|
||||
|
||||
final long startMillis = System.currentTimeMillis();
|
||||
LOG.trace("Start of copy-streams of file {} from {} to {}", filename, start, length);
|
||||
log.trace("Start of copy-streams of file {} from {} to {}", filename, start, length);
|
||||
|
||||
Objects.requireNonNull(from);
|
||||
Objects.requireNonNull(to);
|
||||
@@ -381,7 +377,7 @@ public final class FileStreamingUtil {
|
||||
+ " bytes could not be written to client, total time on write: !" + totalTime + " ms");
|
||||
}
|
||||
|
||||
LOG.trace("Finished copy-stream of file {} with length {} in {} ms", filename, length, totalTime);
|
||||
log.trace("Finished copy-stream of file {} with length {} in {} ms", filename, length, totalTime);
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
@@ -9,18 +9,17 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.rest.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.ResultHandler;
|
||||
import org.springframework.test.web.servlet.result.PrintingResultHandler;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@Slf4j
|
||||
public abstract class MockMvcResultPrinter {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MockMvcResultPrinter.class);
|
||||
|
||||
private MockMvcResultPrinter() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Print {@link MvcResult} details to logger.
|
||||
@@ -39,7 +38,7 @@ public abstract class MockMvcResultPrinter {
|
||||
|
||||
@Override
|
||||
public void printHeading(final String heading) {
|
||||
LOG.debug(String.format("%20s:", heading));
|
||||
log.debug(String.format("%20s:", heading));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,7 +48,7 @@ public abstract class MockMvcResultPrinter {
|
||||
if (value != null && value.getClass().isArray()) {
|
||||
value = CollectionUtils.arrayToList(value);
|
||||
}
|
||||
LOG.debug(String.format("%20s = %s", label, value));
|
||||
log.debug(String.format("%20s = %s", label, value));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user