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

/*
 * $Id: ServerLogicalHandler4.java 62638 2011-04-26 16:08:02Z adf $
 */

package com.sun.ts.tests.jaxws.sharedwebservices.dlhandlerservice;

import com.sun.ts.lib.util.*;
import com.sun.ts.lib.porting.*;
import com.sun.ts.tests.jaxws.common.*;

import javax.xml.ws.handler.LogicalMessageContext;
import javax.xml.ws.soap.SOAPFaultException;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.Name;


import java.io.StringReader;
import javax.xml.transform.stream.StreamSource;

public class ServerLogicalHandler4 extends LogicalHandlerBase
{
    private static final String NAMESPACEURI = "http://dlhandlerservice.org/wsdl";
    private static final String WHICHHANDLERTYPE="Server";
    private static final String HANDLERNAME="ServerLogicalHandler4";

    private final QName FAULTCODE = new QName(NAMESPACEURI, "ItsASoapFault", "tns");
    private static final String FAULTACTOR = "faultActor";
    private Name name = null;
    private javax.xml.soap.SOAPFault sf;


    public ServerLogicalHandler4(){
       super();
       super.setWhichHandlerType(WHICHHANDLERTYPE);
       super.setHandlerName(HANDLERNAME);
    }
    public boolean handleMessage(LogicalMessageContext context)
    {

      System.out.println("in "+this+":handleMessage");
      TestUtil.logTrace("in "+this+":handleMessage");
          Handler_Util.setTraceFlag(Handler_Util.getValueFromMsg(this, context, "harnesslogtraceflag"));

          Handler_Util.initTestUtil(this,
                                    Handler_Util.getValueFromMsg(this, context, "harnessloghost"),
                                    Handler_Util.getValueFromMsg(this, context, "harnesslogport"),
                                    Handler_Util.getValueFromMsg(this, context, "harnesslogtraceflag"));

         String direction = Handler_Util.getDirection(context);
         if (!Handler_Util.checkForMsg(this, context, "GetTrackerData")) {
             HandlerTracker.reportHandleMessage(this, Handler_Util.getDirection(context));
             if (direction.equals(Constants.INBOUND)){
                 if (Handler_Util.checkForMsg(this, context,"ServerLogicalInboundHandleMessageThrowsRuntimeExceptionTest")) {
                    HandlerTracker.reportComment(this, "Throwing an inbound RuntimeException");
                    throw new RuntimeException(HANDLERNAME+ ".handleMessage throwing an inbound RuntimeException");
                 } else if (Handler_Util.checkForMsg(this, context,"ServerLogicalInboundHandleMessageFalseTest")) {
                      String tmp = Handler_Util.getMessageAsString(context);
                      String response = tmp.replaceAll("MyAction","MyResult");
                      context.getMessage().setPayload(new StreamSource(new StringReader(response)));
                      return false;
                 } else if (Handler_Util.checkForMsg(this, context,"ServerLogicalInboundHandleMessageThrowsSOAPFaultTest")) {
                      HandlerTracker.reportComment(this, "Throwing an inbound SOAPFaultException");
                      String faultString = "ServerLogicalHandler4.handleMessage throwing an inbound SOAPFaultException";
                      try {
                          name = SOAPFactory.newInstance().createName("somefaultentry");
                          sf = JAXWS_Util.createSOAPFault("soap11", FAULTCODE, FAULTACTOR, faultString, name);
                      } catch (Exception e) {
                          HandlerTracker.reportThrowable(this, new Exception("Unexpected error in handleMessage for an inbound message"+e));
                      }
                      throw new SOAPFaultException(sf);
                 }
             } else if (direction.equals(Constants.OUTBOUND)){

                 if (Handler_Util.checkForMsg(this, context,"ServerLogicalOutboundHandleMessageThrowsRuntimeExceptionTest")) {
                    HandlerTracker.reportComment(this, "Throwing an outbound RuntimeException");
                    throw new RuntimeException(HANDLERNAME+".handleMessage throwing an outbound RuntimeException");
                 } else if (Handler_Util.checkForMsg(this, context,"ServerLogicalOutboundHandleMessageFalseTest")) {
                      return false;
                 } else if (Handler_Util.checkForMsg(this, context,"ServerLogicalOutboundHandleMessageThrowsSOAPFaultTest")) {
                      HandlerTracker.reportComment(this, "Throwing an outbound SOAPFaultException");
                      String faultString = "ServerLogicalHandler4.handleMessage throwing an outbound SOAPFaultException";
                      try {
                          name = SOAPFactory.newInstance().createName("somefaultentry");
                          sf = JAXWS_Util.createSOAPFault("soap11", FAULTCODE, FAULTACTOR, faultString, name);
                      } catch (Exception e) {
                          HandlerTracker.reportThrowable(this, new Exception("Unexpected error in handleMessage for an outbound message"+e));
                      }
                      throw new SOAPFaultException(sf);
                 }
             }
         }

     System.out.println("exiting "+this+":handleMessage");
     TestUtil.logTrace("exiting "+this+":handleMessage");
     return true;
    }

    public boolean handleFault(LogicalMessageContext context)
    {
       System.out.println("in "+this+":handleFault");
       TestUtil.logTrace("in "+this+":handleFault");
           HandlerTracker.reportHandleFault(this);
           String direction = Handler_Util.getDirection(context);
           if (direction.equals(Constants.OUTBOUND)){
               if (Handler_Util.checkForMsg(this, context,"ServerLogicalHandler5.handleMessage throws SOAPFaultException for ServerLogicalInboundHandleFaultFalseTest")) {
                 return false;
               } else if (Handler_Util.checkForMsg(this, context,"ServerLogicalHandler6.handleMessage throws SOAPFaultException for ServerLogicalInboundHandleFaultThrowsRuntimeExceptionTest")){
                HandlerTracker.reportComment(this, "Throwing an outbound RuntimeException");
                    throw new RuntimeException(HANDLERNAME+".handleFault throwing an outbound RuntimeException");
               } else if (Handler_Util.checkForMsg(this, context,"ServerLogicalHandler6.handleMessage throws SOAPFaultException for ServerLogicalInboundHandleFaultThrowsSOAPFaultExceptionTest")) {
                    HandlerTracker.reportComment(this, "Throwing an outbound SOAPFaultException");
                    String faultString = "ServerLogicalHandler4.handleFault throwing an outbound SOAPFaultException";
                    try {
                        name = SOAPFactory.newInstance().createName("somefaultentry");
                        sf = JAXWS_Util.createSOAPFault("soap11", FAULTCODE, FAULTACTOR, faultString, name);
                    } catch (Exception e) {
                        HandlerTracker.reportThrowable(this, new Exception("Unexpected error occurred in handleFault for an outbound message"+e));
                    }
                    throw new SOAPFaultException(sf);
               }
           }

     System.out.println("exiting "+this+":handleFault");
     TestUtil.logTrace("exiting "+this+":handleFault");
     return true;
   }

}
