package com.fp.webservices.snap.seguridad;

import com.fp.webservices.snap.seguridad.DatosHeader;
import java.util.Set;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class HeaderHandler
        implements SOAPHandler<SOAPMessageContext> {

    DatosHeader DHLista = new DatosHeader();

    public HeaderHandler(DatosHeader DHeader) {
        this.DHLista.setDigest(DHeader.getDigest());
        this.DHLista.setNonce(DHeader.getNonce());
        this.DHLista.setFecha(DHeader.getFecha());
        this.DHLista.setFechaf(DHeader.getFechaf());
        this.DHLista.setUsuario(DHeader.getUsuario());
    }

    public boolean handleMessage(SOAPMessageContext smc) {
        Boolean outboundProperty = (Boolean) smc.get("javax.xml.ws.handler.message.outbound");

        if (outboundProperty.booleanValue()) {

            SOAPMessage message = smc.getMessage();

            try {
                SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
                SOAPHeader header = envelope.addHeader();

                SOAPElement security = header.addChildElement("Security", "wss", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

                SOAPElement timeStamp = security.addChildElement("Timestamp", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

                SOAPElement createdTime = timeStamp.addChildElement("Created", "wsu");

                createdTime.addTextNode(this.DHLista.getFecha());

                SOAPElement expires = timeStamp.addChildElement("Expires", "wsu");

                expires.addTextNode(this.DHLista.getFechaf());

                SOAPElement usernameToken = security.addChildElement("UsernameToken", "wss");

                SOAPElement username = usernameToken.addChildElement("Username", "wss");

                username.addTextNode(this.DHLista.getUsuario());

                SOAPElement password = usernameToken.addChildElement("Password", "wss");

                password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
                password.addTextNode(this.DHLista.getDigest());

                SOAPElement nonce = usernameToken.addChildElement("Nonce", "wss");

                nonce.setAttribute("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
                nonce.addTextNode(this.DHLista.getNonce());

                SOAPElement created = usernameToken.addChildElement("Created", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
                created.addTextNode(this.DHLista.getFecha());

                message.writeTo(System.out);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {

            try {

                SOAPMessage message = smc.getMessage();
                message.writeTo(System.out);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return outboundProperty.booleanValue();
    }

    public Set getHeaders() {
        return null;
    }

    public boolean handleFault(SOAPMessageContext context) {
        return false;
    }

    public void close(MessageContext context) {
    }
}


/* Location:              /data/COMACO/SINCOAR2022/Entorno/produccion/servers/mule-standalone-3.5.0/apps/centralwebservices-2.1.zip!/centralwebservices-2.1/classes/com/fp/webservices/snap/seguridad/HeaderHandler.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       1.1.3
 */
