0

I'm trying to hit a page which contains a xml structure. for that i'm using this code

            @Reference
            private SlingRepository repository;

            adminSession = repository.loginAdministrative( repository.getDefaultWorkspace());
            String pageUrl = "http://localhost:4504"+page+".abc.htm";
            conn = (HttpURLConnection)new URL(pageUrl).openConnection();
            conn.setRequestProperty("Accept-Charset", charset);
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401"); // Do as if you'rusing Firefox 3.6.3
            urlResponse = new BufferedInputStream(conn.getInputStream());
            BufferedReader reader = new BufferedReader( new InputStreamReader(urlResponse) );

While accesing the page i'm getting this issue

org.apache.sling.auth.core.impl.SlingAuthenticator getAnonymousResolver: `Anonymous access not allowed by configuration - requesting credentials`

I'm logged in as an admin and whenever i'm directly hitting this urlfrom browser it is working properly bt while accessing it thriugh my code i'm getting this error.

any suggestion ?

user2142786
  • 1,484
  • 9
  • 41
  • 74
  • 1
    Is the page you are trying to access on the same cq instance ? If so why you are doing a http request at all? You have access to the repository via the [ResourceResolverFactory](https://docs.adobe.com/docs/en/aem/6-0/develop/ref/javadoc/org/apache/sling/api/resource/ResourceResolverFactory.html) or you can use the sling internal request resolution process if you really need to perform – d33t Jun 02 '15 at 12:42

2 Answers2

0

If you are trying to call an url on an author instance, the following method I use in one of my projects might help (using apache commons HttpClient):

private InputStream getContent(final String url)
    HttpClient httpClient = new HttpClient();
    httpClient.getParams().setAuthenticationPreemptive(true);
    httpClient.getState().setCredentials(new AuthScope(null, -1, null),
        new UsernamePasswordCredentials("admin", "admin"));
    try {
        GetMethod get = new GetMethod(url);
        httpClient.executeMethod(get);
        if (get.getStatusCode() == HttpStatus.SC_OK) {
            return get.getResponseBodyAsStream();
        } else {
            LOGGER.error("HTTP Error: ", get.getStatusCode());
        }
    } catch (HttpException e) {
        LOGGER.error("HttpException: ", e);
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    }
}

Though at it is using admin:admin it only works on a local dev instance, if you are on a productive environment, I wouldn't put the admin password in plaintext, even though it is onyl code...

Thomas
  • 6,325
  • 4
  • 30
  • 65
  • thanks Thomas, it works. but if my url is of publish instance and i dont want to pass the crendentials(for publish it doesn't require) then how i can code it . i have to write only httpClient.getState(); or something else ? – user2142786 Jun 02 '15 at 08:21
  • if you are accessing the publish server you don't need to set any credentials at all. So a simplet GetMethod should suffice. So I don't see your problem in the first place. Maybe the page doesn't exist on publish? – Thomas Jun 02 '15 at 11:45
0

You are mixing up sling credentials & http credentials. While you are logged in at the sling-repository the http session is not aware of any authentication informations!

chrysler
  • 448
  • 3
  • 5