package controllers;

import java.io.IOException;
import java.net.URISyntaxException;

import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;
import org.tigris.subversion.javahl.ClientException;
import org.tmatesoft.svn.core.internal.server.dav.DAVServlet;
import org.tmatesoft.svn.core.internal.util.SVNEncodingUtil;

import play.mvc.*;
import playRepository.SVNRepository;
import utils.*;

public class SvnApp extends Controller{
    static DAVServlet davServlet;
    public static final String REPO_PREFIX = "repo/svn/";

    @BodyParser.Of(BodyParser.Raw.class)
    public static Result serviceWithPath(String path) throws ServletException, IOException {
        return service();
    }

    @BodyParser.Of(BodyParser.Raw.class)
    public static Result service() throws ServletException, IOException {
        //FIXME DAVServlet 들어내고 싶다.
        String path;
        try {
            path = new java.net.URI(request().uri()).getPath();
        } catch (URISyntaxException e) {
            return badRequest();
        }
        String pathInfo = SVNEncodingUtil.uriEncode(path.substring(path.indexOf('/',1)));
        
        String userName = pathInfo.substring(1, pathInfo.indexOf('/', 1));
        pathInfo = pathInfo.substring(pathInfo.indexOf('/', 1));
        PlayServletRequest request = new PlayServletRequest(request(), new PlayServletSession(new PlayServletContext()), pathInfo);
        PlayServletResponse response = new PlayServletResponse(response());

        new SVNRepository(userName, "").getCore().service(request, response);

        response.flushBuffer();

        return status(response.getStatus(), response.getBuffer().toByteArray());
    }

    public static void createRepository(String userName, String projectName) throws ClientException, ServletException {
        new SVNRepository(userName, projectName).create();
    }

    public static String getURL(String ownerName, String projectName) {
        String[] pathSegments = { "svn", ownerName, projectName };
        return Config.getScheme("http") + "://" + Config.getHostport(request().host()) + "/"
                + StringUtils.join(pathSegments, "/");
    }

    public static Result showRawCode(String userName, String projectName, String path) {
        // TODO Auto-generated method stub
        
        return null;
    }

    public static void deleteRepository(String userName, String projectName) throws Exception {
        new SVNRepository(userName, projectName).delete();
        
    }
}

@@ -116,24 +116,121 @@
 Exception;%0A%0A
+import models.Project;%0Aimport models.enumeration.Operation;%0Aimport models.enumeration.Resource;%0A%0A
 import org.a
@@ -376,16 +376,164 @@
 ervlet;%0A
+import org.tmatesoft.svn.core.internal.server.dav.SVNPathBasedAccess;%0Aimport org.tmatesoft.svn.core.internal.server.dav.handlers.DAVHandlerFactory;%0A
 import o
@@ -605,16 +605,46 @@
 .mvc.*;%0A
+import play.mvc.Http.Session;%0A
 import p
@@ -985,24 +985,57 @@
 e();%0A    %7D%0A%0A
+    @With(BasicAuthAction.class)%0A
     @BodyPar
@@ -1469,25 +1469,31 @@
   String
- userName
+%5B%5D pathSegments
  = pathI
@@ -1511,36 +1511,518 @@
 ng(1
-, pathInfo.indexOf('/', 1));
+).split(%22/%22);%0A%0A        if (pathSegments.length %3C 2) %7B%0A            return badRequest();%0A        %7D%0A%0A        String userName = pathSegments%5B0%5D;%0A        String projectName = pathSegments%5B1%5D;%0A%0A        Project project = Project.findByName(projectName);%0A%0A        if (!AccessControl.isAllowed(session().get(UserApp.SESSION_USERID), project.id,%0A                Resource.CODE, getRequestedOperation(request().method()), null)) %7B%0A            return forbidden(%22You have no permission to read this repository.%22);%0A        %7D%0A
 %0A   
@@ -3311,10 +3311,444 @@
  %0A    %7D%0A
+%0A    private static Operation getRequestedOperation(String method) %7B%0A        if (DAVHandlerFactory.METHOD_OPTIONS.equals(method)%0A                %7C%7C DAVHandlerFactory.METHOD_PROPFIND.equals(method)%0A                %7C%7C DAVHandlerFactory.METHOD_GET.equals(method)%0A                %7C%7C DAVHandlerFactory.METHOD_REPORT.equals(method)) %7B%0A            return Operation.READ;%0A        %7D else %7B%0A            return Operation.WRITE;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Apply access control using BasicAuthAction