Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. /*** BEGIN META {
  2. "name" : "GetAssets",
  3. "comment" : "Logs in with the specified user, then performs a quicksearch.",
  4. "authors" : [
  5. { "name" : "Carlo Bongiovanni", "email" : "carlo.bongiovanni@gmail.com" }
  6. ]
  7. } END META**/
  8.  
  9. import groovy.json.*
  10. import hudson.FilePath
  11.  
  12. import java.nio.file.Files
  13. import java.util.regex.Matcher
  14. import java.util.regex.Pattern
  15. import java.net.MalformedURLException
  16.  
  17. def vars = this.binding.variables
  18.  
  19. def out = vars['out'] ?: System.out
  20.  
  21. cipServerAddress = vars['CIP_BASE_URL'] ?: 'http://cum10:8080/CIP'
  22. httpAuthUser = vars['USER'] ?: 'sample'
  23. httpAuthPassword = vars['PASSWORD'] ?: 'test..'
  24. catalogAlias = vars['CATALOG_ALIAS'] ?: 'sample'
  25. fieldsResult = vars['FIELDS_RESULT'] ?: 'fields'
  26. searchPath = vars['SEARCH_PATH'] ?: 'zebra'
  27. marchio = vars['MARCHIO']
  28. workspaceSubDir = vars['WORKSPACE_SUB_DIR'] ?: '.'
  29. latestBy = vars['LATEST_BY'] ?: ''
  30.  
  31. def parseJSON(text) {
  32. def slurper = new JsonSlurper()
  33. def result = slurper.parseText(text)
  34.  
  35. return result
  36. }
  37.  
  38. def conn, cipLoginApi, queryResultText
  39.  
  40. cipQuickSearchApi = "/metadata/search/${catalogAlias}/${fieldsResult}?user=${httpAuthUser}&password=${httpAuthPassword}&quicksearchstring=${searchPath}&sortby=ID:descending&sortby=Record%20Name"
  41.  
  42. conn = (cipServerAddress + cipQuickSearchApi).toURL().openConnection()
  43. conn.setRequestMethod("POST");
  44. conn.setDoOutput(true);
  45.  
  46. OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
  47. osw.write(String.format(''));
  48. osw.flush();
  49. osw.close();
  50.  
  51. conn.getInputStream()
  52.  
  53. if( conn.responseCode == 200 ) {
  54. println "########################## CONNECTION ACCEPTED ##########################"
  55. println 'Session Opened'
  56. queryResultText = conn.content.text
  57. } else {
  58. println "########################## CONNECTION REFUSED ##########################"
  59. return false
  60. }
  61.  
  62. def jsonResult = parseJSON(queryResultText)
  63. def resultAssets = jsonResult.totalcount
  64.  
  65. println "Trovati ${resultAssets} assets corrispondendi ai criteri di ricerca forniti."
  66.  
  67. def ids = jsonResult.'items'
  68. ids.each {
  69. println "Copying " + it.'id'
  70.  
  71. saveToFtpUri = cipServerAddress + '/asset/download/' + catalogAlias + '/' + it.id + '?location=export-location'
  72. HttpURLConnection ftpConnection = null
  73. try {
  74. ftpConnection = saveToFtpUri.toURL().openConnection() as HttpURLConnection
  75. } catch (IOException e) {
  76. println("Error opening the connection:")
  77. println(e.class.name)
  78. println(e.message)
  79. }
  80.  
  81. def jsonSlurper = new JsonSlurper();
  82.  
  83. if (ftpConnection) {
  84. int responseCode = -2
  85. String responseMessage = ''
  86. try {
  87. responseCode = ftpConnection.responseCode
  88. responseMessage = ftpConnection.responseMessage
  89. parsedMessage = jsonSlurper.parseText(saveToFtpUri.toURL().getText())
  90. } catch (IOException e) {
  91. println("Error trying to get the Http result:")
  92. println(e.class.name)
  93. println(e.message)
  94. }
  95. println "Connect result: ${responseCode} - ${responseMessage}"
  96. println parsedMessage
  97. }
  98. }
  99.  
  100. return true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement