Last active
December 18, 2015 17:19
-
-
Save jayankandathil/5817953 to your computer and use it in GitHub Desktop.
Sample Java servlet code snippet showing how to use Apache HTTPComponents (tested with httpclient-4.2.3.jar and httpcore-4.2.2.jar) to connect to a remote Adobe Experience Manager (CQ) instance and GET a web page for server health check (heartbeat) purposes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.client.ResponseHandler; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.utils.URIBuilder; | |
import org.apache.http.impl.client.BasicResponseHandler; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.params.BasicHttpParams; | |
import org.apache.http.params.CoreConnectionPNames; | |
import org.apache.http.params.HttpParams; | |
String strHost = "cq-author.company.com"; | |
String cqUser = "admin"; | |
String cqPassword = "admin"; | |
int intPort = 4502; | |
PrintWriter out; | |
DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); | |
// Set the value for the 'User-Agent' HTTP Request Header | |
defaultHttpClient.getParams().setParameter("http.useragent", "Apache HTTPComponents"); | |
// Control connection parameters instead of leaving them to defaults | |
HttpParams httpParams = new BasicHttpParams(); | |
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); // 5 seconds socket connection timeout | |
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10000); // 10 seconds max wait for response from server, after establishing socket connection | |
params.setBooleanParameter(CoreConnectionPNames.SO_KEEPALIVE, false); | |
params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true); | |
defaultHttpClient.setParams(params); | |
// Provide the authentication credentials | |
defaultHttpClient.getCredentialsProvider().setCredentials(new AuthScope(strHost,intPort), new UsernamePasswordCredentials(cqUser, cqPassword)); | |
URIBuilder uriBuilder = new URIBuilder(); | |
// Remember to remove URL encodings in the URL | |
// See http://www.w3schools.com/tags/ref_urlencode.asp | |
uriBuilder.setScheme("http").setHost(strHost).setPort(intPort).setPath("/system/console/jmx/com.adobe.granite:type=QueryStat"); | |
URI uri = null; | |
try | |
{ | |
uri = uriBuilder.build(); | |
} | |
catch (URISyntaxException e) | |
{ | |
System.out.println("Exception building URI [" + uri.toString() + "]"); | |
out = response.getWriter(); | |
e.printStackTrace(out); | |
// No point in continuing... | |
return; | |
} | |
HttpGet httpGet = new HttpGet(uri); | |
System.out.println("Executing HTTP GET request " + httpGet.getURI()); | |
// Create a response handler | |
// http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/BasicResponseHandler.html | |
ResponseHandler<String> responseHandler = new BasicResponseHandler(); | |
// Try connecting to CQ | |
try | |
{ | |
String strResponseBody = defaultHttpClient.execute(httpGet, responseHandler); | |
// Perform other actions with the returned page content in strResponseBody | |
} | |
catch(Exception e) | |
{ | |
out = response.getWriter(); | |
e.printStackTrace(out); | |
} | |
finally // cleanup and reset | |
{ | |
httpGet.releaseConnection(); | |
httpGet.reset(); | |
httpGet = null; | |
responseHandler =null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. I was looking for how to do exactly that.