001package co.codewizards.cloudstore.rest.server.service;
002
003import static co.codewizards.cloudstore.core.util.Util.*;
004
005import java.util.concurrent.Callable;
006
007import javax.ws.rs.Consumes;
008import javax.ws.rs.GET;
009import javax.ws.rs.Path;
010import javax.ws.rs.PathParam;
011import javax.ws.rs.Produces;
012import javax.ws.rs.core.MediaType;
013
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017import co.codewizards.cloudstore.core.concurrent.CallableProvider;
018import co.codewizards.cloudstore.core.concurrent.DeferrableExecutor;
019import co.codewizards.cloudstore.core.dto.RepoFileDTO;
020//import co.codewizards.cloudstore.core.repo.local.LocalRepoRegistry;
021import co.codewizards.cloudstore.core.repo.transport.RepoTransport;
022
023@Path("_RepoFileDTO/{repositoryName}")
024@Consumes(MediaType.APPLICATION_XML)
025@Produces(MediaType.APPLICATION_XML)
026public class RepoFileDTOService extends AbstractServiceWithRepoToRepoAuth
027{
028        private static final Logger logger = LoggerFactory.getLogger(RepoFileDTOService.class);
029
030        {
031                logger.debug("<init>: created new instance");
032        }
033
034        @GET
035        public RepoFileDTO getRepoFileDTO()
036        {
037                return getRepoFileDTO("");
038        }
039
040        @GET
041        @Path("{path:.*}")
042        public RepoFileDTO getRepoFileDTO(final @PathParam("path") String path)
043        {
044                assertNotNull("path", path);
045                final RepoTransport[] repoTransport = new RepoTransport[] { authenticateAndCreateLocalRepoTransport() };
046                try {
047                        String callIdentifier = RepoFileDTOService.class.getName() + ".getRepoFileDTO|" + repositoryName + '|' + getAuth().getUserName() + '|' + path;
048                        return DeferrableExecutor.getInstance().call(
049                                        callIdentifier,
050                                        new CallableProvider<RepoFileDTO>() {
051                                                @Override
052                                                public Callable<RepoFileDTO> getCallable() { // called synchronously during DeferrableExecutor.call(...) - if called at all
053                                                        final RepoTransport rt = repoTransport[0];
054                                                        repoTransport[0] = null;
055                                                        final String unprefixedPath = rt.unprefixPath(path);
056                                                        return new Callable<RepoFileDTO>() {
057                                                                @Override
058                                                                public RepoFileDTO call() throws Exception { // called *A*synchronously
059                                                                        try {
060                                                                                RepoFileDTO repoFileDTO = rt.getRepoFileDTO(unprefixedPath);
061                                                                                return repoFileDTO;
062                                                                        } finally {
063                                                                                rt.close();
064                                                                        }
065                                                                }
066                                                        };
067                                                }
068                                        });
069                } finally {
070                        if (repoTransport[0] != null)
071                                repoTransport[0].close();
072                }
073        }
074}