Tags:
create new tag
view all tags

SID-02020: How to use ConnectByRestPlugin to modify/update TWiki topic?

Status: Answered 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 &lt;String, Object&gt; arguments) throws RemoteException {
    try {
      buildRestProperties(); //initializes the attributes restHost, restPort, restProt, restPrefix, restWeb, wikiPrefix (together they form the REST URL e.g. http://&lt;server&gt;/twiki/bin/rest/ConnectByRestPlugin/createtopic

      List&lt;NameValuePair&gt; formparams = new ArrayList &lt;NameValuePair&gt;();
      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 &gt; 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() &gt; 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

      Change status to:
ALERT! 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.
SupportForm
Status Answered
Title How to use ConnectByRestPlugin to modify/update TWiki topic?
SupportCategory ConnectByRestPlugin
TWiki version

Server OS

Web server

Perl version

Browser & version

Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r3 - 2015-02-23 - MohanKumarM
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2026 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.