使用 groovy 的 SOAP 请求

发布于 2024-11-02 11:59:02 字数 4500 浏览 0 评论 0原文

我创建了以下 groovy 程序 test.groovy 来调用 BI Publisher Web 服务。我能够得到肥皂反应。谁能帮我将soapresponse( Envelope.Body.runReportResponse.runReportReturn.reportBytes )写入pdf文件。

----------------------- test.groovy ------------------------------------

String username = "Administrator"
String password = "Administrator"
String attributeFormat = "pdf" 
String attributeLocale = "en-US"
String reportAbsolutePath = "/WebserviceTest/eodnoticeofdormancy/eodnoticeofdormancy.xdo"
String attributeTemplate = "Notice"
String p_reportdate = "2011-04-08"

def soapRequest = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pub="http://xmlns.oracle.com/oxp/service/PublicReportService">
   <soapenv:Header/>
   <soapenv:Body>
      <pub:runReport>
         <pub:reportRequest>
            <pub:attributeFormat>${attributeFormat}</pub:attributeFormat>
            <pub:attributeLocale>${attributeLocale}</pub:attributeLocale>
            <pub:attributeTemplate>${attributeTemplate}</pub:attributeTemplate>
            <pub:flattenXML>true</pub:flattenXML>
            <pub:parameterNameValues>
               <!--Zero or more repetitions:-->
               <pub:item>
                  <pub:multiValuesAllowed>false</pub:multiValuesAllowed>
                  <pub:name>p_reportdate</pub:name>
                  <pub:values>
                     <pub:item>${p_reportdate}</pub:item>
                  </pub:values>
               </pub:item>
            </pub:parameterNameValues>
            <pub:reportAbsolutePath>${reportAbsolutePath}</pub:reportAbsolutePath>
        <pub:sizeOfDataChunkDownload>1024</pub:sizeOfDataChunkDownload>
         </pub:reportRequest>
         <pub:userID>Administrator</pub:userID>
         <pub:password>Administrator</pub:password>
      </pub:runReport>
   </soapenv:Body>
</soapenv:Envelope>"""

try {
   def soapUrl = new URL("http://localhost/xmlpserver/services/PublicReportService")
   def connection = soapUrl.openConnection()
   connection.setRequestMethod("POST")
   connection.setRequestProperty("Content-Type" ,"text/html")
   connection.setRequestProperty("SOAPAction", "")
   connection.doOutput = true

   Writer writer = new OutputStreamWriter(connection.outputStream)
   writer.write(soapRequest)
   writer.flush()
   writer.close()
   connection.connect()


   def soapResponse = connection.content.text
   def Envelope = new XmlSlurper().parseText(soapResponse)

   println soapResponse

   String reportBytes = Envelope.Body.runReportResponse.runReportReturn.reportBytes
   String reportContentType = Envelope.Body.runReportResponse.runReportReturn.reportContentType

} catch (Exception e) {
   println "Error occurred"
   println "--------------"
   println e
   e.printStackTrace();
}

---------------------程序的soap响应如下-------------- JVBERi0xLjQNCjUgMCBvYmoNCjw8DQovVHlwZSAVWE9iamVjdA0KL1N1YnR5cGUgL0ltYWdlDQovRmlsdGVyIC9 EQ1REZWNvZGUNCi9MZW5ndGggNDM3NTINCi9XaWR0aCAxNDU3DQovSGVpZ2h0IDIyNA0KL0JpdHNQZXJDb21wb25 lbnQgOA0KL0NvbG9yU3BhY2UgL0RldmljZVJHQg0KPj4NCnN0cmVhbQ0K/9j/4AAQSkZJRgABAQEBLAEsAAD/2w BDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zND L/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM jIyMjL/waARCADgBbEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDA WIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3O Dk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t 7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAEC AwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcrRMiMoEIFEKRobHBCSMzUvAVYnLR ChYkNOEl8RcyGRomJygpKjU2Nzg5OkNERUZHSELKU1RVVLDYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOU lZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMB AAIRAxEAPwDkf+GgPHH/AD00/wD8Bv8A69H/AA0B44/56af/AOA3/wBevLaKAPUv+GgPHH/PTT//AAG/+vR/w0B 44/56af8A+A3/ANevLaKAPpP4PfFDxH428W3em6u1qbeKxedfJh2HcJI1HOemGNe2V8wfs4/8lD1D/sFSf+jYq+n 6ACiiigArH1/xRo/hm3EuqXixMw/dxL80kn+6o5/HpXNeKfHhgubjStCeJrm3Xde3z4aKyTcAxx/Gw3D5RnHvyK8 sfULq5jur7T5NqbSl5rmoHc0jlGRo4+PusDkIFLDrkAcb06Lesg==application/pdfxmlp25327697.tmp

I created the following groovy program test.groovy to invoke BI Publisher web service. I am able to get the soap response. Can anyone please help me to write the soapresponse ( Envelope.Body.runReportResponse.runReportReturn.reportBytes ) to pdf file.

----------------------- test.groovy ------------------------------------

String username = "Administrator"
String password = "Administrator"
String attributeFormat = "pdf" 
String attributeLocale = "en-US"
String reportAbsolutePath = "/WebserviceTest/eodnoticeofdormancy/eodnoticeofdormancy.xdo"
String attributeTemplate = "Notice"
String p_reportdate = "2011-04-08"

def soapRequest = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pub="http://xmlns.oracle.com/oxp/service/PublicReportService">
   <soapenv:Header/>
   <soapenv:Body>
      <pub:runReport>
         <pub:reportRequest>
            <pub:attributeFormat>${attributeFormat}</pub:attributeFormat>
            <pub:attributeLocale>${attributeLocale}</pub:attributeLocale>
            <pub:attributeTemplate>${attributeTemplate}</pub:attributeTemplate>
            <pub:flattenXML>true</pub:flattenXML>
            <pub:parameterNameValues>
               <!--Zero or more repetitions:-->
               <pub:item>
                  <pub:multiValuesAllowed>false</pub:multiValuesAllowed>
                  <pub:name>p_reportdate</pub:name>
                  <pub:values>
                     <pub:item>${p_reportdate}</pub:item>
                  </pub:values>
               </pub:item>
            </pub:parameterNameValues>
            <pub:reportAbsolutePath>${reportAbsolutePath}</pub:reportAbsolutePath>
        <pub:sizeOfDataChunkDownload>1024</pub:sizeOfDataChunkDownload>
         </pub:reportRequest>
         <pub:userID>Administrator</pub:userID>
         <pub:password>Administrator</pub:password>
      </pub:runReport>
   </soapenv:Body>
</soapenv:Envelope>"""

try {
   def soapUrl = new URL("http://localhost/xmlpserver/services/PublicReportService")
   def connection = soapUrl.openConnection()
   connection.setRequestMethod("POST")
   connection.setRequestProperty("Content-Type" ,"text/html")
   connection.setRequestProperty("SOAPAction", "")
   connection.doOutput = true

   Writer writer = new OutputStreamWriter(connection.outputStream)
   writer.write(soapRequest)
   writer.flush()
   writer.close()
   connection.connect()


   def soapResponse = connection.content.text
   def Envelope = new XmlSlurper().parseText(soapResponse)

   println soapResponse

   String reportBytes = Envelope.Body.runReportResponse.runReportReturn.reportBytes
   String reportContentType = Envelope.Body.runReportResponse.runReportReturn.reportContentType

} catch (Exception e) {
   println "Error occurred"
   println "--------------"
   println e
   e.printStackTrace();
}

--------------------- soap response of the program as follows --------------
JVBERi0xLjQNCjUgMCBvYmoNCjw8DQovVHlwZSAvWE9iamVjdA0KL1N1YnR5cGUgL0ltYWdlDQovRmlsdGVyIC9EQ1REZWNvZGUNCi9MZW5ndGggNDM3NTINCi9XaWR0aCAxNDU3DQovSGVpZ2h0IDIyNA0KL0JpdHNQZXJDb21wb25lbnQgOA0KL0NvbG9yU3BhY2UgL0RldmljZVJHQg0KPj4NCnN0cmVhbQ0K/9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCADgBbEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDkf+GgPHH/AD00/wD8Bv8A69H/AA0B44/56af/AOA3/wBevLaKAPUv+GgPHH/PTT//AAG/+vR/w0B44/56af8A+A3/ANevLaKAPpP4PfFDxH428W3em6u1qbeKxedfJh2HcJI1HOemGNe2V8wfs4/8lD1D/sFSf+jYq+n6ACiiigArH1/xRo/hm3EuqXixMw/dxL80kn+6o5/HpXNeKfHhgubjStCeJrm3Xde3z4aKyTcAxx/Gw3D5RnHvyK8sfULq5jur7T5NqbSl5rmoHc0jlGRo4+PusDkIFLDrkAcb06Lesg==application/pdfxmlp25327697.tmp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡 2024-11-09 11:59:02

返回的字符串是 Base64 编码,末尾有一些字符似乎表示 < a href="http://en.wikipedia.org/wiki/MIME#Content-Type" rel="nofollow">内容类型(“application/pdf”)和原始文件名(“xmlp25327697.tmp”)。如果您删除从“application”开始的所有内容,则可以将字符串解码为字节并将其写入文件,如下所示:

byte[] decoded = reportBytes.decodeBase64()
def myFile = new File('something.pdf')
myFile.bytes = decoded

The String coming back is Base64 encoded, with some characters at the end that appear to be indicating the content-type ("application/pdf") and the original file name ("xmlp25327697.tmp"). If you strip off everything starting at "application" you can then decode the String to bytes and write it to a file like this:

byte[] decoded = reportBytes.decodeBase64()
def myFile = new File('something.pdf')
myFile.bytes = decoded
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文