0

I am trying to hit URL in cq Author instance from my standalone code. The URL looks like — http://<somehost>:<someport>//libs/dam/gui/content/reports/export.json

Below is the code:

URL url = new URL(newPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");
connection.setReadTimeout(15 * 10000);
connection.connect();

reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

But I got a 401 error, which is expected, as I'm not passing any authentication information — hence Sling says:

getAnonymousResolver: Anonymous access not allowed by configuration - requesting credentials.

How can I get resolve this?

anotherdave
  • 6,656
  • 4
  • 34
  • 65
sudha
  • 11
  • 1

1 Answers1

2

You may use Basic HTTP authentication. Adding it to the HttpURLConnection is little awkward:

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("admin", "admin".toCharArray());
    }
});

Consider using Apache HttpClient:

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
DefaultHttpClient authorizedClient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(url);
request.addHeader(new BasicScheme().authenticate(creds, request));
HttpResponse response = authorizedClient.execute(request);
InputStream stream = response.getEntity().getContent();
Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43