Example usage for org.eclipse.jgit.transport PacketLineOut PacketLineOut

List of usage examples for org.eclipse.jgit.transport PacketLineOut PacketLineOut

Introduction

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

Prototype

public PacketLineOut(OutputStream outputStream) 

Source Link

Document

Create a new packet line writer.

Usage

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

License:Eclipse Distribution License

void execute(final GitDaemonClient client, final String commandLine)
        throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException {
    final String name = commandLine.substring(command.length() + 1);
    Repository db;/*from w  w  w  . j  a  va2s . c om*/
    try {
        db = client.getDaemon().openRepository(client, name);
    } catch (ServiceMayNotContinueException e) {
        // An error when opening the repo means the client is expecting a ref
        // advertisement, so use that style of error.
        PacketLineOut pktOut = new PacketLineOut(client.getOutputStream());
        pktOut.writeString("ERR " + e.getMessage() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
        db = null;
    }
    if (db == null)
        return;
    try {
        if (isEnabledFor(db))
            execute(client, db);
    } finally {
        db.close();
    }
}

From source file:com.google.gerrit.acceptance.ssh.UploadArchiveIT.java

License:Apache License

private InputStream argumentsToInputStream(String c) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PacketLineOut pctOut = new PacketLineOut(out);
    for (String arg : Splitter.on(' ').split(c)) {
        pctOut.writeString("argument " + arg);
    }/*from   w w  w  . j  a v  a2  s .c  o m*/
    pctOut.end();
    return new ByteArrayInputStream(out.toByteArray());
}

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

License:Apache License

@Override
protected void runImpl() throws IOException, Failure {
    PacketLineOut packetOut = new PacketLineOut(out);
    packetOut.setFlushOnEnd(true);/*from   www .  j av  a2 s.  co m*/
    packetOut.writeString("ACK");
    packetOut.end();

    try {
        // Parse Git arguments
        readArguments();

        ArchiveFormat f = allowedFormats.getExtensions().get("." + options.format);
        if (f == null) {
            throw new Failure(3, "fatal: upload-archive not permitted");
        }

        // Find out the object to get from the specified reference and paths
        ObjectId treeId = repo.resolve(options.treeIsh);
        if (treeId == null) {
            throw new Failure(4, "fatal: reference not found");
        }

        // Verify the user has permissions to read the specified reference
        if (!projectControl.allRefsAreVisible() && !canRead(treeId)) {
            throw new Failure(5, "fatal: cannot perform upload-archive operation");
        }

        // The archive is sent in DATA sideband channel
        try (SideBandOutputStream sidebandOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA,
                SideBandOutputStream.MAX_BUF, out)) {
            new ArchiveCommand(repo).setFormat(f.name()).setFormatOptions(getFormatOptions(f)).setTree(treeId)
                    .setPaths(options.path.toArray(new String[0])).setPrefix(options.prefix)
                    .setOutputStream(sidebandOut).call();
            sidebandOut.flush();
        } catch (GitAPIException e) {
            throw new Failure(7, "fatal: git api exception, " + e);
        }
    } catch (Failure f) {
        // Report the error in ERROR sideband channel
        try (SideBandOutputStream sidebandError = new SideBandOutputStream(SideBandOutputStream.CH_ERROR,
                SideBandOutputStream.MAX_BUF, out)) {
            sidebandError.write(f.getMessage().getBytes(UTF_8));
            sidebandError.flush();
        }
        throw f;
    } finally {
        // In any case, cleanly close the packetOut channel
        packetOut.end();
    }
}

From source file:net.community.chest.gitcloud.facade.backend.git.BackendUploadPackFactory.java

License:Apache License

@Override
public UploadPack create(final C request, Repository db)
        throws ServiceNotEnabledException, ServiceNotAuthorizedException {
    final File dir = db.getDirectory();
    final String logPrefix;
    if (request instanceof HttpServletRequest) {
        HttpServletRequest req = (HttpServletRequest) request;
        logPrefix = "create(" + req.getMethod() + ")[" + req.getRequestURI() + "][" + req.getQueryString()
                + "]";
    } else {// w  ww .j  ava 2s.  com
        logPrefix = "create(" + dir.getAbsolutePath() + ")";
    }
    if (logger.isDebugEnabled()) {
        logger.debug(logPrefix + ": " + dir.getAbsolutePath());
    }

    UploadPack up = new UploadPack(db) {
        @Override
        @SuppressWarnings("synthetic-access")
        public void upload(InputStream input, OutputStream output, OutputStream messages) throws IOException {
            InputStream effIn = input;
            OutputStream effOut = output, effMessages = messages;
            if (logger.isTraceEnabled()) {
                LineLevelAppender inputAppender = new LineLevelAppender() {
                    @Override
                    public void writeLineData(CharSequence lineData) throws IOException {
                        logger.trace(logPrefix + " upload(C): " + lineData);
                    }

                    @Override
                    public boolean isWriteEnabled() {
                        return true;
                    }
                };
                effIn = new TeeInputStream(effIn, new HexDumpOutputStream(inputAppender), true);

                LineLevelAppender outputAppender = new LineLevelAppender() {
                    @Override
                    public void writeLineData(CharSequence lineData) throws IOException {
                        logger.trace(logPrefix + " upload(S): " + lineData);
                    }

                    @Override
                    public boolean isWriteEnabled() {
                        return true;
                    }
                };
                effOut = new TeeOutputStream(effOut, new HexDumpOutputStream(outputAppender));

                if (effMessages != null) {
                    LineLevelAppender messagesAppender = new LineLevelAppender() {
                        @Override
                        public void writeLineData(CharSequence lineData) throws IOException {
                            logger.trace(logPrefix + " upload(M): " + lineData);
                        }

                        @Override
                        public boolean isWriteEnabled() {
                            return true;
                        }
                    };
                    // TODO review the decision to use an AsciiLineOutputStream here
                    effMessages = new TeeOutputStream(effMessages, new AsciiLineOutputStream(messagesAppender));
                }
            }

            super.upload(effIn, effOut, effMessages);
        }

        @Override
        @SuppressWarnings("synthetic-access")
        public void sendAdvertisedRefs(RefAdvertiser adv) throws IOException, ServiceMayNotContinueException {
            RefAdvertiser effAdv = adv;
            if (logger.isTraceEnabled() && (adv instanceof PacketLineOutRefAdvertiser)) {
                PacketLineOut pckOut = (PacketLineOut) ReflectionUtils.getField(pckOutField, adv);
                effAdv = new PacketLineOutRefAdvertiser(pckOut) {
                    private final PacketLineOut pckLog = new PacketLineOut( // TODO review the decision to use an AsciiLineOutputStream here
                            new AsciiLineOutputStream(new LineLevelAppender() {
                                @Override
                                public void writeLineData(CharSequence lineData) throws IOException {
                                    logger.trace(logPrefix + " S: " + lineData);
                                }

                                @Override
                                public boolean isWriteEnabled() {
                                    return true;
                                }
                            }));

                    @Override
                    protected void writeOne(CharSequence line) throws IOException {
                        String s = line.toString();
                        super.writeOne(s);
                        pckLog.writeString(s);
                    }

                    @Override
                    protected void end() throws IOException {
                        super.end();
                        pckLog.end();
                    }
                };
            }

            super.sendAdvertisedRefs(effAdv);
        }
    };
    up.setTimeout(uploadTimeoutValue);
    return up;
}

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

License:Open Source License

/**
 * Delegates to the/*from   w  w w .  ja v  a  2 s  .  co  m*/
 * {@link #advertise(WORequest, Repository, PacketLineOutRefAdvertiser)}
 * method on the class and then appends the advertisement to the response.
 *
 * @param request the request
 * @param response the response
 * @param filterChain the filter chain
 * @throws Exception if an error occurred
 */
public void filterRequest(WORequest request, WOResponse response, RequestFilterChain filterChain)
        throws Exception {
    if (service.equals(request.formValueForKey("service"))) {
        try {
            Repository repo = RepositoryRequestUtils.repositoryFromRequest(request);

            response.setHeader("application/x-" + service + "-advertisement", HttpSupport.HDR_CONTENT_TYPE);

            NSMutableDataOutputStream output = new NSMutableDataOutputStream();
            PacketLineOut lineOut = new PacketLineOut(output);

            lineOut.writeString("# service=" + service + "\n");
            lineOut.end();

            advertise(request, repo, new PacketLineOutRefAdvertiser(lineOut));

            output.close();
            response.appendContentData(output.data());
        } catch (IOException e) {
            Log.error("(403) An exception occurred when advertising the " + "repository", e);
            response.setStatus(WOMessage.HTTP_STATUS_FORBIDDEN);
        }
    } else {
        filterChain.filterRequest(request, response);
    }
}

From source file:playRepository.RepositoryService.java

License:Apache License

/**
 * @see <a href="https://www.kernel.org/pub/software/scm/git/docs/git-upload-pack.html">git-upload-pack</a>
 * @see <a href="https://www.kernel.org/pub/software/scm/git/docs/git-receive-pack.html">git-receive-pack</a>
 *///from w w w .  ja va 2s  .c om
public static byte[] gitAdvertise(Project project, String service, Response response) throws IOException {
    response.setContentType("application/x-" + service + "-advertisement");

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PacketLineOut packetLineOut = new PacketLineOut(byteArrayOutputStream);
    packetLineOut.writeString("# service=" + service + "\n");
    packetLineOut.end();
    PacketLineOutRefAdvertiser packetLineOutRefAdvertiser = new PacketLineOutRefAdvertiser(packetLineOut);

    if (service.equals("git-upload-pack")) {
        Repository repository = GitRepository.buildGitRepository(project);
        UploadPack uploadPack = new UploadPack(repository);
        uploadPack.setBiDirectionalPipe(false);
        uploadPack.sendAdvertisedRefs(packetLineOutRefAdvertiser);
    } else if (service.equals("git-receive-pack")) {
        Repository repository = GitRepository.buildGitRepository(project, false);
        ReceivePack receivePack = new ReceivePack(repository);
        receivePack.sendAdvertisedRefs(packetLineOutRefAdvertiser);
    }

    byteArrayOutputStream.close();

    return byteArrayOutputStream.toByteArray();
}