SID-02020: How to use ConnectByRestPlugin to modify/update TWiki topic?
| Status: |
Answered |
TWiki version: |
|
Perl version: |
|
| Category: |
ConnectByRestPlugin |
Server OS: |
|
Last update: |
11 years ago |
Any sample script that shows access/modifying the twiki topic using the REST (
ConnectByRestPlugin ) ??
I have an use-case where I have to generate the twiki content dynamically. Can somebody help me getting this done.
Thanks in Advance!
Mohan
--
Mohan Kumar M - 2015-02-10
Discussion and Answer
Hello Mohan!
below I give you some code extract from our Java application that uses the plugin to create/update TWiki topics. But this would give you rather some orientation than runnable code as it is part of a complex application - not sure if this would help you. I made some additional comments to exlain some method calls.
We use two methods. First one creates the raw code of the topic, consisting of the topic code and the (optional) metastring. We use an argument hash map for passing all information to the method:
/**
* Do the work for create and update Topic
* @param xmlObject
* @param arguments
* @param function
* @return
* @throws RemoteException
*/
private XmlObject manipulateTopic(XmlObject xmlObject, HashMap <String, Object> arguments, String function) throws RemoteException {
// Example URL
// http://<server>/twiki/bin/rest/ConnectByRestPlugin/createtopic
String topicname = arguments.get(WikiRestConstants.REST_TOPICNAME).toString(); //some topic name
String contentName = (String)arguments.get(WikiRestConstants.REST_CONTENT);
if (contentName <span>= null) {
contentName = WikiConstants.WIKI_RETURN;
}
String content = arguments.get(contentName).toString(); //topic code (raw TWiki Markup)
String metaName = (String)arguments.get(WikiRestConstants.REST_METATEXT);
if (metaName =</span> null) {
metaName = WikiConstants.WIKI_METASTRING;
}
String metaText = arguments.get(metaName).toString();
content = content + "\n" + metaText; // create topic code as string
// get properties names
getGeneralProperties(arguments);
String user = adm.getProperties(scenario, inputUser); //user name for TWiki logon
String passwd = adm.getProperties(scenario, inputPasswd); //password for TWiki logon
return callWiki(function, topicname, user, passwd, topicname, content, null, null, null, arguments);
}
Second method that calls the REST service:
/**
* Call to Wiki - Rest
* @param function function to call
* @param query Parameter
* @param metaText
* @param content
* @param string
* @param passwd
* @param user
* @param newValue
* @param formfield
* @param arguments
* @param topicName Web-name (path)
* @return
* @throws RemoteException
*/
private XmlObject callWiki(String function, String topicname, String user, String passwd, String topic, String content, String metaText, String formfield, String newValue, HashMap <String, Object> arguments) throws RemoteException {
try {
buildRestProperties(); //initializes the attributes restHost, restPort, restProt, restPrefix, restWeb, wikiPrefix (together they form the REST URL e.g. http://<server>/twiki/bin/rest/ConnectByRestPlugin/createtopic
List<NameValuePair> formparams = new ArrayList <NameValuePair>();
formparams.add(new BasicNameValuePair ("username", user));
formparams.add(new BasicNameValuePair ("password", passwd));
formparams.add(new BasicNameValuePair ("topic", restWeb + "." + topic.trim()));
if (metaText != null) {
formparams.add(new BasicNameValuePair ("metatext", metaText));
}
if (formfield != null) {
formparams.add(new BasicNameValuePair ("fieldname", formfield));
}
if (newValue != null) {
formparams.add(new BasicNameValuePair ("newval", newValue));
}
if (content != null) {
formparams.add(new BasicNameValuePair ("text", content));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity (formparams, "ISO-8859-1");
System.out.println("Create wiki: " + topic);
URI uri = URIUtils.createURI(restProt, restHost, Integer.parseInt(restPort), restPrefix + "/" + function, null, null);
HttpClient httpclient = new DefaultHttpClient ();
HttpPost httppost = new HttpPost (uri);
httppost.setEntity(formEntity);
HttpResponse respPost = httpclient.execute(httppost);
// Redirect?? Login in via Wiki Template...
if (respPost.getStatusLine().getStatusCode() == 302) {
// Goto redirect location
Header[] locs = respPost.getHeaders("Location");
if (locs.length > 0) {
Header locHeader = locs[0];
String location = locHeader.getValue();
URI uriRedirect = URIUtils.createURI(restProt, restHost, Integer.parseInt(restPort), location, null, null);
HttpGet httpGet = new HttpGet (uriRedirect);
httpclient = new DefaultHttpClient ();
respPost = httpclient.execute(httpGet);
}
if (respPost.getStatusLine().getStatusCode() != 200) {
throw new UIFRuntimeException ("Response from Wiki: " + respPost.getStatusLine());
}
}
StringBuilder resultString = new StringBuilder ();
HttpEntity entity = respPost.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
@SuppressWarnings("unused")
int l;
byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
resultString.append(tmp);
}
}
URI wikiuri = URIUtils.createURI(restProt, restHost, Integer.parseInt(restPort), wikiPrefix + "/" + restWeb + "/" + topic.trim(), null, null);
String wikiUrl = wikiuri.toString();
ObjectType result = new ObjectType ();
result.setType(UIFWikiConstants.UIF_WIKI_RETURN);
result.setName(topicname);
ObjParamType objParam = new ObjParamType ();
result.getObjParam().add(objParam);
objParam.setObject(result);
objParam.setDbValue(restWeb + "/" + topicname.trim());
objParam.setKey(UIFWikiRestConstants.UIF_REST_TOPICNAME);
objParam = new ObjParamType ("", UIFWikiConstants.UIF_WIKI_URL, wikiUrl);
result.getObjParam().add(objParam);
System.out.println("Put " + UIFWikiConstants.UIF_WIKI_URL + ": " + wikiUrl);
arguments.put(UIFWikiConstants.UIF_WIKI_URL, wikiUrl);
if (resultString.length() > 0) {
objParam = new ObjParamType ("", UIFWikiRestConstants.UIF_REST_METATEXT, resultString.toString());
result.getObjParam().add(objParam);
objParam.setObject(result);
}
return result;
}
catch (RemoteException e) {
throw new RemoteException (e.getMessage(), e);
}
catch (MalformedURLException e) {
throw new RemoteException (e.getMessage(), e);
}
catch (IOException e) {
throw new RemoteException (e.getMessage(), e);
}
catch (URISyntaxException e) {
throw new RemoteException (e.getMessage(), e);
}
}
--
Michael Gulitz - 2015-02-20
--
Mohan Kumar M - 2015-02-23
Hi Michael!
Thanks for your response!
We have found a solution for this!
And thanks a lot for your answer, I will use this in future!!
Regards,
Mohan.
--
Mohan Kumar M - 2015-02-23
And sorry that I am new to java, and expected an answer in python / perl ...:)!
--
Mohan Kumar M - 2015-02-23
If you answer a question - or someone answered one of your questions - please remember to edit the page and set the status to answered. The status selector is below the edit box.