/*
 *     Copyright (C) 2015 ASIP Santé
 */
package fr.asipsante.mss.clientcxfmss.spring;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Interceptor CXF permet d'insérer le header HTTP PAOS.
 * @author ASIP Santé
 */
public class PAOSHeaderInterceptor implements PhaseInterceptor<SoapMessage>{

    public static final String[] PAOS_CONTENU_HEADER = new String[]{"ver='urn:liberty:paos:2003-08';'urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp'"};
    public static final String PAOS_NOM_HEADER = "PAOS";
    
    private static final Logger LOGGER=LoggerFactory.getLogger(PAOSHeaderInterceptor.class);
    
     public void handleFault(SoapMessage t) {
    }

    public Set<String> getAfter() {
        return Collections.EMPTY_SET;
    }

    public Set<String> getBefore() {
        return Collections.EMPTY_SET;
    }

    public String getId() {
        return toString();
    }

    public String getPhase() {
        return "pre-protocol";
    }
    
    public Collection<PhaseInterceptor<? extends Message>> getAdditionalInterceptors() {
        return Collections.EMPTY_SET;
    }

    @Override
    public void handleMessage(SoapMessage t) throws Fault{
        Map<String,List<String>> headers=(Map<String, List<String>>)t.get(Message.PROTOCOL_HEADERS);
        headers.put(PAOS_NOM_HEADER, Arrays.asList(PAOS_CONTENU_HEADER));
        LOGGER.debug(headers.entrySet().toString());
    }
    
}
