如何预防<转换为<在JDeveloper中使用Web服务代理类时

时间:2014-10-27 15:47:02

标签: web-services proxy oracle11g oracle-adf jdeveloper

我正在调用发送HTML电子邮件的BPM Web服务。我在JDeveloper 11.1.1.7中生成了一个Web服务代理。电子邮件正文的类型是xsd:string,它应该映射到java String。我理解某些字符,例如< > &,在代理操作期间创建xml文档期间保留和转换。

使用SOAPUI调用服务,我可以将正文作为<h1>My Heading</h1>传递,服务正确响应,按预期发送带有HTML的电子邮件。从调用代理的POJO执行相同操作时,<h1>将转换为&lt;h1&gt;My heading&lt;/h1&gt;

我尝试将身体作为CDATA部分传递,但这没有区别。我已经尝试将主体转换为字节,然后在调用之前将其转换为UTF-8字符串,但仍然没有区别。我可以访问BPM服务代码。有没有办法可以从代理服务器向服务发送html,保留特殊字符?

1 个答案:

答案 0 :(得分: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>your.name@yourdomain.com</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;
}

}

快乐的编码。