Search in sources :

Example 16 with ByteArrayBuffer

use of com.sun.xml.ws.util.ByteArrayBuffer in project metro-jax-ws by eclipse-ee4j.

the class HttpTransportPipe method createResponsePacket.

private Packet createResponsePacket(Packet request, HttpClientTransport con) throws IOException {
    // throws IOE
    con.readResponseCodeAndMessage();
    recordCookies(request, con);
    InputStream responseStream = con.getInput();
    if (dump || LOGGER.isLoggable(Level.FINER)) {
        ByteArrayBuffer buf = new ByteArrayBuffer();
        if (responseStream != null) {
            buf.write(responseStream);
            responseStream.close();
        }
        dump(buf, "HTTP response - " + request.endpointAddress + " - " + con.statusCode, con.getHeaders());
        responseStream = buf.newInputStream();
    }
    // Check if stream contains any data
    int cl = con.contentLength;
    InputStream tempIn = null;
    if (cl == -1) {
        // No Content-Length header
        tempIn = StreamUtils.hasSomeData(responseStream);
        if (tempIn != null) {
            responseStream = tempIn;
        }
    }
    if (cl == 0 || (cl == -1 && tempIn == null)) {
        if (responseStream != null) {
            // No data, so close the stream
            responseStream.close();
            responseStream = null;
        }
    }
    // Allows only certain http status codes for a binding. For all
    // other status codes, throws exception
    // throws ClientTransportException
    checkStatusCode(responseStream, con);
    // To avoid zero-length chunk for One-Way
    if (cl == -1 && con.statusCode == 202 && "Accepted".equals(con.statusMessage) && responseStream != null) {
        ByteArrayBuffer buf = new ByteArrayBuffer();
        // What is within the responseStream?
        buf.write(responseStream);
        responseStream.close();
        responseStream = (buf.size() == 0) ? null : buf.newInputStream();
        buf.close();
    }
    Packet reply = request.createClientResponse(null);
    reply.wasTransportSecure = con.isSecure();
    if (responseStream != null) {
        String contentType = con.getContentType();
        if (contentType != null && contentType.contains("text/html") && binding instanceof SOAPBinding) {
            throw new ClientTransportException(ClientMessages.localizableHTTP_STATUS_CODE(con.statusCode, con.statusMessage));
        }
        codec.decode(responseStream, contentType, reply);
    }
    return reply;
}
Also used : ClientTransportException(com.sun.xml.ws.client.ClientTransportException) Packet(com.sun.xml.ws.api.message.Packet) InputStream(java.io.InputStream) SOAPBinding(jakarta.xml.ws.soap.SOAPBinding) ByteArrayBuffer(com.sun.xml.ws.util.ByteArrayBuffer)

Example 17 with ByteArrayBuffer

use of com.sun.xml.ws.util.ByteArrayBuffer in project metro-jax-ws by eclipse-ee4j.

the class HttpTransportPipe method process.

@Override
public Packet process(Packet request) {
    HttpClientTransport con;
    try {
        // get transport headers from message
        Map<String, List<String>> reqHeaders = new Headers();
        @SuppressWarnings("unchecked") Map<String, List<String>> userHeaders = (Map<String, List<String>>) request.invocationProperties.get(MessageContext.HTTP_REQUEST_HEADERS);
        boolean addUserAgent = true;
        if (userHeaders != null) {
            // userHeaders may not be modifiable like SingletonMap, just copy them
            reqHeaders.putAll(userHeaders);
            // application wants to use its own User-Agent header
            if (userHeaders.get("User-Agent") != null) {
                addUserAgent = false;
            }
        }
        if (addUserAgent) {
            reqHeaders.put("User-Agent", USER_AGENT);
        }
        addBasicAuth(request, reqHeaders);
        addCookies(request, reqHeaders);
        con = getTransport(request, reqHeaders);
        request.addSatellite(new HttpResponseProperties(con));
        ContentType ct = codec.getStaticContentType(request);
        if (ct == null) {
            ByteArrayBuffer buf = new ByteArrayBuffer();
            ct = codec.encode(request, buf);
            // data size is available, set it as Content-Length
            reqHeaders.put("Content-Length", Collections.singletonList(Integer.toString(buf.size())));
            reqHeaders.put("Content-Type", Collections.singletonList(ct.getContentType()));
            if (ct.getAcceptHeader() != null) {
                reqHeaders.put("Accept", Collections.singletonList(ct.getAcceptHeader()));
            }
            if (binding instanceof SOAPBinding) {
                writeSOAPAction(reqHeaders, ct.getSOAPActionHeader());
            }
            if (dump || LOGGER.isLoggable(Level.FINER)) {
                dump(buf, "HTTP request", reqHeaders);
            }
            buf.writeTo(con.getOutput());
        } else {
            // Set static Content-Type
            reqHeaders.put("Content-Type", Collections.singletonList(ct.getContentType()));
            if (ct.getAcceptHeader() != null) {
                reqHeaders.put("Accept", Collections.singletonList(ct.getAcceptHeader()));
            }
            if (binding instanceof SOAPBinding) {
                writeSOAPAction(reqHeaders, ct.getSOAPActionHeader());
            }
            if (dump || LOGGER.isLoggable(Level.FINER)) {
                ByteArrayBuffer buf = new ByteArrayBuffer();
                codec.encode(request, buf);
                dump(buf, "HTTP request - " + request.endpointAddress, reqHeaders);
                OutputStream out = con.getOutput();
                if (out != null) {
                    buf.writeTo(out);
                }
            } else {
                OutputStream os = con.getOutput();
                if (os != null) {
                    codec.encode(request, os);
                }
            }
        }
        con.closeOutput();
        return createResponsePacket(request, con);
    } catch (WebServiceException wex) {
        throw wex;
    } catch (Exception ex) {
        throw new WebServiceException(ex);
    }
}
Also used : WebServiceException(jakarta.xml.ws.WebServiceException) Headers(com.sun.xml.ws.transport.Headers) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) SOAPBinding(jakarta.xml.ws.soap.SOAPBinding) IOException(java.io.IOException) WebServiceException(jakarta.xml.ws.WebServiceException) ClientTransportException(com.sun.xml.ws.client.ClientTransportException) ByteArrayBuffer(com.sun.xml.ws.util.ByteArrayBuffer) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with ByteArrayBuffer

use of com.sun.xml.ws.util.ByteArrayBuffer in project metro-jax-ws by eclipse-ee4j.

the class XmlUtil method getUTF8Stream.

public static InputStream getUTF8Stream(String s) {
    try {
        ByteArrayBuffer bab = new ByteArrayBuffer();
        Writer w = new OutputStreamWriter(bab, StandardCharsets.UTF_8);
        w.write(s);
        w.close();
        return bab.newInputStream();
    } catch (IOException e) {
        throw new RuntimeException("should not happen");
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) ByteArrayBuffer(com.sun.xml.ws.util.ByteArrayBuffer)

Example 19 with ByteArrayBuffer

use of com.sun.xml.ws.util.ByteArrayBuffer in project metro-jax-ws by eclipse-ee4j.

the class SAAJMessageTester method testWriteSOAP11MessagePayloadToXMLStreamWriter.

public void testWriteSOAP11MessagePayloadToXMLStreamWriter() {
    ByteArrayBuffer baos = new ByteArrayBuffer();
    XMLStreamWriter writer = XMLStreamWriterFactory.create(baos);
    try {
        msg.writePayloadTo(writer);
        writer.flush();
        Transformer transformer = XmlUtil.newTransformer();
        StreamSource source = new StreamSource(baos.newInputStream());
        DOMResult domResult = new DOMResult();
        transformer.transform(source, domResult);
        Node n = domResult.getNode().getFirstChild();
        assertTrue(n.getLocalName().equals("Body") && n.getNamespaceURI().equals("urn:test:types"));
        Node c = n.getFirstChild();
        assertTrue(c.getLocalName().equals("value") && c.getNamespaceURI().equals("urn:test:types"));
        assertTrue(c.getFirstChild().getNodeValue().equals("Its Body"));
    } catch (XMLStreamException e) {
        e.printStackTrace();
        assertTrue(false);
    } catch (TransformerException e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) TransformerException(javax.xml.transform.TransformerException) ByteArrayBuffer(com.sun.xml.ws.util.ByteArrayBuffer)

Example 20 with ByteArrayBuffer

use of com.sun.xml.ws.util.ByteArrayBuffer in project metro-jax-ws by eclipse-ee4j.

the class SourceMessageTester method testWriteSOAP11EnvelopeToXMLStreamWriter.

public void testWriteSOAP11EnvelopeToXMLStreamWriter() {
    Message msg = createPayloadSourceMessage();
    ByteArrayBuffer baos = new ByteArrayBuffer();
    XMLStreamWriter writer = XMLStreamWriterFactory.create(baos);
    try {
        msg.writeTo(writer);
        writer.flush();
        // System.out.println("Envelope: "+new String(baos.toByteArray()));
        Transformer transformer = XmlUtil.newTransformer();
        StreamSource source = new StreamSource(baos.newInputStream());
        DOMResult domResult = new DOMResult();
        transformer.transform(source, domResult);
        Node n = domResult.getNode().getFirstChild();
        assertTrue(n.getLocalName().equals("Envelope") && n.getNamespaceURI().equals("http://schemas.xmlsoap.org/soap/envelope/"));
        Node c = n.getFirstChild();
        assertTrue(c.getLocalName().equals("Body") && c.getNamespaceURI().equals("http://schemas.xmlsoap.org/soap/envelope/"));
        c = c.getFirstChild();
        assertTrue(c.getLocalName().equals("Body") && c.getNamespaceURI().equals("urn:test:types"));
        c = c.getFirstChild();
        assertTrue(c.getLocalName().equals("value") && c.getNamespaceURI().equals("urn:test:types"));
        assertTrue(c.getFirstChild().getNodeValue().equals("Its Body"));
    } catch (XMLStreamException e) {
        e.printStackTrace();
        assertTrue(false);
    } catch (TransformerException e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) Message(com.sun.xml.ws.api.message.Message) PayloadSourceMessage(com.sun.xml.ws.message.source.PayloadSourceMessage) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) TransformerException(javax.xml.transform.TransformerException) ByteArrayBuffer(com.sun.xml.ws.util.ByteArrayBuffer)

Aggregations

ByteArrayBuffer (com.sun.xml.ws.util.ByteArrayBuffer)29 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)13 Message (com.sun.xml.ws.api.message.Message)9 WebServiceException (jakarta.xml.ws.WebServiceException)9 Packet (com.sun.xml.ws.api.message.Packet)7 ClientTransportException (com.sun.xml.ws.client.ClientTransportException)7 SAAJMessage (com.sun.xml.ws.message.saaj.SAAJMessage)6 SOAPMessage (jakarta.xml.soap.SOAPMessage)6 IOException (java.io.IOException)6 WSEndpointReference (com.sun.xml.ws.api.addressing.WSEndpointReference)5 ObjectInputStream (java.io.ObjectInputStream)5 ObjectOutputStream (java.io.ObjectOutputStream)5 Transformer (javax.xml.transform.Transformer)5 StreamSource (javax.xml.transform.stream.StreamSource)5 XMLInputFactory (javax.xml.stream.XMLInputFactory)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 URL (java.net.URL)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 TransformerException (javax.xml.transform.TransformerException)3 DOMResult (javax.xml.transform.dom.DOMResult)3