mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-08-07 17:48:35 +00:00
ATS-175 : Replace HTTP numeric codes with constants
This commit is contained in:
@@ -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.getFilenameFromContentDisposition;
|
||||
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 java.io.File;
|
||||
@@ -151,7 +152,7 @@ public abstract class AbstractTransformerController implements TransformControll
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
reply.setStatus(500);
|
||||
reply.setStatus(INTERNAL_SERVER_ERROR.value());
|
||||
reply.setErrorDetails("Failed at reading the source file. " + e.getMessage());
|
||||
|
||||
return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()));
|
||||
@@ -177,7 +178,7 @@ public abstract class AbstractTransformerController implements TransformControll
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
reply.setStatus(500);
|
||||
reply.setStatus(INTERNAL_SERVER_ERROR.value());
|
||||
reply.setErrorDetails("Failed at processing transformation. " + e.getMessage());
|
||||
|
||||
return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()));
|
||||
@@ -205,7 +206,7 @@ public abstract class AbstractTransformerController implements TransformControll
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
reply.setStatus(500);
|
||||
reply.setStatus(INTERNAL_SERVER_ERROR.value());
|
||||
reply.setErrorDetails("Failed at writing the transformed file. " + e.getMessage());
|
||||
|
||||
return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()));
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package org.alfresco.transformer;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
@@ -92,7 +94,7 @@ public interface TransformController
|
||||
String transformerName = getTransformerName();
|
||||
String name = e.getParameterName();
|
||||
String message = "Request parameter " + name + " is of the wrong type";
|
||||
int statusCode = 400;
|
||||
int statusCode = BAD_REQUEST.value();
|
||||
|
||||
logger.error(message);
|
||||
|
||||
@@ -108,7 +110,7 @@ public interface TransformController
|
||||
String transformerName = getTransformerName();
|
||||
String name = e.getParameterName();
|
||||
String message = "Request parameter " + name + " is missing";
|
||||
int statusCode = 400;
|
||||
int statusCode = BAD_REQUEST.value();
|
||||
|
||||
logger.error(message);
|
||||
|
||||
|
@@ -1,5 +1,8 @@
|
||||
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.util.Map;
|
||||
|
||||
@@ -34,12 +37,14 @@ public abstract class AbstractCommandExecutor implements CommandExecutor
|
||||
|
||||
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)
|
||||
{
|
||||
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();
|
||||
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();
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,9 @@
|
||||
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.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
@@ -64,7 +68,7 @@ public class FileManager
|
||||
if (filename == null || filename.isEmpty())
|
||||
{
|
||||
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");
|
||||
}
|
||||
return filename;
|
||||
@@ -78,7 +82,8 @@ public class FileManager
|
||||
}
|
||||
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)
|
||||
{
|
||||
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
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -26,6 +26,7 @@
|
||||
package org.alfresco.transformer.logging;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Collection;
|
||||
@@ -196,7 +197,7 @@ public final class LogEntry
|
||||
public static void complete()
|
||||
{
|
||||
LogEntry logEntry = currentLogEntry.get();
|
||||
if (logEntry.statusCode == 200)
|
||||
if (logEntry.statusCode == OK.value())
|
||||
{
|
||||
logEntry.durationStreamOut = System.currentTimeMillis() - logEntry.start -
|
||||
logEntry.durationStreamIn - max(logEntry.durationTransform, 0) - max(logEntry.durationDelay, 0);
|
||||
|
@@ -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.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.IOException;
|
||||
@@ -187,7 +191,7 @@ public abstract class ProbeTestTransform
|
||||
{
|
||||
String probeMessage = getProbeMessage(isLiveProbe);
|
||||
String message = "Success - No transform.";
|
||||
LogEntry.setStatusCodeAndMessage(200, probeMessage + message);
|
||||
LogEntry.setStatusCodeAndMessage(OK.value(), probeMessage + message);
|
||||
if (!isLiveProbe && !readySent.getAndSet(true))
|
||||
{
|
||||
logger.info(probeMessage+message);
|
||||
@@ -223,9 +227,10 @@ public abstract class ProbeTestTransform
|
||||
|
||||
if (time > maxTime)
|
||||
{
|
||||
throw new TransformException(500, getMessagePrefix(isLiveProbe) +
|
||||
message + " which is more than " + livenessPercent +
|
||||
"% slower than the normal value of " + normalTime + "ms");
|
||||
throw new TransformException(INTERNAL_SERVER_ERROR.value(),
|
||||
getMessagePrefix(isLiveProbe) +
|
||||
message + " which is more than " + livenessPercent +
|
||||
"% slower than the normal value of " + normalTime + "ms");
|
||||
}
|
||||
|
||||
// We don't care if the ready or live probe works out if we are 'ready' to take requests.
|
||||
@@ -240,14 +245,14 @@ public abstract class ProbeTestTransform
|
||||
{
|
||||
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 "+
|
||||
(maxTransformTime *1000)+" seconds");
|
||||
}
|
||||
|
||||
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 "+
|
||||
maxTransformCount+" transformations");
|
||||
}
|
||||
@@ -264,7 +269,7 @@ public abstract class ProbeTestTransform
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new TransformException(507, getMessagePrefix(isLiveProbe)+
|
||||
throw new TransformException(INSUFFICIENT_STORAGE.value(), getMessagePrefix(isLiveProbe)+
|
||||
"Failed to store the source file", e);
|
||||
}
|
||||
long length = sourceFile.length();
|
||||
@@ -320,17 +325,19 @@ public abstract class ProbeTestTransform
|
||||
String probeMessage = getProbeMessage(isLiveProbe);
|
||||
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();
|
||||
if (length < minExpectedLength || length > maxExpectedLength)
|
||||
{
|
||||
throw new TransformException(500, probeMessage +"Target File \""+targetFile.getAbsolutePath()+
|
||||
"\" was the wrong size ("+ length+"). Needed to be between "+minExpectedLength+" and "+
|
||||
maxExpectedLength);
|
||||
throw new TransformException(INTERNAL_SERVER_ERROR.value(),
|
||||
probeMessage + "Target File \"" + targetFile.getAbsolutePath() +
|
||||
"\" was the wrong size (" + length + "). Needed to be between " +
|
||||
minExpectedLength + " and " + maxExpectedLength);
|
||||
}
|
||||
LogEntry.setTargetSize(length);
|
||||
LogEntry.setStatusCodeAndMessage(200, probeMessage +"Success - "+message);
|
||||
LogEntry.setStatusCodeAndMessage(OK.value(), probeMessage + "Success - " + message);
|
||||
}
|
||||
|
||||
private String getMessagePrefix(boolean isLiveProbe)
|
||||
|
@@ -28,6 +28,9 @@ package org.alfresco.transformer;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
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.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
@@ -158,7 +161,7 @@ public abstract class AbstractTransformerControllerTest
|
||||
public void simpleTransformTest() throws Exception
|
||||
{
|
||||
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
|
||||
.andExpect(status().is(200))
|
||||
.andExpect(status().is(OK.value()))
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
|
||||
}
|
||||
@@ -168,7 +171,7 @@ public abstract class AbstractTransformerControllerTest
|
||||
{
|
||||
long start = System.currentTimeMillis();
|
||||
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension, "testDelay", "400"))
|
||||
.andExpect(status().is(200))
|
||||
.andExpect(status().is(OK.value()))
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
|
||||
long ms = System.currentTimeMillis() - start;
|
||||
@@ -181,7 +184,7 @@ public abstract class AbstractTransformerControllerTest
|
||||
public void noTargetFileTest() throws Exception
|
||||
{
|
||||
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", "xxx"))
|
||||
.andExpect(status().is(500));
|
||||
.andExpect(status().is(INTERNAL_SERVER_ERROR.value()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -191,7 +194,7 @@ public abstract class AbstractTransformerControllerTest
|
||||
sourceFile = new MockMultipartFile("file", "../quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes);
|
||||
|
||||
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
|
||||
.andExpect(status().is(200))
|
||||
.andExpect(status().is(OK.value()))
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.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);
|
||||
|
||||
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
|
||||
.andExpect(status().is(200))
|
||||
.andExpect(status().is(OK.value()))
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.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);
|
||||
|
||||
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")));
|
||||
}
|
||||
|
||||
@@ -225,7 +228,7 @@ public abstract class AbstractTransformerControllerTest
|
||||
sourceFile = new MockMultipartFile("file", "", sourceMimetype, expectedSourceFileBytes);
|
||||
|
||||
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")));
|
||||
}
|
||||
|
||||
@@ -233,7 +236,7 @@ public abstract class AbstractTransformerControllerTest
|
||||
public void noTargetExtensionTest() throws Exception
|
||||
{
|
||||
mockMvc.perform(mockMvcRequest("/transform", sourceFile))
|
||||
.andExpect(status().is(400))
|
||||
.andExpect(status().is(BAD_REQUEST.value()))
|
||||
.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.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||
.content(tr))
|
||||
.andExpect(status().is(HttpStatus.BAD_REQUEST.value()))
|
||||
.andExpect(status().is(BAD_REQUEST.value()))
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
|
||||
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, TransformReply.class);
|
||||
|
||||
// Assert the reply
|
||||
assertEquals(HttpStatus.BAD_REQUEST.value(), transformReply.getStatus());
|
||||
assertEquals(BAD_REQUEST.value(), transformReply.getStatus());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user