📄 visitorcredential.java
字号:
/* * VisitorCredential.java * * Created on September 24, 2001, 8:05 PM */package com.sams.jxta.groups;import net.jxta.peergroup.*;import net.jxta.protocol.*;import net.jxta.service.*;import net.jxta.exception.*;import net.jxta.membership.*;import net.jxta.peer.*;import net.jxta.document.*;import net.jxta.credential.*;import java.util.*;import java.net.URL;import net.jxta.id.IDFactory;import net.jxta.id.ID;/** * Credentials are sent with every message to ensure that the * message sender is a valid member of the group. This means we * have to be careful about how much information is in the credential. * We also have to be careful that we don't take too much time * verifying the credential each time a message is received. * * This XML message is outbound and intended fo consumers of messages from this * peer. */public class VisitorCredential implements Credential { protected VisitorMembership source; protected ID peerID; protected PeerGroup peerGroup; protected ID peerGroupID; // Specific Information protected boolean debugMode; protected String visitorName; protected VisitorCredential( VisitorMembership source) { System.out.println(this.getClass().getName()+" **** VisitorCredential()"); this.source = (VisitorMembership) source; peerID = source.getPeerGroup().getPeerID(); peerGroup = source.getPeerGroup(); } protected VisitorCredential( VisitorMembership source, VisitorAuthenticator visitor ) { System.out.println(this.getClass().getName()+" **** VisitorCredential()"); this.source = (VisitorMembership) source; peerID = source.getPeerGroup().getPeerID(); peerGroup = source.getPeerGroup(); // Specific information debugMode = visitor.getDebugMode(); visitorName = visitor.getVisitorName(); } /** * Welcome to the uglyness of JXTA! * * Credentials are created by an Authenticator. This means that * the Authenticator will probably own or create the credentials * based on either a self discovery, user input, or both. Given * that expectation, we pass in our authenticator object to be * digested. */ protected VisitorCredential( VisitorMembership source, PeerGroupID peergroupID, PeerID peerID, VisitorAuthenticator visitor ) throws PeerGroupException { System.out.println(this.getClass().getName()+" **** VisitorCredential()"); peerGroup = source.getPeerGroup(); this.source = (VisitorMembership) source; // Verify that the source group and ID match. if( !source.getPeerGroup().getPeerGroupID().equals( peerGroupID ) ){ throw new PeerGroupException( "Cannot credential for a different peer group." ); } this.peerID = peerID; // Specific information debugMode = visitor.getDebugMode(); visitorName = visitor.getVisitorName(); } /** * Returns the service which generated this credential. */ public net.jxta.membership.MembershipService getSourceService() { return source; } /** * Returns the peer group ID */ public ID getPeerGroupID() { return source.getPeerGroup().getPeerGroupID(); } /** * Returns the peer ID. This is the peer that was * authenticated. Messages with this ID came * from the peer that created this credential. */ public ID getPeerID() { return peerID; } /** * Returns the document representation of this credential. * The document can be saved as local proof of the credential. */ public StructuredDocument getDocument(MimeMediaType as) throws Exception { StructuredDocument doc = StructuredDocumentFactory.newStructuredDocument( as, "VisitorCredential" ); peerGroup.getPeerGroupID(); Element e = doc.createElement( "PeerGroupID", peerGroup.getPeerGroupID().toString() ); doc.appendChild( e ); e = doc.createElement( "PeerID", peerID.toString() ); doc.appendChild( e ); /** * The following are items that we asked the visitor to provide. * Limit this information to avoid cloging the network. */ e = doc.createElement( "VisitorName", visitorName); doc.appendChild( e ); e = doc.createElement( "DebugMode", ""+debugMode); doc.appendChild( e ); return doc; } /** * This constructs a credential from an XML element. */ protected VisitorCredential(VisitorMembership source, Element element) throws PeerGroupException, Exception { System.out.println(this.getClass().getName()+" **** VisitorCredential()"); this.source = source; this.peerGroup = source.getPeerGroup(); if( !"VisitorCredential".equals(element.getKey()) ){ throw new PeerGroupException( "Element does not contain a recognized credential format" ); } Enumeration children = element.getChildren( "PeerGroupID" ); if( !children.hasMoreElements() ){ throw new RuntimeException( "Missing PeerGroupID Element" ); } peerGroupID = (PeerGroupID) IDFactory.fromURL( new URL( (String) ((Element) children.nextElement()).getValue() ) ); //if( children.hasMoreElements() ){ // throw new RuntimeException( "Extra PeerGroupID Elements" ); //} children = element.getChildren( "PeerID" ); if( !children.hasMoreElements() ){ throw new RuntimeException( "Missing PeerID Element" ); } peerID = (PeerID) IDFactory.fromURL( new URL( (String) ((Element) children.nextElement()).getValue() ) ); // Verify that the source group and ID match. if( !source.getPeerGroup().getPeerGroupID().equals( peerGroupID ) ){ throw new PeerGroupException( "Cannot credential for a different peer group." ); } //if( children.hasMoreElements() ){ ??? Don't know why this is here // throw new RuntimeException( "Extra PeerID Elements" ); //} //children = element.getChildren( "PeerID" ); //if( !children.hasMoreElements() ){ // throw new RuntimeException( "Missing PeerID Element" ); //} //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-// // The following are items that we asked the visitor to provide // //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-// children = element.getChildren( "VistorName" ); String visitor = (String) ((Element) children.nextElement()).getValue(); if( children.hasMoreElements() ){ throw new RuntimeException( "VistorName Element Not Found" ); } children = element.getChildren( "DebugMode" ); debugMode = Boolean.getBoolean( (String) ((Element) children.nextElement()).getValue() ); if( children.hasMoreElements() ){ throw new RuntimeException( "DebugMode Element Not Found" ); } } /* From here on are the methods specific to this credential type */ public String getIdentity() { return visitorName; } }// end of VisitorCredentials
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -