Wiki source code of RefactoringRESTXML

Last modified by Thomas Mortagne on 2022/08/08 11:05

Hide last authors
Thomas Mortagne 1.1 1 {{groovy}}
Thomas Mortagne 1.2 2 import org.xwiki.model.reference.*;
Thomas Mortagne 1.1 3
4 // Create a request to move a page using the standard API
Thomas Mortagne 1.13 5 // That's generally the only part you need to customize for your own request need
Thomas Mortagne 1.2 6 var source = new SpaceReference("xwiki", "MyParentPage", "MyPage");
7 var destination = new SpaceReference("xwiki", "MyPageNewLocaltion", "MyPageNewName");
Thomas Mortagne 2.1 8 var jobRequest = services.component.getInstance(org.xwiki.refactoring.script.RequestFactory.class).createMoveRequest(source, destination)
Thomas Mortagne 1.13 9
10 /////////// SERIALIZATION ////////////////////
11
Thomas Mortagne 1.9 12 // cleanup the request from properties which make sense for a script but are pretty much useless in the case of a REST request
Thomas Mortagne 2.1 13 jobRequest.removeProperty("checkrights")
14 jobRequest.removeProperty("user.reference")
15 jobRequest.removeProperty("caller.reference")
Thomas Mortagne 1.1 16
17 // Convert the standard move request into a REST job request
Thomas Mortagne 2.1 18 var restJobRequest = services.component.getInstance(org.xwiki.rest.internal.ModelFactory.class).toRestJobRequest(jobRequest);
Thomas Mortagne 1.1 19
20 // Serialize the REST job request into an XML content
Thomas Mortagne 1.12 21 var writer = new java.io.StringWriter();
22 var context = javax.xml.bind.JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
23 var marshaller = context.createMarshaller();
24 // Format the XML to make the example more readable but the XML obviously does not need to be formatter in the request
25 marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Thomas Mortagne 1.6 26 marshaller.marshal(restJobRequest, writer);
Thomas Mortagne 1.1 27
Thomas Mortagne 1.7 28 println "{{code language='xml'}}"
29 println writer.toString()
30 println "{{/code}}"
Thomas Mortagne 1.1 31 {{/groovy}}