Version 1

    Here's a very simple example showing one way to publish a document whose content is an HTML string.


    A couple bits of information behind this example:


    • The document's title must be unique in the community in order for the publish action to succeed.
    • If you're sending HTML as a simple string, the markup must be escaped, such as by enclosing it in a CDATA.


    import org.apache.commons.httpclient.Credentials;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.UsernamePasswordCredentials;
    import org.apache.commons.httpclient.auth.AuthScope;
    import org.apache.commons.httpclient.methods.PutMethod;
    import org.apache.commons.httpclient.methods.StringRequestEntity;
    public class Publisher {
        // Variables that will be used across REST calls.
        private URL requestURL;
        private HttpClient client = new HttpClient();
        public static void main(String[] args) {      
            // Base part of the REST URI. This points at the community.
            String restContext = "http://jivedocsdm/rpc/rest/";
            // Credentials for the user who has publish rights in the community.
            // Simply using the admin here. These happen to be the credentials
            // for the user (ID 2003) who will be shown as having published
            // the document.
            String userName = "joe";
            String password = "demo";
            // ID of the user who should be shown as having created the document.
            String docCreatorID = "2003";
            // ID for the space in which to publish the document. 
            // You can get this from the admin console when viewing
            // information about spaces, or via the API.
            String spaceID = "2001";
            // The new document's title.
            String docTitle = "Favorite Count Basie Tunes";
            // The document's body. Note that HTML markup here must be 
            // enclosed in <body> or <p> tags.
            String docBody = 
                "<body>" +
                    "<p>One O'Clock Jump</p>" +
                    "<p>Lester Leaps In</p>" +
                    "<p>April in Paris</p>" +
            // Create an instance of this class and publish the document.
            Publisher documentPublisher = new Publisher(restContext, userName,
            documentPublisher.publishDocument(docTitle, docBody, spaceID, docCreatorID);
         * Constructs an instance of this class using a URI that points to the 
         * community, passing the credentials of a user who has permission to
         * connect and publish documents.
         * @param restContext Base URI for setting up a connection against
         *  which to call HTTP methods.
         * @param userName The name of the user logging in for these actions.
         * @param password The user's password.
        public Publisher(String restContext, String userName, String password) {
            setUpHttpConnection(userName, password, restContext);
         * Publishes the specified body as content in a document.
         * @param docTitle The title of the document to create.
         * @param docBody The document's content.
         * @param communityID The numeric ID of the community where the
         *  document should be published.
         * @param docCreatorID The user ID of the person who should be
         *  shown as having created the document.
        public void publishDocument(String docTitle, String docBody,
                String communityID, String docCreatorID) {
            // This is the XML payload sent with the REST put method. 
            // A simple String template of XML here provides a way to 
            // insert the values. 
            String payloadTemplate = 
                "<publishDocument>" + 
                    "<communityID>" + communityID + "</communityID>" + 
                    "<userID>" + docCreatorID + "</userID>" + 
                    "<documentTypeID>" + 1 + "</documentTypeID>" +
                    // Use the default generated ID.
                    "<documentID>" + "" + "</documentID>" + 
                    "<title>" + docTitle + "</title>" + 
                    // The document body HTML string must be escaped
                    // before it can be put into the database.
                    "<body><![CDATA[" + docBody + "]]></body>" + 
            try {
                PutMethod method = new PutMethod(requestURL
                        + "documentService/publish");
                StringRequestEntity requestPayload = new StringRequestEntity(
                        payloadTemplate, "text/html", null);
                // The response code is useful for debugging.
                int response = client.executeMethod(method);
            } catch (HttpException e) {
                System.out.println("Error connecting to the instance. "
                        + e.getCause().getLocalizedMessage());
            } catch (IOException e) {
         * Sets up an HTTP connection to the community through which to call the HTTP
         * REST methods. 
         * @param userName The username of a someone who has permission to connect to 
         *  the community and perform the actions represented by the REST resources 
         *  (such as publishing a document).
         * @param password The user's password.
         * @param restContext The URI at which to connect to the community for 
         * REST calls.
        private void setUpHttpConnection(String userName, String password,
                String restContext) {
            try {
                requestURL = new URL(restContext);
                Credentials defaultcreds = new UsernamePasswordCredentials(
                        userName, password);
                client.getState().setCredentials(AuthScope.ANY, defaultcreds);
            } catch (MalformedURLException e) {
                System.out.println("Couldn't set up the HTTP connection with the community. ");