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 LocalRepoManager localRepoManager = LocalRepoManagerFactory.Helper.getInstance().createLocalRepoManagerForExistingRepository(localRoot); 045 try { 046 localRepositoryId = localRepoManager.getRepositoryId(); 047 localPublicKey = localRepoManager.getPublicKey(); 048 RepoTransport repoTransport = RepoTransportFactoryRegistry.getInstance().getRepoTransportFactory(remoteURL).createRepoTransport(remoteURL, localRepositoryId); 049 try { 050 remoteRepositoryId = repoTransport.getRepositoryId(); 051 remotePublicKey = repoTransport.getPublicKey(); 052// String remotePathPrefix = repoTransport.getPathPrefix(); 053 localRepoManager.putRemoteRepository(remoteRepositoryId, remoteURL, remotePublicKey, localPathPrefix); 054 repoTransport.requestRepoConnection(localRepoManager.getPublicKey()); 055 } finally { 056 repoTransport.close(); 057 } 058 } finally { 059 localRepoManager.close(); 060 } 061 062 System.out.println("Successfully requested to connect the following local and remote repositories:"); 063 System.out.println(); 064 System.out.println(" localRepository.repositoryId = " + localRepositoryId); 065 System.out.println(" localRepository.localRoot = " + localRoot); 066 System.out.println(" localRepository.publicKeySha1 = " + HashUtil.sha1ForHuman(localPublicKey)); 067 System.out.println(); 068 System.out.println(" remoteRepository.repositoryId = " + remoteRepositoryId); 069 System.out.println(" remoteRepository.remoteRoot = " + remoteURL); 070 System.out.println(" remoteRepository.publicKeySha1 = " + HashUtil.sha1ForHuman(remotePublicKey)); 071 System.out.println(); 072 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:"); 073 System.out.println(); 074 System.out.println(String.format(" cloudstore dropRepoConnection %s %s", localRepositoryId, remoteRepositoryId)); 075 } 076}