Finally, I have a working version of the code to access the Xwiki RESTful
API.
It worked smooth on a Tomcat 6.0.14 + JDK 1.5 machine. But had to make
certain changes to the bootstrap files for JDK 1.6 as I mentioned in the
previous post. I have used the Apache HTTP HTTPClient 3.1 jar files here but
soon will upgrade that to Apache HTTPClient 4.0. The only change in the code
from the one posted at
http://platform.xwiki.org/xwiki/bin/view/Features/XWikiRESTfulAPI are the
lines Unmarshaller un = context.createUnmarshaller(); and the line Page page
= (Page) ((JAXBElement)
un.unmarshal(getMethod.getResponseBodyAsStream())).getValue();
The default object that getMethod returns turned out to be a JAXBElement
rather than a Page object. Hence, had to do some class casting.
A big thanks to Fabio Mancinelli & Sergiu Dumitriu for helping me out on
this one.

Just one more request though. Do we cast the
getMethod.getResponseBodyAsStream() to Tags class (if we want to retrieve a
list of pages based on their tags) or the SearchResults class (if we want to
retrieve pages returned through a page search). Even though, I cast the
getMethod.getResponseBodyAsStream to the tags class, my compiler still
throws an error for class cast exception : org.xwiki.Pages. Could someone
please post a snippet to access pages with specific tags or any other
complex querying using classes in the org.xwiki classes. Any help on this
would be greatly appreciated friends.


/*
JDK: 1.5
Tomcat: 6.0.14 (Xwiki server)
Libraries: Apache HTTPClient 3.1, JAXB 2.1, Apache Commons Logging 1.1.1 &
Apache Commons Codec 1.4
Class File Dependencies: org.xwiki package of class files generated from the
XSD file at
http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-rest/src/main/r
esources/xwiki.rest.model.xsd with the help of the XJC compiler available in
the JAXB 2.1 distribution
*/

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.xwiki.Page;

public class Main {

        public static void main(String[] args) throws JAXBException,
HttpException, IOException {

        HttpClient httpClient = new HttpClient();

        JAXBContext context = JAXBContext.newInstance("org.xwiki");

        Unmarshaller un = context.createUnmarshaller();

        GetMethod getMethod = new
GetMethod("http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/We
bHome");
        getMethod.addRequestHeader("Accept", "application/xml");
        httpClient.executeMethod(getMethod);

        Page page = (Page) ((JAXBElement)
un.unmarshal(getMethod.getResponseBodyAsStream())).getValue();

        System.out.println(page.getCreator());
        System.out.println(page.getCreated());
        System.out.println(page.getFullName());
        System.out.println(page.getXwikiAbsoluteUrl());
        

    }
}

_______________________________________________
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users

Reply via email to