ATS-175 : Replace HTTP numeric codes with constants

This commit is contained in:
Cezar.Leahu 2018-10-30 16:11:10 +02:00
parent 2842cafbd9
commit ad4ea1574e
16 changed files with 116 additions and 67 deletions

View File

@ -16,6 +16,7 @@ import static org.alfresco.transformer.fs.FileManager.createSourceFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFile; import static org.alfresco.transformer.fs.FileManager.createTargetFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFileName; import static org.alfresco.transformer.fs.FileManager.createTargetFileName;
import static org.alfresco.transformer.logging.StandardMessages.ENTERPRISE_LICENCE; import static org.alfresco.transformer.logging.StandardMessages.ENTERPRISE_LICENCE;
import static org.springframework.http.HttpStatus.OK;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
@ -155,7 +156,7 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile); final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile);
LogEntry.setTargetSize(targetFile.length()); LogEntry.setTargetSize(targetFile.length());
long time = LogEntry.setStatusCodeAndMessage(200, "Success"); long time = LogEntry.setStatusCodeAndMessage(OK.value(), "Success");
time += LogEntry.addDelay(testDelay); time += LogEntry.addDelay(testDelay);
getProbeTestTransform().recordTransformTime(time); getProbeTestTransform().recordTransformTime(time);
return body; return body;

View File

@ -32,6 +32,8 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -191,7 +193,7 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
.param("allowEnlargement", "true") .param("allowEnlargement", "true")
.param("maintainAspectRatio", "true")) .param("maintainAspectRatio", "true"))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
} }
@ -211,7 +213,7 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
.param("allowEnlargement", "false") .param("allowEnlargement", "false")
.param("maintainAspectRatio", "false")) .param("maintainAspectRatio", "false"))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
} }
@ -231,7 +233,7 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
when(mockExecutionResult.getExitValue()).thenReturn(1); when(mockExecutionResult.getExitValue()).thenReturn(1);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", "xxx")) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", "xxx"))
.andExpect(status().is(400)) .andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("Transformer exit code was not 0: \nSTDERR"))); .andExpect(status().reason(containsString("Transformer exit code was not 0: \nSTDERR")));
} }
@ -259,7 +261,7 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension); headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension);
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource( ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, HttpStatus.OK); sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response); when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));

View File

@ -18,6 +18,8 @@ import static org.alfresco.transformer.fs.FileManager.createTargetFileName;
import static org.alfresco.transformer.logging.StandardMessages.ENTERPRISE_LICENCE; import static org.alfresco.transformer.logging.StandardMessages.ENTERPRISE_LICENCE;
import static org.alfresco.transformer.util.Util.stringToBoolean; import static org.alfresco.transformer.util.Util.stringToBoolean;
import static org.alfresco.transformer.util.Util.stringToInteger; import static org.alfresco.transformer.util.Util.stringToInteger;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
@ -166,7 +168,7 @@ public class ImageMagickController extends AbstractTransformerController
final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile); final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile);
LogEntry.setTargetSize(targetFile.length()); LogEntry.setTargetSize(targetFile.length());
long time = LogEntry.setStatusCodeAndMessage(200, "Success"); long time = LogEntry.setStatusCodeAndMessage(OK.value(), "Success");
time += LogEntry.addDelay(testDelay); time += LogEntry.addDelay(testDelay);
getProbeTestTransform().recordTransformTime(time); getProbeTestTransform().recordTransformTime(time);
return body; return body;
@ -217,7 +219,7 @@ public class ImageMagickController extends AbstractTransformerController
} }
else if (!GRAVITY_VALUES.contains(cropGravity)) else if (!GRAVITY_VALUES.contains(cropGravity))
{ {
throw new TransformException(400, "Invalid cropGravity value"); throw new TransformException(BAD_REQUEST.value(), "Invalid cropGravity value");
} }
} }

View File

@ -32,6 +32,8 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -186,7 +188,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
.file(sourceFile) .file(sourceFile)
.param("targetExtension", targetExtension) .param("targetExtension", targetExtension)
.param("cropGravity", value)) .param("cropGravity", value))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
} }
@ -199,7 +201,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
.file(sourceFile) .file(sourceFile)
.param("targetExtension", targetExtension) .param("targetExtension", targetExtension)
.param("cropGravity", "badValue")) .param("cropGravity", "badValue"))
.andExpect(status().is(400)); .andExpect(status().is(BAD_REQUEST.value()));
} }
@Test @Test
@ -231,7 +233,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
.param("allowEnlargement", "true") .param("allowEnlargement", "true")
.param("maintainAspectRatio", "true")) .param("maintainAspectRatio", "true"))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
} }
@ -265,7 +267,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
.param("allowEnlargement", "false") .param("allowEnlargement", "false")
.param("maintainAspectRatio", "false")) .param("maintainAspectRatio", "false"))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
} }
@ -282,7 +284,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
.param("resizeWidth", "321") .param("resizeWidth", "321")
.param("resizeHeight", "654") .param("resizeHeight", "654")
.param("commandOptions", "( horrible command / );")) .param("commandOptions", "( horrible command / );"))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
} }
@ -302,7 +304,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
when(mockExecutionResult.getExitValue()).thenReturn(1); when(mockExecutionResult.getExitValue()).thenReturn(1);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", "xxx")) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", "xxx"))
.andExpect(status().is(400)) .andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("Transformer exit code was not 0: \nSTDERR"))); .andExpect(status().reason(containsString("Transformer exit code was not 0: \nSTDERR")));
} }
@ -330,7 +332,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension); headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension);
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource( ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, HttpStatus.OK); sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response); when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));

View File

@ -16,6 +16,7 @@ import static org.alfresco.transformer.fs.FileManager.createSourceFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFile; import static org.alfresco.transformer.fs.FileManager.createTargetFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFileName; import static org.alfresco.transformer.fs.FileManager.createTargetFileName;
import static org.alfresco.transformer.logging.StandardMessages.ENTERPRISE_LICENCE; import static org.alfresco.transformer.logging.StandardMessages.ENTERPRISE_LICENCE;
import static org.springframework.http.HttpStatus.OK;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
@ -120,7 +121,7 @@ public class LibreOfficeController extends AbstractTransformerController
final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile); final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile);
LogEntry.setTargetSize(targetFile.length()); LogEntry.setTargetSize(targetFile.length());
long time = LogEntry.setStatusCodeAndMessage(200, "Success"); long time = LogEntry.setStatusCodeAndMessage(OK.value(), "Success");
time += LogEntry.addDelay(testDelay); time += LogEntry.addDelay(testDelay);
getProbeTestTransform().recordTransformTime(time); getProbeTestTransform().recordTransformTime(time);
return body; return body;

View File

@ -1,5 +1,8 @@
package org.alfresco.transformer.executors; package org.alfresco.transformer.executors;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -62,7 +65,8 @@ public class LibreOfficeJavaExecutor implements JavaExecutor
} }
catch (OfficeException e) catch (OfficeException e)
{ {
throw new TransformException(400, "LibreOffice server conversion failed: \n" + throw new TransformException(BAD_REQUEST.value(),
"LibreOffice server conversion failed: \n" +
" from file: " + sourceFile + "\n" + " from file: " + sourceFile + "\n" +
" to file: " + targetFile, e); " to file: " + targetFile, e);
} }
@ -86,7 +90,8 @@ public class LibreOfficeJavaExecutor implements JavaExecutor
if (!targetFile.exists() || targetFile.length() == 0L) if (!targetFile.exists() || targetFile.length() == 0L)
{ {
throw new TransformException(500, "Transformer failed to create an output file"); throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Transformer failed to create an output file");
} }
} }
@ -120,7 +125,8 @@ public class LibreOfficeJavaExecutor implements JavaExecutor
} }
catch (IOException iox) catch (IOException iox)
{ {
throw new TransformException(500, "Error creating empty PDF file", iox); throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Error creating empty PDF file", iox);
} }
} }
} }

View File

@ -24,6 +24,8 @@ import static org.alfresco.transformer.fs.FileManager.createTargetFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFileName; import static org.alfresco.transformer.fs.FileManager.createTargetFileName;
import static org.alfresco.transformer.logging.StandardMessages.ENTERPRISE_LICENCE; import static org.alfresco.transformer.logging.StandardMessages.ENTERPRISE_LICENCE;
import static org.alfresco.transformer.util.Util.stringToBoolean; import static org.alfresco.transformer.util.Util.stringToBoolean;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
@ -129,7 +131,7 @@ public class TikaController extends AbstractTransformerController
{ {
if (!TRANSFORM_NAMES.contains(transform)) if (!TRANSFORM_NAMES.contains(transform))
{ {
throw new TransformException(400, "Invalid transform value"); throw new TransformException(BAD_REQUEST.value(), "Invalid transform value");
} }
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), targetExtension); String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), targetExtension);
@ -148,7 +150,7 @@ public class TikaController extends AbstractTransformerController
final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile); final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile);
LogEntry.setTargetSize(targetFile.length()); LogEntry.setTargetSize(targetFile.length());
long time = LogEntry.setStatusCodeAndMessage(200, "Success"); long time = LogEntry.setStatusCodeAndMessage(OK.value(), "Success");
time += LogEntry.addDelay(testDelay); time += LogEntry.addDelay(testDelay);
getProbeTestTransform().recordTransformTime(time); getProbeTestTransform().recordTransformTime(time);
return body; return body;

View File

@ -1,6 +1,7 @@
package org.alfresco.transformer.executors; package org.alfresco.transformer.executors;
import org.springframework.stereotype.Component; import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -11,6 +12,7 @@ import org.alfresco.transformer.exceptions.TransformException;
import org.alfresco.transformer.logging.LogEntry; import org.alfresco.transformer.logging.LogEntry;
import org.apache.tika.exception.TikaException; import org.apache.tika.exception.TikaException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@Component @Component
@ -35,15 +37,16 @@ public class TikaJavaExecutor implements JavaExecutor
} }
catch (IllegalArgumentException e) catch (IllegalArgumentException e)
{ {
throw new TransformException(400, getMessage(e)); throw new TransformException(BAD_REQUEST.value(), getMessage(e));
} }
catch (Exception e) catch (Exception e)
{ {
throw new TransformException(500, getMessage(e)); throw new TransformException(INTERNAL_SERVER_ERROR.value(), getMessage(e));
} }
if (!targetFile.exists() || targetFile.length() == 0) if (!targetFile.exists() || targetFile.length() == 0)
{ {
throw new TransformException(500, "Transformer failed to create an output file"); throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Transformer failed to create an output file");
} }
} }

View File

@ -63,6 +63,8 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -236,7 +238,7 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
? mockMvcRequest("/transform", sourceFile, "targetExtension", this.targetExtension) ? mockMvcRequest("/transform", sourceFile, "targetExtension", this.targetExtension)
: mockMvcRequest("/transform", sourceFile, "targetExtension", this.targetExtension, "includeContents", includeContents.toString()); : mockMvcRequest("/transform", sourceFile, "targetExtension", this.targetExtension, "includeContents", includeContents.toString());
MvcResult result = mockMvc.perform(requestBuilder) MvcResult result = mockMvc.perform(requestBuilder)
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + this.targetExtension)). .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + this.targetExtension)).
andReturn(); andReturn();
String content = result.getResponse().getContentAsString(); String content = result.getResponse().getContentAsString();
@ -335,7 +337,7 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
mockTransformCommand(PDF, TXT, MIMETYPE_PDF, true); mockTransformCommand(PDF, TXT, MIMETYPE_PDF, true);
targetEncoding = "rubbish"; targetEncoding = "rubbish";
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension)) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(500)); .andExpect(status().is(INTERNAL_SERVER_ERROR.value()));
} }
// --- Archive --- // --- Archive ---
@ -498,7 +500,7 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
{ {
mockTransformCommand(PDF, TXT, MIMETYPE_PDF, true); mockTransformCommand(PDF, TXT, MIMETYPE_PDF, true);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension).param("notExtractBookmarksText", "true")) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension).param("notExtractBookmarksText", "true"))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
} }
@ -538,7 +540,7 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension); headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension);
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource( ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, HttpStatus.OK); sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response); when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));

View File

@ -29,6 +29,7 @@ import static org.alfresco.transformer.fs.FileManager.buildFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFileName; import static org.alfresco.transformer.fs.FileManager.createTargetFileName;
import static org.alfresco.transformer.fs.FileManager.getFilenameFromContentDisposition; import static org.alfresco.transformer.fs.FileManager.getFilenameFromContentDisposition;
import static org.alfresco.transformer.fs.FileManager.save; import static org.alfresco.transformer.fs.FileManager.save;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import java.io.File; import java.io.File;
@ -151,7 +152,7 @@ public abstract class AbstractTransformerController implements TransformControll
} }
catch (Exception e) catch (Exception e)
{ {
reply.setStatus(500); reply.setStatus(INTERNAL_SERVER_ERROR.value());
reply.setErrorDetails("Failed at reading the source file. " + e.getMessage()); reply.setErrorDetails("Failed at reading the source file. " + e.getMessage());
return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus())); return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()));
@ -177,7 +178,7 @@ public abstract class AbstractTransformerController implements TransformControll
} }
catch (Exception e) catch (Exception e)
{ {
reply.setStatus(500); reply.setStatus(INTERNAL_SERVER_ERROR.value());
reply.setErrorDetails("Failed at processing transformation. " + e.getMessage()); reply.setErrorDetails("Failed at processing transformation. " + e.getMessage());
return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus())); return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()));
@ -205,7 +206,7 @@ public abstract class AbstractTransformerController implements TransformControll
} }
catch (Exception e) catch (Exception e)
{ {
reply.setStatus(500); reply.setStatus(INTERNAL_SERVER_ERROR.value());
reply.setErrorDetails("Failed at writing the transformed file. " + e.getMessage()); reply.setErrorDetails("Failed at writing the transformed file. " + e.getMessage());
return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus())); return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()));

View File

@ -1,5 +1,7 @@
package org.alfresco.transformer; package org.alfresco.transformer;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
@ -92,7 +94,7 @@ public interface TransformController
String transformerName = getTransformerName(); String transformerName = getTransformerName();
String name = e.getParameterName(); String name = e.getParameterName();
String message = "Request parameter " + name + " is of the wrong type"; String message = "Request parameter " + name + " is of the wrong type";
int statusCode = 400; int statusCode = BAD_REQUEST.value();
logger.error(message); logger.error(message);
@ -108,7 +110,7 @@ public interface TransformController
String transformerName = getTransformerName(); String transformerName = getTransformerName();
String name = e.getParameterName(); String name = e.getParameterName();
String message = "Request parameter " + name + " is missing"; String message = "Request parameter " + name + " is missing";
int statusCode = 400; int statusCode = BAD_REQUEST.value();
logger.error(message); logger.error(message);

View File

@ -1,5 +1,8 @@
package org.alfresco.transformer.executors; package org.alfresco.transformer.executors;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import java.io.File; import java.io.File;
import java.util.Map; import java.util.Map;
@ -34,12 +37,14 @@ public abstract class AbstractCommandExecutor implements CommandExecutor
if (result.getExitValue() != 0 && result.getStdErr() != null && result.getStdErr().length() > 0) if (result.getExitValue() != 0 && result.getStdErr() != null && result.getStdErr().length() > 0)
{ {
throw new TransformException(400, "Transformer exit code was not 0: \n" + result.getStdErr()); throw new TransformException(BAD_REQUEST.value(),
"Transformer exit code was not 0: \n" + result.getStdErr());
} }
if (!targetFile.exists() || targetFile.length() == 0) if (!targetFile.exists() || targetFile.length() == 0)
{ {
throw new TransformException(500, "Transformer failed to create an output file"); throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Transformer failed to create an output file");
} }
} }
@ -52,13 +57,15 @@ public abstract class AbstractCommandExecutor implements CommandExecutor
RuntimeExec.ExecutionResult result = checkCommand.execute(); RuntimeExec.ExecutionResult result = checkCommand.execute();
if (result.getExitValue() != 0 && result.getStdErr() != null && result.getStdErr().length() > 0) if (result.getExitValue() != 0 && result.getStdErr() != null && result.getStdErr().length() > 0)
{ {
throw new TransformException(500, "Transformer version check exit code was not 0: \n" + result); throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Transformer version check exit code was not 0: \n" + result);
} }
version = result.getStdOut().trim(); version = result.getStdOut().trim();
if (version.isEmpty()) if (version.isEmpty())
{ {
throw new TransformException(500, "Transformer version check failed to create any output"); throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Transformer version check failed to create any output");
} }
} }

View File

@ -1,5 +1,9 @@
package org.alfresco.transformer.fs; package org.alfresco.transformer.fs;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -64,7 +68,7 @@ public class FileManager
if (filename == null || filename.isEmpty()) if (filename == null || filename.isEmpty())
{ {
String sourceOrTarget = source ? "source" : "target"; String sourceOrTarget = source ? "source" : "target";
int statusCode = source ? 400 : 500; int statusCode = source ? BAD_REQUEST.value() : INTERNAL_SERVER_ERROR.value();
throw new TransformException(statusCode, "The " + sourceOrTarget + " filename was not supplied"); throw new TransformException(statusCode, "The " + sourceOrTarget + " filename was not supplied");
} }
return filename; return filename;
@ -78,7 +82,8 @@ public class FileManager
} }
catch (IOException e) catch (IOException e)
{ {
throw new TransformException(507, "Failed to store the source file", e); throw new TransformException(INSUFFICIENT_STORAGE.value(),
"Failed to store the source file", e);
} }
} }
@ -90,7 +95,7 @@ public class FileManager
} }
catch (IOException e) catch (IOException e)
{ {
throw new TransformException(507, "Failed to store the source file", e); throw new TransformException(INSUFFICIENT_STORAGE.value(), "Failed to store the source file", e);
} }
} }
@ -105,12 +110,14 @@ public class FileManager
} }
else else
{ {
throw new TransformException(500, "Could not read the target file: " + file.getPath()); throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Could not read the target file: " + file.getPath());
} }
} }
catch (MalformedURLException e) catch (MalformedURLException e)
{ {
throw new TransformException(500, "The target filename was malformed: " + file.getPath(), e); throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"The target filename was malformed: " + file.getPath(), e);
} }
} }

View File

@ -26,6 +26,7 @@
package org.alfresco.transformer.logging; package org.alfresco.transformer.logging;
import static java.lang.Math.max; import static java.lang.Math.max;
import static org.springframework.http.HttpStatus.OK;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collection; import java.util.Collection;
@ -196,7 +197,7 @@ public final class LogEntry
public static void complete() public static void complete()
{ {
LogEntry logEntry = currentLogEntry.get(); LogEntry logEntry = currentLogEntry.get();
if (logEntry.statusCode == 200) if (logEntry.statusCode == OK.value())
{ {
logEntry.durationStreamOut = System.currentTimeMillis() - logEntry.start - logEntry.durationStreamOut = System.currentTimeMillis() - logEntry.start -
logEntry.durationStreamIn - max(logEntry.durationTransform, 0) - max(logEntry.durationDelay, 0); logEntry.durationStreamIn - max(logEntry.durationTransform, 0) - max(logEntry.durationDelay, 0);

View File

@ -27,6 +27,10 @@ package org.alfresco.transformer.probes;
import static org.alfresco.transformer.fs.FileManager.SOURCE_FILE; import static org.alfresco.transformer.fs.FileManager.SOURCE_FILE;
import static org.alfresco.transformer.fs.FileManager.TARGET_FILE; import static org.alfresco.transformer.fs.FileManager.TARGET_FILE;
import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -187,7 +191,7 @@ public abstract class ProbeTestTransform
{ {
String probeMessage = getProbeMessage(isLiveProbe); String probeMessage = getProbeMessage(isLiveProbe);
String message = "Success - No transform."; String message = "Success - No transform.";
LogEntry.setStatusCodeAndMessage(200, probeMessage + message); LogEntry.setStatusCodeAndMessage(OK.value(), probeMessage + message);
if (!isLiveProbe && !readySent.getAndSet(true)) if (!isLiveProbe && !readySent.getAndSet(true))
{ {
logger.info(probeMessage+message); logger.info(probeMessage+message);
@ -223,7 +227,8 @@ public abstract class ProbeTestTransform
if (time > maxTime) if (time > maxTime)
{ {
throw new TransformException(500, getMessagePrefix(isLiveProbe) + throw new TransformException(INTERNAL_SERVER_ERROR.value(),
getMessagePrefix(isLiveProbe) +
message + " which is more than " + livenessPercent + message + " which is more than " + livenessPercent +
"% slower than the normal value of " + normalTime + "ms"); "% slower than the normal value of " + normalTime + "ms");
} }
@ -240,14 +245,14 @@ public abstract class ProbeTestTransform
{ {
if (die.get()) if (die.get())
{ {
throw new TransformException(429, getMessagePrefix(isLiveProbe) + throw new TransformException(TOO_MANY_REQUESTS.value(), getMessagePrefix(isLiveProbe) +
"Transformer requested to die. A transform took longer than "+ "Transformer requested to die. A transform took longer than "+
(maxTransformTime *1000)+" seconds"); (maxTransformTime *1000)+" seconds");
} }
if (maxTransformCount > 0 && transformCount.get() > maxTransformCount) if (maxTransformCount > 0 && transformCount.get() > maxTransformCount)
{ {
throw new TransformException(429, getMessagePrefix(isLiveProbe) + throw new TransformException(TOO_MANY_REQUESTS.value(), getMessagePrefix(isLiveProbe) +
"Transformer requested to die. It has performed more than "+ "Transformer requested to die. It has performed more than "+
maxTransformCount+" transformations"); maxTransformCount+" transformations");
} }
@ -264,7 +269,7 @@ public abstract class ProbeTestTransform
} }
catch (IOException e) catch (IOException e)
{ {
throw new TransformException(507, getMessagePrefix(isLiveProbe)+ throw new TransformException(INSUFFICIENT_STORAGE.value(), getMessagePrefix(isLiveProbe)+
"Failed to store the source file", e); "Failed to store the source file", e);
} }
long length = sourceFile.length(); long length = sourceFile.length();
@ -320,17 +325,19 @@ public abstract class ProbeTestTransform
String probeMessage = getProbeMessage(isLiveProbe); String probeMessage = getProbeMessage(isLiveProbe);
if (!targetFile.exists() || !targetFile.isFile()) if (!targetFile.exists() || !targetFile.isFile())
{ {
throw new TransformException(500, probeMessage +"Target File \""+targetFile.getAbsolutePath()+"\" did not exist"); throw new TransformException(INTERNAL_SERVER_ERROR.value(),
probeMessage + "Target File \"" + targetFile.getAbsolutePath() + "\" did not exist");
} }
long length = targetFile.length(); long length = targetFile.length();
if (length < minExpectedLength || length > maxExpectedLength) if (length < minExpectedLength || length > maxExpectedLength)
{ {
throw new TransformException(500, probeMessage +"Target File \""+targetFile.getAbsolutePath()+ throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"\" was the wrong size ("+ length+"). Needed to be between "+minExpectedLength+" and "+ probeMessage + "Target File \"" + targetFile.getAbsolutePath() +
maxExpectedLength); "\" was the wrong size (" + length + "). Needed to be between " +
minExpectedLength + " and " + maxExpectedLength);
} }
LogEntry.setTargetSize(length); LogEntry.setTargetSize(length);
LogEntry.setStatusCodeAndMessage(200, probeMessage +"Success - "+message); LogEntry.setStatusCodeAndMessage(OK.value(), probeMessage + "Success - " + message);
} }
private String getMessagePrefix(boolean isLiveProbe) private String getMessagePrefix(boolean isLiveProbe)

View File

@ -28,6 +28,9 @@ package org.alfresco.transformer;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -158,7 +161,7 @@ public abstract class AbstractTransformerControllerTest
public void simpleTransformTest() throws Exception public void simpleTransformTest() throws Exception
{ {
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension)) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
} }
@ -168,7 +171,7 @@ public abstract class AbstractTransformerControllerTest
{ {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension, "testDelay", "400")) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension, "testDelay", "400"))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
long ms = System.currentTimeMillis() - start; long ms = System.currentTimeMillis() - start;
@ -181,7 +184,7 @@ public abstract class AbstractTransformerControllerTest
public void noTargetFileTest() throws Exception public void noTargetFileTest() throws Exception
{ {
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", "xxx")) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", "xxx"))
.andExpect(status().is(500)); .andExpect(status().is(INTERNAL_SERVER_ERROR.value()));
} }
@Test @Test
@ -191,7 +194,7 @@ public abstract class AbstractTransformerControllerTest
sourceFile = new MockMultipartFile("file", "../quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes); sourceFile = new MockMultipartFile("file", "../quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension)) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
} }
@ -203,7 +206,7 @@ public abstract class AbstractTransformerControllerTest
sourceFile = new MockMultipartFile("file", "../quick", sourceMimetype, expectedSourceFileBytes); sourceFile = new MockMultipartFile("file", "../quick", sourceMimetype, expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension)) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(200)) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension)); .andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
} }
@ -215,7 +218,7 @@ public abstract class AbstractTransformerControllerTest
sourceFile = new MockMultipartFile("file", "abc/", sourceMimetype, expectedSourceFileBytes); sourceFile = new MockMultipartFile("file", "abc/", sourceMimetype, expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension)) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(400)) .andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("The source filename was not supplied"))); .andExpect(status().reason(containsString("The source filename was not supplied")));
} }
@ -225,7 +228,7 @@ public abstract class AbstractTransformerControllerTest
sourceFile = new MockMultipartFile("file", "", sourceMimetype, expectedSourceFileBytes); sourceFile = new MockMultipartFile("file", "", sourceMimetype, expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension)) mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(400)) .andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("The source filename was not supplied"))); .andExpect(status().reason(containsString("The source filename was not supplied")));
} }
@ -233,7 +236,7 @@ public abstract class AbstractTransformerControllerTest
public void noTargetExtensionTest() throws Exception public void noTargetExtensionTest() throws Exception
{ {
mockMvc.perform(mockMvcRequest("/transform", sourceFile)) mockMvc.perform(mockMvcRequest("/transform", sourceFile))
.andExpect(status().is(400)) .andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("Request parameter targetExtension is missing"))); .andExpect(status().reason(containsString("Request parameter targetExtension is missing")));
} }
@ -280,12 +283,12 @@ public abstract class AbstractTransformerControllerTest
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.content(tr)) .content(tr))
.andExpect(status().is(HttpStatus.BAD_REQUEST.value())) .andExpect(status().is(BAD_REQUEST.value()))
.andReturn().getResponse().getContentAsString(); .andReturn().getResponse().getContentAsString();
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, TransformReply.class); TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, TransformReply.class);
// Assert the reply // Assert the reply
assertEquals(HttpStatus.BAD_REQUEST.value(), transformReply.getStatus()); assertEquals(BAD_REQUEST.value(), transformReply.getStatus());
} }
} }