/*
 * Copyright (c) 2007, 2009, Oracle and its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * $Id: ServerSOAPHandler.java 52501 2007-01-24 02:29:49Z lschwenk $
 */

package com.sun.ts.tests.jaxws.wsa.j2w.document.literal.refps;

import com.sun.ts.lib.util.*;

import com.sun.ts.tests.jaxws.common.Handler_Util;
import com.sun.ts.tests.jaxws.common.HTTPSOAPHandler;
import com.sun.ts.tests.jaxws.common.JAXWS_Util;
import com.sun.ts.tests.jaxws.common.XMLUtils;
import com.sun.ts.tests.jaxws.wsa.common.EprUtil;
import javax.xml.soap.*;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.WebServiceException;
import org.w3c.dom.Element;
import java.util.List;


public class ServerSOAPHandler extends HTTPSOAPHandler {

    protected void processInboundMessage(SOAPMessageContext context) {
       TestUtil.logMsg("In ServerSOAPHandler:processInboundMessage");
       System.out.println("In ServerSOAPHandler:processInboundMessage");
       SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
       JAXWS_Util.dumpSOAPMessage(msg, true);
       JAXWS_Util.dumpSOAPMessage(msg, false);
       if (Handler_Util.checkForMsg(context,"RequestReferenceParametersTest")){
           List<Element> rp = (List<Element>)context.get(MessageContext.REFERENCE_PARAMETERS);
           boolean result1 = false;
           boolean result2 = false;
           result1 = verifyReferenceParameters(rp,"MyParam1","Hello");
           result2 = verifyReferenceParameters(rp,"MyParam2","There");
           if ((!result1) && (!result2)){
                 throw new RuntimeException("In ServerSOAPHandler:processInboundMessage: Error: Reference Parameter MyParam1 and MyParam2 were either not found or their values were wrong");
           } else if (!result1) {
                 throw new RuntimeException("In ServerSOAPHandler:processInboundMessage: Error: Reference Parameter MyParam1 was not found or it's value was wrong");
           } else if (!result2) {
                 throw new RuntimeException("In ServerSOAPHandler:processInboundMessage: Error: Reference Parameter MyParam2 was not found or it's value was wrong");
           }
       }
   }
    protected boolean verifyReferenceParameters(List<Element> e, String name, String value) {
        boolean result=false;
        TestUtil.logMsg("in ServerSOAPHandler:verifyReferenceParameters");
        TestUtil.logMsg("Number of elements="+e.size());
        TestUtil.logMsg("Searching for Reference Parameter '"+name+"' and its value '"+value+"'");
        System.out.println("in ServerSOAPHandler:verifyReferenceParameters");
        System.out.println("Number of elements="+e.size());
        System.out.println("Searching for Reference Parameter '"+name+"' and its value '"+value+"'");
        if (e.size()>0) {
            for (int i=0;i<e.size();i++){
                Element element = (Element)e.get(i);
                boolean actual = EprUtil.validateReferenceParameter(element, name, value);
                if (actual){
                   result=true;
                   break;
                }
            }
	    if(!result) {
	        TestUtil.logErr("Reference Parameter '"+name+"' with value '"+value+"' was not found");
	        System.err.println("Reference Parameter '"+name+"' with value '"+value+"' was not found");
	    }
        }
        return result;
    }
}
