import javax.xml.rpc.handler.Handler;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.soap.SOAPMessage;
...
import org.apache.xml.security.signature.XMLSignature;
import org.apache.xpath.CachedXPathAPI;
import org.apache.xml.security.utils.Constants;
 
public class AuthenticationHandler extends GenericHandler implements Handler {
 
  private String DSIG_NS = "xmlns:ds";
  private String DSIG_SIGNATURE_SEARCH_EXPR = "//ds:Signature";
  String BaseURI = "http://xml-security";
 
  public AuthenticationHandler() { }
 
  public void init(HandlerInfo config) {
    org.apache.xml.security.Init.init();
  }
 
  public boolean handleRequest(MessageContext context) {
    try {
      SOAPMessageContext soapMessageContext = (SOAPMessageContext) context;
      SOAPMessage soapMessage = soapMessageContext.getMessage();
      Source source = soapMessage.getSOAPPart().getContent();
 
      // pobranie dokumentu ze rda
      Document signedDoc = getDocument(source);
 
      // obliczanie wyrae XPath
      CachedXPathAPI xPath = new CachedXPathAPI();
 
      // uywamy przestrzeni nazw Node do uzyskania prefiksw przestrzeni nazw
      // w nastpujcym wyszukaniu xpath
      Element nsctx = signedDoc.createElement("nsctx");
      nsctx.setAttribute(DSIG_NS, Constants.SignatureSpecNS);
 
      // uycie wyraenia XPath do znalezienia elementu podpisu
      Element signatureElem = (Element)xPath.selectSingleNode(signedDoc,
                              DSIG_SIGNATURE_SEARCH_EXPR, nsctx);
 
      // sprawdzenie, czy dokument jest podpisany
      if (sigatureElem == null) {
        // obsuga braku podpisu
        System.out.println("Dokument nie jest podpisany");
        return false;
      }
 
      // sprawdzanie podpisu
      XMLSignature signature = new XMLSignature(signatureElem, BaseURI);
      boolean varify = 
        signature.checkSignatureValue(signature.getKeyInfo().getPublicKey());
 
      // wywietlenie komunikatu
      System.out.println("Podpis jest" + (verify ? " ": " nie") + "poprawny");
 
      return verify;
    } catch(Exception e) {
      // obsuga wyjtku
    }
    return false;
  }
 
  public boolean handleResponse(MessageContext context) {
    return true;
  }
  ...
}
