Import document with bash script using SOAP
The script imports a document into OpenKM server using SOAP Webservices.
Download the script import_sh.zip.
#!/bin/sh############################################################################### Copyright (c) 2013: Jörg Palmer, JoergPalmer @ OpenKM forums## Script for importing a document into OpenKM via web services# 1. Logon using an "import user"# 2. Import the document into a pre-defined folder# 3. Logoff the server##############################################################################
### Configuration section, please adjust to your setupOKM_ID="autoImport" # User ID / nameOKM_PW="Import42" # Password### Configuration section ends here### DO NOT CHANGE FROM HERE UNLESS YOU KNOW WHAT YOU ARE DOING!
# Constants definitionTOOLNAME="OpenKM_import"VERSION="v1.0"
# Possible exit codesEXIT_OK="0" # 0=successEXIT_BAD_ARGS="1" # 1=wrong number of parametersEXIT_BAD_LOGON="2" # 2=logon to OpenKM failedEXIT_BAD_LOGOFF="3" # 3=Logoff from OpenKM failed
# LOG LevelLOG_ERR="0" # 0=only error messagesLOG_INFO="1" # 1=error messages and some infosLOG_DEBUG="2" # 2=debug level logging
START=`date +%s`
usage() { cat << EOF--------------------------------------------------------------------------------------Script to import a document into an OpenKM server, using SOAP web services.Please adjust the user id settings at the beginning of the script to your needs.
Copyright: Jörg PalmerVersion: $VERSION
Usage: OpenKM_import.sh [-h] [-v] [-g] [-u url] [-p path] document
Options:
-h : Display this help message-v : Increase the verbosity (this option can be used more than once)-g : Activate debug mode: - Set the verbosity to the highest possible-u url : Set the url to the OpenKM instance Default: http://localhost:8080/OpenKM-p path : Set the taxonomy path to be imported to Default: /okm:root/imported
document : The file to be imported--------------------------------------------------------------------------------------EOF}
################################################## Get an absolute path from a relative path to a file## Param1 : Relative path# Returns: 1 if the folder in which the file is located does not exist# 0 otherwise#################################################absolutePath() { local wdsave absolutepath wdsave="$(pwd)" ! cd "$(dirname "$1")" 1> /dev/null 2> /dev/null && return 1 absolutepath="$(pwd)/$(basename "$1")" cd "$wdsave" echo "$absolutepath" return 0}
# Initialization the configuration parameters with default valuesVERBOSITY="$LOG_ERR" # default verbosity levelOKM_URL="http://localhost:8080/OpenKM" # OpenKM URLOKM_PATH="/okm:root/imported" # Taxonomy path to be imported to
# Parse optional command line argumentswhile getopts ":hvgu:p:" opt; do case $opt in h) usage ; exit 0 ;; v) VERBOSITY=$(($VERBOSITY+1)) ;; g) VERBOSITY="10" ;; u) OKM_URL="$OPTARG" ;; p) OKM_PATH="$OPTARG" ;; \?) echo "Invalid option: -$OPTARG" >&2 usage exit $EXIT_BAD_ARGS ;; :) echo "Option -$OPTARG requires an argument" >&2 usage exit $EXIT_BAD_ARGS ;; esacdone
# Remove the optional arguments parsed above.shift $((OPTIND-1))
# Check if the number of mandatory parameters# provided is as expectedif [ "$#" -ne "1" ]; then echo "Document file name is missing! ($# arguments provided)" >&2 usage exit $EXIT_BAD_ARGSfi
[ $VERBOSITY -ge $LOG_INFO ] && echo "$TOOLNAME, version: $VERSION"
# Generate the document informationFILENAME="`absolutePath "$1"`"FILENAME_BASE="${FILENAME##*/}"
[ $VERBOSITY -ge $LOG_INFO ] && echo "Document file: '$FILENAME', basename: '$FILENAME_BASE'"
# Logon[ $VERBOSITY -ge $LOG_DEBUG ] && echo "Logging on to OpenKM Server '$OKM_URL' with '$OKM_ID'"response_Auth_logon=$(curl --silent --header "Content-Type: text/xml;charset=UTF-8" \ --header "SOAPAction:action" --data @- \ --request POST "${OKM_URL}/services/OKMAuth" << EOF <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.openkm.com"> <soapenv:Header/> <soapenv:Body> <ws:login> <user>$OKM_ID</user> <password>$OKM_PW</password> </ws:login> </soapenv:Body> </soapenv:Envelope>EOF)
# Check for response code -> Got access token?token=$(grep -oPm1 "(?<=<return>)[^<]+" <<< "$response_Auth_logon")if [ -z "$token" ]; then echo "Error logging on to OpenKM server!" >&2 echo "Response: "$response_Auth_logon >&2 && exit $EXIT_BAD_LOGONelse [ $VERBOSITY -ge $LOG_DEBUG ] && echo "Logon successful (Token: '"$token"')"fi
# Content must be base64 -> read and encode[ $VERBOSITY -ge $LOG_DEBUG ] && echo "Encoding input file as Base64"content="`base64 "$FILENAME"`"
# Import the document[ $VERBOSITY -ge $LOG_DEBUG ] && echo "Importing document '$FILENAME_BASE' to '$OKM_PATH/$FILENAME_BASE'"response_Doc=$(curl --silent --header "Content-Type: text/xml;charset=UTF-8" \ --header "SOAPAction:action" --data @- \ --request POST "${OKM_URL}/services/OKMDocument" << EOF<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.openkm.com"> <soapenv:Header/> <soapenv:Body> <ws:createSimple> <token>$token</token> <docPath>$OKM_PATH/$FILENAME_BASE</docPath> <content>$content</content> </ws:createSimple> </soapenv:Body></soapenv:Envelope>EOF)
# Check for successfault=$(grep -oPm1 "(?<=<faultcode>)[^<]+" <<< "$response_Doc")if [ -n "$fault" ]; then echo "Error importing document: "$fault": '" \ $(grep -oPm1 "(?<=<faultstring>)[^<]+" <<< "$response_Doc") \ "' -> "$(grep -oPm1 "[^:]+Exception" <<< "$response_Doc") >&2 echo "Response: "$response_Doc >&2else uuid=$(grep -oPm1 "(?<=<uuid>)[^<]+" <<< "$response_Doc") [ $VERBOSITY -ge $LOG_DEBUG ] && echo "Document '$uuid' imported successfully"fi
# Logoff[ $VERBOSITY -ge $LOG_DEBUG ] && echo "Logging off from OpenKM server"response_Auth_logoff=$(curl --silent --header "ConteONnt-Type: text/xml;charset=UTF-8" \ --header "SOAPAction:action" --data @- \ --request POST "${OKM_URL}/services/OKMAuth" << EOF<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.openkm.com"> <soapenv:Header/> <soapenv:Body> <ws:logout> <token>$token</token> </ws:logout> </soapenv:Body></soapenv:Envelope>EOF)
# Check for successfault=$(grep -oPm1 "(?<=<faultcode>)[^<]+" <<< "$response_Auth_logoff")if [ -n "$fault" ]; then echo "Error logging off from server: "$fault": '" \ $(grep -oPm1 "(?<=<faultstring>)[^<]+" <<< "$response_Auth_logoff") \ "' -> "$(grep -oPm1 "[^:]+Exception" <<< "$response_Auth_logoff") >&2 echo "Response: "$response_Auth_logoff >&2else [ $VERBOSITY -ge $LOG_DEBUG ] && echo "Logoff successful"fi
END=`date +%s`[ $VERBOSITY -ge $LOG_DEBUG ] && echo "Script took $(($END-$START)) seconds"
exit $EXIT_OK