private void makePostCall ( URL alfrescoTomcatUrl , List < NameValuePair > postData , String operation ) { CloseableHttpClient client = null ; CloseableHttpResponse response = null ; try { // Set up a HTTP POST request to the Alfresco Webapp we are targeting HttpHost targetHost = new HttpHost ( alfrescoTomcatUrl . getHost ( ) , alfrescoTomcatUrl . getPort ( ) , alfrescoTomcatUrl . getProtocol ( ) ) ; // Set up authentication parameters CredentialsProvider credsProvider = new BasicCredentialsProvider ( ) ; credsProvider . setCredentials ( new AuthScope ( targetHost . getHostName ( ) , targetHost . getPort ( ) ) , new UsernamePasswordCredentials ( refreshUsername , refreshPassword ) ) ; // Create the HTTP Client we will use to make the call client = HttpClients . custom ( ) . setDefaultCredentialsProvider ( credsProvider ) . build ( ) ; // Create AuthCache instance AuthCache authCache = new BasicAuthCache ( ) ; // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme ( ) ; authCache . put ( targetHost , basicAuth ) ; // Add AuthCache to the execution context HttpClientContext localContext = HttpClientContext . create ( ) ; localContext . setAuthCache ( authCache ) ; // Make the call to Refresh the Alfresco Webapp HttpPost httpPost = new HttpPost ( alfrescoTomcatUrl . toURI ( ) ) ; response = client . execute ( httpPost ) ; if ( postData != null ) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity ( postData , "UTF-8" ) ; httpPost . setEntity ( entity ) ; } httpPost . setHeader ( "Accept-Charset" , "iso-8859-1,utf-8" ) ; httpPost . setHeader ( "Accept-Language" , "en-us" ) ; response = client . execute ( httpPost ) ; // If no response, no method has been passed if ( response == null ) { getLog ( ) . error ( "POST request failed to " + alfrescoTomcatUrl . toString ( ) + ", " + getAbortedMsg ( ) ) ; return ; } // Check if we got a successful response or not int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ; if ( statusCode == HttpStatus . SC_OK ) { getLog ( ) . info ( "Successfull " + operation + " for " + refreshWebappName ) ; } else { String reasonPhrase = response . getStatusLine ( ) . getReasonPhrase ( ) ; getLog ( ) . warn ( "Failed to " + operation + " for " + refreshWebappName + ". Response status: " + statusCode + ", message: " + reasonPhrase ) ; } } catch ( Exception ex ) { getLog ( ) . error ( "POST request failed to " + alfrescoTomcatUrl . toString ( ) + ", " + getAbortedMsg ( ) ) ; getLog ( ) . error ( "Exception Msg: " + ex . getMessage ( ) ) ; } finally { closeQuietly ( response ) ; closeQuietly ( client ) ; } }
Helper method to make a POST request to the Alfresco Webapp