com.novartis.opensource.yada.Service Maven / Gradle / Ivy
/**
* Copyright 2016 Novartis Institutes for BioMedical Research Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.novartis.opensource.yada;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.novartis.opensource.yada.adaptor.YADAAdaptorException;
import com.novartis.opensource.yada.adaptor.YADAAdaptorExecutionException;
import com.novartis.opensource.yada.format.DelimitedResponse;
import com.novartis.opensource.yada.format.Response;
import com.novartis.opensource.yada.format.YADAConverterException;
import com.novartis.opensource.yada.format.YADAResponseException;
import com.novartis.opensource.yada.io.YADAIOException;
import com.novartis.opensource.yada.plugin.Bypass;
import com.novartis.opensource.yada.plugin.Postprocess;
import com.novartis.opensource.yada.plugin.Preprocess;
import com.novartis.opensource.yada.plugin.YADAPluginException;
import com.novartis.opensource.yada.util.FileUtils;
import com.novartis.opensource.yada.util.QueryUtils;
/**
* Utility class handling process of execution of stored queries, and formatting of results via http requests.
*
* @author David Varon
*
*/
public class Service {
/**
* Local logger handle
*/
private static Logger l = Logger.getLogger(Service.class);
/**
* Constant equal to: {@value}
*/
private final static String FORMAT_PKG = "com.novartis.opensource.yada.format.";
/**
* Request configuration
*/
private YADARequest yadaReq = new YADARequest();
/**
* Request process manager
*/
private QueryManager qMgr = null;
/**
* Container for result objects
*/
private YADAQueryResult[] queryResults;
/**
* Utility object
*/
private QueryUtils qutils = new QueryUtils();
/**
* Ivar to store ref query currently executing query. Used primarily to pass info to {@link #error(String, Exception)}
*/
private YADAQuery currentQuery = null;
/**
* Default constructor. Usually unused.
*/
public Service() {}
/**
* Commonly used "service params" constructor.
* @param yadaReq YADA request configuration
*/
public Service(YADARequest yadaReq) {
this.yadaReq = yadaReq;
}
/**
* Called by {@code yada.jsp} when using path-style parameters, this method parses the url path into a map,
* stores the request, and then calls {@link #handleRequest(String, Map)}
* @param request the servlet request object handed off from the servlet container
* @param parameterString the resource path from the url
* @throws YADARequestException when there is an issue setting parameters in the configuration
*/
public void handleRequest(HttpServletRequest request, String parameterString)
{
Map map = new LinkedHashMap<>();
String[] pathElements = parameterString.split("/");
/*String[] split = parameterString.split("&");
for (String pair : split)
{
String[] subsplit = pair.split("=");
map.put(subsplit[0], new String[] {subsplit[1]});
}
*/
for(int i=0;i paraMap)
{
try
{
if(paraMap.get(YADARequest.PL_ARGS) != null)
{
this.yadaReq.setArgs(paraMap.get(YADARequest.PL_ARGS));
}
if(paraMap.get(YADARequest.PS_ARGS) != null)
{
this.yadaReq.setArgs(paraMap.get(YADARequest.PS_ARGS));
}
if (paraMap.get(YADARequest.PL_COLHEAD) != null)
{
this.yadaReq.setColhead(paraMap.get(YADARequest.PL_COLHEAD));
}
if (paraMap.get(YADARequest.PL_COMMITQUERY) != null)
{
this.yadaReq.setCommitQuery(paraMap.get(YADARequest.PL_COMMITQUERY));
}
if (paraMap.get(YADARequest.PS_COMMITQUERY) != null)
{
this.yadaReq.setCommitQuery(paraMap.get(YADARequest.PS_COMMITQUERY));
}
if (paraMap.get(YADARequest.PS_COMPACT) != null)
{
this.yadaReq.setCompact(paraMap.get(YADARequest.PS_COMPACT));
}
if (paraMap.get(YADARequest.PL_COMPACT) != null)
{
this.yadaReq.setCompact(paraMap.get(YADARequest.PL_COMPACT));
}
if (paraMap.get(YADARequest.PL_CONVERTER) != null)
{
this.yadaReq.setConverter(paraMap.get(YADARequest.PL_CONVERTER));
}
if (paraMap.get(YADARequest.PS_CONVERTER)!= null)
{
this.yadaReq.setConverter(paraMap.get(YADARequest.PS_CONVERTER));
}
if (paraMap.get(YADARequest.PL_COUNT) != null)
{
this.yadaReq.setCount(paraMap.get(YADARequest.PL_COUNT));
}
if (paraMap.get(YADARequest.PS_COUNT) != null)
{
this.yadaReq.setCount(paraMap.get(YADARequest.PS_COUNT));
}
if (paraMap.get(YADARequest.PL_COOKIES) != null)
{
this.yadaReq.setCookies(paraMap.get(YADARequest.PL_COOKIES));
}
if (paraMap.get(YADARequest.PS_COOKIES) != null)
{
this.yadaReq.setCookies(paraMap.get(YADARequest.PS_COOKIES));
}
if (paraMap.get(YADARequest.PL_COUNTONLY) != null)
{
this.yadaReq.setCountOnly(paraMap.get(YADARequest.PL_COUNTONLY));
}
if (paraMap.get(YADARequest.PS_COUNTONLY) != null)
{
this.yadaReq.setCountOnly(paraMap.get(YADARequest.PS_COUNTONLY));
}
if (paraMap.get(YADARequest.PL_DELIMITER) != null)
{
this.yadaReq.setDelimiter(paraMap.get(YADARequest.PL_DELIMITER));
}
if (paraMap.get(YADARequest.PS_DELIMITER) != null)
{
this.yadaReq.setDelimiter(paraMap.get(YADARequest.PS_DELIMITER));
}
if (paraMap.get(YADARequest.PL_EXPORT) != null)
{
this.yadaReq.setExport(paraMap.get(YADARequest.PL_EXPORT));
}
if (paraMap.get(YADARequest.PS_EXPORT) != null)
{
this.yadaReq.setExport(paraMap.get(YADARequest.PS_EXPORT));
}
if (paraMap.get(YADARequest.PL_EXPORTLIMIT) != null)
{
this.yadaReq.setExportLimit(paraMap.get(YADARequest.PL_EXPORTLIMIT));
}
if (paraMap.get(YADARequest.PS_EXPORTLIMIT) != null)
{
this.yadaReq.setExportLimit(paraMap.get(YADARequest.PS_EXPORTLIMIT));
}
if (paraMap.get(YADARequest.PL_FILTERS) != null)
{
this.yadaReq.setFilters(paraMap.get(YADARequest.PL_FILTERS));
}
if (paraMap.get(YADARequest.PS_FILTERS) != null)
{
this.yadaReq.setFilters(paraMap.get(YADARequest.PS_FILTERS));
}
if (paraMap.get(YADARequest.PL_FORMAT) != null && !paraMap.get(YADARequest.PL_FORMAT).equals(YADARequest.FORMAT_JSON))
{
this.yadaReq.setFormat(paraMap.get(YADARequest.PL_FORMAT));
}
if (paraMap.get(YADARequest.PS_FORMAT) != null && !paraMap.get(YADARequest.PS_FORMAT).equals(YADARequest.FORMAT_JSON))
{
this.yadaReq.setFormat(paraMap.get(YADARequest.PS_FORMAT));
}
if (paraMap.get(YADARequest.PL_HARMONYMAP) != null)
{
this.yadaReq.setHarmonyMap(paraMap.get(YADARequest.PL_HARMONYMAP));
}
if (paraMap.get(YADARequest.PS_HARMONYMAP) != null)
{
this.yadaReq.setHarmonyMap(paraMap.get(YADARequest.PS_HARMONYMAP));
}
if (paraMap.get(YADARequest.PL_JSONPARAMS) != null)
{
this.yadaReq.setJsonParams(paraMap.get(YADARequest.PL_JSONPARAMS));
}
if (paraMap.get(YADARequest.PS_JSONPARAMS) != null)
{
this.yadaReq.setJsonParams(paraMap.get(YADARequest.PS_JSONPARAMS));
}
if (paraMap.get(YADARequest.PL_JOIN) != null)
{
this.yadaReq.setJoin(paraMap.get(YADARequest.PL_JOIN));
}
if (paraMap.get(YADARequest.PS_JOIN) != null)
{
this.yadaReq.setJoin(paraMap.get(YADARequest.PS_JOIN));
}
if (paraMap.get(YADARequest.PL_LEFTJOIN) != null)
{
this.yadaReq.setLeftJoin(paraMap.get(YADARequest.PL_LEFTJOIN));
}
if (paraMap.get(YADARequest.PS_LEFTJOIN) != null)
{
this.yadaReq.setLeftJoin(paraMap.get(YADARequest.PS_LEFTJOIN));
}
if (paraMap.get(YADARequest.PL_LABELS) != null)
{
this.yadaReq.setLabels(paraMap.get(YADARequest.PL_LABELS));
}
if (paraMap.get(YADARequest.PL_MAIL) != null)
{
this.yadaReq.setMail(paraMap.get(YADARequest.PL_MAIL));
}
if (paraMap.get(YADARequest.PL_METHOD) != null && !paraMap.get(YADARequest.PL_METHOD).equals(YADARequest.METHOD_GET))
{
this.yadaReq.setMethod(paraMap.get(YADARequest.PL_METHOD));
}
if (paraMap.get(YADARequest.PS_METHOD) != null && !paraMap.get(YADARequest.PS_METHOD).equals(YADARequest.METHOD_GET))
{
this.yadaReq.setMethod(paraMap.get(YADARequest.PS_METHOD));
}
if (paraMap.get(YADARequest.PL_OVERARGS) != null)
{
this.yadaReq.setBypassargs(paraMap.get(YADARequest.PL_OVERARGS));
}
if (paraMap.get(YADARequest.PS_OVERARGS) != null)
{
this.yadaReq.setBypassargs(paraMap.get(YADARequest.PS_OVERARGS));
}
if (paraMap.get(YADARequest.PL_BYPASSARGS) != null)
{
this.yadaReq.setBypassargs(paraMap.get(YADARequest.PL_BYPASSARGS));
}
if (paraMap.get(YADARequest.PS_BYPASSARGS) != null)
{
this.yadaReq.setBypassargs(paraMap.get(YADARequest.PS_BYPASSARGS));
}
if (paraMap.get(YADARequest.PL_PAGE) != null)
{
this.yadaReq.setPage(paraMap.get(YADARequest.PL_PAGE));
}
if (paraMap.get(YADARequest.PS_PAGE) != null)
{
this.yadaReq.setPage(paraMap.get(YADARequest.PS_PAGE));
}
if (paraMap.get(YADARequest.PL_PAGESIZE) != null)
{
this.yadaReq.setPageSize(paraMap.get(YADARequest.PL_PAGESIZE));
}
if (paraMap.get(YADARequest.PS_PAGESIZE) != null)
{
this.yadaReq.setPageSize(paraMap.get(YADARequest.PS_PAGESIZE));
}
if (paraMap.get(YADARequest.PL_PAGESTART) != null)
{
this.yadaReq.setPageStart(paraMap.get(YADARequest.PL_PAGESTART));
}
if (paraMap.get(YADARequest.PS_PAGESTART) != null)
{
this.yadaReq.setPageStart(paraMap.get(YADARequest.PS_PAGESTART));
}
if (paraMap.get(YADARequest.PL_PARAMS) != null)
{
this.yadaReq.setParams(paraMap.get(YADARequest.PL_PARAMS));
}
if (paraMap.get(YADARequest.PS_PARAMS) != null)
{
this.yadaReq.setParams(paraMap.get(YADARequest.PS_PARAMS));
}
if (paraMap.get(YADARequest.PL_PATH) != null)
{
this.yadaReq.setSortKey(paraMap.get(YADARequest.PL_PATH));
}
if (paraMap.get(YADARequest.PL_PARALLEL) != null)
{
this.yadaReq.setParallel(paraMap.get(YADARequest.PL_PARALLEL));
}
if (paraMap.get(YADARequest.PL_PLUGIN) != null)
{
this.yadaReq.setPlugin(paraMap.get(YADARequest.PL_PLUGIN));
}
if (paraMap.get(YADARequest.PS_PLUGIN) != null)
{
l.debug("really:"+paraMap.get(YADARequest.PS_PLUGIN));
this.yadaReq.setPlugin(paraMap.get(YADARequest.PS_PLUGIN));
}
if (paraMap.get(YADARequest.PL_PLUGINTYPE) != null && !paraMap.get(YADARequest.PL_PLUGINTYPE).equals(YADARequest.PREPROCESS))
{
this.yadaReq.setPluginType(paraMap.get(YADARequest.PL_PLUGINTYPE));
}
if (paraMap.get(YADARequest.PS_PLUGINTYPE) != null && !paraMap.get(YADARequest.PL_PLUGINTYPE).equals(YADARequest.PREPROCESS))
{
this.yadaReq.setPluginType(paraMap.get(YADARequest.PS_PLUGINTYPE));
}
if (paraMap.get(YADARequest.PL_POSTARGS) != null)
{
this.yadaReq.setPostArgs(paraMap.get(YADARequest.PL_POSTARGS));
}
if (paraMap.get(YADARequest.PS_POSTARGS) != null)
{
this.yadaReq.setPostArgs(paraMap.get(YADARequest.PS_POSTARGS));
}
if (paraMap.get(YADARequest.PL_PREARGS) != null)
{
this.yadaReq.setPreArgs(paraMap.get(YADARequest.PL_PREARGS));
}
if (paraMap.get(YADARequest.PS_PREARGS) != null)
{
this.yadaReq.setPreArgs(paraMap.get(YADARequest.PS_PREARGS));
}
if (paraMap.get(YADARequest.PL_PRETTY) != null)
{
this.yadaReq.setPretty(paraMap.get(YADARequest.PL_PRETTY));
}
if (paraMap.get(YADARequest.PS_PRETTY) != null)
{
this.yadaReq.setPretty(paraMap.get(YADARequest.PS_PRETTY));
}
if (paraMap.get(YADARequest.PL_PROXY) != null)
{
this.yadaReq.setProxy(paraMap.get(YADARequest.PL_PROXY));
}
if (paraMap.get(YADARequest.PS_PROXY) != null)
{
this.yadaReq.setProxy(paraMap.get(YADARequest.PS_PROXY));
}
if (paraMap.get(YADARequest.PL_QNAME) != null && !paraMap.get(YADARequest.PL_QNAME).equals(YADARequest.DEFAULT_QNAME))
{
this.yadaReq.setQname(paraMap.get(YADARequest.PL_QNAME));
}
if (paraMap.get(YADARequest.PS_QNAME) != null && !paraMap.get(YADARequest.PS_QNAME).equals(YADARequest.DEFAULT_QNAME))
{
this.yadaReq.setQname(paraMap.get(YADARequest.PS_QNAME));
}
if (paraMap.get(YADARequest.PL_ROW_DELIMITER) != null)
{
this.yadaReq.setRowDelimiter(paraMap.get(YADARequest.PL_ROW_DELIMITER));
}
if (paraMap.get(YADARequest.PS_ROW_DELIMITER) != null)
{
this.yadaReq.setRowDelimiter(paraMap.get(YADARequest.PS_ROW_DELIMITER));
}
if (paraMap.get(YADARequest.PL_RESPONSE) != null)
{
this.yadaReq.setResponse(paraMap.get(YADARequest.PL_RESPONSE));
}
if (paraMap.get(YADARequest.PS_RESPONSE) != null)
{
this.yadaReq.setResponse(paraMap.get(YADARequest.PS_RESPONSE));
}
if (paraMap.get(YADARequest.PL_SORTKEY) != null)
{
this.yadaReq.setSortKey(paraMap.get(YADARequest.PL_SORTKEY));
}
if (paraMap.get(YADARequest.PS_SORTKEY) != null)
{
this.yadaReq.setSortKey(paraMap.get(YADARequest.PS_SORTKEY));
}
if (paraMap.get(YADARequest.PL_SORTORDER) != null && !paraMap.get(YADARequest.PL_SORTORDER).equals(YADARequest.SORT_ASC))
{
this.yadaReq.setSortOrder(paraMap.get(YADARequest.PL_SORTORDER));
}
if (paraMap.get(YADARequest.PS_SORTORDER) != null && !paraMap.get(YADARequest.PS_SORTORDER).equals(YADARequest.SORT_ASC))
{
this.yadaReq.setSortOrder(paraMap.get(YADARequest.PS_SORTORDER));
}
if (paraMap.get(YADARequest.PL_USER) != null && !paraMap.get(YADARequest.PL_USER).equals(YADARequest.DEFAULT_USER))
{
this.yadaReq.setUser(paraMap.get(YADARequest.PL_USER));
}
if (paraMap.get(YADARequest.PS_USER) != null && !paraMap.get(YADARequest.PS_USER).equals(YADARequest.DEFAULT_USER))
{
this.yadaReq.setUser(paraMap.get(YADARequest.PS_USER));
}
if (paraMap.get(YADARequest.PL_VIEWLIMIT) != null)
{
this.yadaReq.setViewLimit(paraMap.get(YADARequest.PL_VIEWLIMIT));
}
if (paraMap.get(YADARequest.PS_VIEWLIMIT) != null)
{
this.yadaReq.setViewLimit(paraMap.get(YADARequest.PS_VIEWLIMIT));
}
if (paraMap.get(YADARequest.PL_UPDATE_STATS) != null)
{
this.yadaReq.setUpdateStats(paraMap.get(YADARequest.PL_UPDATE_STATS));
}
if (paraMap.get(YADARequest.PS_UPDATE_STATS) != null)
{
this.yadaReq.setUpdateStats(paraMap.get(YADARequest.PS_UPDATE_STATS));
}
this.yadaReq.setParameterMap(paraMap);
l.debug("current settings:\n"+this.yadaReq.toString());
}
catch(YADARequestException e)
{
error(e.getMessage(),e);
}
}
/**
* Wraps exceptions and request metadata in json in order to provide useful information to requesting clients in the event
* and uncaught exception bubbles up to {@link #execute()}
* @param msg the message to report to the client
* @param e the exception to report to the client
* @return a json string with error and request information
*/
private String error(String msg, Exception e)
{
JSONObject j = new JSONObject();
String result = "";
try
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
YADAQuery yq = getCurrentQuery();
j.put("Help", "https://github.com/Novartis/YADA#other");
j.put("Source", "https://github.com/Novartis/YADA");
j.put("Exception", e.getClass().getName());
j.put("Message",msg);
j.put("Qname",yq != null ? yq.getQname() : "UNKNOWN");
j.put("Query",yq != null ? yq.getYADACode() : "");
j.put("Params",new JSONArray());
JSONArray ja = j.getJSONArray("Params");
if(yq != null)
{
for(int i=0;i iter = yq.getDataRow(i).keySet().iterator();
JSONObject jo = new JSONObject();
while(iter.hasNext())
{
String key = (String) iter.next();
jo.put(key, Arrays.toString(yq.getDataRow(i).get(key)));
}
ja.put(jo);
}
}
if(this.yadaReq.getRequest() != null && this.yadaReq.getRequest().getMethod() != null)
{
String type = this.yadaReq.getRequest().getMethod();
j.put("Type",type);
// if(type.equals("GET"))
// query = this.yadaReq.getRequest().getQueryString();
// else
// query = "Not available";
// j.put("Query", URLDecoder.decode(query,"UTF-8"));
}
String[] strace = sw.toString().replace("\t","").split("\n");
j.put("StackTrace",new JSONArray());
j.put("Links", new JSONArray());
JSONArray st = j.getJSONArray("StackTrace");
JSONArray links = j.getJSONArray("Links");
Pattern rx_src = Pattern.compile("at com\\.novartis\\.opensource\\.yada\\.(.+)\\..+\\(.+\\.java:(\\d+)\\)");
for(int i=0;i 0)
{
for (String plugin : plugins)
{
l.debug("possible bypass plugin is["+plugin+"]");
if (null != plugin && !"".equals(plugin))
{
try
{
Class> pluginClass = plugin.indexOf(YADARequest.PLUGIN_PKG) > -1
? Class.forName(plugin)
: Class.forName(YADARequest.PLUGIN_PKG + "." + plugin);
Class> bypass = Class.forName(YADARequest.PLUGIN_PKG+"."+YADARequest.BYPASS);
if (pluginClass != null)
{
if(bypass.isAssignableFrom(pluginClass)) // this checks plugin type
{
l.info("Found a QUERY_BYPASS plugin with the classname ["+plugin+"]");
try
{
Object plugObj = pluginClass.newInstance();
yqr = ((Bypass)plugObj).engage(getYADARequest(),yq);
yq.setResult(yqr);
}
catch(InstantiationException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(IllegalAccessException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(ClassCastException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
}
else
{
l.warn("Could not find an BYPASS plugin with the classname ["+plugin+"]");
}
}
}
catch(ClassNotFoundException e)
{
String msg = "Could not find any plugin with the classname ["+plugin+"]";
l.error(msg);
}
}
}
}
}
}
/**
* Executes the preprocess plugin
* @since 0.4.0.0
* @param yq the yada query object
* @throws YADAPluginException when the plugin fails to execute successfully
*/
private void engagePreprocess(YADAQuery yq) throws YADAPluginException
{
if(yq.hasParam(YADARequest.PS_PLUGIN)
&& yq.getParam(YADARequest.PS_PLUGIN).getTarget().equals(yq.getQname()))
{
String[] plugins = yq.getYADAQueryParamValue(YADARequest.PS_PLUGIN);
if ( null != plugins && plugins.length > 0)
{
for (String plugin : plugins)
{
l.debug("possible preprocess plugin is["+plugin+"]");
if (null != plugin && !"".equals(plugin))
{
try
{
Class> pluginClass = plugin.indexOf(YADARequest.PLUGIN_PKG) > -1
? Class.forName(plugin)
: Class.forName(YADARequest.PLUGIN_PKG + "." + plugin);
Class> preproc = Class.forName(YADARequest.PLUGIN_PKG+"."+YADARequest.PREPROCESS);
if(preproc.isAssignableFrom(pluginClass))
{
l.info("Found a query-level PREPROCESS plugin with the classname ["+plugin+"]");
try
{
Object plugObj = pluginClass.newInstance();
((Preprocess)plugObj).engage(getYADARequest(),yq);
// reset the query internals
try
{
this.qMgr.prepQueryForExecution(this.qMgr.endowQuery(yq));
}
catch (YADAQueryConfigurationException|YADAResourceException|YADAUnsupportedAdaptorException e)
{
String msg = "Unable to re-endow YADAQuery with new parameters.";
throw new YADAPluginException(msg, e);
}
catch (YADAConnectionException | YADARequestException | YADAAdaptorException | YADAParserException e)
{
String msg = "Unable to re-prep YADAQuery for execution.";
throw new YADAPluginException(msg, e);
}
}
catch(InstantiationException|IllegalAccessException|ClassCastException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
}
else
{
l.warn("Could not find a PREPROCESS plugin with the classname ["+plugin+"]");
}
}
catch(ClassNotFoundException e)
{
String msg = "Could not find any plugin with the classname ["+plugin+"]";
l.error(msg,e);
}
}
}
}
}
}
/**
* Executes a query-level post-processor plugin
* @param yq the yada query object
* @throws YADAPluginException when the plugin fails to execute successfully
*/
@SuppressWarnings("static-method")
private void engagePostprocess(YADAQuery yq) throws YADAPluginException
{
if(yq.hasParam(YADARequest.PS_PLUGIN)
&& yq.getParam(YADARequest.PS_PLUGIN).getTarget().equals(yq.getQname()))
{
String[] plugins = yq.getYADAQueryParamValue(YADARequest.PS_PLUGIN);
if ( null != plugins && plugins.length > 0 )
{
for (String plugin : plugins)
{
l.debug("possible postprocess plugin is["+plugin+"]");
if (null != plugin && !"".equals(plugin))
{
try
{
Class> pluginClass = plugin.indexOf(YADARequest.PLUGIN_PKG) > -1
? Class.forName(plugin)
: Class.forName(YADARequest.PLUGIN_PKG + "." + plugin);
Class> postproc = Class.forName(YADARequest.PLUGIN_PKG+"."+YADARequest.POSTPROCESS);
if(postproc.isAssignableFrom(pluginClass))
{
l.info("Found a POSTPROCESS plugin with the classname ["+plugin+"]");
try
{
Object plugObj = pluginClass.newInstance();
((Postprocess)plugObj).engage(yq);
}
catch(InstantiationException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(IllegalAccessException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(ClassCastException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
}
else
{
l.warn("Could not find a POSTPROCESS plugin with the classname ["+plugin+"]");
}
}
catch(ClassNotFoundException e)
{
String msg = "Could not find any plugin with the classname ["+plugin+"]";
l.error(msg);
}
}
}
}
}
//TODO if no class, url, or script, should return same 'result' passed in
}
/**
* Executes a request-level Bypass plugin
* @param lyadaReq the request configuration
* @return a result to return to the requesting client
* @throws YADAPluginException when the plugin fails to execute successfully
*/
private String engageBypass(YADARequest lyadaReq) throws YADAPluginException
{
String result = null;
String[] plugins = lyadaReq.getPlugin();
if (null != plugins && plugins.length > 0)
{
for (String plugin : plugins)
{
l.debug("possible Bypass plugin is["+plugin+"]");
if (null != plugin && !"".equals(plugin))
{
try
{
Class> pluginClass = plugin.indexOf(YADARequest.PLUGIN_PKG) > -1
? Class.forName(plugin)
: Class.forName(YADARequest.PLUGIN_PKG + "." + plugin);
Class> bypass = Class.forName(YADARequest.PLUGIN_PKG+"."+YADARequest.BYPASS);
if (pluginClass != null)
{
if(bypass.isAssignableFrom(pluginClass)) // this checks plugin type
{
l.info("Found an BYPASS plugin with the classname ["+plugin+"]");
try
{
Object plugObj = pluginClass.newInstance();
result = ((Bypass)plugObj).engage(getYADARequest());
}
catch(InstantiationException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(IllegalAccessException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(ClassCastException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
}
else
{
l.warn("Could not find a BYPASS plugin with the classname ["+plugin+"]");
}
}
}
catch(ClassNotFoundException e)
{
String msg = "Could not find any plugin with the classname ["+plugin+"]";
l.error(msg);
// throw new YADAPluginException(msg,e);
}
}
}
}
return result; // returns a string
}
/**
* Executes a request-level pre-processor plugin
* @param yReq the request configuration
* @throws YADAPluginException when the plugin fails to execute successfully
*/
private void engagePreprocess(YADARequest yReq) throws YADAPluginException
{
String[] plugins = yReq.getPlugin();
if ( null != plugins && plugins.length > 0)
{
for (String plugin : plugins)
{
l.debug("possible preprocess plugin is["+plugin+"]");
if (null != plugin && !"".equals(plugin))
{
try
{
Class> pluginClass = plugin.indexOf(YADARequest.PLUGIN_PKG) > -1
? Class.forName(plugin)
: Class.forName(YADARequest.PLUGIN_PKG + "." + plugin);
Class> preproc = Class.forName(YADARequest.PLUGIN_PKG+"."+YADARequest.PREPROCESS);
if(preproc.isAssignableFrom(pluginClass))
{
l.info("Found a request-level PREPROCESS plugin with the classname ["+plugin+"]");
try
{
Object plugObj = pluginClass.newInstance();
setYADARequest(((Preprocess)plugObj).engage(yReq));
// reset query manager, as service parameters may have changed
try
{
this.qMgr = new QueryManager(getYADARequest());
}
catch (YADAQueryConfigurationException e)
{
String msg = "Unable to reinitialize QueryManager with new parameters.";
throw new YADAPluginException(msg, e);
}
catch (YADAResourceException e)
{
String msg = "Unable to reinitialize QueryManager with new parameters.";
throw new YADAPluginException(msg, e);
}
catch (YADAConnectionException e)
{
String msg = "Unable to reinitialize QueryManager with new parameters.";
throw new YADAPluginException(msg, e);
}
catch (YADAFinderException e)
{
String msg = "Unable to reinitialize QueryManager with new parameters.";
throw new YADAPluginException(msg, e);
}
catch (YADAUnsupportedAdaptorException e)
{
String msg = "Unable to reinitialize QueryManager with new parameters.";
throw new YADAPluginException(msg, e);
}
catch (YADARequestException e)
{
String msg = "Unable to reinitialize QueryManager with new parameters.";
throw new YADAPluginException(msg, e);
}
catch (YADAAdaptorException e)
{
String msg = "Unable to reinitialize QueryManager with new parameters.";
throw new YADAPluginException(msg, e);
}
catch (YADAParserException e)
{
String msg = "Unable to reinitialize QueryManager with new parameters.";
throw new YADAPluginException(msg, e);
}
}
catch(InstantiationException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(IllegalAccessException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(ClassCastException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
}
else
{
l.warn("Could not find a PREPROCESS plugin with the classname ["+plugin+"]");
}
}
catch(ClassNotFoundException e)
{
String msg = "Could not find any plugin with the classname ["+plugin+"]";
l.error(msg,e);
// throw new YADAPluginException(msg,e);
}
}
}
}
}
/**
* Executes a request-level post-processor plugin
* @param result the raw result serving as plugin input
* @return a result to return to the requesting client
* @throws YADAPluginException when the plugin fails to execute successfully
*/
private String engagePostprocess(String result) throws YADAPluginException
{
String lResult = result;
String[] plugins = this.yadaReq.getPlugin();
if ( null != plugins && plugins.length > 0 )
{
for (String plugin : plugins)
{
l.debug("possible postprocess plugin is["+plugin+"]");
if (null != plugin && !"".equals(plugin))
{
try
{
Class> pluginClass = plugin.indexOf(YADARequest.PLUGIN_PKG) > -1
? Class.forName(plugin)
: Class.forName(YADARequest.PLUGIN_PKG + "." + plugin);
Class> postproc = Class.forName(YADARequest.PLUGIN_PKG+"."+YADARequest.POSTPROCESS);
if(postproc.isAssignableFrom(pluginClass))
{
l.info("Found a POSTPROCESS plugin with the classname ["+plugin+"]");
try
{
Object plugObj = pluginClass.newInstance();
lResult = ((Postprocess)plugObj).engage(this.yadaReq, lResult);
}
catch(InstantiationException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(IllegalAccessException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
catch(ClassCastException e)
{
String msg = "Unable to instantiate plugin for class "+pluginClass.getName();
throw new YADAPluginException(msg,e);
}
}
else
{
l.warn("Could not find a POSTPROCESS plugin with the classname ["+plugin+"]");
}
}
catch(ClassNotFoundException e)
{
String msg = "Could not find any plugin with the classname ["+plugin+"]";
l.error(msg);
// throw new YADAPluginException(msg,e);
}
}
}
}
// if no class, url, or script, should return same 'result' passed in
return lResult;
}
/**
* Standard mutator for variable.
* @param yadaReq YADA request configuration
*/
public void setYADARequest(YADARequest yadaReq) {
this.yadaReq = yadaReq;
}
/**
* Standard accessor for variable.
* @return the {@link YADARequest} object
*/
public YADARequest getYADARequest() {
return this.yadaReq;
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy