/**
   * This calls the unsecured web service for this method.
   *
   * @param body The message to be sent to the web service.
   * @param assertion The assertion information to go with the message.
   * @return The response from the web service.
   * @throws PatientDiscoveryException
   */
  @AdapterDelegationEvent(
      beforeBuilder = PRPAIN201305UV02EventDescriptionBuilder.class,
      afterReturningBuilder = PRPAIN201306UV02EventDescriptionBuilder.class,
      serviceType = "Patient Discovery",
      version = "1.0")
  public PRPAIN201306UV02 respondingGatewayPRPAIN201305UV02(
      PRPAIN201305UV02 body, AssertionType assertion) throws PatientDiscoveryException {
    String url = null;
    PRPAIN201306UV02 response = new PRPAIN201306UV02();
    String sServiceName = NhincConstants.PATIENT_DISCOVERY_ADAPTER_SERVICE_NAME;

    try {
      if (body != null) {
        LOG.debug("Before target system URL look up.");
        url = oProxyHelper.getAdapterEndPointFromConnectionManager(sServiceName);
        LOG.debug(
            "After target system URL look up. URL for service: " + sServiceName + " is: " + url);

        if (NullChecker.isNotNullish(url)) {
          RespondingGatewayPRPAIN201305UV02RequestType request =
              new RespondingGatewayPRPAIN201305UV02RequestType();
          request.setAssertion(assertion);
          request.setPRPAIN201305UV02(body);
          request.setNhinTargetCommunities(null);

          ServicePortDescriptor<AdapterPatientDiscoveryPortType> portDescriptor =
              new AdapterPatientDiscoveryServicePortDescriptor();
          CONNECTClient<AdapterPatientDiscoveryPortType> client =
              CONNECTClientFactory.getInstance()
                  .getCONNECTClientUnsecured(portDescriptor, url, assertion);

          response =
              (PRPAIN201306UV02)
                  client.invokePort(
                      AdapterPatientDiscoveryPortType.class,
                      "respondingGatewayPRPAIN201305UV02",
                      request);
        } else {
          throw new PatientDiscoveryException(
              "Failed to call the adapter web service (" + sServiceName + ").  The URL is null.");
        }
      } else {
        throw new PatientDiscoveryException(
            "Failed to call the web service (" + sServiceName + ").  The input parameter is null.");
      }
    } catch (Exception e) {
      LOG.error(
          "Failed to call the web service ("
              + sServiceName
              + ").  An unexpected exception occurred.  "
              + "Exception: "
              + e.getMessage(),
          e);
      throw new PatientDiscoveryException(e.fillInStackTrace());
    }

    return response;
  }
  /**
   * This test ensures that the interceptor count is the same no matter how many times the decorator
   * is called on the constructor.
   */
  @Test
  public void ensureInterceptorCountIsConstant() {
    CONNECTClient<TestServicePortType> client = createClient();

    Client cxfClient = ClientProxy.getClient(client.getPort());
    int numOutInterceptors = cxfClient.getOutInterceptors().size();

    createClient();
    createClient();
    CONNECTClient<TestServicePortType> client2 = createClient();

    Client cxfClient2 = ClientProxy.getClient(client2.getPort());
    assertEquals(numOutInterceptors, cxfClient2.getOutInterceptors().size());
  }
  public MCCIIN000002UV01 processPatientDiscoveryAsyncResp(
      PRPAIN201306UV02 request, AssertionType assertion, NhinTargetCommunitiesType target) {
    LOG.debug(
        "Begin EntityPatientDiscoveryDeferredResponseProxyWebServiceSecuredImpl.processPatientDiscoveryAsyncResp(...)");
    MCCIIN000002UV01 response = new MCCIIN000002UV01();

    String serviceName = NhincConstants.PATIENT_DISCOVERY_ENTITY_SECURED_ASYNC_RESP_SERVICE_NAME;

    try {
      LOG.debug("Before target system URL look up.");
      String url = oProxyHelper.getUrlLocalHomeCommunity(serviceName);
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "After target system URL look up. URL for service: " + serviceName + " is: " + url);
      }

      if (NullChecker.isNotNullish(url)) {
        RespondingGatewayPRPAIN201306UV02SecuredRequestType securedRequest =
            new RespondingGatewayPRPAIN201306UV02SecuredRequestType();
        if (request != null) {
          securedRequest.setNhinTargetCommunities(target);
          securedRequest.setPRPAIN201306UV02(request);
        }
        ServicePortDescriptor<EntityPatientDiscoverySecuredAsyncRespPortType> portDescriptor =
            new EntityPatientDiscoverySecuredAsyncRespServicePortDescriptor();
        CONNECTClient<EntityPatientDiscoverySecuredAsyncRespPortType> client =
            CONNECTClientFactory.getInstance()
                .getCONNECTClientSecured(portDescriptor, url, assertion);
        response =
            (MCCIIN000002UV01)
                client.invokePort(
                    EntityPatientDiscoverySecuredAsyncRespPortType.class,
                    "processPatientDiscoveryAsyncResp",
                    securedRequest);
      } else {
        LOG.error("Failed to call the web service (" + serviceName + ").  The URL is null.");
      }
    } catch (Exception ex) {
      LOG.error(
          "Error: Failed to retrieve url for service: "
              + serviceName
              + " for local home community");
      LOG.error(ex.getMessage(), ex);
    }

    LOG.debug(
        "End EntityPatientDiscoveryDeferredResponseProxyWebServiceSecuredImpl.processPatientDiscoveryAsyncResp(...)");
    return response;
  }
  /**
   * This method retrieves PatientCorrelation from the targeted community.
   *
   * @param msg PRPAIN201309UV02 HL7 type of Request received.
   * @param assertion Assertion received.
   * @return PatientCorrelationresponse.
   */
  public RetrievePatientCorrelationsResponseType retrievePatientCorrelations(
      PRPAIN201309UV02 msg, AssertionType assertion) {
    LOG.debug("Begin retrievePatientCorrelations");
    RetrievePatientCorrelationsResponseType response =
        new RetrievePatientCorrelationsResponseType();
    RetrievePatientCorrelationsSecuredResponseType securedResp =
        new RetrievePatientCorrelationsSecuredResponseType();

    try {
      String url =
          oProxyHelper.getUrlLocalHomeCommunity(
              NhincConstants.PATIENT_CORRELATION_SECURED_SERVICE_NAME);

      if (msg == null) {
        LOG.error("Message was null");
      } else {
        RetrievePatientCorrelationsSecuredRequestType request =
            new RetrievePatientCorrelationsSecuredRequestType();
        request.setPRPAIN201309UV02(msg);

        ServicePortDescriptor<PatientCorrelationSecuredPortType> portDescriptor =
            getRetrieveServicePortDescriptor(NhincConstants.ADAPTER_API_LEVEL.LEVEL_a0);

        CONNECTClient<PatientCorrelationSecuredPortType> client =
            CONNECTClientFactory.getInstance()
                .getCONNECTClientSecured(portDescriptor, url, assertion);

        securedResp =
            (RetrievePatientCorrelationsSecuredResponseType)
                client.invokePort(
                    PatientCorrelationSecuredPortType.class,
                    "retrievePatientCorrelations",
                    request);
        if (securedResp != null && securedResp.getPRPAIN201310UV02() != null) {
          response.setPRPAIN201310UV02(securedResp.getPRPAIN201310UV02());
        }
      }
    } catch (Exception ex) {
      LOG.error("Error calling retrievePatientCorrelations: " + ex.getMessage(), ex);
    }

    LOG.debug("End retrievePatientCorrelations");
    return response;
  }
  public Element subscribe(
      Element subscribeElement, AssertionType assertion, NhinTargetSystemType target)
      throws Exception {
    Element responseElement = null;

    String url =
        getWebServiceProxyHelper()
            .getAdapterEndPointFromConnectionManager(
                NhincConstants.HIEM_SUBSCRIBE_ADAPTER_SERVICE_NAME);
    if (NullChecker.isNotNullish(url)) {

      WsntSubscribeMarshaller subscribeMarshaller = new WsntSubscribeMarshaller();
      Subscribe subscribe = subscribeMarshaller.unmarshalUnsubscribeRequest(subscribeElement);

      SubscribeRequestType adapterSubcribeRequest = new SubscribeRequestType();
      adapterSubcribeRequest.setSubscribe(subscribe);
      adapterSubcribeRequest.setAssertion(assertion);

      ServicePortDescriptor<AdapterNotificationProducerPortType> portDescriptor =
          new HiemSubscribeAdapterServicePortDescriptor();

      CONNECTClient<AdapterNotificationProducerPortType> client =
          getCONNECTClientUnsecured(portDescriptor, url, assertion);

      SubscribeResponse response =
          (SubscribeResponse)
              client.invokePort(AdapterNotificationProducerPortType.class, "subscribe", subscribe);

      SubscribeResponseMarshaller subscribeResponseMarshaller = new SubscribeResponseMarshaller();
      responseElement = subscribeResponseMarshaller.marshal(response);

    } else {
      LOG.error(
          "Failed to call the web service ("
              + NhincConstants.HIEM_SUBSCRIBE_ADAPTER_SERVICE_NAME
              + ").  The URL is null.");
    }

    return responseElement;
  }
  /**
   * This method verifies that the passed in client is configured for Ws-Security properly.
   *
   * @param client
   */
  public void verifyWsSecurityProperties(CONNECTClient<?> client) {
    Client clientProxy = ClientProxy.getClient(client.getPort());

    WSS4JOutInterceptor wss4jInterceptor = null;
    for (Interceptor<? extends Message> interceptor : clientProxy.getOutInterceptors()) {
      if (interceptor instanceof WSS4JOutInterceptor) {
        wss4jInterceptor = (WSS4JOutInterceptor) interceptor;
        break;
      }
    }

    assertNotNull(wss4jInterceptor);
    assertTrue(wss4jInterceptor.isAllowMTOM());

    Map<String, Object> properties = wss4jInterceptor.getProperties();
    new WsSecurityConfigFactoryTest().verifyWsSecurityProperties(properties);
  }