mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-06-30 18:14:51 +00:00
ATS-68 : ATS-16: Fix error status code mapping for expected invalid requests
- validated the transformRequest on '/transform' endpoint
This commit is contained in:
parent
96a090a862
commit
18f055bf46
@ -39,12 +39,14 @@ import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.transform.client.model.TransformReply;
|
||||
import org.alfresco.transform.client.model.TransformRequest;
|
||||
import org.alfresco.transform.client.model.TransformRequestValidator;
|
||||
import org.alfresco.transformer.model.FileRefResponse;
|
||||
import org.alfresco.util.TempFileProvider;
|
||||
import org.alfresco.util.exec.RuntimeExec;
|
||||
@ -59,6 +61,8 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.DirectFieldBindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@ -108,6 +112,9 @@ public abstract class AbstractTransformerController
|
||||
@Autowired
|
||||
private AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient;
|
||||
|
||||
@Autowired
|
||||
private TransformRequestValidator transformRequestValidator;
|
||||
|
||||
protected static Log logger;
|
||||
|
||||
protected RuntimeExec transformCommand;
|
||||
@ -158,6 +165,17 @@ public abstract class AbstractTransformerController
|
||||
transformReply.setSchema(transformRequest.getSchema());
|
||||
transformReply.setClientData(transformRequest.getClientData());
|
||||
|
||||
Errors errors = validateTransformRequest(transformRequest);
|
||||
if (!errors.getAllErrors().isEmpty())
|
||||
{
|
||||
transformReply.setStatus(HttpStatus.BAD_REQUEST.value());
|
||||
transformReply.setErrorDetails(errors.getAllErrors().stream().map(Object::toString)
|
||||
.collect(Collectors.joining(", ")));
|
||||
|
||||
return new ResponseEntity<>(transformReply,
|
||||
HttpStatus.valueOf(transformReply.getStatus()));
|
||||
}
|
||||
|
||||
// Load the source file
|
||||
File sourceFile;
|
||||
try
|
||||
@ -253,6 +271,13 @@ public abstract class AbstractTransformerController
|
||||
return new ResponseEntity<>(transformReply, HttpStatus.valueOf(transformReply.getStatus()));
|
||||
}
|
||||
|
||||
private Errors validateTransformRequest(TransformRequest transformRequest)
|
||||
{
|
||||
DirectFieldBindingResult errors = new DirectFieldBindingResult(transformRequest, "request");
|
||||
transformRequestValidator.validate(transformRequest, errors);
|
||||
return errors;
|
||||
}
|
||||
|
||||
protected abstract void processTransform(File sourceFile, File targetFile,
|
||||
Map<String, String> transformOptions, Long timeout);
|
||||
|
||||
|
@ -30,13 +30,14 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.alfresco.transform.client.model.TransformRequestValidator;
|
||||
|
||||
@Configuration
|
||||
public class WebApplicationConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(transformInterceptor()).addPathPatterns("/transform", "/live", "/ready");;
|
||||
registry.addInterceptor(transformInterceptor()).addPathPatterns("/transform", "/live", "/ready");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ -55,4 +56,10 @@ public class WebApplicationConfig extends WebMvcConfigurerAdapter {
|
||||
public AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient(){
|
||||
return new AlfrescoSharedFileStoreClient();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TransformRequestValidator transformRequestValidator()
|
||||
{
|
||||
return new TransformRequestValidator();
|
||||
}
|
||||
}
|
||||
|
2
pom.xml
2
pom.xml
@ -24,7 +24,7 @@
|
||||
<dependency.alfresco-jodconverter-core.version>3.0.1.1</dependency.alfresco-jodconverter-core.version>
|
||||
<dependency.ch-qos-logback.version>1.2.3</dependency.ch-qos-logback.version>
|
||||
<env.project_version>${project.version}</env.project_version>
|
||||
<alfresco-transform-data-model.version>0.0.4</alfresco-transform-data-model.version>
|
||||
<alfresco-transform-data-model.version>0.0.5-SNAPSHOT</alfresco-transform-data-model.version>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
|
Loading…
x
Reference in New Issue
Block a user