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

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

Introduction

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

Prototype

public PacketLineIn(InputStream in) 

Source Link

Document

Create a new packet line reader.

Usage

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

License:Eclipse Distribution License

void execute(final Socket sock) throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException {
    rawIn = new BufferedInputStream(sock.getInputStream());
    rawOut = new SafeBufferedOutputStream(sock.getOutputStream());

    if (0 < daemon.getTimeout())
        sock.setSoTimeout(daemon.getTimeout() * 1000);
    String cmd = new PacketLineIn(rawIn).readStringRaw();
    final int nul = cmd.indexOf('\0');
    if (nul >= 0) {
        // Newer clients hide a "host" header behind this byte.
        // Currently we don't use it for anything, so we ignore
        // this portion of the command.
        ////from w  w w.  j  av a2  s . c  o  m
        cmd = cmd.substring(0, nul);
    }

    final GitDaemonService srv = getDaemon().matchService(cmd);
    if (srv == null)
        return;
    sock.setSoTimeout(0);
    srv.execute(this, cmd);
}

From source file:com.gitblit.transport.git.GitDaemonClient.java

License:Eclipse Distribution License

void execute(final Socket sock) throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException {
    rawIn = new BufferedInputStream(sock.getInputStream());
    rawOut = new BufferedOutputStream(sock.getOutputStream());

    if (0 < daemon.getTimeout())
        sock.setSoTimeout(daemon.getTimeout() * 1000);
    String cmd = new PacketLineIn(rawIn).readStringRaw();
    final int nul = cmd.indexOf('\0');
    if (nul >= 0) {
        // Newer clients hide a "host" header behind this byte.
        // Currently we don't use it for anything, so we ignore
        // this portion of the command.
        ////from   www . ja  v a 2s .com
        cmd = cmd.substring(0, nul);
    }

    final GitDaemonService srv = getDaemon().matchService(cmd);
    if (srv == null)
        return;
    sock.setSoTimeout(0);
    srv.execute(this, cmd);
}

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

License:Apache License

@Test
public void zipFormat() throws Exception {
    PushOneCommit.Result r = createChange();
    String abbreviated = r.getCommitId().abbreviate(8).name();
    String c = command(r, abbreviated);

    InputStream out = sshSession.exec2("git-upload-archive " + project.get(), argumentsToInputStream(c));

    // Wrap with PacketLineIn to read ACK bytes from output stream
    PacketLineIn in = new PacketLineIn(out);
    String tmp = in.readString();
    assertThat(tmp).isEqualTo("ACK");
    tmp = in.readString();//  ww w.  ja  v a2s .com

    // Skip length (4 bytes) + 1 byte
    // to position the output stream to the raw zip stream
    byte[] buffer = new byte[5];
    IO.readFully(out, buffer, 0, 5);
    Set<String> entryNames = new TreeSet<>();
    try (ZipArchiveInputStream zip = new ZipArchiveInputStream(out)) {
        ZipArchiveEntry zipEntry = zip.getNextZipEntry();
        while (zipEntry != null) {
            String name = zipEntry.getName();
            entryNames.add(name);
            zipEntry = zip.getNextZipEntry();
        }
    }

    assertThat(entryNames.size()).isEqualTo(1);
    assertThat(Iterables.getOnlyElement(entryNames))
            .isEqualTo(String.format("%s/%s", abbreviated, PushOneCommit.FILE_NAME));
}

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

License:Apache License

private void archiveNotPermitted() throws Exception {
    PushOneCommit.Result r = createChange();
    String abbreviated = r.getCommitId().abbreviate(8).name();
    String c = command(r, abbreviated);

    InputStream out = sshSession.exec2("git-upload-archive " + project.get(), argumentsToInputStream(c));

    // Wrap with PacketLineIn to read ACK bytes from output stream
    PacketLineIn in = new PacketLineIn(out);
    String tmp = in.readString();
    assertThat(tmp).isEqualTo("ACK");
    tmp = in.readString();/*w w w.j av a  2s .  c om*/
    tmp = in.readString();
    tmp = tmp.substring(1);
    assertThat(tmp).isEqualTo("fatal: upload-archive not permitted");
}

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

License:Apache License

/**
 * Read and parse arguments from input stream.
 * This method gets the arguments from input stream, in Pkt-line format,
 * then parses them to fill the options object.
 *///  www  .  j a  v  a  2s  . c  o  m
protected void readArguments() throws IOException, Failure {
    String argCmd = "argument ";
    List<String> args = Lists.newArrayList();

    // Read arguments in Pkt-Line format
    PacketLineIn packetIn = new PacketLineIn(in);
    for (;;) {
        String s = packetIn.readString();
        if (s == PacketLineIn.END) {
            break;
        }
        if (!s.startsWith(argCmd)) {
            throw new Failure(1, "fatal: 'argument' token or flush expected");
        }
        String[] parts = s.substring(argCmd.length()).split("=", 2);
        for (String p : parts) {
            args.add(p);
        }
    }

    try {
        // Parse them into the 'options' field
        CmdLineParser parser = new CmdLineParser(options);
        parser.parseArgument(args);
        if (options.path == null || Arrays.asList(".").equals(options.path)) {
            options.path = Collections.emptyList();
        }
    } catch (CmdLineException e) {
        throw new Failure(2, "fatal: unable to parse arguments, " + e);
    }
}

From source file:it.com.atlassian.labs.speakeasy.util.jgit.FixedTransportHttp.java

License:Eclipse Distribution License

private void readSmartHeaders(final InputStream in, final String service) throws IOException {
    // A smart reply will have a '#' after the first 4 bytes, but
    // a dumb reply cannot contain a '#' until after byte 41. Do a
    // quick check to make sure its a smart reply before we parse
    // as a pkt-line stream.
    ///*  w  w w.j  a v a2 s.  co  m*/
    final byte[] magic = new byte[5];
    IO.readFully(in, magic, 0, magic.length);
    if (magic[4] != '#') {
        throw new TransportException(uri,
                MessageFormat.format(JGitText.get().expectedPktLineWithService, RawParseUtils.decode(magic)));
    }

    final PacketLineIn pckIn = new PacketLineIn(new UnionInputStream(new ByteArrayInputStream(magic), in));
    final String exp = "# service=" + service; //$NON-NLS-1$
    final String act = pckIn.readString();
    if (!exp.equals(act)) {
        throw new TransportException(uri, MessageFormat.format(JGitText.get().expectedGot, exp, act));
    }

    while (pckIn.readString() != PacketLineIn.END) {
        // for now, ignore the remaining header lines
    }
}