Search in sources :

Example 1 with ErroringAction

use of org.gradle.internal.ErroringAction in project gradle by gradle.

the class GitHttpRepository method expectCloneSomething.

public void expectCloneSomething() {
    server.expect(server.get(backingRepo.getName() + "/info/refs", getRefsAction()));
    server.expect(server.post(backingRepo.getName() + "/git-upload-pack", new ErroringAction<HttpExchange>() {

        @Override
        protected void doExecute(HttpExchange httpExchange) throws Exception {
            httpExchange.getResponseHeaders().add("content-type", "application/x-git-upload-pack-result");
            httpExchange.sendResponseHeaders(200, 0);
            ProcessBuilder builder = new ProcessBuilder();
            final Process process = builder.command("git", "upload-pack", "--stateless-rpc", backingRepo.getWorkTree().getAbsolutePath()).redirectErrorStream(true).start();
            InputStream instream = new GZIPInputStream(httpExchange.getRequestBody());
            ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
            IOUtils.copy(instream, requestContent);
            byte[] bytes = requestContent.toByteArray();
            process.getOutputStream().write(bytes);
            process.getOutputStream().flush();
            IOUtils.copy(process.getInputStream(), httpExchange.getResponseBody());
            int result = process.waitFor();
            if (result != 0) {
                throw new RuntimeException("Failed to run git upload-pack");
            }
        }
    }));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ErroringAction(org.gradle.internal.ErroringAction) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with ErroringAction

use of org.gradle.internal.ErroringAction in project gradle by gradle.

the class GitHttpRepository method getRefsAction.

private Action<HttpExchange> getRefsAction() {
    return new ErroringAction<HttpExchange>() {

        @Override
        protected void doExecute(HttpExchange httpExchange) throws Exception {
            ProcessBuilder builder = new ProcessBuilder();
            Process process = builder.command("git", "upload-pack", "--advertise-refs", backingRepo.getWorkTree().getAbsolutePath()).redirectErrorStream(true).start();
            ByteArrayOutputStream content = new ByteArrayOutputStream();
            content.write("001e# service=git-upload-pack\n".getBytes());
            content.write("0000".getBytes());
            IOUtils.copy(process.getInputStream(), content);
            process.waitFor();
            int result = process.waitFor();
            if (result != 0) {
                throw new RuntimeException("Failed to run git upload-pack");
            }
            byte[] bytes = content.toByteArray();
            httpExchange.getResponseHeaders().add("content-type", "application/x-git-upload-pack-advertisement");
            httpExchange.sendResponseHeaders(200, bytes.length);
            httpExchange.getResponseBody().write(bytes);
        }
    };
}
Also used : ErroringAction(org.gradle.internal.ErroringAction) HttpExchange(com.sun.net.httpserver.HttpExchange) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

HttpExchange (com.sun.net.httpserver.HttpExchange)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ErroringAction (org.gradle.internal.ErroringAction)2 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1