You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Günther Schmidt <gu...@web.de> on 2012/05/08 19:54:13 UTC
Trying to post via HttpClient
Hello,
I'm trying to create a directory using Apaches HTTP components HTTPClient.
I've tried for a couple of days now to figure out how authentication is
meant to be used but keep getting Access Denied Errors.
Here's my code:
package de.kmmd.jcr;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
public class ClientTest {
public static void main(String[] args) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:8080/testDir");
MultipartEntity mp = new
MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
httpClient.getCredentialsProvider().setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("admin", "admin"));
try {
mp.addPart("jcr:primaryType", new StringBody("nt:folder"));
httpPost.setEntity(mp);
httpClient.execute(httpPost, new BasicResponseHandler());
} catch (Exception e) {
e.printStackTrace();
}
}
}
The apache sling's code base is so complex that I was unable so far to
figure out how to use the test cases. In some of the test scenarios
DefaultHttpClient with authentication is also used but I'm unable to
follow the code so that I could use it as a guide.
Günther
Re: Trying to post via HttpClient - SOLVED
Posted by Günther Schmidt <gu...@web.de>.
Hi,
as it turns out HTTPPosts to Apache Sling require preemptive
authentication, which the HttpClients 4x (Apache Http Components) do not
do or provide out of the box.
You can have a look here:
http://mail-archives.apache.org/mod_mbox/sling-users/201005.mbox/%3CAANLkTilM7xCnjubIfU-QsuRpINR-Kt8AAyLpz6c3trDZ@mail.gmail.com%3E
which points out the problem
and here for the workaround
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html#d5e1031
I applied this solution and it now works.
Günther
Re: Trying to post via HttpClient
Posted by Günther Schmidt <gu...@web.de>.
Hi Betrand,
thanks, that helps a lot.
Something else, if I may. From your reply I gather you're involved in
Sling development? It's just that I have seen some code that is a bit
unusual for Java, ie. methods that "return this", and callers that make
ample use of it. I've seen this quit a lot in Smalltalk code, do some of
the Sling developers have a dark and secret Smalltalk past?
Günther
Am 08.05.12 22:16, schrieb Bertrand Delacretaz:
> Hi,
>
> On Tue, May 8, 2012 at 10:54 AM, Günther Schmidt<gu...@web.de> wrote:
>> ...I'm trying to create a directory using Apaches HTTP components HTTPClient.
>>
>> I've tried for a couple of days now to figure out how authentication is
>> meant to be used but keep getting Access Denied Errors....
> Our HttpTestBase [1] class includes code to setup httpclient 3.x to
> use credentials, you could use that as an example. I haven't looked in
> detail but you might just be missing the setAuthenticationPreemptive
> option.
>
> Note also that the testing tools module at [2] includes a simplified
> fluent interface to HTTPClient (4.x) which can be useful for such
> things. See [3] for usage examples.
>
> -Bertrand
>
> [1] http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
>
> [2] https://svn.apache.org/repos/asf/sling/trunk/testing/tools/
>
> [3] https://svn.apache.org/repos/asf/sling/trunk/testing/samples/integration-tests/
Re: Trying to post via HttpClient
Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,
On Tue, May 8, 2012 at 10:54 AM, Günther Schmidt <gu...@web.de> wrote:
> ...I'm trying to create a directory using Apaches HTTP components HTTPClient.
>
> I've tried for a couple of days now to figure out how authentication is
> meant to be used but keep getting Access Denied Errors....
Our HttpTestBase [1] class includes code to setup httpclient 3.x to
use credentials, you could use that as an example. I haven't looked in
detail but you might just be missing the setAuthenticationPreemptive
option.
Note also that the testing tools module at [2] includes a simplified
fluent interface to HTTPClient (4.x) which can be useful for such
things. See [3] for usage examples.
-Bertrand
[1] http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
[2] https://svn.apache.org/repos/asf/sling/trunk/testing/tools/
[3] https://svn.apache.org/repos/asf/sling/trunk/testing/samples/integration-tests/
Re: Trying to post via HttpClient
Posted by Günther Schmidt <gu...@web.de>.
Hello Sarwar,
Am 08.05.12 22:04, schrieb Sarwar Bhuiyan:
> Have you had a look at this:
>
> http://sling.apache.org/site/discover-sling-in-15-minutes.html
>
> Might give you some ideas. I believe if you just want to create a folder,
> you can just POST to the url with the folder name at the end (shouldn't
> require that multipartentity). Try it out with curl first.
>
> Just curious, are you writing a standalone java app that communicates with
> sling via HTTP?
I am, eventually it will be a NetBeans based one, and I'd prefer to be
able to use the REST api.
HTTPClient from apache commons seems to be obsolete and I'm thus using
the apache httpcomponents one.
>
>
> Sarwar
>
>
>
>
>
>
>
> On Tue, May 8, 2012 at 6:54 PM, Günther Schmidt<gu...@web.de> wrote:
>
>> Hello,
>>
>> I'm trying to create a directory using Apaches HTTP components HTTPClient.
>>
>> I've tried for a couple of days now to figure out how authentication is
>> meant to be used but keep getting Access Denied Errors.
>>
>> Here's my code:
>>
>> package de.kmmd.jcr;
>>
>> import org.apache.http.auth.**AuthScope;
>> import org.apache.http.auth.**UsernamePasswordCredentials;
>> import org.apache.http.client.**methods.HttpPost;
>> import org.apache.http.entity.mime.**HttpMultipartMode;
>> import org.apache.http.entity.mime.**MultipartEntity;
>> import org.apache.http.entity.mime.**content.StringBody;
>> import org.apache.http.impl.client.**BasicResponseHandler;
>> import org.apache.http.impl.client.**DefaultHttpClient;
>>
>> public class ClientTest {
>>
>>
>> public static void main(String[] args) {
>>
>> DefaultHttpClient httpClient = new DefaultHttpClient();
>> HttpPost httpPost = new HttpPost("http://localhost:**8080/testDir<http://localhost:8080/testDir>
>> ");
>> MultipartEntity mp = new MultipartEntity(**
>> HttpMultipartMode.BROWSER_**COMPATIBLE);
>>
>> httpClient.**getCredentialsProvider().**setCredentials(
>> AuthScope.ANY,
>> new UsernamePasswordCredentials("**admin", "admin"));
>>
>> try {
>> mp.addPart("jcr:primaryType", new StringBody("nt:folder"));
>> httpPost.setEntity(mp);
>> httpClient.execute(httpPost, new BasicResponseHandler());
>> } catch (Exception e) {
>> e.printStackTrace();
>> }
>> }
>>
>> }
>>
>> The apache sling's code base is so complex that I was unable so far to
>> figure out how to use the test cases. In some of the test scenarios
>> DefaultHttpClient with authentication is also used but I'm unable to follow
>> the code so that I could use it as a guide.
>>
>> Günther
>>
Re: Trying to post via HttpClient
Posted by Sarwar Bhuiyan <sa...@gmail.com>.
Have you had a look at this:
http://sling.apache.org/site/discover-sling-in-15-minutes.html
Might give you some ideas. I believe if you just want to create a folder,
you can just POST to the url with the folder name at the end (shouldn't
require that multipartentity). Try it out with curl first.
Just curious, are you writing a standalone java app that communicates with
sling via HTTP?
Sarwar
On Tue, May 8, 2012 at 6:54 PM, Günther Schmidt <gu...@web.de> wrote:
> Hello,
>
> I'm trying to create a directory using Apaches HTTP components HTTPClient.
>
> I've tried for a couple of days now to figure out how authentication is
> meant to be used but keep getting Access Denied Errors.
>
> Here's my code:
>
> package de.kmmd.jcr;
>
> import org.apache.http.auth.**AuthScope;
> import org.apache.http.auth.**UsernamePasswordCredentials;
> import org.apache.http.client.**methods.HttpPost;
> import org.apache.http.entity.mime.**HttpMultipartMode;
> import org.apache.http.entity.mime.**MultipartEntity;
> import org.apache.http.entity.mime.**content.StringBody;
> import org.apache.http.impl.client.**BasicResponseHandler;
> import org.apache.http.impl.client.**DefaultHttpClient;
>
> public class ClientTest {
>
>
> public static void main(String[] args) {
>
> DefaultHttpClient httpClient = new DefaultHttpClient();
> HttpPost httpPost = new HttpPost("http://localhost:**8080/testDir<http://localhost:8080/testDir>
> ");
> MultipartEntity mp = new MultipartEntity(**
> HttpMultipartMode.BROWSER_**COMPATIBLE);
>
> httpClient.**getCredentialsProvider().**setCredentials(
> AuthScope.ANY,
> new UsernamePasswordCredentials("**admin", "admin"));
>
> try {
> mp.addPart("jcr:primaryType", new StringBody("nt:folder"));
> httpPost.setEntity(mp);
> httpClient.execute(httpPost, new BasicResponseHandler());
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> }
>
> The apache sling's code base is so complex that I was unable so far to
> figure out how to use the test cases. In some of the test scenarios
> DefaultHttpClient with authentication is also used but I'm unable to follow
> the code so that I could use it as a guide.
>
> Günther
>