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