2022-03-11 18:11:32 +01:00
|
|
|
package org.gcube.application.cms.plugins.requests;
|
|
|
|
|
2022-05-31 16:10:41 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2022-03-17 17:54:00 +01:00
|
|
|
import com.fasterxml.jackson.databind.ser.Serializers;
|
2022-03-11 18:11:32 +01:00
|
|
|
import lombok.*;
|
|
|
|
import org.bson.Document;
|
|
|
|
import org.gcube.application.cms.plugins.faults.InvalidPluginRequestException;
|
2022-05-31 16:10:41 +02:00
|
|
|
import org.gcube.application.cms.serialization.Serialization;
|
2022-05-31 13:38:12 +02:00
|
|
|
import org.gcube.application.geoportal.common.faults.InvalidRequestException;
|
2022-03-11 18:11:32 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.document.accounting.Context;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.accounting.User;
|
|
|
|
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
@ToString(callSuper = true)
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
public class BaseRequest {
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
UseCaseDescriptor useCaseDescriptor;
|
|
|
|
@NonNull
|
|
|
|
User caller;
|
|
|
|
@NonNull
|
|
|
|
Context context;
|
|
|
|
|
|
|
|
|
|
|
|
Document callParameters;
|
|
|
|
|
|
|
|
public final String getMandatory(String param) throws InvalidPluginRequestException {
|
|
|
|
return getMandatory(param,callParameters);
|
|
|
|
}
|
|
|
|
|
2022-05-31 13:38:12 +02:00
|
|
|
public final String getWithDefault(String param,String defaultValue) {
|
|
|
|
try{
|
|
|
|
return getMandatory(param,callParameters);
|
|
|
|
}catch (InvalidPluginRequestException e){
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 18:11:32 +01:00
|
|
|
public static final String getMandatory(String param,Document params) throws InvalidPluginRequestException {
|
2022-05-31 15:14:23 +02:00
|
|
|
if(params==null || params.isEmpty()|| !params.containsKey(param)) throw new InvalidPluginRequestException("Missing mandatory parameter "+param);
|
2022-05-31 16:10:41 +02:00
|
|
|
try {
|
|
|
|
return Serialization.write(params.get(param));
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
throw new InvalidPluginRequestException(e);
|
|
|
|
}
|
2022-03-11 18:11:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:54:00 +01:00
|
|
|
public BaseRequest validate() throws InvalidPluginRequestException {
|
2022-03-11 18:11:32 +01:00
|
|
|
if(useCaseDescriptor ==null)throw new InvalidPluginRequestException("UseCaseDescriptor cannot be null ");
|
2022-03-17 17:54:00 +01:00
|
|
|
return this;
|
2022-03-11 18:11:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:54:00 +01:00
|
|
|
public BaseRequest setParameter(String key,Object value){
|
|
|
|
if(callParameters==null) callParameters=new Document();
|
|
|
|
callParameters.put(key, value);
|
|
|
|
return this;
|
|
|
|
}
|
2022-03-11 18:11:32 +01:00
|
|
|
}
|