繁体   English   中英

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

[英]How to prevent < converted to &lt; when using web service proxy class in 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到服务,而保留特殊字符呢?

我终于想通了。 尽管JDeveloper Web服务代理生成器在大多数情况下都是有用的,但在这种情况下它就没有用了,因为我需要将xml特殊字符发送到服务。 也许有一种方法可以操纵代理代码来执行您想要的操作,但我无法弄清楚。

特别的帮助是该AMIS博客条目 而且,如果您在JAXB编组期间需要处理特殊字符,那么该条目也将为您提供帮助 这里是使用Java URLConnection类步骤的简要概述,并且答案指向可能会使生活变得更加轻松的库

所以这是下面的原始包装代码。 我们编写的特定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;
}

}

快乐的编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM