001package co.codewizards.cloudstore.client; 002 003import java.net.URL; 004import java.util.UUID; 005 006import org.kohsuke.args4j.Argument; 007 008import co.codewizards.cloudstore.core.repo.local.LocalRepoManager; 009import co.codewizards.cloudstore.core.repo.local.LocalRepoManagerFactory; 010import co.codewizards.cloudstore.core.repo.transport.RepoTransport; 011import co.codewizards.cloudstore.core.repo.transport.RepoTransportFactoryRegistry; 012import co.codewizards.cloudstore.core.util.HashUtil; 013 014/** 015 * {@link SubCommand} implementation for requesting a connection at a remote repository. 016 * 017 * @author Marco หงุ่ยตระà¸à¸¹à¸¥-Schulze - marco at nightlabs dot de 018 */ 019public class RequestRepoConnectionSubCommand extends SubCommandWithExistingLocalRepo 020{ 021 @Argument(metaVar="<remote>", index=1, required=true, usage="A URL to a remote repository. This may be the remote repository's root or any sub-directory. If a sub-directory is specified here, only this sub-directory is connected with the local repository.") 022 private String remote; 023 024 private URL remoteURL; 025 026 @Override 027 public String getSubCommandDescription() { 028 return "Request a remote repository to allow a connection with a local repository."; 029 } 030 031 @Override 032 public void prepare() throws Exception { 033 super.prepare(); 034 035 remoteURL = new URL(remote); 036 } 037 038 @Override 039 public void run() throws Exception { 040 UUID localRepositoryId; 041 UUID remoteRepositoryId; 042 byte[] localPublicKey; 043 byte[] remotePublicKey; 044// String remotePathPrefix; 045 try ( 046 final LocalRepoManager localRepoManager = LocalRepoManagerFactory.Helper.getInstance().createLocalRepoManagerForExistingRepository(localRoot); 047 ) { 048 localRepositoryId = localRepoManager.getRepositoryId(); 049 localPublicKey = localRepoManager.getPublicKey(); 050 try ( 051 final RepoTransport repoTransport = RepoTransportFactoryRegistry.getInstance().getRepoTransportFactory(remoteURL).createRepoTransport(remoteURL, localRepositoryId); 052 ) { 053 remoteRepositoryId = repoTransport.getRepositoryId(); 054 remotePublicKey = repoTransport.getPublicKey(); 055// remotePathPrefix = repoTransport.getPathPrefix(); 056 localRepoManager.putRemoteRepository(remoteRepositoryId, remoteURL, remotePublicKey, localPathPrefix); 057 repoTransport.requestRepoConnection(localRepoManager.getPublicKey()); 058 } 059 } 060 061 System.out.println("Successfully requested to connect the following local and remote repositories:"); 062 System.out.println(); 063 System.out.println(" localRepository.repositoryId = " + localRepositoryId); 064 System.out.println(" localRepository.localRoot = " + localRoot); 065 System.out.println(" localRepository.publicKeySha1 = " + HashUtil.sha1ForHuman(localPublicKey)); 066 System.out.println(); 067 System.out.println(" remoteRepository.repositoryId = " + remoteRepositoryId); 068 System.out.println(" remoteRepository.remoteRoot = " + remoteURL); 069 System.out.println(" remoteRepository.publicKeySha1 = " + HashUtil.sha1ForHuman(remotePublicKey)); 070 System.out.println(); 071// System.out.println(" localPathPrefix = " + localPathPrefix); 072// System.out.println(" remotePathPrefix = " + remotePathPrefix); 073// System.out.println(); 074 System.out.println("Please verify the 'publicKeySha1' fingerprints! If they do not match the fingerprints shown on the server, someone is attacking you and you must cancel this request immediately! To cancel the request, use this command:"); 075 System.out.println(); 076 System.out.println(String.format(" cloudstore dropRepoConnection %s %s", localRepositoryId, remoteRepositoryId)); 077 } 078}