Script runnenr: Calling canned CopyTree script

Dear

I've created a REST Endpoint in which I would like to use the canned CopyTree script.

@BaseScript CustomEndpointDelegate delegate
copyPageTree(
    httpMethod: "POST", groups: ["jira-users", "confluence-users"]
) { MultivaluedMap queryParams, String body ->

    def space = new JsonSlurper().parseText(body)
    //User defaultUser = userAccessor.getUserByName("koen.gillard")

    def copyPageTreeParams = [
        source_page: "ABC:ABC+Home",
        target_page: "DEF:DEF+Home"
    ]
    def confluence = new HTTPBuilder(< url >)
    confluence.client.addRequestInterceptor(new HttpRequestInterceptor() {
        void process(HttpRequest httpRequest, HttpContext httpContext) {
            httpRequest.addHeader('Authorization', 'Basic <...>')
        }
    })
    confluence.request(POST, JSON) { req ->
        uri.path = "/rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.common.admin.CopyTree"
        send URLENC, [scriptParams: new JsonBuilder(copyPageTreeParams)]
        response.success = { resp, xml ->
            println "CopyTree response status: ${resp.statusLine}"
            assert resp.statusLine.statusCode == 200
        }
    }
    return Response.ok(new JsonBuilder([space: space]).toString()).build()
}

 

When I take a look at the log it states these warning and errors:

2016-08-08 15:05:37,835 ERROR [http-nio-8090-exec-17] [runner.rest.common.UserCustomScriptEndpoint] doEndpoint *************************************************************************************
 -- url: /rest/scriptrunner/latest/custom/copyPageTree | traceId: 3d0de25e60a38050 | userName: koen.gillard | sr.execution.id: 3001bb7e-e8b4-4d0c-b02a-f8af2879d960
2016-08-08 15:05:37,837 ERROR [http-nio-8090-exec-17] [runner.rest.common.UserCustomScriptEndpoint] doEndpoint Script endpoint failed on method: POST copyPageTree
 -- url: /rest/scriptrunner/latest/custom/copyPageTree | traceId: 3d0de25e60a38050 | userName: koen.gillard | sr.execution.id: 3001bb7e-e8b4-4d0c-b02a-f8af2879d960
java.lang.IllegalArgumentException: Text must not be null or empty
	at Script586$_run_closure1.doCall(Script586.groovy:33)
	at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.doEndpoint(UserCustomScriptEndpoint.groovy:304)
	at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.postUserEndpoint(UserCustomScriptEndpoint.groovy:208)
2016-08-08 15:06:22,652 WARN [http-nio-8090-exec-10] [common.security.jersey.XsrfResourceFilter] passesAllXsrfChecks XSRF checks failed for request: https://confluencet.blogistics.be/rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.common.admin.CopyTree , origin: null , referrer: null
 -- url: /rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.common.admin.CopyTree | traceId: 0e8783c4e0b4c430 | userName: koen.gillard
2016-08-08 15:06:22,662 ERROR [http-nio-8090-exec-1] [runner.rest.common.UserCustomScriptEndpoint] doEndpoint *************************************************************************************
 -- url: /rest/scriptrunner/latest/custom/copyPageTree | traceId: 678498cee849dab8 | userName: koen.gillard | sr.execution.id: 41121a63-6e96-405b-b680-caeb8150c53e
2016-08-08 15:06:22,664 ERROR [http-nio-8090-exec-1] [runner.rest.common.UserCustomScriptEndpoint] doEndpoint Script endpoint failed on method: POST copyPageTree
 -- url: /rest/scriptrunner/latest/custom/copyPageTree | traceId: 678498cee849dab8 | userName: koen.gillard | sr.execution.id: 41121a63-6e96-405b-b680-caeb8150c53e
groovyx.net.http.HttpResponseException: Forbidden
	at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:651)
	at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:222)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
	at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
	at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:434)
	at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:383)
	at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)
	at Script586$_run_closure1.doCall(Script586.groovy:47)
	at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.doEndpoint(UserCustomScriptEndpoint.groovy:304)
	at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.postUserEndpoint(UserCustomScriptEndpoint.groovy:208)

Any suggestions on how to solve this?

I know the BASIC <infohere> is correct because I'm using this to call the Custom REST Endpoint.

Kind regards

Koen Gillard

2 answers

1 accepted

There are multiple problems here, principally the URL is wrong, and the parameters are wrong in name and type.

How can you know what they are? By using the network tools to examine the request in your browser when you do it via the UI, or using something like Fiddler.

Below is a working script that calls copy tree remotely. I presume the caller is not the same instance as the target host, if it is there are simpler ways to do this.

import groovy.json.JsonBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import org.apache.http.HttpRequest
import org.apache.http.HttpRequestInterceptor
import org.apache.http.protocol.HttpContext

def copyPageTreeParams = [
    FIELD_SRC_PAGE_ID   : "[\"98310\"]",
    FIELD_TARGET_PAGE_ID: "[\"753700\"]",
]

def confluence = new HTTPBuilder("http://localhost:8080/confluence/")
confluence.client.addRequestInterceptor(new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, HttpContext httpContext) {
        httpRequest.addHeader('Authorization', 'Basic ' + 'admin:admin'.bytes.encodeBase64().toString())
        httpRequest.addHeader('X-Atlassian-Token', 'no-check')
    }
})

confluence.request(Method.POST, ContentType.JSON) { req -&gt;
    uri.path = "rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.confluence.admin.CopyTree"
    send ContentType.URLENC, [scriptParams: new JsonBuilder(copyPageTreeParams)]
    response.success = { resp, xml -&gt;
        println "CopyTree response status: ${resp.statusLine}"
        assert resp.statusLine.statusCode == 200
    }
}

 

 

Thanks Jamie!

I indeed want to use the script from 'inside' the same Confluence instance.

You mention a simpler way, are you willing to share this solution?

Please could you use the \{code\} macro, which I have done for you now - code is unreadable without the formatting.

Additionally including the imports makes it much easier for the person trying to help reproduce the issue.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 24, 2018 in Confluence

Atlassian Research opportunity with Confluence templates

Do you use templates with Confluence? Take part in a remote 1-hr workshop. You'll receive USD $100 for your time!   We're looking for people to participate in a   remote 1-hr workshop...

1,031 views 15 13
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you