001package co.codewizards.cloudstore.rest.server.service;
002
003import static java.util.Objects.*;
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                requireNonNull(path, "path");
045                final RepoTransport[] repoTransport = new RepoTransport[] { authenticateAndCreateLocalRepoTransport() };
046                try {
047                        final 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                                                                                final 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}