Example #1
0
  /**
   * {@inheritDoc}
   *
   * @param encodeAs Description of the Parameter
   * @return The document value
   */
  public Document getDocument(MimeMediaType encodeAs) {
    StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);

    if (hasALoop()) {
      throw new IllegalStateException("I won't write a doc for a route with a loop");
    }

    PeerID pid = getDestPeerID();
    AccessPointAdvertisement dest = getDest();

    if ((null != pid) && (null != dest) && (null != dest.getPeerID())) {
      if (!pid.equals(dest.getPeerID())) {
        throw new IllegalStateException(
            "Destination peer id and destination access point adv don't refer to the same peer");
      }
    }

    // HACK Backwards Compatibility
    if ((null == pid) && (null != dest)) {
      pid = dest.getPeerID();
    }

    if (pid != null) {
      Element e0 = adv.createElement(DEST_PID_TAG, pid.toString());

      adv.appendChild(e0);
    }

    if (dest != null) {
      // create the copy without the PID
      AccessPointAdvertisement ap =
          (AccessPointAdvertisement)
              AdvertisementFactory.newAdvertisement(
                  AccessPointAdvertisement.getAdvertisementType());

      ap.setEndpointAddresses(dest.getVectorEndpointAddresses());

      StructuredTextDocument xptDoc = (StructuredTextDocument) ap.getDocument(encodeAs);

      Element e1 = adv.createElement("Dst");

      adv.appendChild(e1);
      StructuredDocumentUtils.copyElements(adv, e1, xptDoc);
    }

    // only include hops if we have some
    if (getHops().hasMoreElements()) {

      Element e2 = adv.createElement("Hops");

      adv.appendChild(e2);

      for (Enumeration e = getHops(); e.hasMoreElements(); ) {
        AccessPointAdvertisement hop = (AccessPointAdvertisement) e.nextElement();

        if (hop != null) {
          StructuredTextDocument xptDoc = (StructuredTextDocument) hop.getDocument(encodeAs);

          StructuredDocumentUtils.copyElements(adv, e2, xptDoc);
        }
      }
    }

    return adv;
  }