how to get response from Author instance through a standalone code | Community
Skip to main content
October 16, 2015
Solved

how to get response from Author instance through a standalone code

  • October 16, 2015
  • 6 replies
  • 2125 views

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.
Best answer by Sam205505050

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

Sam205505050Accepted solution
Level 6
October 16, 2015

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 );
Community Advisor
October 16, 2015

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);
smacdonald2008
Level 10
October 16, 2015

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. 

joerghoh
Adobe Employee
Adobe Employee
October 16, 2015

By default Sling supports basic authentication.

Kind regards,
Jörg

Adobe Employee
October 16, 2015

Hi,

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

Justin

Level 6
October 16, 2015

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