javaee / metro

Metro has been contributed to Eclipse Foundation. Please use the link below to find the latest project
https://github.com/eclipse-ee4j/metro-jax-ws
Other
10 stars 1 forks source link

Cannot call web services which return non-XML responses #24

Open glassfishrobot opened 10 years ago

glassfishrobot commented 10 years ago

Web services which return non-XML data no longer return any data when called via the metro web service library. This worked in metro 1.5, 1.6.2, 2.0, 2.1 and 2.2, but does not work against 2.2.1, 2.2.1-1 or 2.3.

This appears to be because Packet.getMessage() now returns a MessageWrapper object, instead of the real Message object. This in turn means that XMLMessage.getDataSource() can't see that UnknownContent objects implement MessageDataSource, and ends up calling UnknownContent.writePayloadTo(XMLStreamWriter) (which does nothing) instead of UnknownContent.getDataSource().

Affected Versions

[2.2.1, 2.2.1-1, 2.3]

glassfishrobot commented 10 years ago

Reported by nhinds

glassfishrobot commented 10 years ago

nhinds said: Here is a testcase which passes when run with Metro 1.5/1.6.2/2.0/2.1/2.2 and fails when run with metro 2.2.1/2.2.1-1/2.3:

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.http.HTTPBinding;

import org.junit.Test;

import com.sun.xml.ws.api.SOAPVersion;
import com.sun.xml.ws.api.message.Message;
import com.sun.xml.ws.api.message.Messages;
import com.sun.xml.ws.api.message.Packet;
import com.sun.xml.ws.encoding.xml.XMLMessage;
import com.sun.xml.ws.encoding.xml.XMLMessage.MessageDataSource;

public class NonXmlDispatchIssueTest {
    @Test
    public void callNonXmlResourceViaJavaxXmlAPI() throws IOException {
        final List<String> failures = new ArrayList<String>();

        final Service service = Service.create(new QName("dummy", "value"));
        final QName portName = new QName("dummy", "port");
        service.addPort(portName, HTTPBinding.HTTP_BINDING, "http://google.com");       final Dispatch<Source> dispatch = service.createDispatch(portName, Source.class, Mode.MESSAGE);
        dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_METHOD, "GET");

        final Source response = dispatch.invoke(null);

        if (response instanceof StreamSource) {
            final StreamSource streamSource = (StreamSource) response;
            final int firstRead = streamSource.getReader() != null ? streamSource.getReader().read() : streamSource.getInputStream().read();
            if (firstRead == -1) {
                failures.add("Calling a non-XML URL should return some data");
            }
        } else {
            failures.add("Source should be a StreamSource");
        }

        assertTrue("Failures: " + failures, failures.isEmpty());
    }

    @Test
    public void callNonXmlResourceViaComSunXmlAPI() throws IOException {
        final List<String> failures = new ArrayList<String>();

        final Service service = Service.create(new QName("dummy", "value"));
        final QName portName = new QName("dummy", "port");
        service.addPort(portName, HTTPBinding.HTTP_BINDING, "http://google.com");       final Dispatch<Message> dispatch = service.createDispatch(portName, Message.class, Mode.MESSAGE);
        dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_METHOD, "GET");

        final Message response = dispatch.invoke(Messages.createEmpty(SOAPVersion.SOAP_11));

        if (!(response instanceof MessageDataSource)) {
            failures.add("Message should implement MessageDataSource or XMLMessage.getDataSource will not work");
        }

        final DataSource dataSource = XMLMessage.getDataSource(response, null);
        if (dataSource.getInputStream().read() == -1) {
            failures.add("Calling a non-XML URL should return some data");
        }

        assertTrue("Failures: " + failures, failures.isEmpty());
    }

    @Test
    public void getDataSourceFromMessageWrapper() {
        final DataSource inputDataSource = new FileDataSource("");
        final Packet packet = new Packet(new XMLMessage.UnknownContent(inputDataSource));
        final DataSource outputDataSource = XMLMessage.getDataSource(packet.getMessage(), null);
        assertSame("Should be able to retrieve the original DataSource", inputDataSource, outputDataSource);
    }
}
glassfishrobot commented 10 years ago

Was assigned to snajper

glassfishrobot commented 7 years ago

This issue was imported from java.net JIRA METRO-24