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;
}
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);
}
}
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");
}
}
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);
}
}
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);
}
}
Aggregations