package examples.mega.client;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;

import weblogic.webservice.context.WebServiceContext;

public class WebServiceContextClient{

  public void invoke( MegaWebService service, 
      MegaPort port ) throws RemoteException, InvokeFailedException{

    // context is accessed on the server side.
    // look at the server side code.
    port.accessWebServiceContext();
    System.out.println( "Server side accessed the session ok" );

    //server side maintains how may times maintainSessionState()
    //method is called.
    for( int i=0; i<10; i++ ){
      int count = port.maintainSessionState();

      //check to make sure that the session is maintained on the
      //server side
      if( count != i ){
        throw new InvokeFailedException( "Session state should be = " + i +
            ", but got = " + count );
      }
    }

    System.out.println( "Server can maintain state" );

    //pass implicit header
    QName headerName = new QName( "http://www.myheader.namespace/", 
          "my-implicit-header" );

    WebServiceContext context = service.context();
    context.getHeader().put( headerName, DataTypes.getBaseData() );

    //implicitHeader() method returs the soap header
    BaseData result = port.implicitHeader();

    //check whether we got back the same data that was send as
    //headr
    DataTypes.checkData( "implicit header invoke failed", result );
    System.out.println( "Header got back: " + result );

    //header is not removed from the context, so implicitHeader()
    //should again return the same header
    result = port.implicitHeader();

    DataTypes.checkData( "Header was not remove, but the server " +
        "did not return it:", result );

    System.out.println( "Header got back: " + result );

    context.getHeader().remove( headerName );
    result = port.implicitHeader();

    if( result != null ){
      throw new InvokeFailedException( "did not get null header back " +
          "after remove header from the context " + result );
    }
  }
}

