Java Code Examples for javax.xml.soap.SOAPEnvelope#addHeader()

The following examples show how to use javax.xml.soap.SOAPEnvelope#addHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JPlagClientAccessHandler.java    From jplag with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds an "Access" element to the SOAP header
 */
public boolean handleRequest(MessageContext msgct) {
	if (msgct instanceof SOAPMessageContext) {
		SOAPMessageContext smsgct = (SOAPMessageContext) msgct;
		try {
			SOAPMessage msg = smsgct.getMessage();
			SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
			SOAPHeader header = msg.getSOAPHeader();

			if (header == null)
				header = envelope.addHeader(); // add an header if non exists

			SOAPHeaderElement accessElement = header.addHeaderElement(envelope.createName("Access", "ns0", JPLAG_TYPES_NS));
			SOAPElement usernameelem = accessElement.addChildElement("username");
			usernameelem.addTextNode(username);
			SOAPElement passwordelem = accessElement.addChildElement("password");
			passwordelem.addTextNode(password);
			SOAPElement compatelem = accessElement.addChildElement("compatLevel");
			compatelem.addTextNode(compatibilityLevel + "");
		} catch (SOAPException x) {
			System.out.println("Unable to create access SOAP header!");
			x.printStackTrace();
		}
	}
	return true;
}
 
Example 2
Source File: VersionHandlerCtmg.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * The method is invoked for normal processing of outbound messages.
 * 
 * @param context
 *            the message context.
 * @return An indication of whether handler processing should continue for
 *         the current message. Return <code>true</code> to continue
 *         processing.
 * 
 * @throws Exception
 *             Causes the JAX-WS runtime to cease fault message processing.
 **/
@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean request_p = (Boolean) context
            .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (request_p.booleanValue()) {
        try {
            SOAPMessage msg = context.getMessage();
            SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
            SOAPHeader hdr = env.getHeader();
            if (hdr == null) {
                hdr = env.addHeader();
            }
            QName qname_user = new QName("http://com/auth/", "auth");
            SOAPHeaderElement helem_user = hdr.addHeaderElement(qname_user);
            helem_user.setActor(VERSION);
            if (version != null && version.trim().length() != 0) {
                helem_user.addTextNode(version);
            }

            msg.saveChanges();
            message = soapMessage2String(msg);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}
 
Example 3
Source File: VersionHandler.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * The method is invoked for normal processing of outbound messages.
 * 
 * @param context
 *            the message context.
 * @return An indication of whether handler processing should continue for
 *         the current message. Return <code>true</code> to continue
 *         processing.
 * 
 * @throws Exception
 *             Causes the JAX-WS runtime to cease fault message processing.
 **/
@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean request_p = (Boolean) context
            .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (request_p.booleanValue()) {
        try {
            SOAPMessage msg = context.getMessage();
            SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
            SOAPHeader hdr = env.getHeader();
            if (hdr == null) {
                hdr = env.addHeader();
            }
            QName qname_user = new QName("http://com/auth/", "auth");
            SOAPHeaderElement helem_user = hdr.addHeaderElement(qname_user);
            helem_user.setActor(VERSION);
            if (version == null || version.trim().length() == 0) {
                helem_user.addTextNode(apiVersionInfo.getProperty(VERSION));
            } else {
                helem_user.addTextNode(version);
            }

            msg.saveChanges();
            message = soapMessage2String(msg);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}
 
Example 4
Source File: JPlagClientAccessHandler.java    From jplag with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds an "Access" element to the SOAP header
 */
public boolean handleRequest(MessageContext msgct) {
	if(msgct instanceof SOAPMessageContext) {
		SOAPMessageContext smsgct=(SOAPMessageContext) msgct;
		try	{
			SOAPMessage msg=smsgct.getMessage();
			SOAPEnvelope envelope=msg.getSOAPPart().getEnvelope();
			SOAPHeader header=msg.getSOAPHeader();
			
			if(header==null)
				header=envelope.addHeader(); // add an header if non exists
			
			SOAPHeaderElement accessElement=header.addHeaderElement(
					envelope.createName("Access","ns0",
							"http://www.ipd.uni-karlsruhe.de/jplag/types"));
			SOAPElement usernameelem=accessElement.addChildElement(
                       "username");
			usernameelem.addTextNode(username);
			SOAPElement passwordelem=accessElement.addChildElement(
                       "password");
			passwordelem.addTextNode(password);
               SOAPElement compatelem=accessElement.addChildElement(
                       "compatLevel");
               compatelem.addTextNode(compatibilityLevel+"");
		} catch (SOAPException x) {
			System.out.println("Unable to create access SOAP header!");
			x.printStackTrace();
		}
	}
	return true;
}
 
Example 5
Source File: TestMustUnderstandHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
public boolean handleMessage(SOAPMessageContext ctx) {

        boolean continueProcessing = true;

        try {
            Object b = ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            boolean outbound = (Boolean)b;
            SOAPMessage msg = ctx.getMessage();
            if (isServerSideHandler()) {
                if (outbound) {
                    QName qname = new QName("http://cxf.apache.org/mu", "MU");
                    SOAPPart soapPart = msg.getSOAPPart();
                    SOAPEnvelope envelope = soapPart.getEnvelope();
                    SOAPHeader header = envelope.getHeader();
                    if (header == null) {
                        header = envelope.addHeader();
                    }


                    SOAPHeaderElement headerElement
                        = header.addHeaderElement(envelope.createName("MU", "ns1", qname.getNamespaceURI()));

                    // QName soapMustUnderstand = new QName("http://schemas.xmlsoap.org/soap/envelope/",
                    // "mustUnderstand");
                    Name name = SOAPFactory.newInstance()
                        .createName("mustUnderstand", "soap", "http://schemas.xmlsoap.org/soap/envelope/");
                    headerElement.addAttribute(name, "1");
                } else {
                    getHandlerInfoList(ctx).add(getHandlerId());
                }
            }
        } catch (SOAPException e) {
            e.printStackTrace();
        }
        return continueProcessing;
    }
 
Example 6
Source File: SOAPGenerator.java    From cougar with Apache License 2.0 5 votes vote down vote up
private static SOAPMessage generateSOAPMessageShell(HttpCallBean httpCallBean) {
	SOAPMessage message = null;
	String secNameSpace = "http://www.betfair.com/security/";
	httpCallBean.setServiceName("Baseline");
	String nameSpace = httpCallBean.getNameSpace();
	
	if(nameSpace==null || nameSpace.equals(""))
	{
		throw new RuntimeException("Namespace error invalid :" + nameSpace);
	}
	
	MessageFactory mf;

	try {
		mf = MessageFactory.newInstance();

		message = mf.createMessage();

		SOAPPart soapPart = message.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();

		envelope.addNamespaceDeclaration("soapenv",
				"http://schemas.xmlsoap.org/soap/envelope/");
		envelope.addNamespaceDeclaration(BAS, nameSpace);

		envelope.addNamespaceDeclaration("sec", secNameSpace);

		// header
		envelope.getHeader().detachNode();
		SOAPHeader soapHeader = envelope.addHeader();

		Name header = envelope.createName("Header", "soapenv",
				"http://schemas.xmlsoap.org/soap/envelope/");

		soapHeader.addHeaderElement(header);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return message;
}
 
Example 7
Source File: SimpleSecurityHandler.java    From onvif with Apache License 2.0 4 votes vote down vote up
@Override
public boolean handleMessage(final SOAPMessageContext msgCtx) {
  // System.out.println("SimpleSecurityHandler");

  // Indicator telling us which direction this message is going in
  final Boolean outInd = (Boolean) msgCtx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

  // Handler must only add security headers to outbound messages
  if (outInd.booleanValue()) {
    try {
      // Create the xml
      SOAPMessage soapMessage = msgCtx.getMessage();
      SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();
      SOAPHeader header = envelope.getHeader();
      if (header == null) header = envelope.addHeader();

      SOAPPart sp = soapMessage.getSOAPPart();
      SOAPEnvelope se = sp.getEnvelope();
      se.addNamespaceDeclaration(WSSE_PREFIX, WSSE_NS);
      se.addNamespaceDeclaration(WSU_PREFIX, WSU_NS);

      SOAPElement securityElem = header.addChildElement(WSSE_LN, WSSE_PREFIX);
      // securityElem.setAttribute("SOAP-ENV:mustUnderstand", "1");

      SOAPElement usernameTokenElem =
          securityElem.addChildElement(USERNAME_TOKEN_LN, WSSE_PREFIX);

      SOAPElement usernameElem = usernameTokenElem.addChildElement(USERNAME_LN, WSSE_PREFIX);
      usernameElem.setTextContent(username);

      SOAPElement passwordElem = usernameTokenElem.addChildElement(PASSWORD_LN, WSSE_PREFIX);
      passwordElem.setAttribute(PASSWORD_TYPE_ATTR, PASSWORD_DIGEST);
      passwordElem.setTextContent(encryptPassword(password));

      SOAPElement nonceElem = usernameTokenElem.addChildElement(NONCE_LN, WSSE_PREFIX);
      nonceElem.setAttribute("EncodingType", BASE64_ENCODING);
      nonceElem.setTextContent(Base64.encodeBase64String(nonce.getBytes()));

      SOAPElement createdElem = usernameTokenElem.addChildElement(CREATED_LN, WSU_PREFIX);
      createdElem.setTextContent(getLastUTCTime());
    } catch (final Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  return true;
}
 
Example 8
Source File: SOAPRequestBuilder.java    From cougar with Apache License 2.0 4 votes vote down vote up
public void generateSoapHeader(SOAPEnvelope envelope,
		HttpCallBean httpCallBean) throws SOAPException {

	// Remove existing null header
	envelope.getHeader().detachNode();

	// add new soap header
	SOAPHeader soapHeader = envelope.addHeader();

	// header are not set then break
	if (httpCallBean.getAuthority() == null
			&& httpCallBean.getSubject() == null
			&& httpCallBean.getAuthCredentials()== null) {
		return;
	}

	// create element for headers

	if (httpCallBean.getAuthority() != null) {
		String authority = httpCallBean.getAuthority();

		/*soapHeader.addChildElement(
				envelope.createName("Authentication", "", secNameSpace))
				.setTextContent(authority);*/
		
		soapHeader.addChildElement(
				envelope.createName("X-Authentication", "", SECURITY_NAME_SPACE))
				.setValue(authority);

	}

	if (httpCallBean.getAuthCredentials() != null) {
		Map<String, String> credentials = httpCallBean.getAuthCredentials();

		SOAPElement credElement = soapHeader.addChildElement(
				envelope.createName("Credentials", "", SECURITY_NAME_SPACE));
		for(Map.Entry<String,String> entry: credentials.entrySet()){
			credElement.addChildElement(entry.getKey(), "", SECURITY_NAME_SPACE).setValue(entry.getValue());
		}
		
	}

	if (httpCallBean.getSubject() != null) {
		String subject = httpCallBean.getSubject();

		/*soapHeader.addChildElement(
				envelope.createName("Subject", "", secNameSpace))
				.setTextContent(subject);*/
		
		soapHeader.addChildElement(
				envelope.createName("Subject", "", SECURITY_NAME_SPACE))
				.setValue(subject);
	}
}