-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcqjcr
executable file
·417 lines (388 loc) · 12.5 KB
/
cqjcr
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/sh
# CQ Unix Toolkit
# Copyright (C) 2021 Wunderman Thompson Technology
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
_usage() {
cat <<EOT
Usage: $(basename "${0}") [OPTION...] OPERATION
Allows to browse and modify JCR tree in easy and automated manner like UNIX ls,
touch and mkdir commands
Examples:
cqjcr -u admin -l / # Return JCR child nodes for '/' existing on
# the local instance
cqjcr -i http://localhost:5510 # Return JCR child nodes for '/content' for
-l /content -p secret # localhost instance on tcp port 5510 with
# password provided: 'secret'
cqjcr -u admin -s / # Show JCR properties for '/' node in pretty
# format
cqjcr -u admin -s /content -m # Show JCR properties for '/content' node in
# machine-readable format
cqjcr -u admin -c /content/new # Create (or modify) JCR node 'new' under
# '/content' node with default
# 'nt:unstructured' primary type
cqjcr -u admin -c /content/new # Create (or modify) JCR node 'new' under
-t cq:Page # '/content' node with 'cq:Page' primary
# type
cqjcr -u admin -a /content/www # Create or modify JCR node 'www' under
-n name1 -v val1 # '/content' node with 2 properties: one
-n name2 -v val2 # with name: 'name1' and value 'val1', second
# with name: 'name2' and value 'val2'
cqjcr -u admin -d /content/trash # Delete node 'trash' under '/content' for
# local instance
Operations:
-s PATHSPEC show properties for specified JCR nodes 'PATHSPEC'
-d PATHSPEC delete node specified by path 'PATHSPEC'
-l PATHSPEC show JCR children nodes for specified 'PATHSPEC' node
-a PATHSPEC create/modify node specified by 'PATHSPEC'
-c PATHSPEC create empty node specified by 'PATHSPEC'
Each non-exising element from path will be created
Options:
-u use specified username for connection
-p use provided password for authentication
-i use specified instance URL to connect
-m machine-friendly mode for -s option
-t JCRTYPE in conjuction with -c allows to specify jcr:PrimaryType
-n NAME in conjuction with -a allows to specify property name
(can be used many times but in pairs with -v)
-v VALUE in conjuction with -a allows to specify property value
(can be used many times but in pairs with -n)
EOT
exit 1
}
_list_children() {
FILEPATH="${root}.1.json"
URL="${instance}${FILEPATH}"
REFERERHEADER="Referer: ${URL}"
STATUS=$("${CURLBIN}" \
-s \
--write-out "%{http_code}" \
-u "${AUTH}" \
-H "${REFERERHEADER}" \
"${URL}")
EXITCODE="${?}"
"${API}" -C "${EXITCODE}"
if [ ${EXITCODE} -ne 0 ]; then
exit ${EXITCODE}
fi
STATUSCODE=$(echo "${STATUS}" | grep -o -E '[0-9]{3}' | tail -n 1)
"${API}" -H "${STATUSCODE}"
EXITCODE2=$?
if [ ${EXITCODE2} -ne 0 ]; then
exit ${EXITCODE2}
fi
STATUS=$(echo "${STATUS}" | ${SEDX} 's#[0-9]{3}$##')
NEW_NODE=':\{'
NODE_NAME='[^"]+'
STR_SEP='"'
NODE_REGEXP_FULL_EXTRACT="(${STR_SEP}${NODE_NAME}${STR_SEP}${NEW_NODE})"
NODE_REGEXP_EXTRACT="${STR_SEP}(${NODE_NAME})${STR_SEP}${NEW_NODE}"
FULL_NODE_REGEXP=".*${NODE_REGEXP_EXTRACT}.*"
CLEARED_STATUS=$(echo "${STATUS}" |
tr -d '\t' |
${SEDX} "s#${NODE_REGEXP_FULL_EXTRACT}#${TAB}\1#g" |
tr '\t' '\n' |
grep -E "${FULL_NODE_REGEXP}" |
${SEDX} "s#${FULL_NODE_REGEXP}#\1#")
if [ ! -z "${CLEARED_STATUS}" ]; then
printf "%s\n" "${CLEARED_STATUS}"
fi
exit 0
}
_list_properties() {
FILEPATH="${root}.json"
URL="${instance}${FILEPATH}"
REFERERHEADER="Referer: ${URL}"
STATUS=$(${CURLBIN} \
-s \
--write-out "%{http_code}" \
-u "${AUTH}" \
-H "${REFERERHEADER}" \
"${URL}")
EXITCODE=${?}
"${API}" -C "${EXITCODE}"
if [ ${EXITCODE} -ne 0 ]; then
exit ${EXITCODE}
fi
STATUSCODE=$(echo "${STATUS}" | grep -o -E '[0-9]{3}' | tail -n 1)
"${API}" -H "${STATUSCODE}"
EXITCODE2=$?
if [ ${EXITCODE2} -ne 0 ]; then
exit ${EXITCODE2}
fi
NODE_NAME='[^"]+'
NUMBER='-?[0-9\.]+'
STRING='[^"]*'
ARR='\[("[^"]*",?)*\]'
BOOL='(true|false)'
QT='"'
PROP_ARR="(${QT}${NODE_NAME}${QT}:${ARR},?)"
PROP_BOOL="(${QT}${NODE_NAME}${QT}:${BOOL},?)"
PROP_STR="(${QT}${NODE_NAME}${QT}:${QT}${STRING}${QT},?)"
PROP_NUM="(${QT}${NODE_NAME}${QT}:${NUMBER},?)"
TOKENS_ARR="${QT}(${NODE_NAME})${QT}:(${ARR}),?"
TOKENS_BOOL="${QT}(${NODE_NAME})${QT}:(${BOOL}),?"
TOKENS_STR="${QT}(${NODE_NAME})${QT}:${QT}(${STRING})${QT},?"
TOKENS_NUM="${QT}(${NODE_NAME})${QT}:(${NUMBER}),?"
STATUS=$(echo "${STATUS}" | ${SEDX} 's#[0-9]{3}$##')
STATUS=$(echo "${STATUS}" | ${SEDX} 's#\\\"#%9999%#' |
${SEDX} "s#${PROP_NUM}#${TAB}\1${TAB}#g" |
tr '\t' '\n' |
${SEDX} "s#${PROP_STR}#${TAB}\1${TAB}#g" |
tr '\t' '\n' |
${SEDX} "s#${PROP_BOOL}#${TAB}\1${TAB}#g" |
tr '\t' '\n' |
${SEDX} "s#${PROP_ARR}#${TAB}\1${TAB}#g" |
tr '\t' '\n' |
grep -E "(${PROP_NUM}|${PROP_STR}|${PROP_ARR}|${PROP_BOOL})" |
${SEDX} "s#.*${TOKENS_NUM}.*#\1${TAB}\2#" |
${SEDX} "s#.*${TOKENS_STR}.*#\1${TAB}\2#" |
${SEDX} "s#.*${TOKENS_BOOL}.*#\1${TAB}\2#" |
${SEDX} "s#.*${TOKENS_ARR}.*#\1${TAB}\2#" |
${SEDX} 's#%9999%#"#g')
if [ "${machine_friendly}" -eq 0 ]; then
SEP='|'
COLUMN=$(which column)
if [ ${?} -ne 0 -o -z "${COLUMN}" ]; then
echo "Cannot find column utility" >&2
exit 1
fi
printf "NAME\tVALUE\n%s\n" "${STATUS}" | tr '\t' "${SEP}" |
"${COLUMN}" -xt -c 2 -s "${SEP}" 2>/dev/null
else
printf "%s\n" "${STATUS}"
fi
exit 0
}
_modify_properties() {
if [ ${n_counter} -le 0 ]; then
echo "Use -n/-v to provide property name/value to add or modify." >&2
_usage
fi
echo "Creating/modyfing ${root}"
actions=""
for operation in ${operations}; do
propname=$(echo "${operation}" | cut -f1 -d '=' | ${SEDX} 's#%20# #g')
propvalue=$(echo "${operation}" | cut -f2- -d '=' | ${SEDX} 's#%20# #g')
echo " Setting '${propname}' to: '${propvalue}' "
actions="${actions}\055F\000${propname}=${propvalue}\000"
done
FILEPATH="${root}"
URL="${instance}${FILEPATH}"
REFERERHEADER="Referer: ${URL}"
args="${actions}${URL}"
# shellcheck disable=SC2059
STATUS=$(printf "${args}" |
xargs -0 "${CURLBIN}" \
-s -X POST \
--write-out "%{http_code}" \
-u "${AUTH}" \
-H "${REFERERHEADER}")
EXITCODE=${?}
if [ ${EXITCODE} -ne 0 ]; then
echo "There was a problem calling: xargs + curl" >&2
exit ${EXITCODE}
fi
STATUSCODE=$(echo "${STATUS}" | grep -o -E '[0-9]{3}' | tail -n 1)
"${API}" -H "${STATUSCODE}"
EXITCODE2=$?
if [ ${EXITCODE2} -ne 0 ]; then
exit ${EXITCODE2}
fi
echo "Successfully created/modified: ${root}"
}
_create_node() {
[ -z "${type}" ] && type="nt:unstructured"
FILEPATH="${root}"
URL="${instance}${FILEPATH}"
REFERERHEADER="Referer: ${URL}"
STATUS=$(${CURLBIN} \
-s \
-X POST \
-F"jcr:primaryType=${type}" \
--write-out "%{http_code}" \
-u "${AUTH}" \
-H "${REFERERHEADER}" \
"${URL}")
EXITCODE=${?}
"${API}" -C "${EXITCODE}"
if [ ${EXITCODE} -ne 0 ]; then
exit ${EXITCODE}
fi
STATUSCODE=$(echo "$STATUS" | grep -o -E '[0-9]{3}' | tail -n 1)
"${API}" -H "${STATUSCODE}"
EXITCODE2=$?
if [ ${EXITCODE2} -ne 0 ]; then
exit ${EXITCODE2}
fi
echo "Created/modified: ${root}"
}
_delete_node() {
FILEPATH="${root}"
URL="${instance}${FILEPATH}"
REFERERHEADER="Referer: ${URL}"
STATUS=$(${CURLBIN} \
-s \
-X DELETE \
--write-out "%{http_code}" \
-u "${AUTH}" \
-H "${REFERERHEADER}" \
"${URL}")
EXITCODE=${?}
"${API}" -C ${EXITCODE}
if [ ${EXITCODE} -ne 0 ]; then
exit ${EXITCODE}
fi
STATUSCODE=$(echo "${STATUS}" | grep -o -E '[0-9]{3}' | tail -n 1)
"${API}" -H "${STATUSCODE}"
EXITCODE2=$?
if [ ${EXITCODE2} -ne 0 ]; then
exit ${EXITCODE2}
fi
echo "Deleted: ${root}"
}
_use_option_warning() {
echo "You need to choose one of the following operation:" >&2
echo " -l / -s / -c / -a" >&2
_usage
}
TAB=$(printf '\t')
CWD=$(dirname "${0}")
API="${CWD}/cqapi"
"${API}" -P >/dev/null 2>/dev/null
if [ ${?} -ne 0 ]; then
echo "Fatal: cannot find or test cqapi command" >&2
exit 1
fi
CURLBIN=$("${API}" -c)
if [ ${?} -ne 0 ]; then
echo "Fatal: cannot find curl" >&2
exit 1
fi
SEDX=$("${API}" -s)
# API common options
cmdapi=$("${API}" -P "${@}")
username=$(echo "${cmdapi}" | cut -f1)
password=$(echo "${cmdapi}" | cut -f2)
instance=$(echo "${cmdapi}" | cut -f3)
passed=$(echo "${cmdapi}" | cut -f4)
apigetopts=$(echo "${cmdapi}" | cut -f5)
root=""
list=0
modify=0
properties=0
create=0
type=""
operations=""
property_id=""
n_counter=0
v_counter=0
machine_friendly=0
delete=0
while getopts ":l:s:a:c:t:n:v:d:m${apigetopts}" opt; do
case "${opt}" in
l)
list=1
root="${OPTARG}"
;;
s)
properties=1
root="${OPTARG}"
;;
a)
modify=1
root="${OPTARG}"
;;
c)
create=1
root="${OPTARG}"
;;
d)
delete=1
root="${OPTARG}"
;;
m)
machine_friendly=1
;;
t)
type="${OPTARG}"
;;
v)
v_counter=$((v_counter + 1))
modify=1
if [ ! -z "${property_id}" ]; then
propvalue=$(echo "${OPTARG}" | sed 's# #%20#g')
operations="${operations} ${property_id}=${propvalue}"
property_id=""
else
echo "For each -v option there must be -n preceeding" \
"option specified" >&2
echo ""
_usage
fi
;;
n)
n_counter=$((n_counter + 1))
modify=1
property_id=$(echo "${OPTARG}" | sed 's# #%20#g')
;;
\?)
echo "Invalid option: -$OPTARG" >&2
_usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
_usage
;;
esac
done
shift $((OPTIND - 1))
if [ ${#} -gt 0 -o "${passed}" -eq 0 ]; then
_use_option_warning
fi
if [ $((list + properties + create + delete + modify)) -ne 1 ]; then
_use_option_warning
fi
if [ ${n_counter} -ne ${v_counter} ]; then
echo "Options: -n and -v are connected together. You need to specify" \
"matching pairs of such options (name/value)." >&2
echo ""
_usage
fi
if [ ${machine_friendly} -eq 1 -a ${properties} -eq 0 ]; then
echo "Option: -m can be used only in conjuction with -s" >&2
echo ""
_usage
fi
if [ ${create} -eq 0 -a ! -z "${type}" ]; then
echo "Option: -t can be used only in conjuction with -c" >&2
echo ""
_usage
fi
if [ ${modify} -eq 0 -a ${n_counter} -gt 0 ]; then
echo "Options: -n/-v can be used only in conjuction with -a" >&2
echo ""
_usage
fi
root_valid=$(echo "${root}" | grep -c '^/')
if [ "${root_valid}" -ne 1 ]; then
echo "PATHSPEC argument needs to be valid JCR path, i.e. /content" >&2
echo ""
_usage
fi
AUTH="${username}:${password}"
[ ${list} -eq 1 ] && _list_children
[ ${properties} -eq 1 ] && _list_properties
[ ${modify} -eq 1 ] && _modify_properties
[ ${create} -eq 1 ] && _create_node
[ ${delete} -eq 1 ] && _delete_node