[xwiki-users] help on sample of using RestFul API to create new page in a xwiki
Could anyone can share a exmaple of how to using httpClient 4.3 to put a new page int to a xwiki. I've tried could of times, but failed. All the examples are not with httpClient 4.3. I got error like 401, not authenticated, or 500,HTTP/1.1 500 Internal Server Error <html> <head> <title>Status page</title> </head> <body style="font-family: sans-serif;"> <p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Internal Server Error</p> <p>The server encountered an unexpected condition which prevented it from fulfilling the request</p> <p>You can get technical details <a href=" http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1">here</a> .<br> Please continue your visit at our <a href="/">home page</a>. </p> </body> </html> Here is my code: package org.cfgr.xwiki; import java.beans.Encoder; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.net.URLEncoder; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import org.apache.commons.codec. EncoderException; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpEntity; import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.config.Lookup; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import xwiki.rest.model.jaxb.ObjectFactory; import xwiki.rest.model.jaxb.Page; import xwiki.rest.model.jaxb.PageSummary; import xwiki.rest.model.jaxb.Pages; public class Importer { public static CloseableHttpClient getClient(String user, String pass){ Credentials defaultcreds = new UsernamePasswordCredentials(user, pass); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("www.chinafgr.com", 8000), new UsernamePasswordCredentials(user, pass)); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .build(); return httpClient; } public static HttpClientContext createContext(String user, String pass){ CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("www.chinafgr.com", 8000), new UsernamePasswordCredentials(user, pass)); HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); // context.setAuthSchemeRegistry(authRegistry); // context.setAuthCache(authCache); return context; } // public Space putPage(CloseableHttpClient httpClient, String wiki, String space, Page page){ // // JAXBContext context; // try { // context = JAXBContext.newInstance("org"); // Unmarshaller unmarshaller = context.createUnmarshaller(); // Marshaller marshaller = context.createMarshaller(); // String name = page.getName(); // name = URLEncoder.encode(name, "UTF-8"); // page.setName(name); // page.setXwikiRelativeUrl("http://www.xwiki.org/rel/page"); // HttpPut putMethod = new HttpPut(" http://2.2.2.2:8080/xwiki/rest/wikis/"+ wiki +"/spaces/" + space + "/pages/" + page.getName() ); // putMethod.addHeader("Accept", "application/xml"); // putMethod.addHeader("Accept-Ranges", "bytes"); // Page p = new Page(); // p.setContent(page.getContent()); // p.setTitle(page.getTitle()); // p.setParent(page.getParent()); // JAXBElement<Page> jbe = new ObjectFactory().createPage(p); // StringWriter writer = new StringWriter(); // // marshaller.marshal(jbe, writer); // Fill the StringWriter with the page xml // // System.out.println("WRITER = [[[[" + writer + "]]]]"); // // putMethod.setRequestEntity(new StringRequestEntity(writer.toString(), // "application/xml", "UTF-8")); // // httpClient.execute(putMethod); // JAXBElement obj = (JAXBElement) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream()); // System.out.println("Return is " + obj.getValue()); // } public static void main(String[] args) { CloseableHttpClient httpclient = getClient("admin","admin"); JAXBContext context; try { context = JAXBContext.newInstance("xwiki.rest.model.jaxb"); Unmarshaller unmarshaller = context.createUnmarshaller(); HttpGet getMethod = new HttpGet(" http://www.chinafgr.com:8000/xwiki/rest/wikis/xwiki/spaces/AppTest/pages/Web... "); getMethod.addHeader("Accept", "application/xml"); CloseableHttpResponse response = httpclient.execute(getMethod); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to bother about connection release if (entity != null) { InputStream instream = entity.getContent(); JAXBElement o = (JAXBElement) unmarshaller.unmarshal(instream); Page page = (Page) o.getValue(); Marshaller marshaller = context.createMarshaller(); // String name = page.getName(); // name = URLEncoder.encode(name, "UTF-8"); // page.setName(name); // page.setXwikiRelativeUrl(" http://www.xwiki.org/rel/page"); HttpPut putMethod = new HttpPut(" http://www.chinafgr.com:8000/xwiki/rest/wikis/xwiki/spaces/wiki/pages/" + "NewPage" ); putMethod.addHeader("Content-Type", "application/xml"); putMethod.addHeader("Accept", "application/xml"); putMethod.addHeader("Authorization", "Basic " + new Base64().encode("Admin:admin".getBytes())); Page p = new Page(); // p.setContent(page.getContent()); // p.setTitle(page.getTitle()); p.setContent("Hello the world"); p.setTitle("ThePAGE"); p.setSyntax("xwiki/2.0"); p.setParent(page.getParent()); JAXBElement<Page> jbe = new ObjectFactory().createPage(p); StringWriter writer = new StringWriter(); marshaller.marshal(jbe, writer); // Fill the StringWriter with the page xml System.out.println("WRITER = [[[[" + writer + "]]]]"); putMethod.setEntity(new StringEntity(writer.toString(), "application/xml", "UTF-8")); response = httpclient.execute(putMethod); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // JAXBElement obj = (JAXBElement) unmarshaller.unmarshal(response.getEntity().getContent()); //System.out.println("Return is " + obj.getValue()); for(;;){ int c=response.getEntity().getContent().read(); if(c<0) break; System.out.print((char)c); } System.out.println(); // Pages page = (Pages)unmarshaller.unmarshal(instream); // for(PageSummary ps:page.getPageSummary()) // System.out.println(ps.getName()); } } finally { response.close(); } } catch (JAXBException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
From what I remember the difficulty was to setup preemptive authentication (by default XWiki does not send any challenged especially when guess has the right to access) since it's quite a pain in HTTPClient 4.3.
You can find a REST access example (with authentication) using HTTPClient 4.3 on https://github.com/xwiki/xwiki-commons/blob/master/xwiki-commons-core/xwiki-.... The client is created in https://github.com/xwiki/xwiki-commons/blob/master/xwiki-commons-core/xwiki-.... On Mon, Apr 21, 2014 at 2:11 AM, Zhihua Zheng <zhihua25@gmail.com> wrote:
Could anyone can share a exmaple of how to using httpClient 4.3 to put a new page int to a xwiki. I've tried could of times, but failed. All the examples are not with httpClient 4.3.
I got error like 401, not authenticated, or 500,HTTP/1.1 500 Internal Server Error <html> <head> <title>Status page</title> </head> <body style="font-family: sans-serif;"> <p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Internal Server Error</p> <p>The server encountered an unexpected condition which prevented it from fulfilling the request</p> <p>You can get technical details <a href=" http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1">here</a> .<br> Please continue your visit at our <a href="/">home page</a>. </p> </body> </html>
Here is my code: package org.cfgr.xwiki;
import java.beans.Encoder; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.net.URLEncoder;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller;
import org.apache.commons.codec. EncoderException; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpEntity; import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.config.Lookup; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients;
import xwiki.rest.model.jaxb.ObjectFactory; import xwiki.rest.model.jaxb.Page; import xwiki.rest.model.jaxb.PageSummary; import xwiki.rest.model.jaxb.Pages;
public class Importer { public static CloseableHttpClient getClient(String user, String pass){ Credentials defaultcreds = new UsernamePasswordCredentials(user, pass); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("www.chinafgr.com", 8000), new UsernamePasswordCredentials(user, pass)); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .build();
return httpClient; }
public static HttpClientContext createContext(String user, String pass){ CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("www.chinafgr.com", 8000), new UsernamePasswordCredentials(user, pass));
HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); // context.setAuthSchemeRegistry(authRegistry); // context.setAuthCache(authCache); return context; }
// public Space putPage(CloseableHttpClient httpClient, String wiki, String space, Page page){ // // JAXBContext context; // try { // context = JAXBContext.newInstance("org"); // Unmarshaller unmarshaller = context.createUnmarshaller(); // Marshaller marshaller = context.createMarshaller(); // String name = page.getName(); // name = URLEncoder.encode(name, "UTF-8"); // page.setName(name); // page.setXwikiRelativeUrl("http://www.xwiki.org/rel/page"); // HttpPut putMethod = new HttpPut(" http://2.2.2.2:8080/xwiki/rest/wikis/"+ wiki +"/spaces/" + space + "/pages/" + page.getName() ); // putMethod.addHeader("Accept", "application/xml"); // putMethod.addHeader("Accept-Ranges", "bytes"); // Page p = new Page(); // p.setContent(page.getContent()); // p.setTitle(page.getTitle()); // p.setParent(page.getParent()); // JAXBElement<Page> jbe = new ObjectFactory().createPage(p); // StringWriter writer = new StringWriter(); // // marshaller.marshal(jbe, writer); // Fill the StringWriter with the page xml // // System.out.println("WRITER = [[[[" + writer + "]]]]"); // // putMethod.setRequestEntity(new StringRequestEntity(writer.toString(), // "application/xml", "UTF-8")); // // httpClient.execute(putMethod); // JAXBElement obj = (JAXBElement) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream()); // System.out.println("Return is " + obj.getValue()); // }
public static void main(String[] args) { CloseableHttpClient httpclient = getClient("admin","admin"); JAXBContext context; try {
context = JAXBContext.newInstance("xwiki.rest.model.jaxb"); Unmarshaller unmarshaller = context.createUnmarshaller();
HttpGet getMethod = new HttpGet(" http://www.chinafgr.com:8000/xwiki/rest/wikis/xwiki/spaces/AppTest/pages/Web... "); getMethod.addHeader("Accept", "application/xml"); CloseableHttpResponse response = httpclient.execute(getMethod); try {
System.out.println("----------------------------------------"); System.out.println(response.getStatusLine());
// Get hold of the response entity HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need // to bother about connection release if (entity != null) { InputStream instream = entity.getContent(); JAXBElement o = (JAXBElement) unmarshaller.unmarshal(instream); Page page = (Page) o.getValue();
Marshaller marshaller = context.createMarshaller(); // String name = page.getName(); // name = URLEncoder.encode(name, "UTF-8"); // page.setName(name); // page.setXwikiRelativeUrl(" http://www.xwiki.org/rel/page");
HttpPut putMethod = new HttpPut(" http://www.chinafgr.com:8000/xwiki/rest/wikis/xwiki/spaces/wiki/pages/" + "NewPage" ); putMethod.addHeader("Content-Type", "application/xml"); putMethod.addHeader("Accept", "application/xml"); putMethod.addHeader("Authorization", "Basic " + new Base64().encode("Admin:admin".getBytes()));
Page p = new Page(); // p.setContent(page.getContent()); // p.setTitle(page.getTitle()); p.setContent("Hello the world"); p.setTitle("ThePAGE"); p.setSyntax("xwiki/2.0"); p.setParent(page.getParent()); JAXBElement<Page> jbe = new ObjectFactory().createPage(p); StringWriter writer = new StringWriter();
marshaller.marshal(jbe, writer); // Fill the StringWriter with the page xml System.out.println("WRITER = [[[[" + writer + "]]]]"); putMethod.setEntity(new StringEntity(writer.toString(), "application/xml", "UTF-8")); response = httpclient.execute(putMethod);
System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // JAXBElement obj = (JAXBElement) unmarshaller.unmarshal(response.getEntity().getContent()); //System.out.println("Return is " + obj.getValue()); for(;;){ int c=response.getEntity().getContent().read(); if(c<0) break; System.out.print((char)c); } System.out.println(); // Pages page = (Pages)unmarshaller.unmarshal(instream); // for(PageSummary ps:page.getPageSummary()) // System.out.println(ps.getName());
} } finally { response.close(); } } catch (JAXBException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
}
} _______________________________________________ users mailing list users@xwiki.org http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
It looks all the issue is due to The preemptive authentication. I got a successfull sample. Here is my code HttpHost target = new HttpHost("www.chinafgr.com", 8000, "http"); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); // Add AuthCache to the execution context HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); response = httpclient.execute(target,putMethod,localContext); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); I think it's better the mention that need peemptive anuthentication in the doc. thanks,Thamas. 2014-04-21 15:29 GMT+08:00 Thomas Mortagne <thomas.mortagne@xwiki.com>:
From what I remember the difficulty was to setup preemptive authentication (by default XWiki does not send any challenged especially when guess has the right to access) since it's quite a pain in HTTPClient 4.3.
You can find a REST access example (with authentication) using HTTPClient 4.3 on
https://github.com/xwiki/xwiki-commons/blob/master/xwiki-commons-core/xwiki-... . The client is created in
https://github.com/xwiki/xwiki-commons/blob/master/xwiki-commons-core/xwiki-... .
On Mon, Apr 21, 2014 at 2:11 AM, Zhihua Zheng <zhihua25@gmail.com> wrote:
Could anyone can share a exmaple of how to using httpClient 4.3 to put a new page int to a xwiki. I've tried could of times, but failed. All the examples are not with httpClient 4.3.
I got error like 401, not authenticated, or 500,HTTP/1.1 500 Internal Server Error <html> <head> <title>Status page</title> </head> <body style="font-family: sans-serif;"> <p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Internal Server Error</p> <p>The server encountered an unexpected condition which prevented it from fulfilling the request</p> <p>You can get technical details <a href=" http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1 ">here</a> .<br> Please continue your visit at our <a href="/">home page</a>. </p> </body> </html>
Here is my code: package org.cfgr.xwiki;
import java.beans.Encoder; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.net.URLEncoder;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller;
import org.apache.commons.codec. EncoderException; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpEntity; import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.config.Lookup; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients;
import xwiki.rest.model.jaxb.ObjectFactory; import xwiki.rest.model.jaxb.Page; import xwiki.rest.model.jaxb.PageSummary; import xwiki.rest.model.jaxb.Pages;
public class Importer { public static CloseableHttpClient getClient(String user, String pass){ Credentials defaultcreds = new UsernamePasswordCredentials(user, pass); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("www.chinafgr.com", 8000), new UsernamePasswordCredentials(user, pass)); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .build();
return httpClient; }
public static HttpClientContext createContext(String user, String pass){ CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("www.chinafgr.com", 8000), new UsernamePasswordCredentials(user, pass));
HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); // context.setAuthSchemeRegistry(authRegistry); // context.setAuthCache(authCache); return context; }
// public Space putPage(CloseableHttpClient httpClient, String wiki, String space, Page page){ // // JAXBContext context; // try { // context = JAXBContext.newInstance("org"); // Unmarshaller unmarshaller = context.createUnmarshaller(); // Marshaller marshaller = context.createMarshaller(); // String name = page.getName(); // name = URLEncoder.encode(name, "UTF-8"); // page.setName(name); // page.setXwikiRelativeUrl("http://www.xwiki.org/rel/page"); // HttpPut putMethod = new HttpPut(" http://2.2.2.2:8080/xwiki/rest/wikis/"+ wiki +"/spaces/" + space + "/pages/" + page.getName() ); // putMethod.addHeader("Accept", "application/xml"); // putMethod.addHeader("Accept-Ranges", "bytes"); // Page p = new Page(); // p.setContent(page.getContent()); // p.setTitle(page.getTitle()); // p.setParent(page.getParent()); // JAXBElement<Page> jbe = new ObjectFactory().createPage(p); // StringWriter writer = new StringWriter(); // // marshaller.marshal(jbe, writer); // Fill the StringWriter with the page xml // // System.out.println("WRITER = [[[[" + writer + "]]]]"); // // putMethod.setRequestEntity(new StringRequestEntity(writer.toString(), // "application/xml", "UTF-8")); // // httpClient.execute(putMethod); // JAXBElement obj = (JAXBElement) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream()); // System.out.println("Return is " + obj.getValue()); // }
public static void main(String[] args) { CloseableHttpClient httpclient = getClient("admin","admin"); JAXBContext context; try {
context = JAXBContext.newInstance("xwiki.rest.model.jaxb"); Unmarshaller unmarshaller = context.createUnmarshaller();
HttpGet getMethod = new HttpGet("
"); getMethod.addHeader("Accept", "application/xml"); CloseableHttpResponse response = httpclient.execute(getMethod); try {
System.out.println("----------------------------------------"); System.out.println(response.getStatusLine());
// Get hold of the response entity HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need // to bother about connection release if (entity != null) { InputStream instream = entity.getContent(); JAXBElement o = (JAXBElement) unmarshaller.unmarshal(instream); Page page = (Page) o.getValue();
Marshaller marshaller = context.createMarshaller(); // String name = page.getName(); // name = URLEncoder.encode(name, "UTF-8"); // page.setName(name); // page.setXwikiRelativeUrl(" http://www.xwiki.org/rel/page");
HttpPut putMethod = new HttpPut(" http://www.chinafgr.com:8000/xwiki/rest/wikis/xwiki/spaces/wiki/pages/"
http://www.chinafgr.com:8000/xwiki/rest/wikis/xwiki/spaces/AppTest/pages/Web... +
"NewPage" ); putMethod.addHeader("Content-Type", "application/xml"); putMethod.addHeader("Accept", "application/xml"); putMethod.addHeader("Authorization", "Basic " + new Base64().encode("Admin:admin".getBytes()));
Page p = new Page(); // p.setContent(page.getContent()); // p.setTitle(page.getTitle()); p.setContent("Hello the world"); p.setTitle("ThePAGE"); p.setSyntax("xwiki/2.0"); p.setParent(page.getParent()); JAXBElement<Page> jbe = new ObjectFactory().createPage(p); StringWriter writer = new StringWriter();
marshaller.marshal(jbe, writer); // Fill the StringWriter with the page xml System.out.println("WRITER = [[[[" + writer + "]]]]"); putMethod.setEntity(new StringEntity(writer.toString(), "application/xml", "UTF-8")); response = httpclient.execute(putMethod);
System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // JAXBElement obj = (JAXBElement) unmarshaller.unmarshal(response.getEntity().getContent()); //System.out.println("Return is " + obj.getValue()); for(;;){ int c=response.getEntity().getContent().read(); if(c<0) break; System.out.print((char)c); } System.out.println(); // Pages page = (Pages)unmarshaller.unmarshal(instream); // for(PageSummary ps:page.getPageSummary()) // System.out.println(ps.getName());
} } finally { response.close(); } } catch (JAXBException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
}
} _______________________________________________ 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
participants (2)
-
Thomas Mortagne -
Zhihua Zheng