csip.PayloadFormData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of csip-core Show documentation
Show all versions of csip-core Show documentation
The Cloud Services Integration Platform is a SoA implementation to offer a Model-as-a-Service framework, Application Programming Interface, deployment infrastructure, and service implementations for environmental modeling.
/*
* $Id: PayloadFormData.java c4b9d8c126c7 2020-07-28 [email protected] $
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2019, Olaf David and others, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package csip;
import csip.utils.Services.FormDataParameter;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
/**
* PayloadFormData input
*
* @author od
*/
public class PayloadFormData {
private final Map formData;
private final ModelDataService mds;
PayloadFormData(Map formData, ModelDataService mds) {
this.formData = formData;
this.mds = mds;
}
/**
* Check if the key exist.
*
* @param key The name key
* @return if payload form data is not null and contains key
*/
public boolean hasKey(String key) {
return formData != null && formData.containsKey(key);
}
/**
* Check if Value exists.
*
* @param value value
* @return if payload form data is not null and contains value
*/
public boolean hasValue(String value) {
return formData != null && formData.containsValue(value);
}
/**
* Check is a required key exists.
*
* @param name the key name
* @return this object
* @throws ServiceException if the parameter is not found.
*/
public PayloadFormData require(String name) throws ServiceException {
if (!hasKey(name)) {
throw new ServiceException("Form data key not found :" + name);
}
return this;
}
/**
* Check is a required key exists.
*
* @param name the key name
* @return this object
* @throws ServiceException if the parameter is not found.
*/
public PayloadFormData requireValue(String name) throws ServiceException {
if (!hasValue(name)) {
throw new ServiceException("Form data value not found :" + name);
}
return this;
}
/**
* Get the file input by form name.
*
* @param key the form key name
* @return the file in the workspace
* @throws ServiceException if file not found
*/
public File getFile(String key) throws ServiceException {
if (hasKey(key)) {
FormDataParameter p = formData.get(key);
if (p.isFile()) {
File f = mds.getWorkspaceFile(p.getFilename());
if (f.exists() && f.canRead()) {
return f;
}
}
}
throw new ServiceException("File not found or cannot access: " + key);
}
/**
* Get the value of a given form name
*
* @param key the form key name
* @return the String value
* @throws ServiceException if key does not exist or is not a file
*/
public String getString(String key) throws ServiceException {
if (formData != null && formData.containsKey(key)) {
FormDataParameter p = formData.get(key);
if (!p.isFile()) {
return p.getValue();
}
}
throw new ServiceException("Form input not found: " + key);
}
/**
* Get all form names.
* @return the from names.
*/
public Collection getKeys() {
return formData == null ? Collections.emptySet()
: Collections.unmodifiableCollection(formData.keySet());
}
/**
* Check if there is form data
* @return true is there is
*/
public boolean hasFormData() {
return formData != null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy