private String executeScriptExecuteRequest ( String rScript ) throws IOException { URI uri = getScriptExecutionUri ( ) ; HttpPost httpPost = new HttpPost ( uri ) ; NameValuePair nameValuePair = new BasicNameValuePair ( "x" , rScript ) ; httpPost . setEntity ( new UrlEncodedFormEntity ( singletonList ( nameValuePair ) ) ) ; String openCpuSessionKey ; try ( CloseableHttpResponse response = httpClient . execute ( httpPost ) ) { int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ; if ( statusCode >= 200 && statusCode < 300 ) { Header openCpuSessionKeyHeader = response . getFirstHeader ( "X-ocpu-session" ) ; if ( openCpuSessionKeyHeader == null ) { throw new IOException ( "Missing 'X-ocpu-session' header" ) ; } openCpuSessionKey = openCpuSessionKeyHeader . getValue ( ) ; EntityUtils . consume ( response . getEntity ( ) ) ; } else if ( statusCode == 400 ) { HttpEntity entity = response . getEntity ( ) ; String rErrorMessage = EntityUtils . toString ( entity ) ; EntityUtils . consume ( entity ) ; throw new ScriptException ( rErrorMessage ) ; } else { throw new ClientProtocolException ( format ( FORMAT_UNEXPECTED_RESPONSE_STATUS , statusCode ) ) ; } } return openCpuSessionKey ; }
Execute R script using OpenCPU