All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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.plugin.YADASecurityException;
import com.novartis.opensource.yada.util.FileUtils;
import com.novartis.opensource.yada.util.QueryUtils;
import com.novartis.opensource.yada.util.YADAUtils;

/**
 * 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) {
		setYADARequest(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)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PL_ARGS);
  		}
  		if(paraMap.get(YADARequest.PS_ARGS) != null)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PS_ARGS);
  		}
  		if (paraMap.get(YADARequest.PL_COLHEAD) != null)
  		{
  			getYADARequest().setColhead(paraMap.get(YADARequest.PL_COLHEAD));
  		}
  		if (paraMap.get(YADARequest.PL_COMMITQUERY) != null)
      {
        getYADARequest().setCommitQuery(paraMap.get(YADARequest.PL_COMMITQUERY));
      }
      if (paraMap.get(YADARequest.PS_COMMITQUERY) != null)
      {
        getYADARequest().setCommitQuery(paraMap.get(YADARequest.PS_COMMITQUERY));
      }
      if (paraMap.get(YADARequest.PS_COMPACT) != null)
      {
        getYADARequest().setCompact(paraMap.get(YADARequest.PS_COMPACT));
      }
  		if (paraMap.get(YADARequest.PL_COMPACT) != null)
  		{
  			getYADARequest().setCompact(paraMap.get(YADARequest.PL_COMPACT));
  		}
  		if (paraMap.get(YADARequest.PL_CONVERTER) != null)
      {
        getYADARequest().setConverter(paraMap.get(YADARequest.PL_CONVERTER));
      }
      if (paraMap.get(YADARequest.PS_CONVERTER)!= null)
      {
        getYADARequest().setConverter(paraMap.get(YADARequest.PS_CONVERTER));
      }
  		if (paraMap.get(YADARequest.PL_COUNT) != null)
  		{
  			getYADARequest().setCount(paraMap.get(YADARequest.PL_COUNT));
  		}
  		if (paraMap.get(YADARequest.PS_COUNT) != null)
  		{
  			getYADARequest().setCount(paraMap.get(YADARequest.PS_COUNT));
  		}
  		if (paraMap.get(YADARequest.PL_COOKIES) != null)
      {
        getYADARequest().setCookies(paraMap.get(YADARequest.PL_COOKIES));
      }
      if (paraMap.get(YADARequest.PS_COOKIES) != null)
      {
        getYADARequest().setCookies(paraMap.get(YADARequest.PS_COOKIES));
      }
  		if (paraMap.get(YADARequest.PL_COUNTONLY) != null)
  		{
  			getYADARequest().setCountOnly(paraMap.get(YADARequest.PL_COUNTONLY));
  		}
  		if (paraMap.get(YADARequest.PS_COUNTONLY) != null)
  		{
  			getYADARequest().setCountOnly(paraMap.get(YADARequest.PS_COUNTONLY));
  		}
  		if (paraMap.get(YADARequest.PL_DELIMITER) != null)
  		{
  			getYADARequest().setDelimiter(paraMap.get(YADARequest.PL_DELIMITER));
  		}
  		if (paraMap.get(YADARequest.PS_DELIMITER) != null)
  		{
  			getYADARequest().setDelimiter(paraMap.get(YADARequest.PS_DELIMITER));
  		}
  		if (paraMap.get(YADARequest.PL_EXPORT) != null)
  		{
  			getYADARequest().setExport(paraMap.get(YADARequest.PL_EXPORT));
  		}
  		if (paraMap.get(YADARequest.PS_EXPORT) != null)
  		{
  			getYADARequest().setExport(paraMap.get(YADARequest.PS_EXPORT));
  		}
  		if (paraMap.get(YADARequest.PL_EXPORTLIMIT) != null)
  		{
  			getYADARequest().setExportLimit(paraMap.get(YADARequest.PL_EXPORTLIMIT));
  		}
  		if (paraMap.get(YADARequest.PS_EXPORTLIMIT) != null)
  		{
  			getYADARequest().setExportLimit(paraMap.get(YADARequest.PS_EXPORTLIMIT));
  		}
  		if (paraMap.get(YADARequest.PL_FILTERS) != null)
  		{
  			getYADARequest().setFilters(paraMap.get(YADARequest.PL_FILTERS));
  		}
  		if (paraMap.get(YADARequest.PS_FILTERS) != null)
  		{
  			getYADARequest().setFilters(paraMap.get(YADARequest.PS_FILTERS));
  		}
  		if (paraMap.get(YADARequest.PL_FORMAT) != null && !paraMap.get(YADARequest.PL_FORMAT).equals(YADARequest.FORMAT_JSON))
  		{
  			getYADARequest().setFormat(paraMap.get(YADARequest.PL_FORMAT));
  		}
  		if (paraMap.get(YADARequest.PS_FORMAT) != null && !paraMap.get(YADARequest.PS_FORMAT).equals(YADARequest.FORMAT_JSON))
  		{
  			getYADARequest().setFormat(paraMap.get(YADARequest.PS_FORMAT));
  		}
  		if (paraMap.get(YADARequest.PL_HARMONYMAP) != null)
  		{
  			getYADARequest().setHarmonyMap(paraMap.get(YADARequest.PL_HARMONYMAP));
  		}
  		if (paraMap.get(YADARequest.PS_HARMONYMAP) != null)
  		{
  			getYADARequest().setHarmonyMap(paraMap.get(YADARequest.PS_HARMONYMAP));
  		}
  		if (paraMap.get(YADARequest.PL_JSONPARAMS) != null)
  		{
  			getYADARequest().setJsonParams(paraMap.get(YADARequest.PL_JSONPARAMS));
  		}
  		if (paraMap.get(YADARequest.PS_JSONPARAMS) != null)
  		{
  			getYADARequest().setJsonParams(paraMap.get(YADARequest.PS_JSONPARAMS));
  		}
  		
  		if (paraMap.get(YADARequest.PL_JOIN) != null)
      {
        getYADARequest().setJoin(paraMap.get(YADARequest.PL_JOIN));
      }
  		if (paraMap.get(YADARequest.PS_JOIN) != null)
      {
        getYADARequest().setJoin(paraMap.get(YADARequest.PS_JOIN));
      }
  		if (paraMap.get(YADARequest.PL_LEFTJOIN) != null)
      {
        getYADARequest().setLeftJoin(paraMap.get(YADARequest.PL_LEFTJOIN));
      }
      if (paraMap.get(YADARequest.PS_LEFTJOIN) != null)
      {
        getYADARequest().setLeftJoin(paraMap.get(YADARequest.PS_LEFTJOIN));
      }
      
  		if (paraMap.get(YADARequest.PL_LABELS) != null)
  		{
  			getYADARequest().setLabels(paraMap.get(YADARequest.PL_LABELS));
  		}
  		if (paraMap.get(YADARequest.PL_MAIL) != null)
  		{
  			getYADARequest().setMail(paraMap.get(YADARequest.PL_MAIL));
  		}
  		if (paraMap.get(YADARequest.PL_METHOD) != null && !paraMap.get(YADARequest.PL_METHOD).equals(YADARequest.METHOD_GET))
  		{
  			getYADARequest().setMethod(paraMap.get(YADARequest.PL_METHOD));
  		}
  		if (paraMap.get(YADARequest.PS_METHOD) != null && !paraMap.get(YADARequest.PS_METHOD).equals(YADARequest.METHOD_GET))
  		{
  			getYADARequest().setMethod(paraMap.get(YADARequest.PS_METHOD));
  		}
  		if (paraMap.get(YADARequest.PL_OVERARGS) != null)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PL_OVERARGS);
  		}
  		if (paraMap.get(YADARequest.PS_OVERARGS) != null)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PS_OVERARGS);
  		}
  		if (paraMap.get(YADARequest.PL_BYPASSARGS) != null)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PL_BYPASSARGS);
  		}
  		if (paraMap.get(YADARequest.PS_BYPASSARGS) != null)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PS_BYPASSARGS);
  		}
  		if (paraMap.get(YADARequest.PL_PAGE) != null)
  		{
  			getYADARequest().setPage(paraMap.get(YADARequest.PL_PAGE));
  		}
  		if (paraMap.get(YADARequest.PS_PAGE) != null)
  		{
  			getYADARequest().setPage(paraMap.get(YADARequest.PS_PAGE));
  		}
  		if (paraMap.get(YADARequest.PL_PAGESIZE) != null)
  		{
  			getYADARequest().setPageSize(paraMap.get(YADARequest.PL_PAGESIZE));
  		}
  		if (paraMap.get(YADARequest.PS_PAGESIZE) != null)
  		{
  			getYADARequest().setPageSize(paraMap.get(YADARequest.PS_PAGESIZE));
  		}
  		if (paraMap.get(YADARequest.PL_PAGESTART) != null)
  		{
  			getYADARequest().setPageStart(paraMap.get(YADARequest.PL_PAGESTART));
  		}
  		if (paraMap.get(YADARequest.PS_PAGESTART) != null)
  		{
  			getYADARequest().setPageStart(paraMap.get(YADARequest.PS_PAGESTART));
  		}
  		if (paraMap.get(YADARequest.PL_PARAMS) != null)
  		{
  			getYADARequest().setParams(paraMap.get(YADARequest.PL_PARAMS));
  		}
  		if (paraMap.get(YADARequest.PS_PARAMS) != null)
  		{
  			getYADARequest().setParams(paraMap.get(YADARequest.PS_PARAMS));
  		}
  		if (paraMap.get(YADARequest.PL_PATH) != null)
  		{
  			getYADARequest().setSortKey(paraMap.get(YADARequest.PL_PATH));
  		}
  		if (paraMap.get(YADARequest.PL_PARALLEL) != null)
  		{
  			getYADARequest().setParallel(paraMap.get(YADARequest.PL_PARALLEL));
  		}
  		if (paraMap.get(YADARequest.PL_PLUGINTYPE) != null && !paraMap.get(YADARequest.PL_PLUGINTYPE).equals(YADARequest.PREPROCESS))
  		{
  			getYADARequest().setPluginType(paraMap.get(YADARequest.PL_PLUGINTYPE));
  		}
  		if (paraMap.get(YADARequest.PS_PLUGINTYPE) != null && !paraMap.get(YADARequest.PL_PLUGINTYPE).equals(YADARequest.PREPROCESS))
  		{
  			getYADARequest().setPluginType(paraMap.get(YADARequest.PS_PLUGINTYPE));
  		}
  		if (paraMap.get(YADARequest.PL_POSTARGS) != null)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PL_POSTARGS);
  		}
  		if (paraMap.get(YADARequest.PS_POSTARGS) != null)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PS_POSTARGS);
  		}
  		if (paraMap.get(YADARequest.PL_PREARGS) != null)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PL_PREARGS);
  		}
  		if (paraMap.get(YADARequest.PS_PREARGS) != null)
  		{
  		  setDeprecatedPlugin(paraMap, YADARequest.PS_PREARGS);
  		}
  		
  		// the next two conditionals must come after pre, post, and bypass arg handling
  		if (paraMap.get(YADARequest.PL_PLUGIN) != null)
      {
        getYADARequest().setPlugin(paraMap.get(YADARequest.PL_PLUGIN));
      }
      if (paraMap.get(YADARequest.PS_PLUGIN) != null)
      {
        getYADARequest().setPlugin(paraMap.get(YADARequest.PS_PLUGIN));
      }
      
      
  		if (paraMap.get(YADARequest.PL_PRETTY) != null)
  		{
  			getYADARequest().setPretty(paraMap.get(YADARequest.PL_PRETTY));
  		}
  		if (paraMap.get(YADARequest.PS_PRETTY) != null)
  		{
  			getYADARequest().setPretty(paraMap.get(YADARequest.PS_PRETTY));
  		}
  		if (paraMap.get(YADARequest.PL_PROXY) != null)
  		{
  			getYADARequest().setProxy(paraMap.get(YADARequest.PL_PROXY));
  		}
  		if (paraMap.get(YADARequest.PS_PROXY) != null)
  		{
  			getYADARequest().setProxy(paraMap.get(YADARequest.PS_PROXY));
  		}
  		if (paraMap.get(YADARequest.PL_QNAME) != null && !paraMap.get(YADARequest.PL_QNAME).equals(YADARequest.DEFAULT_QNAME))
  		{
  			getYADARequest().setQname(paraMap.get(YADARequest.PL_QNAME));
  		}
  		if (paraMap.get(YADARequest.PS_QNAME) != null && !paraMap.get(YADARequest.PS_QNAME).equals(YADARequest.DEFAULT_QNAME))
  		{
  			getYADARequest().setQname(paraMap.get(YADARequest.PS_QNAME));
  		}
  		if (paraMap.get(YADARequest.PL_ROW_DELIMITER) != null)
  		{
  			getYADARequest().setRowDelimiter(paraMap.get(YADARequest.PL_ROW_DELIMITER));
  		}
  		if (paraMap.get(YADARequest.PS_ROW_DELIMITER) != null)
  		{
  			getYADARequest().setRowDelimiter(paraMap.get(YADARequest.PS_ROW_DELIMITER));
  		}
  		if (paraMap.get(YADARequest.PL_RESPONSE) != null)
  		{
  			getYADARequest().setResponse(paraMap.get(YADARequest.PL_RESPONSE));
  		}
  		if (paraMap.get(YADARequest.PS_RESPONSE) != null)
  		{
  			getYADARequest().setResponse(paraMap.get(YADARequest.PS_RESPONSE));
  		}
  		if (paraMap.get(YADARequest.PL_SORTKEY) != null)
  		{
  			getYADARequest().setSortKey(paraMap.get(YADARequest.PL_SORTKEY));
  		}
  		if (paraMap.get(YADARequest.PS_SORTKEY) != null)
  		{
  			getYADARequest().setSortKey(paraMap.get(YADARequest.PS_SORTKEY));
  		}
  		if (paraMap.get(YADARequest.PL_SORTORDER) != null && !paraMap.get(YADARequest.PL_SORTORDER).equals(YADARequest.SORT_ASC))
  		{
  			getYADARequest().setSortOrder(paraMap.get(YADARequest.PL_SORTORDER));
  		}
  		if (paraMap.get(YADARequest.PS_SORTORDER) != null && !paraMap.get(YADARequest.PS_SORTORDER).equals(YADARequest.SORT_ASC))
  		{
  			getYADARequest().setSortOrder(paraMap.get(YADARequest.PS_SORTORDER));
  		}
  		if (paraMap.get(YADARequest.PL_USER) != null && !paraMap.get(YADARequest.PL_USER).equals(YADARequest.DEFAULT_USER))
  		{
  			getYADARequest().setUser(paraMap.get(YADARequest.PL_USER));
  		}
  		if (paraMap.get(YADARequest.PS_USER) != null && !paraMap.get(YADARequest.PS_USER).equals(YADARequest.DEFAULT_USER))
  		{
  			getYADARequest().setUser(paraMap.get(YADARequest.PS_USER));
  		}
  		if (paraMap.get(YADARequest.PL_VIEWLIMIT) != null)
  		{
  			getYADARequest().setViewLimit(paraMap.get(YADARequest.PL_VIEWLIMIT));
  		}
  		if (paraMap.get(YADARequest.PS_VIEWLIMIT) != null)
  		{
  			getYADARequest().setViewLimit(paraMap.get(YADARequest.PS_VIEWLIMIT));
  		}
  		if (paraMap.get(YADARequest.PL_UPDATE_STATS) != null)
      {
        getYADARequest().setUpdateStats(paraMap.get(YADARequest.PL_UPDATE_STATS));
      }
      if (paraMap.get(YADARequest.PS_UPDATE_STATS) != null)
      {
        getYADARequest().setUpdateStats(paraMap.get(YADARequest.PS_UPDATE_STATS));
      }
  		getYADARequest().setParameterMap(paraMap);
  		
  		l.debug("current settings:\n"+getYADARequest().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("Version", YADAUtils.getVersion());
			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(getYADARequest().getRequest() != null && getYADARequest().getRequest().getMethod() != null)
			{
				String type = getYADARequest().getRequest().getMethod(); 
				j.put("Type",type);
			}
			
			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 (int i=0; i < plugins.length; i++)
        {
          String plugin = plugins[i];
					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();
										if(getYADARequest().getPluginArgs().size() > 0) // api call might not set any args
										  getYADARequest().setArgs(getYADARequest().getPluginArgs().get(i));
										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 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 (int i=0; i < plugins.length; i++)
	      {
			    String plugin = plugins[i];
					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();
									if(getYADARequest().getPluginArgs().size() > 0) // api call might not set any args
									  getYADARequest().setArgs(getYADARequest().getPluginArgs().get(i));
									((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);
						}
					}
				}
			}
		}
		else
		{
	    if(yq.isProtected())
	    {
	      String msg = "Unauthorized";
	      throw new YADASecurityException(msg);
	    }
		}
	}
	
	/**
	 * 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 (int i=0; i < plugins.length; i++)
        {
          String plugin = plugins[i];
					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 (int i=0; i < plugins.length; i++)
      {
        String plugin = plugins[i];
				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();
									if(getYADARequest().getPluginArgs().size() > 0) // api call might not set any args
									  getYADARequest().setArgs(getYADARequest().getPluginArgs().get(i));
									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)
		{
		  // TODO evaluate vulnerabilities to circumvent query-level security plugins
			for (int i=0; i < plugins.length; i++)
			{
			  String plugin = plugins[i];
				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();
								if(getYADARequest().getPluginArgs().size() > 0) // api call might not set any args
								  yReq.setArgs(yReq.getPluginArgs().get(i));
								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 = getYADARequest().getPlugin();
		if ( null != plugins && plugins.length > 0 )
		{
		  for (int i=0; i < plugins.length; i++)
      {
        String plugin = plugins[i];
				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();
								if(getYADARequest().getPluginArgs().size() > 0) // api call might not set any args
								  getYADARequest().setArgs(getYADARequest().getPluginArgs().get(i));
								lResult = ((Postprocess)plugObj).engage(getYADARequest(), 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;
	}
	
	/**
	 * Takes the old-style argument parameters and appends them to the {@link YADARequest#PS_PLUGIN} parameter.
	 * The new config is then handled downstream during normal plugin parameter processing
	 * @param paraMap the {@link Map} passed in the {@link HttpRequest}
	 * @param constant the {@link YADARequest} argument constant
	 */
	private void setDeprecatedPlugin(Map paraMap, String constant)
	{
	  String[] plugins = paraMap.get(YADARequest.PS_PLUGIN);
    String plugin = "";
    if(plugins == null)
    {
      plugins = paraMap.get(YADARequest.PL_PLUGIN);
      if(plugins != null)
        paraMap.remove(YADARequest.PL_PLUGIN);
    }
    if(plugins != null)
    {
      plugin = plugins[0] + "," + paraMap.get(constant)[0]; 
    }
    paraMap.put(YADARequest.PS_PLUGIN,new String[] { plugin });
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy