-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMigration.groovy
More file actions
93 lines (72 loc) · 3.17 KB
/
Copy pathMigration.groovy
File metadata and controls
93 lines (72 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import groovy.transform.Field
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
def cli = new CliBuilder()
cli.with {
h(longOpt: 'help', 'Help - Usage Information')
f(longOpt: 'fileReference', 'Path of XML file', type: String, args: 1)
b(longOpt: 'baseContentPath', 'Root for content', type: String, required:true, args: 1)
h(longOpt: 'host', 'Host Name(Using localhost by default)', type: String, args: 1)
p(longOpt: 'port', 'Port(Using 4502 by default)', type: String, args: 1)
}
def opt = cli.parse(args)
if (!opt) return
if (opt.h) cli.usage()
def baseContentPath = opt.b
String filePath = opt.f?:"blogs.xml"
String hostName = opt.h?:"localhost"
String portNumber = opt.p?:"4502"
MigrationConfiguration.client = new HTTPBuilder("http://${hostName}:${portNumber}" as String)
@Field String resourceType = "myproj/components/resource"
println ":::Using Following configuration:::"
println " File ::: ${filePath}"
println " Server ::: http:://${hostName}:${portNumber}"
println "Root for posting content :::: ${baseContentPath}"
println "::::::::::::::::::::::::::::::::::::"
def file = new File(filePath)
if(!file.exists()){
println "Incorrect file path ${filePath}... Exiting"
return
}
void callPost(String baseURL, Map contentMap) {
/*Setting auth basic in request doesnt work... Had to set it in headers*/
// http.auth.basic("admin", "admin")
MigrationConfiguration.client.request(Method.POST) {
uri.path = baseURL
requestContentType = ContentType.URLENC
headers.'Authorization' = "Basic ${"admin:admin".bytes.encodeBase64().toString()}"
body = contentMap
response.success = { resp ->
}
response.failure = { resp -> println "\nERROR: ${resp.statusLine} for ${uri.path}" }
}
}
def timeStart = System.currentTimeMillis()
println "Starting XML Parsing at ::: ${timeStart}"
/*Using Groovy XML Parser to parse XML*/
def records = new XmlParser().parseText(file.text)?.blog
/*Iterate over all XML records and create a map that will be POSTed to AEM*/
records.eachWithIndex { blog, idx ->
def name = blog.name.text()
def content = blog.outline.text()
def status = blog.status.text()
def parentSubject = blog.subject_parent.text()
Map contentMap = [
"./jcr:primaryType": "cq:Page",
"./jcr:content/jcr:primaryType": "cq:PageContent",
"./jcr:content/jcr:title": "${name}",
"./jcr:content/blog/sling:resourceType": resourceType,
"./jcr:content/blog/status": status,
"./jcr:content/blog/parentSubject": parentSubject,
"./jcr:content/blog/text/sling:resourceType": "foundation/components/text",
"./jcr:content/blog/text/text": "${content}"
]
callPost("${baseContentPath}blog${idx}", contentMap)
}
println "Done with posting ${records.size()} requests at : ${System.currentTimeMillis()}"
println "Completed in ${(System.currentTimeMillis() - timeStart)/1000} sec"
class MigrationConfiguration{
static HTTPBuilder client
}