/**
   * Write credential into a document. as is a mime media-type specification and provides the form
   * of the document which is being requested. Two standard document forms are defined. "text/text"
   * encodes the document in a form nice for printing out and "text/xml" which provides an XML
   * format.
   *
   * @param as The mime media type of the encoding format being requested.
   * @return the StructuredDocument which represents this credential.
   * @throws Exception When errors occur.
   */
  public StructuredDocument getDocument(MimeMediaType as) throws Exception {

    StructuredDocument doc = StructuredDocumentFactory.newStructuredDocument(as, "jxta:Cred");

    if (doc instanceof Attributable) {
      ((Attributable) doc).addAttribute("xmlns:jxta", "http://jxta.org");
      ((Attributable) doc).addAttribute("xml:space", "preserve");
      ((Attributable) doc).addAttribute("type", "AuthenticationCredential");
    }

    Element e = doc.createElement("Method", getMethod());

    doc.appendChild(e);

    e = doc.createElement("PeerGroupID", getPeerGroupID().toString());
    doc.appendChild(e);

    e = doc.createElement("PeerID", getPeerID().toString());
    doc.appendChild(e);

    if (null != identityInfo) {
      e = doc.createElement("IdentityInfo");
      doc.appendChild(e);

      StructuredDocumentUtils.copyElements(doc, e, identityInfo);
    }

    return doc;
  }
  /**
   * Creates new AuthenticationCredential
   *
   * @param peergroup The peergroup context in which this AuthenticationCredential is created.
   * @param method The authentication method which will be requested when the
   *     AuthentiationCredential is provided to the peergroup Membership Service. Authentication
   *     methods are specific to Membership services. Consult the Documentation for the Membership
   *     service you will be authenticating against in order to determine the valid <tt>method</tt>
   *     values. Every Membership service should support a default authentication method which can
   *     be specified as <tt>null</null>.
   * @param indentityInfo Optional additional information which is used by the authentication method
   *     or <tt>null</tt>. This information is passed to the authentication method during the apply
   *     operation of the Membership Service. Consult the documentation for the specific Membership
   *     Service you are using for details on how this information is used (if at all).
   */
  public AuthenticationCredential(PeerGroup peergroup, String method, Element indentityInfo) {
    this.peergroup = peergroup;

    authenticationMethod = method;

    if (null != indentityInfo) {
      this.identityInfo = StructuredDocumentUtils.copyAsDocument(indentityInfo);
    }
  }
  private void outputModules(
      StructuredTextDocument doc, Hashtable modulesTable, String mainTag, MimeMediaType encodeAs) {

    Enumeration allClasses = modulesTable.keys();
    while (allClasses.hasMoreElements()) {
      ModuleClassID mcid = (ModuleClassID) allClasses.nextElement();
      Object val = modulesTable.get(mcid);

      // For applications, we ignore the role ID. It is not meaningfull,
      // and a new one is assigned on the fly when loading this adv.

      if (val instanceof Advertisement) {
        TextElement m = doc.createElement(mainTag);
        doc.appendChild(m);

        if (!(modulesTable == appsTable || mcid.equals(mcid.getBaseClass()))) {
          // It is not an app and there is a role ID. Output it.

          TextElement i = doc.createElement(mcidTag, mcid.toString());
          m.appendChild(i);
        }

        StructuredTextDocument advdoc =
            (StructuredTextDocument) ((Advertisement) val).getDocument(encodeAs);

        StructuredDocumentUtils.copyElements(doc, m, advdoc);

      } else if (val instanceof ModuleSpecID) {
        TextElement m;

        if (modulesTable == appsTable || mcid.equals(mcid.getBaseClass())) {

          // Either it is an app or there is no role ID.
          // So the specId is good enough.
          m = doc.createElement(mainTag, ((ModuleSpecID) val).toString());
          doc.appendChild(m);
        } else {
          // The role ID matters, so the classId must be separate.
          m = doc.createElement(mainTag);
          doc.appendChild(m);

          TextElement i;
          i = doc.createElement(mcidTag, mcid.toString());
          m.appendChild(i);

          i = doc.createElement(msidTag, ((ModuleSpecID) val).toString());
          m.appendChild(i);
        }
      } else {
        if (LOG.isEnabledFor(Level.WARN)) LOG.warn("unsupported class in modules table");
      }
    }
  }
  /**
   * Process an individual element from the document.
   *
   * @param elem the element to be processed.
   * @return true if the element was recognized, otherwise false.
   */
  protected boolean handleElement(TextElement elem) {
    if (elem.getName().equals("PeerGroupID")) {
      try {
        URI gID = new URI(elem.getTextValue());
        ID pgid = IDFactory.fromURI(gID);

        if (!pgid.equals(getPeerGroupID())) {
          throw new IllegalArgumentException(
              "Operation is from a different group. " + pgid + " != " + getPeerGroupID());
        }
      } catch (URISyntaxException badID) {
        throw new IllegalArgumentException("Unusable ID in advertisement: " + elem.getTextValue());
      }
      return true;
    }

    if (elem.getName().equals("PeerID")) {
      try {
        URI pID = new URI(elem.getTextValue());
        ID pid = IDFactory.fromURI(pID);

        if (!pid.equals(getPeerID())) {
          throw new IllegalArgumentException(
              "Operation is from a different group. " + pid + " != " + getPeerID());
        }
      } catch (URISyntaxException badID) {
        throw new IllegalArgumentException("Bad Peer ID in advertisement: " + elem.getTextValue());
      } catch (ClassCastException badID) {
        throw new IllegalArgumentException("Id is not a peer id: " + elem.getTextValue());
      }
      return true;
    }

    if (elem.getName().equals("Method")) {
      setMethod(elem.getTextValue());
      return true;
    }

    if (elem.getName().equals("IdentityInfo")) {
      Enumeration firstChild = elem.getChildren();

      if (!firstChild.hasMoreElements()) {
        throw new IllegalArgumentException("Missing identity info");
      }

      identityInfo = StructuredDocumentUtils.copyAsDocument((Element) firstChild.nextElement());

      return true;
    }

    // element was not handled
    return false;
  }
 /**
  * Returns the StructuredDocument Element containing the identity information which was originally
  * provided when this AuthenticationCredential was created.
  *
  * @return StructuredDocument Element containing the identity information which was originally
  *     provided when this AuthenticationCredential was created.
  */
 public Element getIdentityInfo() {
   return (null == identityInfo) ? null : StructuredDocumentUtils.copyAsDocument(identityInfo);
 }
Beispiel #6
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;
  }