Generate Refactoring Job REST request XML

Last modified by Thomas Mortagne on 2022/08/08 14:28

cogA script to help with the delicate task for created the right XML for a Refactoring Job for non Java clients
Type
CategoryOther
Developed by

Thomas Mortagne

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

The following example create a move request in a groovy script that you can execute in any wiki page (provided you have programming right):

{{groovy}}
import org.xwiki.model.reference.*;

// Create a request to move a page using the standard API
// That's generally the only part you need to customize for your own request need
var source = new SpaceReference("xwiki", "MyParentPage", "MyPage");
var destination = new SpaceReference("xwiki", "MyPageNewLocaltion", "MyPageNewName");
var jobRequest = services.component.getInstance(org.xwiki.refactoring.script.RequestFactory.class).createMoveRequest(source, destination)

/////////// SERIALIZATION ////////////////////

// cleanup the request from properties which make sense for a script but are pretty much useless in the case of a REST request
jobRequest.setInteractive(false);
jobRequest.removeProperty("checkrights")
jobRequest.removeProperty("checkAuthorRights")
jobRequest.removeProperty("user.reference")
jobRequest.removeProperty("caller.reference")

// Convert the standard move request into a REST job request
var restJobRequest = services.component.getInstance(org.xwiki.rest.internal.ModelFactory.class).toRestJobRequest(jobRequest);

// Serialize the REST job request into an XML content
var writer = new java.io.StringWriter();
var context = javax.xml.bind.JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
var marshaller = context.createMarshaller();
// Format the XML to make the example more readable but the XML obviously does not need to be formatter in the request
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(restJobRequest, writer);

println "{{code language='xml'}}"
println writer.toString()
println "{{/code}}"
{{/groovy}}

You can find various other examples of what the refactoring API can do on Refactoring Module.

Get Connected