Re: [xwiki-users] xWiki RESTful/HTTP Connection

2016-04-19 Thread Thomas Mortagne
That's because what Mohamed is referring is the old (3.x) HttpClient
and not the new (4.x) one which have different (sometime very
different) API.

The main issue you have is probably the fact that XWiki does not ask
the client for credential by default since anonymous access is valid.
That means you need to force preemptive authentication which is very
easy in 3.x (just a call to setAuthenticationPreemptive like in
Mohamed's example) and quite harder in 4.x (they find dangerous to
support it like they used to...).

You can find various examples on Google and the official documentation
is on 
https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
(section "4.6. Preemptive authentication").

On Mon, Apr 18, 2016 at 3:05 PM, Tobi  wrote:
> For some reasons it gives me this
>
> Exception in thread "main" java.lang.NoClassDefFoundError:
> org/apache/commons/logging/LogFactory
> at 
> org.apache.commons.httpclient.HttpClient.(HttpClient.java:65)
> at RestTest2.getClient(RestTest2.java:16)
> at RestTest2.main(RestTest2.java:27)
> Caused by: java.lang.ClassNotFoundException:
> org.apache.commons.logging.LogFactory
> at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
> ... 3 more
>
> Code:
>
> import java.io.IOException;
>
> import org.apache.commons.httpclient.HttpClient;
> import org.apache.commons.httpclient.methods.GetMethod;
> import org.apache.commons.httpclient.methods.PutMethod;
> import org.xwiki.rest.model.jaxb.ObjectFactory;
> import org.xwiki.rest.model.jaxb.Page;
> import org.apache.commons.httpclient.methods.PostMethod;
> import org.apache.commons.httpclient.UsernamePasswordCredentials;
> import org.apache.commons.httpclient.auth.AuthScope;
> import org.apache.commons.httpclient.NameValuePair;
>
> public class RestTest2 {
>
>  static HttpClient getClient(String host, int port, String username, 
> String
> password) {
>   HttpClient httpClient = new HttpClient();
>   httpClient.getParams().setAuthenticationPreemptive(true);
>   httpClient.getState().setCredentials(
>  new AuthScope(host, port, AuthScope.ANY_REALM),
>  new UsernamePasswordCredentials(username, password)
>   );
>   return httpClient;
>}
>
>public static void main(String[] args) throws IOException {
>  // Get request example
>   HttpClient httpClient = getClient("xwiki.aircr.ru", 8080, "Tobi",
> "gaben1337");
>   GetMethod getMethod = new
> GetMethod("http://xwiki.aircr.ru/xwiki/bin/view/Main/";);
>   getMethod.addRequestHeader("Accept", "application/json");
>   getMethod.setDoAuthentication( true );
>   String getURL = "http://xwiki.aircr.ru/xwiki/bin/view/Main/";;
>   try {
>   int status = httpClient.executeMethod(getMethod);
>  System.out.println(getMethod.getResponseBodyAsString());
>} catch (Exception e) {
>  //...
>   } finally {
>   // release any connection resources used by the method
>   getMethod.releaseConnection();
>}
>
>   // Put request example
>   String putURL = "http://xwiki.aircr.ru/xwiki/bin/view/Main/";;
>   PutMethod putMethod = new PutMethod(putURL);
> putMethod.addRequestHeader("Accept", "application/json");
> putMethod.setDoAuthentication( true );
> try {
>int status = httpClient.executeMethod(putMethod);
>   System.out.println(putMethod.getResponseBodyAsString());
> } catch (Exception e) {
>// ...
>   } finally {
>// release any connection resources used by the method
>   putMethod.releaseConnection();
> }
>   }
>
> }
>
>
>
> --
> View this message in context: 
> http://xwiki.475771.n2.nabble.com/xWiki-RESTful-HTTP-Connection-tp7599026p7599035.html
> Sent from the XWiki- Users mailing list archive at Nabble.com.
> ___
> users mailing list
> users@xwiki.org
> http://lists.xwiki.org/mailman/listinfo/users



-- 
Thomas Mortagne
___
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users


Re: [xwiki-users] xWiki RESTful/HTTP Connection

2016-04-18 Thread Tobi
For some reasons it gives me this

Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.(HttpClient.java:65)
at RestTest2.getClient(RestTest2.java:16)
at RestTest2.main(RestTest2.java:27)
Caused by: java.lang.ClassNotFoundException:
org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more

Code:

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.methods.GetMethod; 
import org.apache.commons.httpclient.methods.PutMethod;
import org.xwiki.rest.model.jaxb.ObjectFactory;
import org.xwiki.rest.model.jaxb.Page;
import org.apache.commons.httpclient.methods.PostMethod; 
import org.apache.commons.httpclient.UsernamePasswordCredentials; 
import org.apache.commons.httpclient.auth.AuthScope; 
import org.apache.commons.httpclient.NameValuePair; 

public class RestTest2 {

 static HttpClient getClient(String host, int port, String username, 
String
password) { 
  HttpClient httpClient = new HttpClient(); 
  httpClient.getParams().setAuthenticationPreemptive(true); 
  httpClient.getState().setCredentials( 
 new AuthScope(host, port, AuthScope.ANY_REALM), 
 new UsernamePasswordCredentials(username, password) 
  ); 
  return httpClient; 
   } 

   public static void main(String[] args) throws IOException { 
 // Get request example 
  HttpClient httpClient = getClient("xwiki.aircr.ru", 8080, "Tobi", 
"gaben1337"); 
  GetMethod getMethod = new
GetMethod("http://xwiki.aircr.ru/xwiki/bin/view/Main/";); 
  getMethod.addRequestHeader("Accept", "application/json"); 
  getMethod.setDoAuthentication( true ); 
  String getURL = "http://xwiki.aircr.ru/xwiki/bin/view/Main/";; 
  try { 
  int status = httpClient.executeMethod(getMethod); 
 System.out.println(getMethod.getResponseBodyAsString()); 
   } catch (Exception e) { 
 //... 
  } finally { 
  // release any connection resources used by the method 
  getMethod.releaseConnection(); 
   } 

  // Put request example 
  String putURL = "http://xwiki.aircr.ru/xwiki/bin/view/Main/";; 
  PutMethod putMethod = new PutMethod(putURL); 
putMethod.addRequestHeader("Accept", "application/json"); 
putMethod.setDoAuthentication( true ); 
try { 
   int status = httpClient.executeMethod(putMethod); 
  System.out.println(putMethod.getResponseBodyAsString()); 
} catch (Exception e) { 
   // ... 
  } finally { 
   // release any connection resources used by the method 
  putMethod.releaseConnection(); 
} 
  } 

}



--
View this message in context: 
http://xwiki.475771.n2.nabble.com/xWiki-RESTful-HTTP-Connection-tp7599026p7599035.html
Sent from the XWiki- Users mailing list archive at Nabble.com.
___
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users


Re: [xwiki-users] xWiki RESTful/HTTP Connection

2016-04-18 Thread Mohamed Boussaa
Hi,

I forget to add the packages to import in my previous code.

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.NameValuePair;


Find the API of the PutMethod here:
https://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/PutMethod.html



Regards,
Mohamed.




On Sun, Apr 17, 2016 at 10:02 PM, Tobi  wrote:

> Thank you for your response!
>
> As far as I know there is no PutMethod (which you use in your code) class
> in
> new versions of Apache's HTTP library. Same goes for many other methods
> which.
>
> Or maybe I'm just missing the point and there IS a library with required
> Classes and Methods of which I know nothing about (if it's true then I
> would
> be really grateful if your provide me a link).
>
>
>
> --
> View this message in context:
> http://xwiki.475771.n2.nabble.com/xWiki-RESTful-HTTP-Connection-tp7599026p7599028.html
> Sent from the XWiki- Users mailing list archive at Nabble.com.
> ___
> users mailing list
> users@xwiki.org
> http://lists.xwiki.org/mailman/listinfo/users
>
___
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users


Re: [xwiki-users] xWiki RESTful/HTTP Connection

2016-04-17 Thread Tobi
Thank you for your response!

As far as I know there is no PutMethod (which you use in your code) class in
new versions of Apache's HTTP library. Same goes for many other methods
which.

Or maybe I'm just missing the point and there IS a library with required
Classes and Methods of which I know nothing about (if it's true then I would
be really grateful if your provide me a link).



--
View this message in context: 
http://xwiki.475771.n2.nabble.com/xWiki-RESTful-HTTP-Connection-tp7599026p7599028.html
Sent from the XWiki- Users mailing list archive at Nabble.com.
___
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users


Re: [xwiki-users] xWiki RESTful/HTTP Connection

2016-04-17 Thread Mohamed Boussaa
Hi,

You can use the new version of Apache's HTTP library, as follows, hope
it'll help you.

public class RestTest {
   /**
   * Create and initialize the http client.
   *
   * @return an HttpClient object
   */
   HttpClient getClient(host, port, username, password) {
  HttpClient httpClient = new HttpClient();
  httpClient.getParams().setAuthenticationPreemptive(true);
  httpClient.getState().setCredentials(
 new AuthScope(host, port, AuthScope.ANY_REALM),
 new UsernamePasswordCredentials(username, password)
  );
  return httpClient;
   }

   public static void main(String[] args) throws IOException {
 // Get request example
  HttpClient httpClient = getClient(yourHost, yourPort, yourUsername,
yourPassword);
  GetMethod getMethod = new GetMethod(getURL);
  getMethod.addRequestHeader("Accept", "application/json");
  getMethod.setDoAuthentication( true );
  String getURL = "your get url";
  try {
  int status = httpClient.executeMethod(getMethod);
 System.out.println(getMethod.getResponseBodyAsString());
   } catch (Exception e) {
 //...
  } finally {
  // release any connection resources used by the method
  getMethod.releaseConnection();
   }

  // Put request example
  String putURL = "your put url";
  PutMethod putMethod = new PutMethod(putURL);
putMethod.addRequestHeader("Accept", "application/json");
putMethod.setDoAuthentication( true );
try {
   int status = httpClient.executeMethod(putMethod);
  System.out.println(putMethod.getResponseBodyAsString());
} catch (Exception e) {
   // ...
  } finally {
   // release any connection resources used by the method
  putMethod.releaseConnection();
}
  }
}




Best regards,
Mohamed.

On Sun, Apr 17, 2016 at 9:24 PM, Mohamed Boussaa 
wrote:

> Hi,
>
> You can use the new version of Apache's HTTP library, as follows, hope
> it'll help you.
>
> public class RestTest {
>/**
>* Create and initialize the http client.
>*
>* @return an HttpClient object
>*/
>HttpClient getClient(host, port, username, password) {
>   HttpClient httpClient = new HttpClient();
>   httpClient.getParams().setAuthenticationPreemptive(true);
>   httpClient.getState().setCredentials(
>  new AuthScope(host, port, AuthScope.ANY_REALM),
>  new UsernamePasswordCredentials(username, password)
>   );
>   return httpClient;
>}
>
>public static void main(String[] args) throws IOException {
>  // Get request example
>   HttpClient httpClient = getClient(yourHost, yourPort, yourUsername,
> yourPassword);
>   GetMethod getMethod = new GetMethod(getURL);
>   getMethod.addRequestHeader("Accept", "application/json");
>   getMethod.setDoAuthentication( true );
>   String getURL = "your get url";
>   try {
>   int status = httpClient.executeMethod(getMethod);
>  System.out.println(getMethod.getResponseBodyAsString());
>} catch (Exception e) {
> //...
>   } finally {
>   // release any connection resources used by the method
>   getMethod.releaseConnection();
>}
>
>   // Put request example
>   String putURL = "your put url";
>   PutMethod putMethod = new PutMethod(putURL);
> putMethod.addRequestHeader("Accept", "application/json");
> putMethod.setDoAuthentication( true );
> try {
>int status = httpClient.executeMethod(putMethod);
>   System.out.println(putMethod.getResponseBodyAsString());
> } catch (Exception e) {
>// ...
>   } finally {
>// release any connection resources used by the method
>   putMethod.releaseConnection();
> }
>   }
> }
>
>
>
>
> Best regards,
> Mohamed.
>
> On Sun, Apr 17, 2016 at 7:25 PM, Tobi  wrote:
>
>> Hello,
>>
>> I was trying to write a program which would upload new pages on my wiki. I
>> was following these instructions
>> http://platform.xwiki.org/xwiki/bin/view/Features/XWikiRESTfulAPI , but
>> unfortunately most of the examples use old version of Apache's HTTP
>> library,
>> so they are not very useful for me.
>>
>> So I decided to try writing a program by following Apache's examples, but
>> when I was testing connectivity I realised that I can't even get access to
>> admin-only pages.
>>
>> Here's my code:
>>
>> import java.io.IOException;
>> import org.apache.http.auth.AuthScope;
>> import org.apache.http.auth.UsernamePasswordCredentials;
>> import org.apache.http.client.CredentialsProvider;
>> import org.apache.http.client.methods.CloseableHttpResponse;
>> import org.apache.http.client.methods.HttpGet;
>> import org.apache.http.impl.client.BasicCredentialsProvider;
>> import org.apache.http.impl.client.CloseableHttpClient;
>> import org.apache.http.impl.client.HttpClients;
>> import org.apache.http.util.EntityUtils;
>>
>> public class RestTest {
>>
>> public static void main(String[] args) throws IOException {
>> CredentialsProvider credsProvider = new
>> BasicCrede