Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*** BEGIN META {
- "name" : "GetAssets",
- "comment" : "Logs in with the specified user, then performs a quicksearch.",
- "authors" : [
- { "name" : "Carlo Bongiovanni", "email" : "carlo.bongiovanni@gmail.com" }
- ]
- } END META**/
- import groovy.json.*
- import hudson.FilePath
- import java.nio.file.Files
- import java.util.regex.Matcher
- import java.util.regex.Pattern
- import java.net.MalformedURLException
- def vars = this.binding.variables
- def out = vars['out'] ?: System.out
- cipServerAddress = vars['CIP_BASE_URL'] ?: 'http://cum10:8080/CIP'
- httpAuthUser = vars['USER'] ?: 'sample'
- httpAuthPassword = vars['PASSWORD'] ?: 'test..'
- catalogAlias = vars['CATALOG_ALIAS'] ?: 'sample'
- fieldsResult = vars['FIELDS_RESULT'] ?: 'fields'
- searchPath = vars['SEARCH_PATH'] ?: 'zebra'
- marchio = vars['MARCHIO']
- workspaceSubDir = vars['WORKSPACE_SUB_DIR'] ?: '.'
- latestBy = vars['LATEST_BY'] ?: ''
- def parseJSON(text) {
- def slurper = new JsonSlurper()
- def result = slurper.parseText(text)
- return result
- }
- def conn, cipLoginApi, queryResultText
- cipQuickSearchApi = "/metadata/search/${catalogAlias}/${fieldsResult}?user=${httpAuthUser}&password=${httpAuthPassword}&quicksearchstring=${searchPath}&sortby=ID:descending&sortby=Record%20Name"
- conn = (cipServerAddress + cipQuickSearchApi).toURL().openConnection()
- conn.setRequestMethod("POST");
- conn.setDoOutput(true);
- OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
- osw.write(String.format(''));
- osw.flush();
- osw.close();
- conn.getInputStream()
- if( conn.responseCode == 200 ) {
- println "########################## CONNECTION ACCEPTED ##########################"
- println 'Session Opened'
- queryResultText = conn.content.text
- } else {
- println "########################## CONNECTION REFUSED ##########################"
- return false
- }
- def jsonResult = parseJSON(queryResultText)
- def resultAssets = jsonResult.totalcount
- println "Trovati ${resultAssets} assets corrispondendi ai criteri di ricerca forniti."
- def ids = jsonResult.'items'
- ids.each {
- println "Copying " + it.'id'
- saveToFtpUri = cipServerAddress + '/asset/download/' + catalogAlias + '/' + it.id + '?location=export-location'
- HttpURLConnection ftpConnection = null
- try {
- ftpConnection = saveToFtpUri.toURL().openConnection() as HttpURLConnection
- } catch (IOException e) {
- println("Error opening the connection:")
- println(e.class.name)
- println(e.message)
- }
- def jsonSlurper = new JsonSlurper();
- if (ftpConnection) {
- int responseCode = -2
- String responseMessage = ''
- try {
- responseCode = ftpConnection.responseCode
- responseMessage = ftpConnection.responseMessage
- parsedMessage = jsonSlurper.parseText(saveToFtpUri.toURL().getText())
- } catch (IOException e) {
- println("Error trying to get the Http result:")
- println(e.class.name)
- println(e.message)
- }
- println "Connect result: ${responseCode} - ${responseMessage}"
- println parsedMessage
- }
- }
- return true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement