001package co.codewizards.cloudstore.rest.server.service;
002
003import static co.codewizards.cloudstore.core.util.Util.*;
004
005import java.net.MalformedURLException;
006import java.net.URL;
007
008import javax.ws.rs.Consumes;
009import javax.ws.rs.POST;
010import javax.ws.rs.Path;
011import javax.ws.rs.PathParam;
012import javax.ws.rs.core.MediaType;
013
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017import co.codewizards.cloudstore.core.dto.RepositoryDTO;
018import co.codewizards.cloudstore.core.repo.local.LocalRepoRegistry;
019import co.codewizards.cloudstore.core.repo.transport.RepoTransport;
020import co.codewizards.cloudstore.core.repo.transport.RepoTransportFactory;
021import co.codewizards.cloudstore.core.repo.transport.RepoTransportFactoryRegistry;
022
023@Path("_requestRepoConnection/{repositoryName}")
024public class RequestRepoConnectionService
025{
026        private static final Logger logger = LoggerFactory.getLogger(RequestRepoConnectionService.class);
027
028        {
029                logger.debug("<init>: created new instance");
030        }
031
032        private @PathParam("repositoryName") String repositoryName;
033
034        @POST
035        @Consumes(MediaType.APPLICATION_XML)
036        public void requestConnection(RepositoryDTO clientRepositoryDTO)
037        {
038                requestConnection("", clientRepositoryDTO);
039        }
040
041        @POST
042        @Path("{pathPrefix:.*}")
043        @Consumes(MediaType.APPLICATION_XML)
044        public void requestConnection(@PathParam("pathPrefix") String pathPrefix, RepositoryDTO clientRepositoryDTO)
045        {
046                assertNotNull("pathPrefix", pathPrefix);
047                assertNotNull("repositoryDTO", clientRepositoryDTO);
048
049                URL localRootURL = LocalRepoRegistry.getInstance().getLocalRootURLForRepositoryNameOrFail(repositoryName);
050
051                if (!"".equals(pathPrefix)) {
052                        String localRootURLString = localRootURL.toExternalForm();
053                        if (localRootURLString.endsWith("/"))
054                                localRootURLString = localRootURLString.substring(0, localRootURLString.length() - 1);
055
056                        if (!pathPrefix.startsWith("/"))
057                                pathPrefix = '/' + pathPrefix;
058
059                        localRootURLString = localRootURLString + pathPrefix;
060                        try {
061                                localRootURL = new URL(localRootURLString);
062                        } catch (MalformedURLException e) {
063                                throw new RuntimeException(e);
064                        }
065                }
066
067                RepoTransportFactory repoTransportFactory = RepoTransportFactoryRegistry.getInstance().getRepoTransportFactory(localRootURL);
068                RepoTransport repoTransport = repoTransportFactory.createRepoTransport(localRootURL, clientRepositoryDTO.getRepositoryId());
069                try {
070                        repoTransport.requestRepoConnection(clientRepositoryDTO.getPublicKey());
071                } finally {
072                        repoTransport.close();
073                }
074        }
075}