Then the Authenticator method are filtered and sorted and placed into a Vector, keeping only the ones thatare relevant to the authentication process (starting with “setAuth”) And goes through all the Authentication method place into looking for “setAuth1Identity” and”setAuth2_Password” and invokes them with the appropriate parameters: Object [] AuthId = {login}; Object [] AuthPasswd = {passwd}; for( int eachAuthMethod=0;eachAuthMethod
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services
Source Code: SecurePeerGroup import java.io.StringWriter; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.net.URL; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import net.jxta.credential.AuthenticationCredential; import net.jxta.discovery.DiscoveryService; import net.jxta.document.Advertisement; import net.jxta.document.AdvertisementFactory; import net.jxta.document.Element; import net.jxta.document.MimeMediaType; import net.jxta.document.StructuredDocument; import net.jxta.document.StructuredDocumentFactory; import net.jxta.document.StructuredTextDocument; import net.jxta.document.TextElement; import net.jxta.endpoint.*; import net.jxta.exception.PeerGroupException; import net.jxta.id.ID; import net.jxta.id.IDFactory; import net.jxta.impl.membership.PasswdMembershipService; import net.jxta.impl.protocol.*; import net.jxta.membership.Authenticator; import net.jxta.membership.MembershipService; import net.jxta.peergroup.PeerGroup; import net.jxta.peergroup.PeerGroupFactory; import net.jxta.peergroup.PeerGroupID; import net.jxta.platform.ModuleSpecID; import net.jxta.protocol.ModuleImplAdvertisement; import net.jxta.protocol.PeerGroupAdvertisement; import net.jxta.impl.peergroup.StdPeerGroupParamAdv ; public class SecurePeerGroup { private PeerGroup myNetPeerGroup=null, satellaPeerGroup=null,discoveredSatellaPeerGroup=null; private static PeerGroupID satellaPeerGroupID; private final static String GROUPID = “jxta:uuid 4d6172676572696e204272756e6f202002″; /** Creates new RootWS */ public SecurePeerGroup() { // Starts the JXTA Platform myNetPeerGroup=this.startJxta(); 127 JXTA v2.3.x: Java Programmer s Guide
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services
AdvertisementFactory.newAdvertisement( PeerGroupAdvertisement.getAdvertisementType()); And initializes the specifics of this instance of our authenticated peer group. That is: Its peer group ID. In this example, the peer group ID is fixed, so that each time the platform is startedthe same peer group is created : satellaPeerGroupAdv.setPeerGroupID(satellaPeerGroupID); Its Module Spec ID advertisement from which the peer group will find which peer groupimplementation to use. In this example, this implementation is the Password Membership ModuleImplementation satellaPeerGroupAdv.setModuleSpecID( passwdMembershipModuleImplAdv.getModuleSpecID()); Its name and description : satellaPeerGroupAdv.setName(groupName); satellaPeerGroupAdv.setDescription( “Peer Group using Password Authentication”); User and password information is structured as a “login” XML Element and is included into the XMLdocument describing the Service Parameters of the Peer group. Line shows the creation of this Service Parameters XML document: StructuredTextDocument loginAndPasswd= (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument(new MimeMediaType(”text/xml”),”Parm”); Whereas lines -show the creation of the “login” XML Element: String loginAndPasswdString = login + “:” + PasswdMembershipService.makePsswd(passwd) + “:”; TextElement loginElement = loginAndPasswd.createElement(”login”,loginAndPasswdString); discoverPeerGroup() This method extracts the discovery service from the parent group (netpeergroup, in our example) : myNetPeerGroupDiscoveryService = myNetPeerGroup.getDiscoveryService(); And uses this service to look for the newly created peer group (”SatellaGroup”) advertisement in thelocal cache. The search is conducted by looking for peer group advertisements whose peer group IDmatches the “SatellaGroup” one. The method loops until it finds it. Since we published the peer group Advertisement locally we knowit is there, and therefore there is no need to remote query the P2P network : localPeerGroupAdvertisementEnumeration= myNetPeerGroupDiscoveryService.getLocalAdvertisements( DiscoveryService.GROUP,”GID”,satellaPeerGroupID.toString()); Once the correct peer group advertisement is found, the corresponding peer group is created using theparent group (here, netPeerGroup) newgroup() method : satellaPeerGroup=myNetPeerGroup.newGroup(satellaPeerGroupAdv); joinPeerGroup() This method is very similar to the joinGroup() method described earlier (see on page 111 ). It uses thesame “apply” and “join” steps. But, unlike the nullAuthenticationService where there is no authenticationto complete, the PasswdAuthenticationService requires some authentication. It essentially resides inproviding a user login and a password : completeAuth(auth, login, passwd); completeAuth() This method performs the authentication completion required before being able to join the peer group. Inorders to complete the authentication, the authentication methods needs to be extracted from theAuthenticator. These method s name starts with “setAuth”. Specifically the “setAuth1Identity” method needto be provided with the correct login and “setAuth2_Password” with the correct password. The methods are extracted from the Authenticator : Method [] methods = auth.getClass().getMethods(); 125 JXTA v2.3.x: Java Programmer s Guide
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services
return satellaPeerGroup; createPasswdMembershipPeerGroupModuleImplAdv () This method creates the module implementation advertisement for the peer group. It relies on a secondmethod createPasswdMembershipServiceModuleImplAdv () for creating the module implementationadvertisement for the membership service. This method relies on generic, standard “allPurpose” Advertisements that it modifies to take into accountthe new membership implementation. (Appendix E contains a typical All Purpose Peer Group ModuleImplementation Advertisement for your reference.) You can see that the “Param” Element contains all the peer group services including the MembershipService (see ). Therefore, most of the work will be performed of this piece of the document. The following tasks are performed: Create a standard generic peer group module implementation advertisement : allPurposePeerGroupImplAdv= rootPeerGroup.getAllPurposePeerGroupImplAdvertisement(); Extract its “Param” element. As mentioned above, this contains the services provided by the peer group : passwdMembershipPeerGroupParamAdv = new StdPeerGroupParamAdv(allPurposePeerGroupImplAdv.getParam()); From this “Param” element, extract the peer group services and their associated service IDs : Hashtable allPurposePeerGroupServicesHashtable= passwdMembershipPeerGroupParamAdv.getServices(); Enumeration allPurposePeerGroupServicesEnumeration= allPurposePeerGroupServicesHashtable.keys(); Loop through all this services looking for the Membership Services. The search is performed bylooking for the ID matching the MembershipService ID : if (allPurposePeerGroupServiceID.equals(PeerGroup.membershipClassID)) Once found, extract the generic membership service advertisement : ModuleImplAdvertisement allPurposePeerGroupMemershipServiceModuleImplAdv= (ModuleImplAdvertisement) allPurposePeerGroupServicesHashtable.get (allPurposePeerGroupServiceID); Use this generic advertisement to generate a custom one for the Password Membership Service usingthe createPasswdMembershipServiceModuleImplAdv() method : passwdMembershipServiceModuleImplAdv= this.createPasswdMembershipServiceModuleImplAdv (allPurposePeerGroupMemershipServiceModuleImplAdv); Remove the generic Membership advertisement : allPurposePeerGroupServicesHashtable.remove(allPurposePeerGroupServiceID); And replace it by the new one : allPurposePeerGroupServicesHashtable.put(PeerGroup.membershipClassID,passwdMembershipServiceModuleImplAdv); Finally replace the “Param” element that has just been updated with the newPasswdMembershipService in the peer group module implementation : passwdMembershipPeerGroupModuleImplAdv.setParam( (Element)PasswdMembershipPeerGroupParamAdv.getDocument(new MimeMediaType(”text/xml”))); And Update the Password Membership peer group module implementation advertisement spec ID . Since the new Peer group module implementation advertisement is no longer the “AllPurpose” one, itshould therefore not refer to the “allPurpose” peer group spec advertisement: passwdGrpModSpecID = IDFactory.fromURL(new URL(”urn”,”", “jxta:uuid-”+”DeadBeefDeafBabaFeedBabe00000001″ +”04″ +”06″ ) ); passwdMembershipPeerGroupModuleImplAdv.setModuleSpecID( (ModuleSpecID) passwdGrpModSpecID); 123 JXTA v2.3.x: Java Programmer s Guide
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services
CreatePasswdMembershipServiceModuleImplAdv() This method works like the previous one: it takes a generic advertisement and uses it to create acustomized one. lists the generic advertisement that is receives as argument by this method. 0XML representation of a typical MembershipService, extracted from the Parm element of a peer groupmodule implementation advertisement. urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE000000050106 JDK1.4 V1.0 Ref Impl net.jxta.impl.membership.NullMembershipService http://www.jxta.org/download/jxta.jar sun.com Reference Implementation of the MembershipServiceservice This method needs only to update the Module Spec ID, the code, and description with values specific tothe PasswdMembershipService : passwdMembershipServiceModuleImplAdv.setModuleSpecID( PasswdMembershipService.passwordMembershipSpecID); passwdMembershipServiceModuleImplAdv.setCode( PasswdMembershipService.class.getName()); passwdMembershipServiceModuleImplAdv.setDescription( “Module Impl Advertisement for the PasswdMembership Service”); The rest of the PasswdMembershipServiceAdvertisement is just plain copies of the generic one : passwdMembershipServiceModuleImplAdv.setCompat( allPurposePeerGroupMemershipServiceModuleImplAdv.getCompat()); passwdMembershipServiceModuleImplAdv.setUri( allPurposePeerGroupMemershipServiceModuleImplAdv.getUri()); passwdMembershipServiceModuleImplAdv.setProvider( allPurposePeerGroupMemershipServiceModuleImplAdv.getProvider()); createPeerGroupAdvertisement() This methods creates peer group advertisement from scratch using the advertisement factory : PeerGroupAdvertisement satellaPeerGroupAdv= (PeerGroupAdvertisement) 124 JXTA v2.3.x: Java Programmer s Guide
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services
Creating a Secure Peer Group This example11 illustrates how to create and join a new peer group that implements authentication via alogin and a password. shows example output when this application is run: 0Example output: Creating and joining a peer group that requires authentication. JXTA platform Started … Peer Group Created … Peer Group Found … Peer Group Joined … | XML Advertisement for Peer Group Advertisement | urn:jxta:uuid 4D6172676572696E204272756E6F202002 urn:jxta:uuid DEADBEEFDEAFBABAFEEDBABE000000010406 SatellaGroup Peer Group using Password Authentication urn:jxta:uuid DEADBEEFDEAFBABAFEEDBABE0000000505 SecurePeerGroups:FZUH: Note The password encryption used in net.jxta.impl.membership.PasswdMembershipService isextremely weak and has been cracked over 2 millenniums ago. So, this method is highly unsecure. Butthe principle for joining a group with better password encryption method remains the same. You can also join the authenticated peer group with other JXTAapplications, such as the JXTA shell, using the following user and password: Login: SecurePeerGroups 11 This example was provided by Bruno Margerin of Science System & Applications, Inc. Portions of the code were taken from the Instant P2P and JXTA Shell projects. 121 JXTA v2.3.x: Java Programmer s Guide
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services
Password: RULE main() This method call the constructor SecurePeerGroup() of the class and instantiates a new SecurePeerGroupObject called satellaRoot. The constructor method SecurePeerGroup() This method creates and joins the secure peer group, and then prints the peer group s advertisement. More specifically, this method: Instantiates the JXTAplatform and creates the default netPeerGroup by calling thestartJxta() method Instantiates the user login, password, group name and group ID Creates the authenticated peer group called “SatellaGroup” by calling the createPeerGroup() method Searches for the “SatellaGroup” peer group by calling the discoverPeerGroup() method Joins the “SatellaGroup” peer group by calling the joinPeerGroup() method Prints on standard output the XML Advertisement of the “SatellaGroup” peer group bycalling the printXmlAdvertisement() method StartJxta() This method instantiates the JXTA platform, creates (and later returns) the default netPeerGroup calledmyNetPeeGroup : myNetPeerGroup=PeerGroupFactory.newNetPeerGroup(); createPeerGroup() The peer group that is being built does not have the same characteristics than the standard peer group. Indeed, it has a different membership implementation: it uses thenet.jxta.impl.membership.PasswdMembershipService instead of the regularnet.jxta.impl.membership.NullMembershipService. Therefore, it is required to create and publish a newPeer Group Module Implementation that reflects this new implementation of the Membership Service : passwdMembershipModuleImplAdv= this.createPasswdMembershipPeerGroupModuleImplAdv(rootPeerGroup); Once created, this advertisement is published locally and remotely in the parent group using the parentpeer group s Discovery Service : rootPeerGroupDiscoveryService.publish(passwdMembershipModuleImplAdvPeerGroup.DEFAULT_LIFETIME, PeerGroup.DEFAULT_EXPIRATION); rootPeerGroupDiscoveryService.remotePublish(passwdMembershipModuleImplAdv, PeerGroup.DEFAULT_EXPIRATION); Once this Peer Group Module Implementation in created and published, the createPeerGroup() methodbinds the new Module Implementation advertisement, peer group name, login and password together intothe actual Peer Group advertisement by calling the createPeerGroupAdvertisement() method,: satellaPeerGroupAdv = this.createPeerGroupAdvertisement(passwdMembershipModuleImplAdv, groupName,login,passwd); And publishes it locally and remotely in the parent group using the parent peer group s DiscoveryService : rootPeerGroupDiscoveryService.publish(satellaPeerGroupAdv, PeerGroup.DEFAULT_LIFETIME, PeerGroup.DEFAULT_EXPIRATION); rootPeerGroupDiscoveryService.remotePublish(satellaPeerGroupAdv, PeerGroup.DEFAULT_EXPIRATION); Finally the peer group is created from the peer group advertisement : satellaPeerGroup=rootPeerGroup.newGroup(satellaPeerGroupAdv); And returned : 122 JXTA v2.3.x: Java Programmer s Guide
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services
// in the advertisement. PipeAdvertisement pipeadv = mdsadv.getPipeAdvertisement(); // Ok we have our pipe advertiseemnt to talk to the service // create the output pipe endpoint to connect // to the server, try 3 times to bind the pipe endpoint to // the listening endpoint pipe of the service for (int i=0; i<3; i++) { myPipe = pipes.createOutputPipe(pipeadv, 10000); } // create the data string to send to the server String data = "Hello my friend!"; // create the pipe message msg = new Message(); StringMessageElement sme = new StringMessageElement( "DataTag", data , null); msg.addMessageElement(null, sme); // send the message to the service pipe myPipe.send (msg); System.out.println("message "" + data + "" sent to the Server"); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Client: Error sending message to the service"); } } } 120 JXTA v2.3.x: Java Programmer s Guide
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services
en = discovery.getLocalAdvertisements (DiscoveryService.ADV, “Name”, “JXTASPEC:JXTA-EX1″); // Ok we got something in our local cache does not // need to go further! if ((en != null) && en.hasMoreElements()) { break; } // nothing in the local cache?, let’s remotely query // for the service advertisement. discovery.getRemoteAdvertisements(null, DiscoveryService.ADV, “Name”, “JXTASPEC:JXTA-EX1″, 1, null); // The discovery is asynchronous as we do not know // how long is going to take try { // sleep as much as we want. Yes we // should implement asynchronous listener pipe… Thread.sleep(2000); } catch (Exception e) {} } catch (IOException e) { // found nothing! move on } System.out.print(”.”); } System.out.println(”we found the service advertisement”); // Ok get the service advertisement as a Spec Advertisement ModuleSpecAdvertisement mdsadv = (ModuleSpecAdvertisement) en.nextElement(); try { // let’s print the advertisement as a plain text document StructuredTextDocument doc = (StructuredTextDocument) mdsadv.getDocument (MimeMediaType.TEXT_DEFAULTENCODING); StringWriter out = new StringWriter(); doc.sendToWriter(out); System.out.println(out.toString()); out.close(); // we can find the pipe to connect to the service 119 JXTAv2.3.x: Java Programmer s Guide
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services
Source Code: Client 1. import java.io.IOException; import java.io.StringWriter; import java.net.MalformedURLException; import java.net.URL; import java.util.Enumeration; import net.jxta.discovery.DiscoveryService; import net.jxta.document.AdvertisementFactory; import net.jxta.document.MimeMediaType; import net.jxta.document.StructuredTextDocument; import net.jxta.document.TextElement; import net.jxta.endpoint.Message; import net.jxta.endpoint.StringMessageElement; import net.jxta.exception.PeerGroupException; import net.jxta.id.IDFactory; import net.jxta.peergroup.PeerGroup; import net.jxta.peergroup.PeerGroupFactory; import net.jxta.pipe.OutputPipe; import net.jxta.pipe.PipeID; import net.jxta.pipe.PipeService; import net.jxta.protocol.ModuleSpecAdvertisement; import net.jxta.protocol.PeerGroupAdvertisement; import net.jxta.protocol.PipeAdvertisement; /** * Client Side: This is the client side of the JXTA-EX1 * application. The client application is a simple example on how to * start a client, connect to a JXTA enabled service, and invoke the * service via a pipe advertised by the service. The * client searches for the module specification advertisement * associated with the service, extracts the pipe information to * connect to the service, creates a new output to connect to the * service and sends a message to the service. * The client just sends a string to the service no response * is expected from the service. */ public class Client { static PeerGroup netPeerGroup = null; static PeerGroupAdvertisement groupAdvertisement = null; private DiscoveryService discovery; private PipeService pipes; private OutputPipe myPipe; // Output pipe to connect the service 117 JXTAv2.3.x: Java Programmer s Guide
Note: If you are looking for good and high quality web space to host and run your java application check Lunarwebhost java web hosting services