perform-service_broken/src/main/java/org/gcube/application/perform/service/rest/InterfaceCommons.java

31 lines
1.1 KiB
Java

package org.gcube.application.perform.service.rest;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
public class InterfaceCommons {
public static final void checkMandatory(Object toCheck, String name) throws WebApplicationException{
if(toCheck==null)
throw new WebApplicationException(String.format("Parameter %1$s is mandatory",name),Response.Status.BAD_REQUEST);
}
public static final List<String> getParameter(MultivaluedMap<String,String> map,String paramName, boolean mandatory){
if(map.containsKey(paramName)) {
return map.get(paramName);
}else if(mandatory) throw new WebApplicationException(String.format("Parameter %1$s is mandatory",paramName),Response.Status.BAD_REQUEST);
return new ArrayList<String>();
}
public static final String getParamOrDefault(MultivaluedMap<String,String> map,String paramName, String defaultValue) {
List<String> found=getParameter(map,paramName,false);
if(found.isEmpty()) return defaultValue;
else return found.get(0);
}
}