001package co.codewizards.cloudstore.rest.server.service;
002
003import static java.util.Objects.*;
004
005import javax.ws.rs.Consumes;
006import javax.ws.rs.DELETE;
007import javax.ws.rs.DefaultValue;
008import javax.ws.rs.GET;
009import javax.ws.rs.HeaderParam;
010import javax.ws.rs.PUT;
011import javax.ws.rs.Path;
012import javax.ws.rs.PathParam;
013import javax.ws.rs.Produces;
014import javax.ws.rs.QueryParam;
015import javax.ws.rs.core.MediaType;
016
017import org.slf4j.Logger;
018import org.slf4j.LoggerFactory;
019
020import co.codewizards.cloudstore.core.dto.DateTime;
021import co.codewizards.cloudstore.core.dto.RepoFileDto;
022import co.codewizards.cloudstore.core.repo.transport.RepoTransport;
023import co.codewizards.cloudstore.rest.server.webdav.COPY;
024import co.codewizards.cloudstore.rest.server.webdav.MKCOL;
025import co.codewizards.cloudstore.rest.server.webdav.MOVE;
026import co.codewizards.cloudstore.rest.server.webdav.PROPFIND;
027
028// TODO We should implement WebDAV: http://tools.ietf.org/html/rfc2518 + http://en.wikipedia.org/wiki/WebDAV
029// TODO We should *additionally* provide browsing via HTML replies (=> @Produces(MediaType.HTML))
030@Path("{repositoryName:[^_/][^/]*}")
031public class WebDavService extends AbstractServiceWithRepoToRepoAuth {
032        private static final Logger logger = LoggerFactory.getLogger(WebDavService.class);
033
034        {
035                logger.debug("<init>: created new instance");
036        }
037
038//      @GET
039//      @Produces(MediaType.WILDCARD)
040//      public Object getContents() {
041//              return getContents("");
042//      }
043
044        @GET
045        @Path("{path:.*}")
046        @Produces(MediaType.APPLICATION_OCTET_STREAM)
047        public byte[] getFileData(
048                        @PathParam("path") String path,
049                        @QueryParam("offset") final long offset,
050                        @QueryParam("length") @DefaultValue("-1") final int length)
051        {
052                requireNonNull(path, "path");
053                try (final RepoTransport repoTransport = authenticateAndCreateLocalRepoTransport()) {
054                        path = repoTransport.unprefixPath(path);
055                        return repoTransport.getFileData(path, offset, length);
056                }
057        }
058
059        @MKCOL
060        @Path("{path:.*}")
061        public void mkcol(@PathParam("path") final String path, @QueryParam("lastModified") final DateTime lastModified) {
062                throw new UnsupportedOperationException("NYI");
063        }
064
065        @DELETE
066        @Path("{path:.*}")
067        public void delete(@PathParam("path") String path) {
068                requireNonNull(path, "path");
069                try (final RepoTransport repoTransport = authenticateAndCreateLocalRepoTransport();) {
070                        path = repoTransport.unprefixPath(path);
071                        repoTransport.delete(path);
072                }
073        }
074
075        @PUT
076        @Path("{path:.*}")
077        @Consumes(MediaType.APPLICATION_OCTET_STREAM)
078        public void putFileData(@PathParam("path") String path, @QueryParam("offset") final long offset, final byte[] fileData) {
079                requireNonNull(path, "path");
080                try (final RepoTransport repoTransport = authenticateAndCreateLocalRepoTransport();) {
081                        path = repoTransport.unprefixPath(path);
082                        repoTransport.putFileData(path, offset, fileData);
083                }
084        }
085
086        @GET
087        @Produces(MediaType.TEXT_HTML)
088        public String browse() {
089                return browse("");
090        }
091
092        @GET
093        @Path("{path:.*}")
094        @Produces(MediaType.TEXT_HTML)
095        public String browse(@PathParam("path") String path){
096                requireNonNull(path, "path");
097                try (final RepoTransport repoTransport = authenticateWithLdap()) {
098                        path = repoTransport.unprefixPath(path);
099                        RepoFileDto dto = repoTransport.getRepoFileDto(path);
100                        return "<html><body>" + dto.toString() + "</body></html>";
101                }
102        }
103
104
105        @COPY
106        @Path("{path:.*}")
107        public void copy(@PathParam("path") final String path, @HeaderParam("DESTINATION") final String destination) {
108                throw new UnsupportedOperationException("NYI");
109        }
110
111        @MOVE
112        @Path("{path:.*}")
113        public void move(@PathParam("path") final String path, @HeaderParam("DESTINATION") final String destination) {
114                throw new UnsupportedOperationException("NYI");
115        }
116
117        @PROPFIND
118        @Path("{path:.*}")
119        public void propfind(@HeaderParam("CONTENT_LENGTH") final long contentLength) {
120                throw new UnsupportedOperationException("NYI");
121        }
122}