File registration request validation

This commit is contained in:
Fabio Sinibaldi 2021-10-06 14:34:07 +02:00
parent c81861c3e5
commit fe3239a1bc
7 changed files with 61 additions and 4 deletions

View File

@ -150,6 +150,7 @@ public class DefaultMongoConcessioni implements MongoConcessioni{
@Override @Override
public Concessione registerFileSet(String id, AddSectionToConcessioneRequest request) throws Exception { public Concessione registerFileSet(String id, AddSectionToConcessioneRequest request) throws Exception {
log.info("Registering {} in {}",request,id); log.info("Registering {} in {}",request,id);
request.validate();
Call<WebTarget,Concessione> call= endpoint -> { Call<WebTarget,Concessione> call= endpoint -> {
Response resp= endpoint.path(InterfaceConstants.Methods.REGISTER_FILES_PATH). Response resp= endpoint.path(InterfaceConstants.Methods.REGISTER_FILES_PATH).
path(id).request(MediaType.APPLICATION_JSON).post(Entity.entity(mapper.writeValueAsString(request), path(id).request(MediaType.APPLICATION_JSON).post(Entity.entity(mapper.writeValueAsString(request),

View File

@ -0,0 +1,22 @@
package org.gcube.application.geoportal.common.faults;
public class InvalidRequestException extends Exception{
public InvalidRequestException() {
}
public InvalidRequestException(String message) {
super(message);
}
public InvalidRequestException(String message, Throwable cause) {
super(message, cause);
}
public InvalidRequestException(Throwable cause) {
super(cause);
}
public InvalidRequestException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -7,6 +7,7 @@ import java.util.List;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.application.geoportal.common.faults.InvalidRequestException;
import org.gcube.application.geoportal.common.model.legacy.report.Check; import org.gcube.application.geoportal.common.model.legacy.report.Check;
import org.gcube.application.geoportal.common.model.legacy.report.ConstraintCheck; import org.gcube.application.geoportal.common.model.legacy.report.ConstraintCheck;
import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport; import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport;
@ -107,6 +108,23 @@ public class Concessione extends Record{
private static String makeByIndex(String base,int value) {return String.format("%1$s[%2$d]",base,value);} private static String makeByIndex(String base,int value) {return String.format("%1$s[%2$d]",base,value);}
private static String makeByStringField(String base,String field,String value) private static String makeByStringField(String base,String field,String value)
{return String.format("%1$s.%2$s:\"%3$s\"",base,field,value);} {return String.format("%1$s.%2$s:\"%3$s\"",base,field,value);}
public static boolean validate(String path) throws InvalidRequestException {
switch(path) {
case Paths.RELAZIONE :
case Paths.POSIZIONAMENTO :
case Paths.ABSTRACT_RELAZIONE: return true;
}
if(path.matches("\\w+\\[\\d+\\]")) {
// Array section
return true;
}
if(path.matches("\\w+\\.\\w+\\s*\\:\\s*\\\"\\w+\\\"")) {
// Map section
return true;
}
throw new InvalidRequestException("Invalid Path "+path);
}
} }
@Override @Override

View File

@ -7,6 +7,8 @@ import javax.xml.bind.annotation.XmlRootElement;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.gcube.application.geoportal.common.faults.InvalidRequestException;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.rest.TempFile; import org.gcube.application.geoportal.common.rest.TempFile;
@XmlRootElement @XmlRootElement
@ -17,5 +19,11 @@ public class AddSectionToConcessioneRequest {
private String destinationPath; private String destinationPath;
private List<TempFile> streams; private List<TempFile> streams;
public void validate()throws InvalidRequestException {
Concessione.Paths.validate(destinationPath);
if(streams==null || streams.isEmpty()) throw new InvalidRequestException("No Temp File declared");
for(TempFile t : streams) t.validate();
}
} }

View File

@ -3,6 +3,7 @@ package org.gcube.application.geoportal.common.rest;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.gcube.application.geoportal.common.faults.InvalidRequestException;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ -11,5 +12,9 @@ public class TempFile {
private String id; private String id;
private String filename; private String filename;
public void validate()throws InvalidRequestException {
if(id==null || id.isEmpty()) throw new InvalidRequestException("Invalid temp file "+this+" : ID null or empty");
if(filename==null || filename.isEmpty()) throw new InvalidRequestException("Invalid temp file "+this+" : filename null or empty");
}
} }

View File

@ -81,8 +81,6 @@ public class ConcessioniOverMongo {
}; };
}.execute().getResult(); }.execute().getResult();
} }
@ -177,6 +175,7 @@ public class ConcessioniOverMongo {
log.info("Registering {} file(s) for {} Concessione ID {}", log.info("Registering {} file(s) for {} Concessione ID {}",
request.getStreams().size(), request.getStreams().size(),
request.getDestinationPath(),id); request.getDestinationPath(),id);
request.validate();
ConcessioniMongoManager manager=new ConcessioniMongoManager(); ConcessioniMongoManager manager=new ConcessioniMongoManager();
return manager.persistContent(id, request.getDestinationPath(), request.getStreams()); return manager.persistContent(id, request.getDestinationPath(), request.getStreams());
} }

View File

@ -1,5 +1,6 @@
package org.gcube.application.geoportal.service.engine.caches; package org.gcube.application.geoportal.service.engine.caches;
import com.mongodb.MongoWaitQueueFullException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.cms.tests.TokenSetter;
import org.gcube.application.geoportal.service.BasicServiceTestUnit; import org.gcube.application.geoportal.service.BasicServiceTestUnit;
@ -79,8 +80,11 @@ public class Caches extends BasicServiceTestUnit {
new ConcessioniMongoManager().list(); new ConcessioniMongoManager().list();
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
} catch (MongoWaitQueueFullException e) {
log.info("Too many connections... ");
}finally{ }finally{
executed.incrementAndGet(); executed.incrementAndGet();
try {Thread.sleep(500);} catch (InterruptedException i) {}
} }
} }
}); });