[xwiki-dev] Dynamic Page to Static Page

Frank A Nuzzi fanuzzi at us.ibm.com
Thu Oct 19 21:40:40 CEST 2006


Robin,
        Is there no way to do this from within xwiki?
Thanks,

Frank (Tony) Nuzzi
AIS - Install Solution Developer
Phone 838-3781, t/l 678-3781
External E-mail - fanuzzi at us.ibm.com



"Robin Fernandes" <rewbs.soal at gmail.com> 
10/12/2006 06:03 PM
Please respond to
robin at soal.org


To
xwiki-dev at objectweb.org
cc

Subject
Re: [xwiki-dev] Dynamic Page to Static Page






Thinking about it, I'm not sure why I felt XML-RPC was necessary. :)
Here's a simpler example that does it all over straight http get/post.
Requires the apache commons httpclient lib, which requires commons
logging and commons codec.

---8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<--
package org.soal.XWikiPost;

import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class XWikiPoster {
                 //----( Configuration [change me!] )----
                 static final String SERVER = "http://my.xwiki.com";
                 static final String USERNAME = "myName"; // exactly how 
you'd enter
                 static final String PASSWORD = "myPass"; // them in the 
web login form
                 static final String SPACE = "Sandbox";
                 static final String DYNAMIC = "Dynamic";
                 static final String STATIC = "Static";
                 //--------------------------------------
 
 
                 static HttpClient client = new HttpClient();
 
                 public static void main(String[] args) throws 
HttpException, IOException {
 client.getParams().setParameter("http.protocol.cookie-policy",
CookiePolicy.BROWSER_COMPATIBILITY);
                                 login();
                                 String renderedContent = 
getRenderedContent(SPACE, DYNAMIC);
                                 postContent(SPACE, STATIC, 
renderedContent);
                 }

                 private static void login() throws HttpException, 
IOException {
                                 String url = 
String.format("%s/xwiki/bin/login/XWiki/XWikiLogin", SERVER);
                                 PostMethod method = new PostMethod(url);
                                 method.addParameter("j_username", 
USERNAME);
                                 method.addParameter("j_password", 
PASSWORD);
                                 method.addParameter("j_rememberme", 
"false");
                                 int statusCode = 
client.executeMethod(method);
                                 if (statusCode != HttpStatus.SC_OK) {
                                                 System.err.println("Login 
response (redirect is normal): " +
method.getStatusLine());
                                 } 
                 }

                 private static String getRenderedContent(String space, 
String page)
throws IOException, HttpException {
                                 String template="xpage=plain";
                                 String url = 
String.format("%s/xwiki/bin/view/%s/%s?%s", SERVER,
space, page, template);
                                 GetMethod method = new GetMethod(url);
                                 int statusCode = 
client.executeMethod(method);
                                 if (statusCode != HttpStatus.SC_OK) {
                                                 System.err.println("Get 
response: " + method.getStatusLine());
                                 }
                                 String responseBody = new 
String(method.getResponseBody());
                                 return responseBody;
                 } 
 
                 private static void postContent(String space, String 
page, String
content) throws HttpException, IOException {
                                 String url = 
String.format("%s/xwiki/bin/save/%s/%s", SERVER, space, page);
                                 PostMethod method = new PostMethod(url);
                                 method.addParameter("content", content);
                                 int statusCode = 
client.executeMethod(method);
                                 if (statusCode != HttpStatus.SC_OK) {
                                                 System.err.println("Save 
response (redirect is normal): " +
method.getStatusLine());
                                 }
                 }

}
---8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<--

Regards,
Robin.

On 12/10/06, Robin Fernandes <rewbs.soal at gmail.com> wrote:
> Hi,
>
> There's probably several ways to do this... the first one that comes
> to my mind is to write an XML-RPC client to grab the rendered content
> from one page and save it back into another. Then set up a cron job to
> kick off your XML-RPC client periodically. A simple implementation of
> the XML-RPC client is below.
>
> Alternatively you could write the code to do the copy in a groovy
> script in another XWiki document and hit it periodically.
>
> ---8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<--
>
> package com.ibm.robinf.xwikixmlrpcclient;
>
> import java.io.IOException;
> import java.net.MalformedURLException;
> import java.util.Hashtable;
> import java.util.Vector;
>
> import org.apache.xmlrpc.XmlRpcClient;
> import org.apache.xmlrpc.XmlRpcException;
>
> /**
>  * @author Robin
>  */
> public class XWikiRPCTest {
>         //----( Configuration [change me!] )----
>         protected final static String XMLRPC_SERVER_URL =
> "http://mywiki.blah.com/xwiki/bin/xmlrpc/confluence";
>         protected final static String DYNAMIC = "Play.Dynamic";
>         protected final static String STATIC = "Play.Static";
>         protected final static String WIKI_NAME = 
"robin_fernandesatukibmcom";
>         protected final static String PASSWORD = "WXYZ";
>         //--------------------------------------
>
>         protected XmlRpcClient client;
>
>         public XWikiRPCTest() throws MalformedURLException {
>                 client = new XmlRpcClient(XMLRPC_SERVER_URL);
>         }
>
>         public static void main(String[] args) throws XmlRpcException, 
IOException {
>                 XWikiRPCTest myRPCTest = new XWikiRPCTest();
>                 String token = myRPCTest.login();
>                 String renderedContent = 
myRPCTest.getRenderedContent(DYNAMIC, token);
>                 Hashtable<String, String> staticPage = 
myRPCTest.getPage(STATIC, token);
>                 staticPage.put("content", renderedContent);
>                 myRPCTest.putPage(staticPage, token);
>                 myRPCTest.logout(token);
>         }
>
>         private String getRenderedContent(String pageName, String token)
> throws XmlRpcException, IOException {
>                 Hashtable<String, String> dynamicPage = getPage(DYNAMIC, 
token);
>                 String rawContent = dynamicPage.get("content");
>
>                 Vector<String> params = new Vector<String>(4);
>                 params.add(token);
>                 params.add("Play");
>                 params.add(pageName);
>                 params.add(rawContent);
>
>                 String renderedContent = (String)
> client.execute("confluence1.renderContent", params);
>                 return renderedContent;
>         }
>
>         protected String login() throws XmlRpcException, IOException {
>                 String loginToken;
>                 Vector<String> loginParams = new Vector<String>(2);
>                 loginParams.add(WIKI_NAME);
>                 loginParams.add(PASSWORD);
>                 loginToken = (String) client.execute 
("confluence1.login", loginParams);
>                 System.out.println("Login: " + loginToken);
>                 return loginToken;
>         }
>
>         protected void logout(String loginToken) throws XmlRpcException, 
IOException {
>                 Vector<String> logoutParams = new Vector<String>(1);
>                 logoutParams.add(loginToken);
>                 System.out.println("Logout: " + client.execute
> ("confluence1.logout", logoutParams));
>         }
>
>         private Hashtable<String, String> getPage(String pageName, 
String
> token) throws XmlRpcException, IOException {
>         Vector<String> params = new Vector<String>();
>         params.add(token);
>         params.add(pageName);
>         Hashtable<String, String> page = (Hashtable<String, String>)
> client.execute("confluence1.getPage", params);
>         return page;
>         }
>
>         private void putPage(Hashtable page, String token) throws
> XmlRpcException, IOException {
>         Vector<Object> params = new Vector<Object>();
>         params.add(token);
>         params.add(page);
>         client.execute ("confluence1.storePage", params);
>         }
>
> }
> ---8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<--
>
> Cheers,
> Robin
>
>
> On 12/10/06, fanuzzi at us.ibm.com <fanuzzi at us.ibm.com> wrote:
> > All,
> >   Here is what I am wanting to do.  I have a page that generates some 
dynamic data via a plug-in and every so often I was to "snap-shot" it to a 
static page.  Mean, take the dynamic page data and create a static xwiki 
page from it.
> >
> > 1) Is it possible to do?
> > 2) If so, how do I do it.
> >
> > Thanks,
> >
> > Tony Nuzzi
> >
> >
> >
> >
> > --
> > You receive this message as a subscriber of the 
xwiki-dev at objectweb.org mailing list.
> > To unsubscribe: mailto:xwiki-dev-unsubscribe at objectweb.org
> > For general help: mailto:sympa at objectweb.org?subject=help
> > ObjectWeb mailing lists service home page: 
http://www.objectweb.org/wws
> >
> >
> >
>


--
You receive this message as a subscriber of the xwiki-dev at objectweb.org 
mailing list.
To unsubscribe: mailto:xwiki-dev-unsubscribe at objectweb.org
For general help: mailto:sympa at objectweb.org?subject=help
ObjectWeb mailing lists service home page: http://www.objectweb.org/wws

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.xwiki.org/pipermail/devs/attachments/20061019/a53b950c/attachment.html 


More information about the devs mailing list