Commit 7da56fb8 authored by Javi Corvi's avatar Javi Corvi
Browse files

upload files validations

parent 9fe09369
Pipeline #27717 passed with stage
in 5 minutes and 37 seconds
......@@ -42,14 +42,15 @@ public class FileController {
fileNames.add(file.getOriginalFilename());
});
}else {
errors.add("Validation fail, no document was uploaded.");
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(errors);
}
List<String> messages = new ArrayList<String>();
messages.add("Files where upload correctly");
messages.add("The documents with their annotations were uploaded correctly.");
return ResponseEntity.status(HttpStatus.OK).body(messages);
}catch (Exception e){
List<String> messages = new ArrayList<String>();
messages.add("An error occurr when uploading files");
messages.add("An error occurred while uploading documents.");
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(messages);
}
}
......
......@@ -8,56 +8,28 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import ch.qos.logback.core.net.LoginAuthenticator;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
// httpSecurity
// .authorizeRequests().antMatchers("/**")
// .authenticated()
// .and()
// .sessionManagement()
// .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
// .and()
// .cors()
// .and()
// .csrf()
// .disable()
// .oauth2ResourceServer()
// .jwt();
httpSecurity.headers().frameOptions().sameOrigin();
httpSecurity
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/liveness").permitAll()
.antMatchers(HttpMethod.GET, "/readiness").permitAll()
.antMatchers(HttpMethod.POST, "/upload").permitAll()
.anyRequest().authenticated()
.anyRequest().authenticated()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
//.formLogin()
//.and()
.cors()
.and()
.csrf()
.disable()
.oauth2ResourceServer()
.jwt();
//
// httpSecurity
// .authorizeRequests(authz -> authz
// .antMatchers(HttpMethod.GET, "/**").hasAuthority("SCOPE_read")
// .antMatchers(HttpMethod.POST, "/**").hasAuthority("SCOPE_write")
// .anyRequest().authenticated())
// .oauth2ResourceServer(oauth2 -> oauth2.jwt());
}
}
......@@ -16,6 +16,8 @@ public interface DocumentService {
Document findByDocumentId2(Long id);
Document findByDocumentName(String name);
//String findTextSnippetByDocumentIdAndFindingId2(Long id, Integer findingId);
String findFindingEvidenceByDocumentIdAndFindingId(Long id, Integer findingId);
......
......@@ -87,24 +87,9 @@ public class DocumentServiceImpl implements DocumentService {
return documentRepository.findByDocumentId(id);
}
// @Override
// public String findTextSnippetByDocumentIdAndFindingId2(Long id, Integer findingId) {
// DocumentAnnotations documentAnnotations = this.findDocumentAnnotationsByDocumentId(id);
// Document document = this.findByDocumentId(id);
// Finding findingSelected = null;
// for (Finding finding : documentAnnotations.getFindings()) {
// if(finding.getFindingId().equals(findingId)) {
// findingSelected = finding;
// break;
// }
// }
//
// if(findingSelected!=null) {
// return this.generateFindingSnippet2(document.getText(), findingSelected);
// }
// return "";
// }
public Document findByDocumentName(String name) {
return documentRepository.findByName(name);
}
@Override
public Boolean setFindingValidation(Long id, Integer findingId, String status) {
......
......@@ -39,6 +39,10 @@ public interface FileService {
Metodo para Borrar un archivo
*/
public String deleteFile(String filename);
/**
*
* @param files
* @return
*/
public List<String> validate(MultipartFile[] files);
}
......@@ -40,6 +40,9 @@ public class FileServiceImp implements FileService {
@Autowired
MongoTemplate mongoTemplate;
@Autowired
DocumentService documentService;
@Override
public void init() {
try {
......@@ -74,8 +77,33 @@ public class FileServiceImp implements FileService {
e.printStackTrace();
}
}
/**
* return if document id exist in database.
* @param doc
*/
private Boolean documentIdExist(Document doc) {
Long documentId = doc.getLong("id");
es.bsc.inb.ades.rest.api.model.Document document = documentService.findByDocumentId(documentId);
if(document!=null) {
return true;
}
return false;
}
@Override
/**
* Return if document name already exist in database.
* @param doc
*/
private Boolean documentNameExist(Document doc) {
String name = doc.getString("name");
es.bsc.inb.ades.rest.api.model.Document document = documentService.findByDocumentName(name);
if(document!=null) {
return true;
}
return false;
}
@Override
public Resource load(String filename) {
try {
Path file = root.resolve(filename);
......@@ -128,6 +156,7 @@ public class FileServiceImp implements FileService {
Map<String, MultipartFile[]> map = new HashMap<String, MultipartFile[]>();
List<String> annotations_files = new ArrayList<String>();
List<String> documents_files = new ArrayList<String>();
List<MultipartFile> documents_files_multipart = new ArrayList<MultipartFile>();
for (MultipartFile multipartFile : files) {
if(multipartFile.getOriginalFilename().endsWith("_annotations.json")) {
//annotations
......@@ -137,23 +166,79 @@ public class FileServiceImp implements FileService {
//documents
String file = multipartFile.getOriginalFilename().subSequence(0, multipartFile.getOriginalFilename().indexOf("_documents.json")).toString();
documents_files.add(file);
documents_files_multipart.add(multipartFile);
}else {
errors.add("The following file is not a valid format, it must be an 'xxxx_annotations.json' or 'xxxx_documents.json' format: " + multipartFile.getOriginalFilename());
errors.add("The following file is not a valid format, it must be an 'xxxx_annotations.json' or 'xxxx_documents.json' format: " + multipartFile.getOriginalFilename() + ".");
}
}
List<String> differences_1 = new ArrayList<>((CollectionUtils.removeAll(annotations_files, documents_files)));
for (String string : differences_1) {
errors.add("The following document is incomplete : " + string + ", please review that both files : "+ string +"_annotations.json and " + string + "_documents.json where included");
errors.add("The following document is incomplete : " + string + ", please review that both files : "+ string +"_annotations.json and " + string + "_documents.json where included.");
}
List<String> differences_2 = new ArrayList<>((CollectionUtils.removeAll(documents_files,annotations_files)));
for (String string : differences_2) {
errors.add("The following document is incomplete : " + string + ", please review that both files : "+ string +"_annotations.json and " + string + "_documents.json where included");
errors.add("The following document is incomplete : " + string + ", please review that both files : "+ string +"_annotations.json and " + string + "_documents.json where included.");
}
errors.addAll(validateDocumentsInDatabase(documents_files_multipart));
Boolean upload_again = false;
if(!upload_again) {
//solo se esta validando los _documents.json habria que tambien tener en cuenta los annotations.
errors.addAll(validateDocumentsNamesInDatabase(documents_files_multipart));
}
return errors;
}
/**
* Validate if there the document is already in the database
* @param files
* @return
*/
private List<String> validateDocumentsInDatabase(List<MultipartFile> files) {
List<String> errors = new ArrayList<String>();
for (MultipartFile multipartFile : files) {
try {
ByteArrayInputStream stream = new ByteArrayInputStream(multipartFile.getBytes());
String jsonString = IOUtils.toString(stream, "UTF-8");
Document doc = Document.parse(jsonString);
if(this.documentIdExist(doc)) {
String name = multipartFile.getOriginalFilename().subSequence(0, multipartFile.getOriginalFilename().indexOf("_documents.json")).toString();
errors.add("The document " + name + " has a documentId ("+doc.getLong("id")+") that was already processed and is present in the database. A document with the same documentId cannot be processed again.");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return errors;
}
/**
* Validate if there the is a document with the same name in the database
* @param files
* @return
*/
private List<String> validateDocumentsNamesInDatabase(List<MultipartFile> files) {
List<String> errors = new ArrayList<String>();
for (MultipartFile multipartFile : files) {
try {
ByteArrayInputStream stream = new ByteArrayInputStream(multipartFile.getBytes());
String jsonString = IOUtils.toString(stream, "UTF-8");
Document doc = Document.parse(jsonString);
if(this.documentNameExist(doc)) {
String name = multipartFile.getOriginalFilename().subSequence(0, multipartFile.getOriginalFilename().indexOf("_documents.json")).toString();
errors.add("The document with the name: " + name + " was already processed and is present in the database. If you want to process again please select the option upload again. You will have two documents with the same name, you can difference them throught the process date. You shoud use this option if you want to upload the same document with different pipelines executions." );
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return errors;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment