Элемент Marshal JAXB в SOAP, обрабатывающий escape-символы - PullRequest
0 голосов
/ 01 ноября 2018

Я пытаюсь маршалировать элемент JAXB в обертки SOAPBody, мне удалось избежать закодированных символов в JAXB, но когда я перенаправил его в документ SOAP, он не экранировал эти символы xml. Любая помощь. Заранее спасибо.

И мой проект мал, мы не используем никаких внешних пакетов, пытаясь сделать это в open jdk.

         package test;

        import java.io.File;
        import java.io.IOException;
        import java.io.OutputStreamWriter;
        import java.io.UnsupportedEncodingException;
        import java.io.Writer;

        import javax.xml.bind.JAXBContext;
        import javax.xml.bind.JAXBException;
        import javax.xml.bind.Marshaller;
        import javax.xml.soap.MessageFactory;
        import javax.xml.soap.SOAPBody;
        import javax.xml.soap.SOAPEnvelope;
        import javax.xml.soap.SOAPException;
        import javax.xml.soap.SOAPMessage;
        import javax.xml.soap.SOAPPart;

        import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;

        public class JAXBExample {
            public static void main(String[] args) throws SOAPException, IOException {

                Customer customer = new Customer();
                customer.setId(100);
                customer.setName("Hel..<..lo");
                customer.setAge(29);

                try {

                    MessageFactory messageFactory = MessageFactory.newInstance();
                    SOAPMessage soapMessage = messageFactory.createMessage();
                    SOAPPart soapPart = soapMessage.getSOAPPart();
                    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
                    SOAPBody soapBody = soapEnvelope.getBody();
                    JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
                    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                    // jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(), new CustomCharacterEscapeHandler());
                    jaxbMarshaller.setProperty("com.sun.xml.internal.bind.characterEscapeHandler",
                            new CharacterEscapeHandler() {
                                public void escape(char[] ch, int start, int length, boolean isAttVal, Writer writer)
                                        throws IOException {
                                    writer.write(ch, start, length);
                                }
                            });


                    // output pretty printed
                    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

                    jaxbMarshaller.marshal(customer, System.out);
                    jaxbMarshaller.marshal(customer, soapBody);
                    soapMessage.saveChanges();
                    soapMessage.writeTo(System.out);

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

            }
        }


 // result just with jaxb ***
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customer id="100">
        <payload><![CDATA[Hel..<..lo]]></payload>
        <age>29</age>
        <name>Hel..<..lo</name>
    </customer>
// result jaxb wrapped into SOAPbody***
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/>
    <SOAP-ENV:Body>
    <customer id="100">
    <payload>&lt;![CDATA[Hel..&lt;..lo]]&gt;</payload><age>29</age><name>Hel..&lt;..lo</name></customer>
    </SOAP-ENV:Body></SOAP-ENV:Envelope>

Ответы [ 3 ]

0 голосов
/ 02 ноября 2018

Вы можете использовать apache StringEscapeUtils. Поскольку я не знаю, как выглядит ваш класс клиента, я просто создаю быстрый пример, см. Классы ниже. Результат выглядит так:

soapMessage.writeTo (System.out);

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;yes&amp;quot;?&amp;gt;
            &amp;lt;customer&amp;gt;
            &amp;lt;id&amp;gt;123&amp;amp;lt;&amp;amp;gt;&amp;amp;amp;g&amp;lt;/id&amp;gt;
            &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;
            &amp;lt;lastname&amp;gt;World*+;:-&amp;amp;lt;&amp;amp;gt;&amp;amp;amp;%$&amp;lt;/lastname&amp;gt;
            &amp;lt;/customer&amp;gt;
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

System.out.println (StringEscapeUtils.unescapeXml (soapBody.getValue ()));

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customer>
    <id>123&lt;&gt;&amp;g</id>
    <name>Hello</name>
    <lastname>World*+;:-&lt;&gt;&amp;%$</lastname>
</customer>

Maven

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.6</version>
</dependency>

Main.java

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import java.io.*;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import org.apache.commons.text.StringEscapeUtils;

public class Main {
    private static final Logger log = LogManager.getLogger(Main.class);

    public static void main(String[] args) {
        try {
            Customer customer = new Customer().setID("123<>&g").setName("Hello").setLastname("World*+;:-<>&%$");
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();
            SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
            SOAPBody soapBody = soapEnvelope.getBody();

            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

            StringWriter sw = new StringWriter();
            jaxbMarshaller.marshal(customer, sw);

            soapBody.setValue(StringEscapeUtils.escapeXml11(sw.toString()));

            soapMessage.saveChanges();
            soapMessage.writeTo(System.out);
            System.out.println("\r\n\r\n-----\r\n\r\n");
            System.out.println(StringEscapeUtils.unescapeXml(soapBody.getValue()));
        } catch(Exception e) {
            log.error("no! ", e);
        }

        log.info("stop");
    }
}

Customer.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
    @XmlElement(name="id")
    private String id;
    @XmlElement(name="name")
    private String name;
    @XmlElement(name="lastname")
    private String lastname;

    public String getID() {
        return this.id;
    }

    public Customer setID(String value) {
        this.id = value;
        return this;
    }

    public String getName() {
        return this.name;
    }

    public Customer setName(String value) {
        this.name = value;
        return this;
    }

    public String getLastname() {
        return this.lastname;
    }

    public Customer setLastname(String value) {
        this.lastname = value;
        return this;
    }
}
0 голосов
/ 03 ноября 2018

Ну, без утилит apache escape, как насчет (некрасивого) обходного пути. Обычно вы делаете маршалл дважды, затем вырезаете нужную вам часть и устанавливаете ее прямо на ваше мыльное тело, см. Пример ниже.

soapMessage.writeTo (System.out);

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>&amp;lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&amp;gt;
        &amp;lt;customer&amp;gt;
        &amp;lt;id&amp;gt;Hello &amp;amp;lt;&amp;amp;gt;&amp;amp;amp;g World*+;:-&amp;amp;lt;&amp;amp;gt;&amp;amp;amp;%$&amp;lt;/id&amp;gt;
        &amp;lt;/customer&amp;gt;
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Main.java

public class Main {
    private static final Logger log = LogManager.getLogger(Main.class);

    public static void main(String[] args) {
        try {
            Customer customer = new Customer().setID("Hello <>&g World*+;:-<>&%$");
            String xml = marshall(new Workaround().setID(marshall(customer)));
            xml = xml.substring(0, xml.indexOf("</id>"));
            xml = xml.substring(xml.indexOf("<id>") + 4);
            System.out.println("\r\n\r\nxml:\r\n" + xml);


            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();
            SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
            SOAPBody soapBody = soapEnvelope.getBody();
            soapBody.setValue(xml);

            System.out.println("\r\n\r\nsoap:");
            soapMessage.writeTo(System.out);
        } catch (Exception e) {
            log.error("no! ", e);
        }

        log.info("stop");
    }

    public static String marshall(Object o) throws Exception {
        System.out.println("-------------------");
        StringWriter sw = new StringWriter();
        JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.marshal(o, System.out);
        jaxbMarshaller.marshal(o, sw);

        return sw.toString();
    }
}

Customer.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
    @XmlElement(name="id")
    private String id;

    public String getID() {
        return this.id;
    }

    public Customer setID(String value) {
        this.id = value;
        return this;
    }
}

Workaround.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="workaround")
@XmlAccessorType(XmlAccessType.FIELD)
public class Workaround {
    @XmlElement(name="id")
    private String id;

    public String getID() {
        return this.id;
    }

    public Workaround setID(String value) {
        this.id = value;
        return this;
    }
}
0 голосов
/ 02 ноября 2018

Свойства Marshaller.JAXB_ENCODING и com.sun.xml.internal.bind.characterEscapeHandler действуют только при сортировке в поток, но не при сортировке в DOM. Если вам нужен раздел CDATA в конечном результате, вероятно, лучше просто позволить JAXB упорядочить данные в обычном режиме, а затем постобработать DOM, изменив текстовые узлы на разделы CDATA там, где вы хотите.

...