sparqlGraphLibrary/src/main/java/com/ge/research/semtk/services/client/RestClient.java
public Object execute(boolean returnRawResponse) throws ConnectException, Exception {
		
		// set all parameters available upon instantiation
		buildParametersJSON();  
		
		if (conf.method != RestClientConfig.Methods.PUT) {
			if(parametersJSON == null){
				throw new Exception("Service parameters not set");
			}
		} else {
			if(parametersJSON != null && parametersJSON.size() > 0){
				throw new Exception("Service parameters are not implemented for PUT");
			}
		}

		// create entity
		// HttpEntity entity = new ByteArrayEntity(parametersJSON.toJSONString().getBytes("UTF-8")); // perform html encoding in stream	
		// js version:  return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/%/g, "&#37;")
		HttpEntity entity = null;
		
		if (putContent != null) {
			if (conf.method == RestClientConfig.Methods.PUT) {
				entity = new StringEntity(putContent);
			} else {
				throw new Exception("Internal: putContent is provided for non-PUT request: " + conf.method.name());
			}
			
		} else if (fileParameter != null) {
			// add the file
			FileBody bin = new FileBody(fileParameter);
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.addPart(this.fileParameterName, bin);
			
			// add parametersJSON as StringBody
			for (Object k : parametersJSON.keySet()) {
				builder.addPart((String) k, new StringBody((String)parametersJSON.get(k)));
			}
			entity = builder.build();
			
		} else {
			// add just parametersJSON
			entity = new ByteArrayEntity(parametersJSON.toString().getBytes("UTF-8"));
		}

		// create a request - GET or POST 
		HttpRequestBase httpreq = null;
		if (conf.method == RestClientConfig.Methods.GET) {
			HttpGet httpget = new HttpGet(this.conf.getServiceURL());
			httpget.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
			httpreq = httpget;
		} else if (conf.method == RestClientConfig.Methods.PUT) {
			HttpPut httpput = new HttpPut(this.conf.getServiceURL());
			if (entity != null) {
				httpput.setEntity(entity);
			}
			httpreq = httpput;
			
		} else if (conf.method == RestClientConfig.Methods.DELETE) {
			HttpDelete httpdelete = new HttpDelete(this.conf.getServiceURL());
			httpreq = httpdelete;
			
		} else {
			HttpPost httppost = new HttpPost(this.conf.getServiceURL());
			httppost.setEntity(entity);
			if (fileParameter == null) {
				httppost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
			}
			httpreq = httppost;
		}

		// add default headers
		for (BasicHeader header : this.getDefaultHeaders()) {
			httpreq.addHeader(header);
		}
		
		// execute
		HttpHost targetHost = new HttpHost(this.conf.getServiceServer(), this.conf.getServicePort(), this.conf.getServiceProtocol());		
		this.httpResponse = null;
		try {
			HttpClient httpclient = HttpClients.custom().build();
			//HttpClient httpclient = HttpClients.custom().setDefaultHeaders(RestClient.getDefaultHeaders()).build();
			LocalLogger.logToStdOut("Connecting to: " + this.conf.getServiceURL());
			this.httpResponse = httpclient.execute(targetHost, httpreq);
		} catch (Exception e) {
			throw new Exception(String.format("Error connecting to %s", this.conf.getServiceURL()), e);
		}
		
		// handle the output			
		String responseTxt = null;
		HttpEntity responseEntity = this.httpResponse.getEntity();
		if (responseEntity != null) {
			responseTxt = EntityUtils.toString(responseEntity, "UTF-8");		
		}
		
		// Null response for GET or post is Exception
		if(responseTxt == null && (this.conf.method == Methods.GET || this.conf.method == Methods.POST)){ 
			throw new Exception("Received null response text"); 
			
		// Handle other null or empty responses with implementation-specific behavior
		}else if(responseTxt == null || responseTxt.trim().isEmpty()){
			handleEmptyResponse();
		}
		
		if(returnRawResponse){
			// check raw response for HTTP errors, which should be json with status!=200
			try {
				JSONObject responseParsed = (JSONObject) JSONValue.parse(responseTxt);
				if (responseParsed.containsKey("status") && (long)responseParsed.get("status") != 200) {
					throw new Exception(responseTxt);
				}
			} catch (NullPointerException e) {
			} catch (ClassCastException e) {
			} catch (ParseException e) {
			}
			return responseTxt;		// return the raw string
			
		}else{
			// return empty response (legal and common for PUT)
			if (responseTxt == null || responseTxt.isEmpty()) {
				return new JSONObject();
				
			} else {
				// parse response into JSON or throw an error
				JSONObject responseParsed = (JSONObject) JSONValue.parse(responseTxt);
				if (responseParsed == null) {
						throw new Exception(responseTxt);
				}
				return responseParsed;	// return the response parsed into JSON
			}
		}
	}
/**
	 * Make the service call.  
	 * Subclasses may override and return a more useful Object.
	 * @param returnRawResponse True to return raw response.  False to return response parsed into JSON.
	 * @return the raw response string (if returnRawResponse is true), or an Object that can be cast to a JSONObject (if returnRawResponse is false).     
	 */
Make the service call.
Subclasses may override and return a more useful Object.
@param returnRawResponse True to return raw response.  False to return response parsed into JSON.
@return the raw response string (if returnRawResponse is true), or an Object that can be cast to a JSONObject (if returnRawResponse is false).
Make the service call . Subclasses may override and return a more useful Object . @param returnRawResponse True to return raw response . False to return response parsed into JSON . @return the raw response string ( if returnRawResponse is true ) or an Object that can be cast to a JSONObject ( if returnRawResponse is false ) .
public Object execute(boolean returnRawResponse) throws ConnectException, Exception {
		buildParametersJSON();  
		if (conf.method != RestClientConfig.Methods.PUT) {
			if(parametersJSON == null){
				throw new Exception("Service parameters not set");
			}
		} else {
			if(parametersJSON != null && parametersJSON.size() > 0){
				throw new Exception("Service parameters are not implemented for PUT");
			}
		}
		HttpEntity entity = null;
		if (putContent != null) {
			if (conf.method == RestClientConfig.Methods.PUT) {
				entity = new StringEntity(putContent);
			} else {
				throw new Exception("Internal: putContent is provided for non-PUT request: " + conf.method.name());
			}
		} else if (fileParameter != null) {
			FileBody bin = new FileBody(fileParameter);
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.addPart(this.fileParameterName, bin);
			for (Object k : parametersJSON.keySet()) {
				builder.addPart((String) k, new StringBody((String)parametersJSON.get(k)));
			}
			entity = builder.build();
		} else {
			entity = new ByteArrayEntity(parametersJSON.toString().getBytes("UTF-8"));
		}
		HttpRequestBase httpreq = null;
		if (conf.method == RestClientConfig.Methods.GET) {
			HttpGet httpget = new HttpGet(this.conf.getServiceURL());
			httpget.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
			httpreq = httpget;
		} else if (conf.method == RestClientConfig.Methods.PUT) {
			HttpPut httpput = new HttpPut(this.conf.getServiceURL());
			if (entity != null) {
				httpput.setEntity(entity);
			}
			httpreq = httpput;
		} else if (conf.method == RestClientConfig.Methods.DELETE) {
			HttpDelete httpdelete = new HttpDelete(this.conf.getServiceURL());
			httpreq = httpdelete;
		} else {
			HttpPost httppost = new HttpPost(this.conf.getServiceURL());
			httppost.setEntity(entity);
			if (fileParameter == null) {
				httppost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
			}
			httpreq = httppost;
		}
		for (BasicHeader header : this.getDefaultHeaders()) {
			httpreq.addHeader(header);
		}
		HttpHost targetHost = new HttpHost(this.conf.getServiceServer(), this.conf.getServicePort(), this.conf.getServiceProtocol());		
		this.httpResponse = null;
		try {
			HttpClient httpclient = HttpClients.custom().build();
			LocalLogger.logToStdOut("Connecting to: " + this.conf.getServiceURL());
			this.httpResponse = httpclient.execute(targetHost, httpreq);
		} catch (Exception e) {
			throw new Exception(String.format("Error connecting to %s", this.conf.getServiceURL()), e);
		}
		String responseTxt = null;
		HttpEntity responseEntity = this.httpResponse.getEntity();
		if (responseEntity != null) {
			responseTxt = EntityUtils.toString(responseEntity, "UTF-8");		
		}
		if(responseTxt == null && (this.conf.method == Methods.GET || this.conf.method == Methods.POST)){ 
			throw new Exception("Received null response text"); 
		}else if(responseTxt == null || responseTxt.trim().isEmpty()){
			handleEmptyResponse();
		}
		if(returnRawResponse){
			try {
				JSONObject responseParsed = (JSONObject) JSONValue.parse(responseTxt);
				if (responseParsed.containsKey("status") && (long)responseParsed.get("status") != 200) {
					throw new Exception(responseTxt);
				}
			} catch (NullPointerException e) {
			} catch (ClassCastException e) {
			} catch (ParseException e) {
			}
			return responseTxt;		
		}else{
			if (responseTxt == null || responseTxt.isEmpty()) {
				return new JSONObject();
			} else {
				JSONObject responseParsed = (JSONObject) JSONValue.parse(responseTxt);
				if (responseParsed == null) {
						throw new Exception(responseTxt);
				}
				return responseParsed;	
			}
		}
	}
public Object execute ( boolean returnRawResponse ) throws ConnectException , Exception { buildParametersJSON ( ) ; if ( conf . method != RestClientConfig . Methods . PUT ) { if ( parametersJSON == null ) { throw new Exception ( "Service parameters not set" ) ; } } else { if ( parametersJSON != null && parametersJSON . size ( ) > 0 ) { throw new Exception ( "Service parameters are not implemented for PUT" ) ; } } HttpEntity entity = null ; if ( putContent != null ) { if ( conf . method == RestClientConfig . Methods . PUT ) { entity = new StringEntity ( putContent ) ; } else { throw new Exception ( "Internal: putContent is provided for non-PUT request: " + conf . method . name ( ) ) ; } } else if ( fileParameter != null ) { FileBody bin = new FileBody ( fileParameter ) ; MultipartEntityBuilder builder = MultipartEntityBuilder . create ( ) ; builder . addPart ( this . fileParameterName , bin ) ; for ( Object k : parametersJSON . keySet ( ) ) { builder . addPart ( ( String ) k , new StringBody ( ( String ) parametersJSON . get ( k ) ) ) ; } entity = builder . build ( ) ; } else { entity = new ByteArrayEntity ( parametersJSON . toString ( ) . getBytes ( "UTF-8" ) ) ; } HttpRequestBase httpreq = null ; if ( conf . method == RestClientConfig . Methods . GET ) { HttpGet httpget = new HttpGet ( this . conf . getServiceURL ( ) ) ; httpget . setHeader ( HttpHeaders . CONTENT_TYPE , "application/json" ) ; httpreq = httpget ; } else if ( conf . method == RestClientConfig . Methods . PUT ) { HttpPut httpput = new HttpPut ( this . conf . getServiceURL ( ) ) ; if ( entity != null ) { httpput . setEntity ( entity ) ; } httpreq = httpput ; } else if ( conf . method == RestClientConfig . Methods . DELETE ) { HttpDelete httpdelete = new HttpDelete ( this . conf . getServiceURL ( ) ) ; httpreq = httpdelete ; } else { HttpPost httppost = new HttpPost ( this . conf . getServiceURL ( ) ) ; httppost . setEntity ( entity ) ; if ( fileParameter == null ) { httppost . setHeader ( HttpHeaders . CONTENT_TYPE , "application/json" ) ; } httpreq = httppost ; } for ( BasicHeader header : this . getDefaultHeaders ( ) ) { httpreq . addHeader ( header ) ; } HttpHost targetHost = new HttpHost ( this . conf . getServiceServer ( ) , this . conf . getServicePort ( ) , this . conf . getServiceProtocol ( ) ) ; this . httpResponse = null ; try { HttpClient httpclient = HttpClients . custom ( ) . build ( ) ; LocalLogger . logToStdOut ( "Connecting to: " + this . conf . getServiceURL ( ) ) ; this . httpResponse = httpclient . execute ( targetHost , httpreq ) ; } catch ( Exception e ) { throw new Exception ( String . format ( "Error connecting to %s" , this . conf . getServiceURL ( ) ) , e ) ; } String responseTxt = null ; HttpEntity responseEntity = this . httpResponse . getEntity ( ) ; if ( responseEntity != null ) { responseTxt = EntityUtils . toString ( responseEntity , "UTF-8" ) ; } if ( responseTxt == null && ( this . conf . method == Methods . GET || this . conf . method == Methods . POST ) ) { throw new Exception ( "Received null response text" ) ; } else if ( responseTxt == null || responseTxt . trim ( ) . isEmpty ( ) ) { handleEmptyResponse ( ) ; } if ( returnRawResponse ) { try { JSONObject responseParsed = ( JSONObject ) JSONValue . parse ( responseTxt ) ; if ( responseParsed . containsKey ( "status" ) && ( long ) responseParsed . get ( "status" ) != 200 ) { throw new Exception ( responseTxt ) ; } } catch ( NullPointerException e ) { } catch ( ClassCastException e ) { } catch ( ParseException e ) { } return responseTxt ; } else { if ( responseTxt == null || responseTxt . isEmpty ( ) ) { return new JSONObject ( ) ; } else { JSONObject responseParsed = ( JSONObject ) JSONValue . parse ( responseTxt ) ; if ( responseParsed == null ) { throw new Exception ( responseTxt ) ; } return responseParsed ; } } }
Make the service call.
Make the service call .
// set all parameters available upon instantiation // create entity // HttpEntity entity = new ByteArrayEntity(parametersJSON.toJSONString().getBytes("UTF-8")); // perform html encoding in stream	 // js version:  return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/%/g, "&#37;") // add the file // add parametersJSON as StringBody // add just parametersJSON // create a request - GET or POST  // add default headers // execute //HttpClient httpclient = HttpClients.custom().setDefaultHeaders(RestClient.getDefaultHeaders()).build(); // handle the output			 // Null response for GET or post is Exception // Handle other null or empty responses with implementation-specific behavior // check raw response for HTTP errors, which should be json with status!=200 // return the raw string // return empty response (legal and common for PUT) // parse response into JSON or throw an error // return the response parsed into JSON