2014-10-27 16 views
0

我正在調用發送HTML電子郵件的BPM Web服務。我在JDeveloper 11.1.1.7中生成了一個Web服務代理。電子郵件正文的類型是xsd:string,它應該映射到java String。我知道某些字符(例如<> &)在代理操作期間在xml文檔創建期間被保留和轉換。如何防止在JDeveloper中使用Web服務代理類時轉換爲<

使用SOAPUI來調用服務,我可以傳遞正文<h1>My Heading</h1>和服務正確響應,發送電子郵件與預期的HTML。當從調用代理的POJO執行相同操作時,<h1>轉換爲&lt;h1&gt;My heading&lt;/h1&gt;

我試過將身體作爲CDATA部分傳遞,但這並沒有什麼區別。我曾嘗試將該體轉換爲字節,然後在調用之前返回到UTF-8字符串,但仍然沒有區別。我有權訪問BPM服務代碼。有沒有一種方法可以將HTML從代理髮送到服務,並保留特殊字符?

回答

0

我終於明白了這一點。雖然JDeveloper Web服務代理生成器在大多數情況下都很有用,但在這種情況下,並不是因爲我需要將xml特殊字符發送到服務。也許有辦法操縱代理代碼來做你想做的事情,但我無法弄清楚。

特別的幫助是this AMIS blog entry。如果您在JAXB編組過程中需要處理特殊字符,this entry will help you toosteps to use the java URLConnection class is here的一個偉大的總結,並回答points to a library that would probably make life even easier

所以這裏是下面的原始包裝代碼。我們編寫的特定BPM電子郵件服務也寫入日誌,並解釋了原始xml輸入中的複雜類型。當然,我會從主sendMail包裝方法中傳入的POJO對象中填充電子郵件的值。

package com.yourdomain.sendmail.methods; 

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 

import java.util.List; 
import java.util.Map; 
import java.util.Scanner; 

import oracle.adf.model.connection.url.URLConnectionProxy; 
import oracle.adf.share.ADFContext; 


public class SendMailWrapper { 

public SendMailWrapper() { 
    super(); 
} 

public static void main(String[] args) throws MalformedURLException, IOException { 
    SendMailWrapper w = new SendMailWrapper(); 
    w.sendMail(); 
} 

public void sendMail() throws MalformedURLException, IOException { 
    String xmlInput = 
     "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " + 
     "xmlns:sen=\"http://xmlns.oracle.com/bpmn/bpmnProcess/SendEmailProcess\" " + 
     "xmlns:ema=\"http://www.wft.com/BPM/SendEmail/Email\">\n" + 
     "<soapenv:Header/>" + 
     "<soapenv:Body>\n" + 
     "<sen:start>\n" + 
      "<ema:emailInput>\n" + 
       "<ema:emailContent>\n" + 
        "<ema:toAddr>[email protected]</ema:toAddr>\n" + 
        "<ema:fromAddr></ema:fromAddr>\n" + 
        "<ema:ccAddr></ema:ccAddr>\n" + 
        "<ema:bccAddr></ema:bccAddr>\n" + 
        "<ema:subject>SendMail HTML</ema:subject>\n" + 
        "<ema:body><h1>My Heading</h1><p>Text</p></ema:body>\n" + 
        "<ema:contentType>text/html</ema:contentType>\n" + 
       "</ema:emailContent>\n" + 
       "<ema:emailHistory>\n" + 
        "<ema:projectName>Soap Test</ema:projectName>\n" + 
        "<ema:reqID></ema:reqID>\n" + 
        "<ema:compositeID></ema:compositeID>\n" + 
        "<ema:processID></ema:processID>\n" + 
        "<ema:processName></ema:processName>\n" + 
        "<ema:activityName></ema:activityName>\n" + 
        "<ema:insertDate></ema:insertDate>\n" + 
        "<ema:insertByID></ema:insertByID>\n" + 
        "<ema:insertByName></ema:insertByName>\n" + 
        "<ema:commentType></ema:commentType>\n" + 
        "<ema:commentInfo></ema:commentInfo>\n" + 
       "</ema:emailHistory>\n" + 
      "</ema:emailInput>\n" + 
     "</sen:start>\n" + 
     "</soapenv:Body>\n" + 
     "</soapenv:Envelope>\n"; 

    System.out.println(xmlInput); 

    String wsURL = getWsdlUrl(); 
    URL url = new URL(wsURL); 
    URLConnection connection = url.openConnection(); 
    HttpURLConnection httpConn = (HttpURLConnection)connection; 

    ByteArrayOutputStream bout = new ByteArrayOutputStream(); 
    byte[] buffer = new byte[xmlInput.length()]; 
    buffer = xmlInput.getBytes(); 
    bout.write(buffer); 
    byte[] b = bout.toByteArray(); 
    String SOAPAction = "start"; //this is the method in the service 
    httpConn.setRequestProperty("Content-Length", String.valueOf(b.length)); 
    httpConn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); 

    //some other props available but don't need to be set... 
    //httpConn.setRequestProperty("Accept-Encoding", "gzip,deflate"); 
    //httpConn.setRequestProperty("Host", "your.host.com:80"); 
    //httpConn.setRequestProperty("Connection", "Keep-Alive"); 
    //httpConn.setRequestProperty("User-Agent", "Apache-HttpClient/4.1.1 (java 1.5)"); 

    httpConn.setRequestProperty("SOAPAction", SOAPAction); 
    httpConn.setRequestMethod("POST"); 
    httpConn.setDoOutput(true); 
    httpConn.setDoInput(true); 

    OutputStream out = httpConn.getOutputStream(); 
    out.write(b); 
    out.close(); 

    //check response code... 
    int status = httpConn.getResponseCode(); 
    String respMessage = httpConn.getResponseMessage(); 
    System.out.println("RESPONSE CODE: " + status + " RESPONSE MESSAGE: " + respMessage); 

    //check response headers... 
    for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { 
     System.out.println(header.getKey() + "=" + header.getValue()); 
    } 

    //check error stream - this helps alot when debugging... 
    InputStream errorStream = ((HttpURLConnection)connection).getErrorStream(); 
    if (errorStream != null) { 
     System.out.println("Error Stream: " + convertStreamToString(errorStream)); 
    } 

    //if there was an expected response, you need to parse it... 
    /* String responseString = ""; 
    String outputString = ""; 
    InputStreamReader isr = new InputStreamReader(httpConn.getInputStream()); 
    BufferedReader in = new BufferedReader(isr); 
    while ((responseString = in.readLine()) != null) { 
     outputString = outputString + responseString; 
    } 
    isr.close(); 
    System.out.println("OUT: " + outputString); */ 
} 

static String convertStreamToString(InputStream is) { 
    Scanner s = new Scanner(is).useDelimiter("\\A"); 
    return s.hasNext() ? s.next() : ""; 
} 

private static String getWsdlUrl() { 
    String result = null; 

    try { 
     URLConnectionProxy wsConnection = (URLConnectionProxy)ADFContext.getCurrent().getConnectionsContext().lookup("SendMailProxyConnection"); 
     result = wsConnection.getURL().toExternalForm(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return result; 
} 

}

編碼愉快。

相關問題