@Override protected Packet getResponse(Packet request, @Nullable SOAPMessage returnValue, WSDLPort port, WSBinding binding) { Packet response = super.getResponse(request, returnValue, port, binding); // Populate SOAPMessage's transport headers if (returnValue != null && response.supports(Packet.OUTBOUND_TRANSPORT_HEADERS)) { MimeHeaders hdrs = returnValue.getMimeHeaders(); Map<String, List<String>> headers = new HashMap<String, List<String>>(); Iterator i = hdrs.getAllHeaders(); while(i.hasNext()) { MimeHeader header = (MimeHeader)i.next(); if(header.getName().equalsIgnoreCase("SOAPAction")) // SAAJ sets this header automatically, but it interferes with the correct operation of JAX-WS. // so ignore this header. continue; List<String> list = headers.get(header.getName()); if (list == null) { list = new ArrayList<String>(); headers.put(header.getName(), list); } list.add(header.getValue()); } response.put(Packet.OUTBOUND_TRANSPORT_HEADERS, headers); } return response; }
/** * Send the SOAP message using Jetty HTTP client. Jetty is used in preference to * HttpURLConnection which can result in the HNAP interface becoming unresponsive. * * @param action - SOAP Action to send * @param timeout - Connection timeout in milliseconds * @return The result * @throws IOException * @throws SOAPException * @throws SAXException * @throws ExecutionException * @throws TimeoutException * @throws InterruptedException */ protected Document sendReceive(final SOAPMessage action, final int timeout) throws IOException, SOAPException, SAXException, InterruptedException, TimeoutException, ExecutionException { Document result; final Request request = httpClient.POST(uri); request.timeout(timeout, TimeUnit.MILLISECONDS); final Iterator<?> it = action.getMimeHeaders().getAllHeaders(); while (it.hasNext()) { final MimeHeader header = (MimeHeader) it.next(); request.header(header.getName(), header.getValue()); } try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { action.writeTo(os); request.content(new BytesContentProvider(os.toByteArray())); final ContentResponse response = request.send(); try (final ByteArrayInputStream is = new ByteArrayInputStream(response.getContent())) { result = parser.parse(is); } } return result; }
/** * Check whether at least one of the headers of this object matches a provided header * * @param headers * @return <b>true</b> if at least one header of this AttachmentPart matches a header in the * provided <code>headers</code> parameter, <b>false</b> if none of the headers of this * AttachmentPart matches at least one of the header in the provided * <code>headers</code> parameter */ public boolean matches(MimeHeaders headers) { for (Iterator i = headers.getAllHeaders(); i.hasNext();) { MimeHeader hdr = (javax.xml.soap.MimeHeader)i.next(); String values[] = mimeHeaders.getHeader(hdr.getName()); boolean found = false; if (values != null) { for (int j = 0; j < values.length; j++) { if (!hdr.getValue().equalsIgnoreCase(values[j])) { continue; } found = true; break; } } if (!found) { return false; } } return true; }
/** * Removes all the AttachmentPart objects that have header entries that match the specified * headers. Note that the removed attachment could have headers in addition to those specified. * * @param headers - a MimeHeaders object containing the MIME headers for which to search * @since SAAJ 1.3 */ public void removeAttachments(MimeHeaders headers) { Collection<AttachmentPart> newAttachmentParts = new ArrayList<AttachmentPart>(); for (AttachmentPart attachmentPart : attachmentParts) { //Get all the headers for (Iterator iterator = headers.getAllHeaders(); iterator.hasNext();) { MimeHeader mimeHeader = (MimeHeader)iterator.next(); String[] headerValues = attachmentPart.getMimeHeader(mimeHeader.getName()); //if values for this header name, do not remove it if (headerValues.length != 0) { if (!(headerValues[0].equals(mimeHeader.getValue()))) { newAttachmentParts.add(attachmentPart); } } } } attachmentParts.clear(); this.attachmentParts = newAttachmentParts; }
static Map<String, List<String>> toMap(MimeHeaders headers) { HashMap<String, List<String>> map = new HashMap<String, List<String>>(); for (Iterator<MimeHeader> i = headers.getAllHeaders(); i.hasNext();) { MimeHeader mh = i.next(); List<String> values = map.get(mh.getName()); if (values == null) { values = new ArrayList<String>(); map.put(mh.getName(), values); } values.add(mh.getValue()); } return map; }
Packet createPacket(SOAPMessage arg) { Iterator iter = arg.getMimeHeaders().getAllHeaders(); Headers ch = new Headers(); while(iter.hasNext()) { MimeHeader mh = (MimeHeader) iter.next(); ch.add(mh.getName(), mh.getValue()); } Packet packet = new Packet(SAAJFactory.create(arg)); packet.invocationProperties.put(MessageContext.HTTP_REQUEST_HEADERS, ch); return packet; }
public static MimeHeaders copy(MimeHeaders headers) { MimeHeaders newHeaders = new MimeHeaders(); Iterator eachHeader = headers.getAllHeaders(); while (eachHeader.hasNext()) { MimeHeader currentHeader = (MimeHeader) eachHeader.next(); newHeaders.addHeader( currentHeader.getName(), currentHeader.getValue()); } return newHeaders; }
/** * Adds the given MIME headers into the HTTP response object. * * @param headers MIME headers to be added * @param res the Http servlet response */ public static void putHeaders(MimeHeaders headers, HttpServletResponse res) { for (Iterator it = headers.getAllHeaders(); it.hasNext();) { MimeHeader header = (MimeHeader) it.next(); String[] values = headers.getHeader(header.getName()); res.setHeader(header.getName(), buildHeaderValue(header, values)); } }
/** * Constructs a string containing the value of the given header. If the * header has multiple values, they're represented in a comma separated * string. * * @param header MIME header * @param values String array containing the values of the given header * @return comma separated string containing all the values of the given * header */ private static String buildHeaderValue(MimeHeader header, String[] values) { if (values.length == 1) { return header.getValue(); } else { StringBuilder concat = new StringBuilder(); for (int i = 0; i < values.length; i++) { if (i != 0) { concat.append(','); } concat.append(values[i]); } return concat.toString(); } }
/** * Puts the mime headers into a mime message. * * @param mimeHeaders the mime headers. * @param message the mime message. * @throws MessagingException if error occurred when adding the headers. */ private void putHeaders(MimeHeaders mimeHeaders, MimeMessage message) throws MessagingException { for (Iterator iterator = mimeHeaders.getAllHeaders(); iterator .hasNext();) { MimeHeader mimeHeader = (MimeHeader) iterator.next(); String[] mimeValues = mimeHeaders.getHeader(mimeHeader.getName()); for (int i = 0; i < mimeValues.length;) { String value = mimeValues[i++]; message.addHeader(mimeHeader.getName(), value); } } }
private static MimeHeader parseMimeHeader(byte[] line) { String lineString = new String(line).trim(); int colonIndex = lineString.indexOf(':'); if (colonIndex >= 0) { String header = lineString.substring(0, colonIndex).trim(); String value = lineString.substring(colonIndex + 1).trim(); return new MimeHeader(header, value); } else { return null; } }
private ByteArrayOutputStream printWebMessage(SOAPMessage message, String headerType, ByteArrayOutputStream bye) throws IOException, SOAPException { PrintStream printStream = new PrintStream(bye); bye.reset(); printStream.println("\n************************************************"); if (message != null) { //get the mime headers and print them printStream.println("\n\nHeader: " + headerType); if (message.saveRequired()) { message.saveChanges(); } MimeHeaders headers = message.getMimeHeaders(); Iterator iter = headers.getAllHeaders(); while (iter.hasNext()) { MimeHeader header = (MimeHeader)iter.next(); System.out.println("the header is " + header.getValue()); printStream.println("\t" + header.getName() + " : " + header.getValue()); } //print the message itself printStream.println("\n\nMessage: " + headerType); message.writeTo(bye); } return bye; }
public Message createFrom(SOAPMessage message) throws XMLStreamException, WebServiceException { try { // Create a Message with an XMLPart from the SOAPEnvelope Message m = new MessageImpl(message.getSOAPPart().getEnvelope()); MimeHeaders mimeHeaders = message.getMimeHeaders(); HashMap map = new HashMap(); Iterator iterator = mimeHeaders.getAllHeaders(); while (iterator.hasNext()) { MimeHeader mimeHeader = (MimeHeader)iterator.next(); String key = mimeHeader.getName(); String value = mimeHeader.getValue(); if(key != null && value != null) { if(!HTTPConstants.HEADER_CONTENT_TYPE.equalsIgnoreCase(key)) { map.put(key, value); } } } m.setMimeHeaders(map); if (message.countAttachments() > 0) { Iterator it = message.getAttachments(); m.setDoingSWA(true); while (it.hasNext()) { AttachmentPart ap = (AttachmentPart)it.next(); m.addDataHandler(new WrappedDataHandler(ap.getDataHandler(), ap.getContentType()), ap.getContentId()); } } return m; } catch (Exception e) { throw ExceptionFactory.makeWebServiceException(e); } }
/** * Create a copy of an existing {@link MimeHeaders} object. * * @param headers the object to copy * @return a copy of the {@link MimeHeaders} object */ public static MimeHeaders copyMimeHeaders(MimeHeaders headers) { MimeHeaders result = new MimeHeaders(); Iterator iterator = headers.getAllHeaders(); while (iterator.hasNext()) { MimeHeader hdr = (MimeHeader)iterator.next(); result.addHeader(hdr.getName(), hdr.getValue()); } return result; }
private void extractHeaderDataSOAP(SOAPMessage response, HttpResponseBean responseBean) throws SOAPException { //extract MimeHeaders MimeHeaders mime = response.getMimeHeaders(); Iterator<MimeHeader> iter = mime.getAllHeaders(); while(iter.hasNext()) { MimeHeader mimeH = iter.next(); responseBean.addEntryToResponseHeaders(mimeH.getName(),mimeH.getValue()); } //extract SOAPHeaders from the envelope and a them to the mimeHeaders if(response.getSOAPHeader()!=null) { javax.xml.soap.SOAPHeader header = response.getSOAPHeader(); NodeList nodes = header.getChildNodes(); for(int x=0; x<nodes.getLength();x++) { //if the header entry contains child nodes - write them with the node names if(nodes.item(x).hasChildNodes()){ NodeList childnodes = nodes.item(x).getChildNodes(); for(int y = 0; y<childnodes.getLength();y++){ responseBean.addEntryToResponseHeaders(nodes.item(x).getLocalName(),childnodes.item(y).getLocalName()+":"+childnodes.item(y).getTextContent()); } } else{ responseBean.addEntryToResponseHeaders(nodes.item(x).getLocalName(), nodes.item(x).getTextContent()); } } } }
Packet createPacket(SOAPMessage arg) { Iterator iter = arg.getMimeHeaders().getAllHeaders(); Headers ch = new Headers(); while(iter.hasNext()) { MimeHeader mh = (MimeHeader) iter.next(); ch.add(mh.getName(), mh.getValue()); } Packet packet = new Packet(new SAAJMessage(arg)); packet.invocationProperties.put(MessageContext.HTTP_REQUEST_HEADERS, ch); return packet; }
@Override public Iterator<MimeHeader> getAllMimeHeaders() { return headers.getAllHeaders(); }
@Override public Iterator<MimeHeader> getMatchingMimeHeaders(String[] names) { return headers.getMatchingHeaders(names); }