Example usage for org.eclipse.jgit.transport UploadPack upload

List of usage examples for org.eclipse.jgit.transport UploadPack upload

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport UploadPack upload.

Prototype

public void upload(InputStream input, OutputStream output, @Nullable OutputStream messages) throws IOException 

Source Link

Document

Execute the upload task on the socket.

Usage

From source file:com.gitblit.git.GitDaemon.java

License:Eclipse Distribution License

public GitDaemon(IGitblit gitblit) {

    IStoredSettings settings = gitblit.getSettings();
    int port = settings.getInteger(Keys.git.daemonPort, 0);
    String bindInterface = settings.getString(Keys.git.daemonBindInterface, "localhost");

    if (StringUtils.isEmpty(bindInterface)) {
        myAddress = new InetSocketAddress(port);
    } else {//from w  ww.j  ava  2 s .co  m
        myAddress = new InetSocketAddress(bindInterface, port);
    }

    repositoryResolver = new RepositoryResolver<GitDaemonClient>(gitblit);
    uploadPackFactory = new GitblitUploadPackFactory<GitDaemonClient>(gitblit);
    receivePackFactory = new GitblitReceivePackFactory<GitDaemonClient>(gitblit);

    run = new AtomicBoolean(false);
    processors = new ThreadGroup("Git-Daemon");
    services = new GitDaemonService[] { new GitDaemonService("upload-pack", "uploadpack") {
        {
            setEnabled(true);
            setOverridable(false);
        }

        @Override
        protected void execute(final GitDaemonClient dc, final Repository db)
                throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException {
            UploadPack up = uploadPackFactory.create(dc, db);
            InputStream in = dc.getInputStream();
            OutputStream out = dc.getOutputStream();
            up.upload(in, out, null);
        }
    }, new GitDaemonService("receive-pack", "receivepack") {
        {
            setEnabled(true);
            setOverridable(false);
        }

        @Override
        protected void execute(final GitDaemonClient dc, final Repository db)
                throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException {
            ReceivePack rp = receivePackFactory.create(dc, db);
            InputStream in = dc.getInputStream();
            OutputStream out = dc.getOutputStream();
            rp.receive(in, out, null);
        }
    } };
}

From source file:com.gitblit.transport.ssh.git.Upload.java

License:Apache License

@Override
protected void runImpl() throws Failure {
    try {//w w  w.  j a  v  a  2  s  .co m
        SshKey key = getContext().getClient().getKey();
        if (key != null && !key.canClone()) {
            throw new Failure(1, "Sorry, your SSH public key is not allowed to clone!");
        }
        UploadPack up = uploadPackFactory.create(getContext().getClient(), repo);
        up.upload(in, out, null);
    } catch (Exception e) {
        throw new Failure(1, "fatal: Cannot upload pack: ", e);
    }
}

From source file:com.google.gerrit.sshd.commands.Upload.java

License:Apache License

@Override
protected void runImpl() throws IOException, Failure {
    if (!projectControl.canRunUploadPack()) {
        throw new Failure(1, "fatal: upload-pack not permitted on this server");
    }/*  ww w . ja v  a2  s. c om*/

    final UploadPack up = new UploadPack(repo);
    if (!projectControl.allRefsAreVisible()) {
        up.setAdvertiseRefsHook(
                new VisibleRefFilter(tagCache, changeCache, repo, projectControl, db.get(), true));
    }
    up.setPackConfig(config.getPackConfig());
    up.setTimeout(config.getTimeout());

    List<PreUploadHook> allPreUploadHooks = Lists.newArrayList(preUploadHooks);
    allPreUploadHooks.add(uploadValidatorsFactory.create(project, repo, session.getRemoteAddressAsString()));
    up.setPreUploadHook(PreUploadHookChain.newChain(allPreUploadHooks));
    try {
        up.upload(in, out, err);
    } catch (UploadValidationException e) {
        // UploadValidationException is used by the UploadValidators to
        // stop the uploadPack. We do not want this exception to go beyond this
        // point otherwise it would print a stacktrace in the logs and return an
        // internal server error to the client.
        if (!e.isOutput()) {
            up.sendMessage(e.getMessage());
        }
    }
}

From source file:com.sonatype.sshjgit.core.gitcommand.Upload.java

License:Apache License

@Override
protected void runImpl() throws IOException, Failure {
    SecurityUtils.getSubject().checkPermission("gitrepo:fetch:" + getRepoNameAsPermissionParts(repo));
    UploadPack up = new UploadPack(repo);
    up.upload(in, out, err);
}

From source file:com.tasktop.c2c.server.scm.web.GitHandler.java

License:Open Source License

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    final boolean containerSupportsChunkedIO = computeContainerSupportsChunkedIO();

    String pathInfo = request.getPathInfo();
    log.info("Git request: " + request.getMethod() + " " + request.getRequestURI() + " " + pathInfo);

    Repository repository = null;//from   w w  w  .j a  v a 2 s  .  c o  m
    try {
        // only work on Git requests
        Matcher matcher = pathInfo == null ? null : GIT_COMMAND_PATTERN.matcher(pathInfo);
        if (matcher == null || !matcher.matches()) {
            log.info("Unexpected path: " + pathInfo);
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        String requestCommand = matcher.group(1);
        String requestPath = matcher.group(2);

        // sanity check on path, disallow path separator components
        if (requestPath == null || requestPath.contains("/") || requestPath.contains("..")) {
            badPathResponse();
        }

        repository = repositoryResolver.open(request, requestPath);

        InputStream requestInput = request.getInputStream();
        if (!containerSupportsChunkedIO) {
            requestInput = new ChunkedInputStream(requestInput);
        }

        MultiplexingOutputStream mox = createMultiplexingOutputStream(response, containerSupportsChunkedIO);
        // indicate that we're ok to handle the request
        // note that following this there will be a two-way communication with the process
        // that might still encounter errors. That's ok.
        startOkResponse(response, containerSupportsChunkedIO);

        // identify the git command
        GitCommand command = GitCommand.fromCommandName(requestCommand);
        if (command != null) {
            // permissions check
            if (!Security.hasOneOfRoles(command.getRoles())) {
                log.info("Access denied to " + Security.getCurrentUser() + " for " + command.getCommandName()
                        + " on " + TenancyUtil.getCurrentTenantProjectIdentifer() + " " + requestPath);
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
            switch (command) {
            case RECEIVE_PACK:
                ReceivePack rp = new ReceivePack(repository);
                rp.setPostReceiveHook(postReceiveHook);
                rp.receive(requestInput, mox.stream(PacketType.STDOUT), mox.stream(PacketType.STDERR));
                break;
            case UPLOAD_PACK:
                UploadPack up = new UploadPack(repository);
                up.upload(requestInput, mox.stream(PacketType.STDOUT), mox.stream(PacketType.STDERR));
                break;
            default:
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        }

        // at this stage we're done with IO
        // send the exit value and closing chunk
        try {
            int exitValue = 0;

            if (exitValue != 0) {
                log.info("Exit value: " + exitValue);
            }
            mox.writeExitCode(exitValue);
            mox.close();
        } catch (IOException e) {
            // ignore
            log.debug("Cannot complete writing exit state", e);
        }

        // clear interrupt status
        Thread.interrupted();

    } catch (ErrorResponseException e) {
        createGitErrorResponse(response, containerSupportsChunkedIO, e.getMessage());
    } catch (ServiceNotAuthorizedException e) {
        createGitErrorResponse(response, containerSupportsChunkedIO, e.getMessage());
    } catch (ServiceNotEnabledException e) {
        createGitErrorResponse(response, containerSupportsChunkedIO, e.getMessage());
    } finally {
        log.info("Git request complete");
        if (repository != null) {
            repository.close();
        }
    }
}

From source file:net.antoniy.gidder.beta.ssh.Upload.java

License:Apache License

@Override
protected void runImpl() throws IOException {
    if (!hasPermission()) {
        err.write(MSG_REPOSITORY_PERMISSIONS.getBytes());
        err.flush();/*from  ww w. j  a v a  2  s.c o m*/
        onExit(CODE_OK, MSG_REPOSITORY_PERMISSIONS);
        return;
    }

    Config config = new Config();
    //      int timeout = Integer.parseInt(config.getString("transfer", null,
    //            "timeout"));
    int timeout = 10;

    PackConfig packConfig = new PackConfig();
    packConfig.setDeltaCompress(false);
    packConfig.setThreads(1);
    packConfig.fromConfig(config);

    final UploadPack up = new UploadPack(repo);
    up.setPackConfig(packConfig);
    up.setTimeout(timeout);
    up.upload(in, out, err);
}

From source file:org.webcat.core.git.http.UploadPackRequestHandler.java

License:Open Source License

/**
 * Handles the request and generates the UploadPack response.
 *
 * @param request the request/*w w  w.j  av a2  s . co  m*/
 * @param response the response
 * @throws Exception if an error occurs
 */
public void handleRequest(WORequest request, WOResponse response) throws Exception {
    String contentType = request.headerForKey(HttpSupport.HDR_CONTENT_TYPE);

    if (UPLOAD_PACK_REQUEST_TYPE.equals(contentType)) {
        Repository repo = RepositoryRequestUtils.repositoryFromRequest(request);

        UploadPack up = new UploadPack(repo);
        up.setBiDirectionalPipe(false);

        response.setHeader(UPLOAD_PACK_RESULT_TYPE, HttpSupport.HDR_CONTENT_TYPE);

        InputStream input = RequestUtils.inputStreamForRequest(request);
        SmartGZIPOutputStream output = new SmartGZIPOutputStream(request, response);

        up.upload(input, output, null);

        output.close();
    } else {
        response.setStatus(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE);
    }
}

From source file:playRepository.RepositoryService.java

License:Apache License

private static void uploadPack(final InputStream input, Repository repository, final OutputStream output) {
    final UploadPack uploadPack = new UploadPack(repository);
    uploadPack.setBiDirectionalPipe(false);
    new Thread() {
        @Override/*  w  w w .  j  a  va 2 s. com*/
        public void run() {
            try {
                uploadPack.upload(input, output, null);
            } catch (IOException e) {
                Logger.error("uploadPack failed", e);
            }

            closeStreams("uploadPack", input, output);
        }
    }.start();
}