-
-
Save saulovenancio/9494350 to your computer and use it in GitHub Desktop.
This file contains 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