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