/*
   * for internal usage, creates a NOTIFY notify, asks the content to the
   * concrete implementation component, and then sends the request to the
   * subscriber
   */
  public void createAndSendNotify(
      EntityManager entityManager,
      Subscription subscription,
      Dialog dialog,
      ImplementedSubscriptionControlSbbLocalObject childSbb)
      throws TransactionDoesNotExistException, SipException, ParseException {

    // create notify
    Request notify = createNotify(dialog, subscription);
    // add content if subscription is active
    if (subscription.getStatus().equals(Subscription.Status.active)) {
      if (subscription.getKey().getEventPackage().endsWith(".winfo")) {
        // winfo content, increment version before adding the content
        subscription.incrementVersion();
        entityManager.persist(subscription);
        entityManager.flush();
        notify.setContent(
            sipSubscriptionHandler
                .sbb
                .getWInfoSubscriptionHandler()
                .getFullWatcherInfoContent(entityManager, subscription),
            sipSubscriptionHandler.sbb.getWInfoSubscriptionHandler().getWatcherInfoContentHeader());
      } else {
        // specific event package content
        NotifyContent notifyContent = childSbb.getNotifyContent(subscription);
        // add content
        if (notifyContent != null) {
          try {
            notify =
                setNotifyContent(
                    subscription,
                    notify,
                    notifyContent.getContent(),
                    notifyContent.getContentTypeHeader(),
                    childSbb);
          } catch (Exception e) {
            logger.error("failed to set notify content", e);
          }
        }
      }
    }

    // ....aayush added code here (with ref issue #567)
    notify.addHeader(addPChargingVectorHeader());

    // send notify
    ClientTransaction clientTransaction =
        sipSubscriptionHandler.sbb.getSipProvider().getNewClientTransaction(notify);
    dialog.sendRequest(clientTransaction);
    if (logger.isDebugEnabled()) {
      logger.debug("Request sent:\n" + notify.toString());
    }
  }
  public Request setNotifyContent(
      Subscription subscription,
      Request notify,
      Object content,
      ContentTypeHeader contentTypeHeader,
      ImplementedSubscriptionControlSbbLocalObject childSbb)
      throws JAXBException, ParseException, IOException {

    if (!subscription.getResourceList()) {
      // filter content per subscriber (notifier rules)
      Object filteredContent =
          childSbb.filterContentPerSubscriber(
              subscription.getSubscriber(),
              subscription.getNotifier(),
              subscription.getKey().getEventPackage(),
              content);
      // filter content per notifier (subscriber rules)
      // TODO
      // marshall content to string
      StringWriter stringWriter = new StringWriter();
      childSbb.getMarshaller().marshal(filteredContent, stringWriter);
      notify.setContent(stringWriter.toString(), contentTypeHeader);
      stringWriter.close();
    } else {
      // resource list subscription, no filtering
      if (content instanceof JAXBElement) {
        // marshall content to string
        StringWriter stringWriter = new StringWriter();
        childSbb.getMarshaller().marshal(content, stringWriter);
        notify.setContent(stringWriter.toString(), contentTypeHeader);
        stringWriter.close();
      } else {
        notify.setContent(content, contentTypeHeader);
      }
    }
    return notify;
  }
  /**
   * Requests authorization for the new sip subscription.
   *
   * @param event
   * @param aci
   * @param subscriber
   * @param subscriberDisplayName
   * @param notifier
   * @param key
   * @param expires
   * @param content
   * @param contentType
   * @param contentSubtype
   * @param entityManager
   * @param childSbb
   */
  public void authorizeNewSipSubscription(
      RequestEvent event,
      ActivityContextInterface aci,
      String subscriber,
      String subscriberDisplayName,
      Notifier notifier,
      SubscriptionKey key,
      int expires,
      String content,
      String contentType,
      String contentSubtype,
      boolean eventList,
      SubscriptionControlDataSource dataSource,
      ImplementedSubscriptionControlSbbLocalObject childSbb) {

    // ask authorization
    if (key.getEventPackage().endsWith(".winfo")) {
      // winfo package, only accept subscriptions when subscriber and
      // notifier are the same
      newSipSubscriptionAuthorization(
          event.getServerTransaction(),
          aci,
          subscriber,
          subscriberDisplayName,
          notifier,
          key,
          expires,
          (subscriber.equals(notifier.getUri()) ? Response.OK : Response.FORBIDDEN),
          eventList,
          dataSource,
          childSbb);
    } else {
      if (notifier.isPresList() && subscriber.equals(notifier.getUri())) {
        // no need to auth a subscription to subscriber's own pres list
        newSipSubscriptionAuthorization(
            event.getServerTransaction(),
            aci,
            subscriber,
            subscriberDisplayName,
            notifier,
            key,
            expires,
            Response.OK,
            eventList,
            dataSource,
            childSbb);
      } else {
        childSbb.isSubscriberAuthorized(
            subscriber,
            subscriberDisplayName,
            notifier,
            key,
            expires,
            content,
            contentType,
            contentSubtype,
            eventList,
            event.getServerTransaction());
      }
    }
  }