You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Tony Giaccone <to...@giaccone.org> on 2010/05/12 03:56:48 UTC

missing something..


I'm trying to do in java the example given in the docs using Curl. 


	String repoURL = "http://admin:admin@localhost:8080/sling/content/myNode";

	    PostMethod post = new PostMethod(repoURL);
	    NameValuePair[] formData = { 
	            new NameValuePair("sling:resourceType", "foo/bar"), 
	            new NameValuePair("title", "Another Title")
	           };
	    post.setRequestBody(formData); 
		HttpClient client = new HttpClient();
		String response = null;
		int status;
		try {
			status = client.executeMethod( post );
			response = post.getResponseBodyAsString();
			System.out.println(response);
		} catch (Exception  e) {
			e.printStackTrace();
			retValue = false;
		} finally {
			post.releaseConnection();
		}


I'm getting back a response code of 500

with:
        <tbody>
            <tr>
                <td>Status</td>
                <td><div id="Status">500</div></td>
            </tr>
            <tr>
                <td>Message</td>
                <td><div id="Message">javax.jcr.AccessDeniedException: /content/myNode/sling:resourceType: not allowed to add or modify item</div></td>
            </tr>
            <tr>
                <td>Location</td>
                <td><a href="/sling/myNode" id="Location">/sling/myNode</a></td>
            </tr>


what am I misssing?


Tony


Re: missing something..

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Wed, May 12, 2010 at 12:41 PM, Alexander Klimetschek
<ak...@day.com> wrote:
> ...Note sure, but I think commons-httpclient doesn't use credentials
> given in the url like "admin:admin". You have to use a Credentials
> object.
>
> Or maybe it is not doing a preemptive authentication (which is
> required for a POST to sling for the out-of-the-box http basic auth
> handler in Sling, afaik)....

Might be the problem(s) indeed, to check that I would compare the HTTP
traffic between a curl example that works and your java code.

See also [1] for how the HttpClient is setup for the Sling integration tests:

  httpClient = new HttpClient();
  httpClient.getParams().setAuthenticationPreemptive(true);
  Credentials defaultcreds = new UsernamePasswordCredentials("admin", "admin");
  httpClient.getState().setCredentials(new AuthScope(url.getHost(),
url.getPort(), AuthScope.ANY_REALM), defaultcreds);

-Bertrand

[1] http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java

Re: missing something..

Posted by Alexander Klimetschek <ak...@day.com>.
On Wed, May 12, 2010 at 03:56, Tony Giaccone <to...@giaccone.org> wrote:
>        String repoURL = "http://admin:admin@localhost:8080/sling/content/myNode";
>
>            PostMethod post = new PostMethod(repoURL);
> ...
>                        status = client.executeMethod( post );
> ...
> javax.jcr.AccessDeniedException: /content/myNode/sling:resourceType: not allowed to add or modify item


Note sure, but I think commons-httpclient doesn't use credentials
given in the url like "admin:admin". You have to use a Credentials
object.

Or maybe it is not doing a preemptive authentication (which is
required for a POST to sling for the out-of-the-box http basic auth
handler in Sling, afaik).

See here: http://hc.apache.org/httpclient-3.x/authentication.html

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com