
prerna.reactor.utils.PostRequestReactor Maven / Gradle / Ivy
The newest version!
package prerna.reactor.utils;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import prerna.project.impl.ProjectHeaderAuthEvaluator;
import prerna.reactor.AbstractReactor;
import prerna.sablecc2.om.GenRowStruct;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.ReactorKeysEnum;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.security.HttpHelperUtility;
import prerna.util.Constants;
import prerna.util.DIHelper;
import prerna.util.Utility;
public class PostRequestReactor extends AbstractReactor {
private static final Logger classLogger = LogManager.getLogger(PostRequestReactor.class);
public PostRequestReactor() {
this.keysToGet = new String[]{ReactorKeysEnum.URL.getKey(), ReactorKeysEnum.HEADERS_MAP.getKey(), "bodyMap",
ReactorKeysEnum.USE_APPLICATION_CERT.getKey()};
}
@Override
public NounMetadata execute() {
organizeKeys();
String url = this.keyValue.get(this.keysToGet[0]);
Utility.checkIfValidDomain(url);
Map headersMap = getHeadersMap();
Map bodyMap = getBody();
String keyStore = null;
String keyStorePass = null;
String keyPass = null;
boolean useApplicationCert = Boolean.parseBoolean(this.keyValue.get(this.keysToGet[3]) + "");
if(useApplicationCert) {
keyStore = DIHelper.getInstance().getProperty(Constants.SCHEDULER_KEYSTORE);
keyStorePass = DIHelper.getInstance().getProperty(Constants.SCHEDULER_KEYSTORE_PASSWORD);
keyPass = DIHelper.getInstance().getProperty(Constants.SCHEDULER_CERTIFICATE_PASSWORD);
}
return new NounMetadata(HttpHelperUtility.postRequestUrlEncodedBody(url, headersMap, bodyMap, keyStore, keyStorePass, keyPass), PixelDataType.CONST_STRING);
}
/**
* Get headers to add to the request
* @return
*/
private Map getHeadersMap() {
GenRowStruct headersGrs = this.store.getNoun(this.keysToGet[1]);
if(headersGrs != null && !headersGrs.isEmpty()) {
Map headers = new HashMap<>();
for(int i = 0; i < headersGrs.size(); i++) {
NounMetadata noun = headersGrs.getNoun(i);
if(noun.getNounType() == PixelDataType.PROJECT_AUTHORIZATION_HEADER) {
try {
headers.putAll( ((ProjectHeaderAuthEvaluator) noun.getValue()).eval() );
} catch (UnsupportedEncodingException e) {
classLogger.error(Constants.STACKTRACE, e);
throw new IllegalArgumentException("An error occurred trying to get the project authorization headers");
}
} else {
headers.putAll( (Map) noun.getValue() );
}
}
return headers;
}
return null;
}
/**
* Get headers to add to the request
* @return
*/
private Map getBody() {
GenRowStruct bodyGrs = this.store.getNoun(this.keysToGet[2]);
if(bodyGrs != null && !bodyGrs.isEmpty()) {
Map body = new HashMap<>();
for(int i = 0; i < bodyGrs.size(); i++) {
body.putAll( (Map) bodyGrs.get(i));
}
return body;
}
return null;
}
@Override
protected String getDescriptionForKey(String key) {
if(key.equals("headersMap")) {
return "Map containing key-value pairs to send in the POST request";
} else if(key.equals("bodyMap")) {
return "Map containing key-value pairs to send in the body of the POST request";
}
return super.getDescriptionForKey(key);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy