You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Zhongmiao Li <ma...@gmail.com> on 2010/11/19 13:39:44 UTC

question about accessing Sling in Java

Hi,     I   have got stuck in a very  strange problem and hope anycould help
me!

The address I use to access Sling is as follows:

http://admin:admin@141.83.80.149:8080/content/nemoclient?sling:authRequestLogin=1

On my own computer ( the computer running Sling, whose Ip Address
is 141.83.80.149), I can connect to Sling properly in Java program(Using
HttpUrlConnection).  However, I can not connect to the server  in my  Java
program on another computer! The error code is 401, Unauthorized.
 But I can upload  and download files on any computer by HTML form. So can
anyone help me to solve this problem?

-- 
Best regards
Li Zhongmiao
Digital Media, College of Computer Science, Zhejiang University

Re: question about accessing Sling in Java

Posted by Zhongmiao Li <ma...@gmail.com>.
Yes, that really helps!  Thanks very much!

On 11/19/10, Bertrand Delacretaz <bd...@apache.org> wrote:
> Hi,
>
> On Fri, Nov 19, 2010 at 6:41 PM, Zhongmiao Li <ma...@gmail.com> wrote:
>> ...now I'm using HttpClient to upload file, but still unsuccessful!  The
>> error
>> code is 415, unsupported media, but the file I want to update is just a
>> JPG!...
>
> See the upload method of the SlingIntegrationTestClient for an example
> upload that is known to work.
>
> The class is in the same package as the HttpTestBase class referred to
> earlier in this thread.
>
> You could actually use those classes as a simple Sling client instead
> of creating your own - they're used in our integration tests so you
> can be sure they will work.
>
> -Bertrand
>


-- 
Best regards
Li Zhongmiao
Digital Media, College of Computer Science, Zhejiang University

Re: question about accessing Sling in Java

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Fri, Nov 19, 2010 at 6:41 PM, Zhongmiao Li <ma...@gmail.com> wrote:
> ...now I'm using HttpClient to upload file, but still unsuccessful!  The error
> code is 415, unsupported media, but the file I want to update is just a JPG!...

See the upload method of the SlingIntegrationTestClient for an example
upload that is known to work.

The class is in the same package as the HttpTestBase class referred to
earlier in this thread.

You could actually use those classes as a simple Sling client instead
of creating your own - they're used in our integration tests so you
can be sure they will work.

-Bertrand

Re: question about accessing Sling in Java

Posted by Zhongmiao Li <ma...@gmail.com>.
Hi Everyone,


now I'm using HttpClient to upload file, but still unsuccessful!  The error
code is 415, unsupported media, but the file I want to update is just a JPG!
Before that, I can use
http://admin:admin@141.83.80.149:8080/content/nemoclient?sling:authRequestLogin=1
to upload files!
So  can someone help me to fix this error? Thanks very much!


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


PutMethod put = new PutMethod(transferURL);
InputStream stream;
 stream = new FileInputStream(localFile);
 RequestEntity entity = new InputStreamRequestEntity(stream);
 put.setRequestEntity(entity);
 put.setDoAuthentication(true);
 HttpClientParams params=new HttpClientParams();


  params.setIntParameter("sling:authRequestLogin", 1);
 httpClient.setParams(params);
  httpClient.executeMethod(put);

On Fri, Nov 19, 2010 at 10:19 PM, Bertrand Delacretaz <
bdelacretaz@apache.org> wrote:

> Hi,
>
> On Fri, Nov 19, 2010 at 3:12 PM, Zhongmiao Li <ma...@gmail.com>
> wrote:
> > Thanks very much!  And can you tell me the specfic code of
> >  registering with  the HttpClient object...
>
> The setUp method of our HttpTestBase class [1] should help.
>
> -Bertrand
>
> [1]
> http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
>



-- 
Best regards
Li Zhongmiao
Digital Media, College of Computer Science, Zhejiang University

Re: question about accessing Sling in Java

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Fri, Nov 19, 2010 at 3:12 PM, Zhongmiao Li <ma...@gmail.com> wrote:
> Thanks very much!  And can you tell me the specfic code of
>  registering with  the HttpClient object...

The setUp method of our HttpTestBase class [1] should help.

-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: question about accessing Sling in Java

Posted by Zhongmiao Li <ma...@gmail.com>.
Thanks very much!  And can you tell me the specfic code of
 registering with  the HttpClient object
?
I just can not find the way to do this.....  Thanks!

On Fri, Nov 19, 2010 at 5:24 AM, Felix Meschberger <fm...@gmail.com>wrote:

> Hi,
>
> If you are connecting using Java you have to provide the HTTP Basic
> authentication header containing your credentials. We generally use the
> Apache HttpComponents (or HttpClient) libraries to to HTTP connections.
> See also the Integration tests, where we are leveraging HttpClient to
> fire requests at Sling.
>
> In HttpClient (I assume HttpComponents is similar) you would create a
> Credentials object with the user name and password and registering with
> the HttpClient object so that it is transparently used as requested by
> the server.
>
> Using the user name and password in the URL does not work.
>
> Hope this helps.
>
> Regards
> Felix
>
>
> Am Freitag, den 19.11.2010, 04:39 -0800 schrieb Zhongmiao Li:
> > Hi,     I   have got stuck in a very  strange problem and hope anycould
> help
> > me!
> >
> > The address I use to access Sling is as follows:
> >
> >
> http://admin:admin@141.83.80.149:8080/content/nemoclient?sling:authRequestLogin=1
> >
> > On my own computer ( the computer running Sling, whose Ip Address
> > is 141.83.80.149), I can connect to Sling properly in Java program(Using
> > HttpUrlConnection).  However, I can not connect to the server  in my
>  Java
> > program on another computer! The error code is 401, Unauthorized.
> >  But I can upload  and download files on any computer by HTML form. So
> can
> > anyone help me to solve this problem?
> >
>
>
>


-- 
Best regards
Li Zhongmiao
Digital Media, College of Computer Science, Zhejiang University

Re: question about accessing Sling in Java

Posted by Felix Meschberger <fm...@gmail.com>.
Hi,

If you are connecting using Java you have to provide the HTTP Basic
authentication header containing your credentials. We generally use the
Apache HttpComponents (or HttpClient) libraries to to HTTP connections.
See also the Integration tests, where we are leveraging HttpClient to
fire requests at Sling.

In HttpClient (I assume HttpComponents is similar) you would create a
Credentials object with the user name and password and registering with
the HttpClient object so that it is transparently used as requested by
the server.

Using the user name and password in the URL does not work.

Hope this helps.

Regards
Felix


Am Freitag, den 19.11.2010, 04:39 -0800 schrieb Zhongmiao Li: 
> Hi,     I   have got stuck in a very  strange problem and hope anycould help
> me!
> 
> The address I use to access Sling is as follows:
> 
> http://admin:admin@141.83.80.149:8080/content/nemoclient?sling:authRequestLogin=1
> 
> On my own computer ( the computer running Sling, whose Ip Address
> is 141.83.80.149), I can connect to Sling properly in Java program(Using
> HttpUrlConnection).  However, I can not connect to the server  in my  Java
> program on another computer! The error code is 401, Unauthorized.
>  But I can upload  and download files on any computer by HTML form. So can
> anyone help me to solve this problem?
>