Java Code Examples for javax.xml.soap.SOAPHeaderElement#getChildElements()

The following examples show how to use javax.xml.soap.SOAPHeaderElement#getChildElements() . 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: WsaDecoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@Override
public List<WsaHeader> decode(List<SOAPHeaderElement> list) {
    List<WsaHeader> wsaHeaders = Lists.newArrayListWithCapacity(list.size());
    boolean to = false;
    boolean replyTo = false;
    boolean messageId = false;
    boolean action = false;
    for (SOAPHeaderElement soapHeaderElement : list) {
        if (soapHeaderElement.getLocalName().equals(WsaConstants.EN_TO)) {
            wsaHeaders.add(new WsaToHeader(soapHeaderElement.getValue()));
            to = true;
        } else if (soapHeaderElement.getLocalName().equals(WsaConstants.EN_ACTION)) {
            wsaHeaders.add(new WsaActionHeader(soapHeaderElement.getValue()));
            action = true;
        } else if (soapHeaderElement.getLocalName().equals(WsaConstants.EN_REPLY_TO)) {
            Iterator<?> iter = soapHeaderElement.getChildElements();
            while (iter.hasNext()) {
                Node node = (Node) iter.next();
                if (node.getLocalName() != null && node.getLocalName().equals(WsaConstants.EN_ADDRESS)) {
                    wsaHeaders.add(new WsaReplyToHeader(node.getValue()));
                    replyTo = true;
                }
            }
        } else if (soapHeaderElement.getLocalName().equals(WsaConstants.EN_MESSAGE_ID)) {
            wsaHeaders.add(new WsaMessageIDHeader(soapHeaderElement.getValue()));
            messageId = true;
        }
    }
    if ((to || replyTo || messageId) && !action) {
        wsaHeaders.add(new WsaActionHeader(WsaConstants.WSA_FAULT_ACTION));
    }
    return wsaHeaders;
}
 
Example 2
Source File: JPlagServerAccessHandler.java    From jplag with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Extracts the username out of the header of a SOAP message
 */
public static String extractUsername(SOAPMessageContext smsg) {
	try {
		SOAPHeader header = smsg.getMessage().getSOAPHeader();
		if (header != null) {
			@SuppressWarnings("unchecked")
			Iterator<SOAPHeaderElement> headers = header.examineAllHeaderElements();
			while (headers.hasNext()) {
				SOAPHeaderElement he = headers.next();

				if (he.getElementName().getLocalName().equals("Access")) {
					@SuppressWarnings("unchecked")
					Iterator<SOAPElement> elements = he.getChildElements();
					while (elements.hasNext()) {
						SOAPElement e = elements.next();
						String name = e.getElementName().getLocalName();
						if (name.equals("username"))
							return e.getValue();
					}
				}
			}
		}
	} catch (SOAPException x) {
		x.printStackTrace();
	}
	return null;
}
 
Example 3
Source File: JPlagServerAccessHandler.java    From jplag with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Checks whether the SOAP message contains a valid Access element (correct
 * username + password) and whether the account may still be used
 */
public boolean handleRequest(MessageContext context) {
	String username = null;
	String password = null;
	int compatLevel = 0;
	if (context instanceof SOAPMessageContext) {
		SOAPMessageContext smsg = (SOAPMessageContext) context;

		/*
		 * Iterator iter = context.getPropertyNames();
		 * System.out.println("Context properties:"); while(iter.hasNext())
		 * { String propname = (String) iter.next();
		 * System.out.println(propname + " = " +
		 * context.getProperty(propname).toString()); }
		 * 
		 * HttpServletRequest request = (HttpServletRequest)
		 * context.getProperty(
		 * "com.sun.xml.rpc.server.http.HttpServletRequest");
		 * System.out.println("Client IP: " + request.getRemoteAddr());
		 */

		try {
			SOAPHeader header = smsg.getMessage().getSOAPHeader();
			if (header != null) {
				@SuppressWarnings("unchecked")
				Iterator<SOAPHeaderElement> headers = header.examineAllHeaderElements();
				while (headers.hasNext()) {
					SOAPHeaderElement he = headers.next();

					if (he.getElementName().getLocalName().equals("Access")) {
						@SuppressWarnings("unchecked")
						Iterator<SOAPElement> elements = he.getChildElements();
						while (elements.hasNext()) {
							SOAPElement e = elements.next();
							String name = e.getElementName().getLocalName();
							if (name.equals("username"))
								username = e.getValue();
							else if (name.equals("password"))
								password = e.getValue();
							else if (name.equals("compatLevel")) {
								try {
									compatLevel = Integer.parseInt(e.getValue());
								} catch (NumberFormatException ex) {
									compatLevel = -1;
								}
							}
						}
					}
				}
				if (compatLevel < compatibilityLevel) {
					replaceByJPlagException(smsg, "Client outdated!", "Please update your client " + "to compatibility level "
							+ compatibilityLevel + ".");
					return false;
				}
				if (username != null && password != null) {
					int state = JPlagCentral.getInstance().getUserAdmin().getLoginState(username, password);
					if ((state & UserAdmin.MASK_EXPIRED) != 0) {
						replaceByJPlagException(smsg, "Access denied!", "Your account has " + "expired! Please contact the JPlag "
								+ "administrator to reactivate it!");
						return false;
					} else if ((state & UserAdmin.MASK_DEACTIVATED) != 0) {
						replaceByJPlagException(smsg, "Access denied!", "Your account has " + "been deactivated! Please contact the "
								+ "JPlag administrator to reactivate it!");
						return false;
					} else if (state != UserAdmin.USER_INVALID)
						return true;
				}
			} else {
				System.out.println("No header available!");
				replaceByJPlagException(smsg, "Access denied!", "The SOAP message doesn't contain an access header!");
				return false;
			}
		} catch (SOAPException x) {
			x.printStackTrace();
		}
		System.out.println("[" + new Date() + "] Access denied for user \"" + username + "\"!");

		replaceByJPlagException(smsg, "Access denied!", "Check your username and password!");
		return false;
	}
	System.out.println("Not a SOAP message context!!!");
	return false;
}