Skip to main content
Oct 16, 2015 2:26 AM UTC
Solved

how to get response from Author instance through a standalone code

I am trying to hit URL in cq Author instance from my standalone code. 
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()));
            stringBuilder = new StringBuilder();

But I got error 401 which is expected as I'm not passing any authentication information. hence Sling says that "getAnonymousResolver: Anonymous access not allowed by configuration - requesting credentials"

Please advice how to proceed on this.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Sam205505050Accepted solution
Level 6
Oct 16, 2015 2:26 AM UTC

Add the below code - 

  String authString = "admin" + ":" + "admin";
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty  ("Authorization", "Basic " + authStringEnc );

6 replies

Level 6
Oct 16, 2015 2:26 AM UTC

justin_at_adobe wrote...

Hi,

This works, but it would be significantly easier to user a higher-level HTTP client API which supports basic authentication.

Justin

 

Thanks Justin, will keep eye on it

Translate
Adobe Employee
Oct 16, 2015 2:26 AM UTC

Hi,

This works, but it would be significantly easier to user a higher-level HTTP client API which supports basic authentication.

Justin

Translate
joerghoh
Adobe Employee
Adobe Employee
Oct 16, 2015 2:26 AM UTC

By default Sling supports basic authentication.

Kind regards,
Jörg

Translate
smacdonald2008
Level 10
Oct 16, 2015 2:26 AM UTC

You can also code a Sling Servlet so that authentication is not required by using a Property annotation.

@Property(name = "sling.auth.requirements", value = "-/bin/upfile")

http://helpx.adobe.com/experience-manager/using/post_files.html

Note this this article:

Although this development shows you how to configure a Sling Servlet so that authentication is not required, it is strongly recommended that you use this only when required. There are some use cases for this. For example, when you need to create a Sling Servlet that collects data from anonymous users. In most use cases, it is strongly recommended that you code your Sling Servlets so that authentication is required. 

Translate
Community Advisor
Oct 16, 2015 2:26 AM UTC

As per the slingAuthentication logic it doesn't allow aonymous login, try setting username and password.

https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.auth.core-1.1.0/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java // If we get here, anonymous access is not allowed: redirect // to the login servlet log.info("getAnonymousResolver: Anonymous access not allowed by configuration - requesting credentials"); doLogin(request, response);
Translate
Sam205505050Accepted solution
Level 6
Oct 16, 2015 2:26 AM UTC

Add the below code - 

  String authString = "admin" + ":" + "admin";
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty  ("Authorization", "Basic " + authStringEnc );
Translate