0

我有以下脚本代码,它基本上是在 JSON blob 上迭代并使用 Artifactory(AQL 查询),我试图找到存储在 Artifactory 中的工件属性。

myscript Groovy 文件

import groovy.json.JsonSlurper

if (args.length < 1) {
    println("Please provide a properties file.")
    println("Usage: groovy myscript.groovy properties.json")
    System.exit(1)
}

println args[0]

Properties properties = new Properties()
File propertiesFile = new File(args[1])
propertiesFile.withInputStream {
    properties.load(it)
}

username = properties["ArtifactoryUsername"]
password = properties["ArtifactoryPassword"]
aqlUrl = properties["AqlUrl"]
repoRestUrl = properties["RepoRestUrl"]
repoName = properties["RepoName"]

//decodeBase64(String value);
//Once the properties are loaded for various variables, use JSON blob mapping file. Arg0 is the name of JSON mapping file.
def jsonSlurper = new JsonSlurper()
File mappingFile = new File(args[0])
jsonMapping = jsonSlurper.parseText(mappingFile.text)

def list = jsonMapping.extension[0].rpm

auth = new MyAuthenticator()
auth.setUserPass(username, password)
Authenticator.setDefault(auth);  //this solves UNAUTHORIZED: Status code (401)  (Unauthenticated)
String userPass = "Basic " + username + ":" + password;

list.each { rpm ->
    println(rpm.name)

    url = new URL(aqlUrl)
    conn = url.openConnection()

    conn.setRequestProperty("Authorization", userPass);  

    conn.setRequestMethod("POST")
    conn.setRequestProperty("Content-Type", "text/plain")
    conn.doOutput = true

    def writer = new OutputStreamWriter(conn.outputStream)
    // RpmName1, RpmName2, RpmName2, ..
    writer.write('items.find({"$and" : ' +
                      '[' +
                            '{"repo": "' + repoName + '"}, ' +
                            '{"name": {"$match" : "' + rpm.name + '*"}}' +
                      ']' +
                    '})')

    //writer.flush()
    writer.close()


    //println(conn.)
    println(rpm.name + " (connection response code was): " + conn.getResponseCode())
    //conn.disconnect()
    //System.sleep(500); // this doesn't work. upto 5000.
    //System.sleep(10000); though, 10 seconds returns 200 each time for connection response code
}



我的映射文件如下所示:

{
  "extension": [
    {
      "rpm": [
        {
          "name": "RpmName1",
          "belongsTo": {
            "level1_products": [
              "product1",
              "product2"
            ],
            "level2_products": [
              "Sigma"
            ],
            "sub-system": [
              "sub-system-A"
            ],
            "level3_Products": [
              "level3_AAA",
              "level3_BBB"
            ],
            "final_level": [
              "Colorado"
            ]
          }
        },
        {
          "name": "RpmName2",
          "belongsTo": {
            "level1_products": [
              "product3",
              "product4"
            ],
            "level2_products": [
              "Platinum"
            ],
            "sub-system": [
              "sub-system-B"
            ],
            "level3_Products": [
              "level3_XXX",
              "level3_YYY",
              "level3_ZZZ"
            ],
            "final_level": [
              "California"
            ]
          }
        },
        {
         ....
          .....
         ..... more similar entries here
         ...
        }
       ]
      },
      {
      "zip": [
        {
          "name": "ZipName1",
          "belongsTo": {
            "level1_products": [
              "Zip_AA",
              "Zip_BB"
            ],
            "level2_products": [
              "Zip_level2_i",
              "Zip_level2_i"
            ],
            "final_level": [
              "UberZip_California", "UberZip_Colorado"
            ]
          }
        }
      ]
    }
  ]
}

输出如下:

RpmName1
RpmName1 (connection response code was): 200
RpmName2
RpmName2 (connection response code was): 403
RpmName2
RpmName3 (connection response code was): 403
RpmName4
RpmName4 (connection response code was): 200
...
.....
i.e. mix of both 200 and 403 response code
...
......

当我设置睡眠超过 7 秒时System.sleep(7000),我每次都会收到响应代码打印为 200。

小于 5 的任何值都是不稳定的,即每次运行此脚本时,我得到的 200 或 403 都不同。

似乎我没有做正确的事情,即正确关闭连接,所以我也尝试了disconnect() for connection方法.close() for writing ,但没有任何帮助。

403 的堆栈跟踪输出

Caught: java.io.IOException: Server returned HTTP response code: 403 for URL: http://artifactory.prod.com:9085/artifactory/api/search/aql
java.io.IOException: Server returned HTTP response code: 403 for URL: http://artifactory.prod.com:9085/artifactory/api/search/aql
4

0 回答 0