Skip to content

Instantly share code, notes, and snippets.

@dfparker2002
Forked from justinedelson/Test.java
Created February 12, 2016 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfparker2002/a13a1c34c368261003cf to your computer and use it in GitHub Desktop.
Save dfparker2002/a13a1c34c368261003cf to your computer and use it in GitHub Desktop.
How to make a http request to get the auth token in AEM
import java.io.IOException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class Test {
private static final String PROTOCOL = "http";
private static final int PORT = 4503;
private static final String HOST = "localhost";
private static final String COOKIE_NAME = "login-token";
public static void main(String[] args) throws Exception {
String username = "admin";
String password = "admin";
String specialPath = "/home/users/a/admin.json";
HttpClient client = new HttpClient();
String token = getToken(username, password, client);
if (token == null) {
System.err.println("No login cookie set.");
return;
}
System.out.println("token = " + token);
GetMethod get = new GetMethod(String.format("%s://%s:%s%s", PROTOCOL, HOST, PORT, specialPath));
get.addRequestHeader("Cookie", String.format("%s=%s", COOKIE_NAME, token));
int status = client.executeMethod(get);
if (status == 200) {
System.out.println(get.getResponseBodyAsString());
} else {
System.err
.println("Unexcepted response code " + status + "; msg: " + get.getResponseBodyAsString());
}
}
private static String getToken(String username, String password, HttpClient client) throws IOException,
HttpException {
String token = null;
PostMethod authRequest = new PostMethod(String.format("%s://%s:%s/j_security_check", PROTOCOL, HOST, PORT));
authRequest.setParameter("j_username", username);
authRequest.setParameter("j_password", password);
authRequest.setParameter("j_validate", "true");
int status = client.executeMethod(authRequest);
if (status == 200) {
Header[] headers = authRequest.getResponseHeaders("Set-Cookie");
for (Header header : headers) {
String value = header.getValue();
if (value.startsWith(COOKIE_NAME + "=")) {
int endIdx = value.indexOf(';');
if (endIdx > 0) {
token = value.substring(COOKIE_NAME.length() + 1, endIdx);
}
}
}
} else {
System.err
.println("Unexcepted response code " + status + "; msg: " + authRequest.getResponseBodyAsString());
}
return token;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment