private void setupHeaders(
      BindingOperationInfo op,
      BindingMessageInfo bMsg,
      BindingMessageInfo unwrappedBMsg,
      MessageInfo msg,
      SoapBindingConfiguration config) {
    List<MessagePartInfo> parts = new ArrayList<MessagePartInfo>();
    for (MessagePartInfo part : msg.getMessageParts()) {
      if (config.isHeader(op, part)) {
        SoapHeaderInfo headerInfo = new SoapHeaderInfo();
        headerInfo.setPart(part);
        headerInfo.setUse(config.getUse());

        bMsg.addExtensor(headerInfo);
      } else {
        parts.add(part);
      }
    }
    unwrappedBMsg.setMessageParts(parts);
  }
  private List<MessagePartInfo> handleMimePart(
      MIMEPart mpart,
      List<MessagePartInfo> attParts,
      MessageInfo msg,
      BindingMessageInfo bmsg,
      List<MessagePartInfo> bodyParts,
      List<MessagePartInfo> messageParts) {
    if (mpart.getExtensibilityElements().size() < 1) {
      throw new RuntimeException("MIMEPart should at least contain one element!");
    }
    String partName = null;
    for (Object content : mpart.getExtensibilityElements()) {
      if (content instanceof MIMEContent) {
        MIMEContent mc = (MIMEContent) content;
        partName = mc.getPart();

        if (attParts == null) {
          attParts = new LinkedList<MessagePartInfo>();
        }

        if (StringUtils.isEmpty(partName)) {
          throw new RuntimeException(
              "Problem with WSDL: mime content element in operation "
                  + bmsg.getBindingOperation().getName().getLocalPart()
                  + " does not specify a part.");
        }

        MessagePartInfo mpi =
            msg.getMessagePart(new QName(msg.getName().getNamespaceURI(), partName));
        mpi.setProperty(Message.CONTENT_TYPE, mc.getType());
        attParts.add(mpi);
        // Attachments shouldn't be part of the body message
        bmsg.getMessageParts().remove(mpi);
      } else if (SOAPBindingUtil.isSOAPBody(content)) {
        SoapBody sb = SOAPBindingUtil.getSoapBody(content);
        if (sb.getParts() != null && sb.getParts().size() == 1) {
          partName = (String) sb.getParts().get(0);
        }

        // We can have a list of empty part names here.
        if (partName != null) {
          addSoapBodyPart(msg, bodyParts, partName);
        }
      } else if (SOAPBindingUtil.isSOAPHeader(content)) {
        SoapHeader header = SOAPBindingUtil.getSoapHeader(content);

        SoapHeaderInfo headerInfo = new SoapHeaderInfo();
        headerInfo.setUse(header.getUse());

        if (StringUtils.isEmpty(header.getPart())) {
          throw new RuntimeException(
              "Problem with WSDL: soap:header element in operation "
                  + bmsg.getBindingOperation().getName().getLocalPart()
                  + " does not specify a part.");
        }

        MessagePartInfo mpi =
            msg.getMessagePart(new QName(msg.getName().getNamespaceURI(), header.getPart()));

        if (mpi != null
            && header.getMessage() != null
            && !mpi.getMessageInfo().getName().equals(header.getMessage())) {
          mpi = null;
          // out of band, let's find it
          for (MessagePartInfo mpi2 : msg.getOutOfBandParts()) {
            if (mpi2.getName().getLocalPart().equals(header.getPart())
                && mpi2.getMessageInfo().getName().equals(header.getMessage())) {
              mpi = mpi2;
            }
          }
        }

        if (mpi != null) {
          headerInfo.setPart(mpi);
          messageParts.remove(mpi);
          bmsg.getMessageParts().remove(mpi);
          bmsg.addExtensor(headerInfo);
        }
      }
    }
    return attParts;
  }
  private void initializeMessage(
      SoapBindingInfo bi, BindingOperationInfo boi, BindingMessageInfo bmsg) {
    MessageInfo msg = bmsg.getMessageInfo();

    List<MessagePartInfo> messageParts = new ArrayList<MessagePartInfo>();
    messageParts.addAll(msg.getMessageParts());

    List<SoapHeader> headers =
        SOAPBindingUtil.getSoapHeaders(bmsg.getExtensors(ExtensibilityElement.class));
    if (headers != null) {
      for (SoapHeader header : headers) {
        SoapHeaderInfo headerInfo = new SoapHeaderInfo();
        headerInfo.setUse(header.getUse());
        if (StringUtils.isEmpty(header.getPart())) {
          throw new RuntimeException(
              "Problem with WSDL: soap:header element in operation "
                  + boi.getName().getLocalPart()
                  + " does not specify a part.");
        }
        MessagePartInfo part =
            msg.getMessagePart(new QName(msg.getName().getNamespaceURI(), header.getPart()));
        if (part != null
            && header.getMessage() != null
            && !part.getMessageInfo().getName().equals(header.getMessage())) {
          part = null;
          // out of band, let's find it
          for (MessagePartInfo mpi : msg.getOutOfBandParts()) {
            if (mpi.getName().getLocalPart().equals(header.getPart())
                && mpi.getMessageInfo().getName().equals(header.getMessage())) {
              part = mpi;
            }
          }
        }
        if (part != null) {
          headerInfo.setPart(part);
          messageParts.remove(part);
          bmsg.addExtensor(headerInfo);
        }
      }

      // Exclude the header parts from the message part list.
      bmsg.setMessageParts(messageParts);
    }

    SoapBodyInfo bodyInfo = new SoapBodyInfo();
    SoapBody soapBody = SOAPBindingUtil.getSoapBody(bmsg.getExtensors(ExtensibilityElement.class));

    List<?> parts = null;
    if (soapBody == null) {
      MIMEMultipartRelated mmr = bmsg.getExtensor(MIMEMultipartRelated.class);
      if (mmr != null) {
        parts = mmr.getMIMEParts();
      }
    } else {
      bmsg.addExtensor(soapBody);
      bodyInfo.setUse(soapBody.getUse());
      parts = soapBody.getParts();
    }

    // Initialize the body parts.
    List<MessagePartInfo> attParts = null;
    if (parts != null) {
      List<MessagePartInfo> bodyParts = new ArrayList<MessagePartInfo>();
      for (Iterator<?> itr = parts.iterator(); itr.hasNext(); ) {
        Object part = itr.next();
        if (part instanceof MIMEPart) {
          MIMEPart mpart = (MIMEPart) part;
          attParts = handleMimePart(mpart, attParts, msg, bmsg, bodyParts, messageParts);
        } else {
          addSoapBodyPart(msg, bodyParts, (String) part);
        }
      }
      bodyInfo.setParts(bodyParts);
      bodyInfo.setAttachments(attParts);
    } else {
      bodyInfo.setParts(messageParts);
    }

    bmsg.addExtensor(bodyInfo);
  }
  private void createSoapBinding(final SoapBindingInfo bi) throws WSDLException {
    boolean isSoap12 = bi.getSoapVersion() instanceof Soap12;
    Bus bs = getBus();
    WSDLManager m = bs.getExtension(WSDLManager.class);
    ExtensionRegistry extensionRegistry = m.getExtensionRegistry();

    SoapBinding soapBinding = SOAPBindingUtil.createSoapBinding(extensionRegistry, isSoap12);
    soapBinding.setStyle(bi.getStyle());
    soapBinding.setTransportURI(bi.getTransportURI());
    bi.addExtensor(soapBinding);

    for (BindingOperationInfo b : bi.getOperations()) {
      for (BindingFaultInfo faultInfo : b.getFaults()) {
        SoapFault soapFault = SOAPBindingUtil.createSoapFault(extensionRegistry, isSoap12);
        soapFault.setUse("literal");
        soapFault.setName(faultInfo.getFaultInfo().getFaultName().getLocalPart());
        faultInfo.addExtensor(soapFault);
      }
      SoapOperationInfo soi = b.getExtensor(SoapOperationInfo.class);

      SoapOperation soapOperation =
          SOAPBindingUtil.createSoapOperation(extensionRegistry, isSoap12);
      soapOperation.setSoapActionURI(soi.getAction());
      soapOperation.setStyle(soi.getStyle());
      boolean isRpc = "rpc".equals(soapOperation.getStyle());

      b.addExtensor(soapOperation);

      if (b.getInput() != null) {
        List<String> bodyParts = null;
        List<SoapHeaderInfo> headerInfos = b.getInput().getExtensors(SoapHeaderInfo.class);
        if (headerInfos != null && headerInfos.size() > 0) {
          bodyParts = new ArrayList<String>();
          for (MessagePartInfo part : b.getInput().getMessageParts()) {
            bodyParts.add(part.getName().getLocalPart());
          }

          for (SoapHeaderInfo headerInfo : headerInfos) {
            SoapHeader soapHeader =
                SOAPBindingUtil.createSoapHeader(extensionRegistry, BindingInput.class, isSoap12);
            soapHeader.setMessage(b.getInput().getMessageInfo().getName());
            soapHeader.setPart(headerInfo.getPart().getName().getLocalPart());
            soapHeader.setUse("literal");
            bodyParts.remove(headerInfo.getPart().getName().getLocalPart());
            headerInfo.getPart().setProperty(HEADER, true);
            b.getInput().addExtensor(soapHeader);
          }
        }
        SoapBody body =
            SOAPBindingUtil.createSoapBody(extensionRegistry, BindingInput.class, isSoap12);
        body.setUse("literal");
        if (isRpc) {
          body.setNamespaceURI(b.getName().getNamespaceURI());
        }

        if (bodyParts != null) {
          body.setParts(bodyParts);
        }

        b.getInput().addExtensor(body);
      }

      if (b.getOutput() != null) {
        List<String> bodyParts = null;
        List<SoapHeaderInfo> headerInfos = b.getOutput().getExtensors(SoapHeaderInfo.class);
        if (headerInfos != null && headerInfos.size() > 0) {
          bodyParts = new ArrayList<String>();
          for (MessagePartInfo part : b.getOutput().getMessageParts()) {
            bodyParts.add(part.getName().getLocalPart());
          }
          for (SoapHeaderInfo headerInfo : headerInfos) {
            SoapHeader soapHeader =
                SOAPBindingUtil.createSoapHeader(extensionRegistry, BindingOutput.class, isSoap12);
            soapHeader.setMessage(b.getOutput().getMessageInfo().getName());
            soapHeader.setPart(headerInfo.getPart().getName().getLocalPart());
            soapHeader.setUse("literal");
            bodyParts.remove(headerInfo.getPart().getName().getLocalPart());
            b.getOutput().addExtensor(soapHeader);
          }
        }
        SoapBody body =
            SOAPBindingUtil.createSoapBody(extensionRegistry, BindingOutput.class, isSoap12);
        body.setUse("literal");
        if (isRpc) {
          body.setNamespaceURI(b.getName().getNamespaceURI());
        }

        if (bodyParts != null) {
          body.setParts(bodyParts);
        }

        b.getOutput().addExtensor(body);
      }
    }
  }