/*
   * (non-Javadoc)
   * @see org.apache.axis2.jaxws.core.controller.InvocationController#prepareRequest(org.apache.axis2.jaxws.core.MessageContext)
   */
  protected void prepareRequest(MessageContext requestMsgCtx) {
    try {
      if (requestMsgCtx == null) {
        // throw an exception
      }

      org.apache.axis2.context.MessageContext axisRequestMsgCtx =
          requestMsgCtx.getAxisMessageContext();

      // The MessageContext will contain a Message object with the
      // contents that need to be sent.  We need to get those contents
      // in a form that Axis2 can consume them, an AXIOM SOAPEnvelope.
      MessageUtils.putMessageOnMessageContext(
          requestMsgCtx.getMessage(), // JAX-WS Message
          axisRequestMsgCtx // Axis 2 MessageContext
          );

      if (log.isDebugEnabled()) {
        log.debug("Properties: " + axisRequestMsgCtx.getProperties().toString());
      }
    } catch (WebServiceException e) {
      throw ExceptionFactory.makeWebServiceException(Messages.getMessage("prepareRequestFail"));
    } catch (AxisFault e) {
      throw ExceptionFactory.makeWebServiceException(Messages.getMessage("prepareRequestFail"), e);
    }
  }
  /*
   * TODO: This is a first pass at filtering the properties that are set on the
   * RequestContext.  Right now it's called during the invoke, but needs to be
   * moved over to when the property is set.  This should not be in the path
   * of performance.
   */
  private void setupProperties(MessageContext mc) { // , Options ops) {

    // Enable MTOM
    Message msg = mc.getMessage();
    if (msg.isMTOMEnabled()) {
      mc.setProperty(Configuration.ENABLE_MTOM, "true");
    }

    // Enable session management
    if (mc.isMaintainSession()) {
      mc.getAxisMessageContext().getOptions().setManageSession(true);
    }

    // Check to see if BASIC_AUTH is enabled.  If so, make sure
    // the properties are setup correctly.
    if (mc.containsKey(BindingProvider.USERNAME_PROPERTY)
        || mc.containsKey(BindingProvider.PASSWORD_PROPERTY)) {

      String userId = (String) mc.getProperty(BindingProvider.USERNAME_PROPERTY);
      if (userId == null || userId == "") {
        throw ExceptionFactory.makeWebServiceException(Messages.getMessage("checkUserName"));
      }

      String password = null;
      if (mc.containsKey(BindingProvider.PASSWORD_PROPERTY)) {
        password = (String) mc.getProperty(BindingProvider.PASSWORD_PROPERTY);
      }

      URL url = null;
      try {
        url = new URL((String) mc.getProperty(BindingProvider.ENDPOINT_ADDRESS_PROPERTY));
      } catch (MalformedURLException e) {
        throw ExceptionFactory.makeWebServiceException(e);
      }

      HttpTransportProperties.Authenticator basicAuthentication =
          new HttpTransportProperties.Authenticator();
      basicAuthentication.setUsername(userId);
      basicAuthentication.setPassword(password);
      basicAuthentication.setHost(url.getHost());
      basicAuthentication.setPort(url.getPort());
      basicAuthentication.setPreemptiveAuthentication(true);

      mc.setProperty(HTTPConstants.AUTHENTICATE, basicAuthentication);
    }
  }
  /*
   * (non-Javadoc)
   * @see org.apache.axis2.jaxws.core.controller.InvocationController#invoke(org.apache.axis2.jaxws.core.InvocationContext)
   */
  public MessageContext doInvoke(MessageContext request) {

    // We need the qname of the operation being invoked to know which
    // AxisOperation the OperationClient should be based on.
    // Note that the OperationDesc is only set through use of the Proxy. Dispatch
    // clients do not use operations, so the operationDesc will be null.  In this
    // case an anonymous AxisService with anoymouns AxisOperations for the supported
    // MEPs will be created; and it is that anonymous operation name which needs to
    // be specified
    QName operationName = getOperationNameToUse(request, ServiceClient.ANON_OUT_IN_OP);

    // TODO: Will the ServiceClient stick around on the InvocationContext
    // or will we need some other mechanism of creating this?
    // Try to create an OperationClient from the passed in ServiceClient
    InvocationContext ic = request.getInvocationContext();
    ServiceClient svcClient = ic.getServiceClient();
    OperationClient opClient = createOperationClient(svcClient, operationName);

    initOperationClient(opClient, request);

    // Setup the client so that it knows whether the underlying call to
    // Axis2 knows whether or not to start a listening port for an
    // asynchronous response.
    Boolean useAsyncMep = (Boolean) request.getProperty(Constants.USE_ASYNC_MEP);
    if ((useAsyncMep != null && useAsyncMep.booleanValue())
        || opClient.getOptions().isUseSeparateListener()) {
      configureAsyncListener(opClient);
    } else {
      if (log.isDebugEnabled()) {
        log.debug(
            "Asynchronous message exchange not enabled.  The invocation will be synchronous.");
      }
    }

    org.apache.axis2.context.MessageContext axisRequestMsgCtx = request.getAxisMessageContext();
    org.apache.axis2.context.MessageContext axisResponseMsgCtx = null;

    MessageContext response = null;

    AxisFault faultexception =
        null; // don't let the keyword "fault" confuse you.  This is an exception class.
    try {
      execute(opClient, true, axisRequestMsgCtx);
    } catch (AxisFault af) {
      // If an AxisFault was thrown, we need to cleanup the original OperationContext.
      // Failure to do so results in a memory leak.
      opClient.getOperationContext().cleanup();
      // save the fault in case it didn't come from the endpoint, and thus
      // there would be no message on the MessageContext
      faultexception = af;
      if (log.isDebugEnabled()) {
        log.debug(
            axisRequestMsgCtx.getLogIDString()
                + " AxisFault received from client: "
                + af.getMessage());
      }
    }

    try {
      // Collect the response MessageContext and envelope
      axisResponseMsgCtx = opClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
      response = new MessageContext(axisResponseMsgCtx);
      response.setMEPContext(request.getMEPContext());

      // If the Message object is still null, then it's possible that a
      // local AxisFault was thrown and we need to save it for later throwing
      // We do not want to create a message and go through the whole handler or
      // XMLFault processing because it's unnecessary.
      //
      // Same is true if we get a valid non-fault server response but some jaxws
      // client processing (a handler, perhaps) throws an exception.
      //
      // If the response message itself is a fault message, let it pass through.
      if ((faultexception != null)
          && ((response.getMessage() == null) || (!response.getMessage().isFault()))) {
        MessageFactory factory = (MessageFactory) FactoryRegistry.getFactory(MessageFactory.class);
        Message message = factory.create(request.getMessage().getProtocol());
        response.setLocalException(faultexception);
        response.setMessage(message);
      }

      // This assumes that we are on the ultimate execution thread
      ThreadContextMigratorUtil.performMigrationToThread(
          Constants.THREAD_CONTEXT_MIGRATOR_LIST_ID, axisResponseMsgCtx);
    } catch (Exception e) {
      throw ExceptionFactory.makeWebServiceException(e);
    }

    return response;
  }