Example usage for org.eclipse.jgit.errors TransportException TransportException

List of usage examples for org.eclipse.jgit.errors TransportException TransportException

Introduction

In this page you can find the example usage for org.eclipse.jgit.errors TransportException TransportException.

Prototype

public TransportException(String s, Throwable cause) 

Source Link

Document

Constructs an TransportException with the specified detail message.

Usage

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

License:Eclipse Distribution License

private FetchConnection newDumbConnection(InputStream in) throws IOException, PackProtocolException {
    HttpObjectDB d = new HttpObjectDB(objectsUrl);
    BufferedReader br = toBufferedReader(in);
    Map<String, Ref> refs;
    try {// www  .  ja  va 2 s. c  o m
        refs = d.readAdvertisedImpl(br);
    } finally {
        br.close();
    }

    if (!refs.containsKey(Constants.HEAD)) {
        // If HEAD was not published in the info/refs file (it usually
        // is not there) download HEAD by itself as a loose file and do
        // the resolution by hand.
        //
        HttpURLConnection conn = httpOpen(new URL(baseUrl, Constants.HEAD));
        int status = HttpSupport.response(conn);
        switch (status) {
        case HttpURLConnection.HTTP_OK: {
            br = toBufferedReader(openInputStream(conn));
            try {
                String line = br.readLine();
                if (line != null && line.startsWith(RefDirectory.SYMREF)) {
                    String target = line.substring(RefDirectory.SYMREF.length());
                    Ref r = refs.get(target);
                    if (r == null)
                        r = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, target, null);
                    r = new SymbolicRef(Constants.HEAD, r);
                    refs.put(r.getName(), r);
                } else if (line != null && ObjectId.isId(line)) {
                    Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.NETWORK, Constants.HEAD,
                            ObjectId.fromString(line));
                    refs.put(r.getName(), r);
                }
            } finally {
                br.close();
            }
            break;
        }

        case HttpURLConnection.HTTP_NOT_FOUND:
            break;

        default:
            throw new TransportException(uri,
                    MessageFormat.format(JGitText.get().cannotReadHEAD, status, conn.getResponseMessage()));
        }
    }

    WalkFetchConnection wfc = new WalkFetchConnection(this, d);
    wfc.available(refs);
    return wfc;
}

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

License:Eclipse Distribution License

private HttpURLConnection connect(final String service) throws TransportException, NotSupportedException {
    final URL u;
    try {//from  ww w . j a va  2  s. c om
        final StringBuilder b = new StringBuilder();
        b.append(baseUrl);

        if (b.charAt(b.length() - 1) != '/')
            b.append('/');
        b.append(Constants.INFO_REFS);

        if (useSmartHttp) {
            b.append(b.indexOf("?") < 0 ? '?' : '&'); //$NON-NLS-1$
            b.append("service="); //$NON-NLS-1$
            b.append(service);
        }

        u = new URL(b.toString());
    } catch (MalformedURLException e) {
        throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
    }

    try {
        int authAttempts = 1;
        for (;;) {
            final HttpURLConnection conn = httpOpen(u);
            if (useSmartHttp) {
                String exp = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$
                conn.setRequestProperty(HDR_ACCEPT, exp + ", */*"); //$NON-NLS-1$
            } else {
                conn.setRequestProperty(HDR_ACCEPT, "*/*"); //$NON-NLS-1$
            }
            final int status = HttpSupport.response(conn);
            switch (status) {
            case HttpURLConnection.HTTP_OK:
                return conn;

            case HttpURLConnection.HTTP_NOT_FOUND:
                throw new NoRemoteRepositoryException(uri, MessageFormat.format(JGitText.get().uriNotFound, u));

            case HttpURLConnection.HTTP_UNAUTHORIZED:
                authMethod = HttpAuthMethod.scanResponse(conn);
                if (authMethod == HttpAuthMethod.NONE)
                    throw new TransportException(uri,
                            MessageFormat.format(JGitText.get().authenticationNotSupported, uri));
                if (1 < authAttempts || !authMethod.authorize(uri, getCredentialsProvider())) {
                    throw new TransportException(uri, JGitText.get().notAuthorized);
                }
                authAttempts++;
                continue;

            case HttpURLConnection.HTTP_FORBIDDEN:
                throw new TransportException(uri,
                        MessageFormat.format(JGitText.get().serviceNotPermitted, service));

            default:
                String err = status + " " + conn.getResponseMessage(); //$NON-NLS-1$
                throw new TransportException(uri, err);
            }
        }
    } catch (NotSupportedException e) {
        throw e;
    } catch (TransportException e) {
        throw e;
    } catch (IOException e) {
        throw new TransportException(uri, MessageFormat.format(JGitText.get().cannotOpenService, service), e);
    }
}

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

License:Eclipse Distribution License

IOException wrongContentType(String expType, String actType) {
    final String why = MessageFormat.format(JGitText.get().expectedReceivedContentType, expType, actType);
    return new TransportException(uri, why);
}

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.
    ////from   w  ww.j a va  2 s  . c  o 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
    }
}

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

License:Eclipse Distribution License

private void queueWants(final Collection<Ref> want) throws TransportException {
    final HashSet<ObjectId> inWorkQueue = new HashSet<ObjectId>();
    for (final Ref r : want) {
        final ObjectId id = r.getObjectId();
        try {//from  w  w  w.j  a  v  a  2s  .c o  m
            final RevObject obj = revWalk.parseAny(id);
            if (obj.has(COMPLETE))
                continue;
            if (inWorkQueue.add(id)) {
                obj.add(IN_WORK_QUEUE);
                workQueue.add(obj);
            }
        } catch (MissingObjectException e) {
            if (inWorkQueue.add(id))
                workQueue.add(id);
        } catch (IOException e) {
            throw new TransportException(MessageFormat.format(JGitText.get().cannotRead, id.name()), e);
        }
    }
}

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

License:Eclipse Distribution License

private void process(final ObjectId id) throws TransportException {
    final RevObject obj;
    try {//  w w w  .  ja  v  a2 s .c  o m
        if (id instanceof RevObject) {
            obj = (RevObject) id;
            if (obj.has(COMPLETE))
                return;
            revWalk.parseHeaders(obj);
        } else {
            obj = revWalk.parseAny(id);
            if (obj.has(COMPLETE))
                return;
        }
    } catch (IOException e) {
        throw new TransportException(MessageFormat.format(JGitText.get().cannotRead, id.name()), e);
    }

    switch (obj.getType()) {
    case Constants.OBJ_BLOB:
        processBlob(obj);
        break;
    case Constants.OBJ_TREE:
        processTree(obj);
        break;
    case Constants.OBJ_COMMIT:
        processCommit(obj);
        break;
    case Constants.OBJ_TAG:
        processTag(obj);
        break;
    default:
        throw new TransportException(MessageFormat.format(JGitText.get().unknownObjectType, id.name()));
    }

    // If we had any prior errors fetching this object they are
    // now resolved, as the object was parsed successfully.
    //
    fetchErrors.remove(id);
}

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

License:Eclipse Distribution License

private void processBlob(final RevObject obj) throws TransportException {
    try {//from  ww  w  .j  ava 2s .  c  om
        if (reader.has(obj, Constants.OBJ_BLOB))
            obj.add(COMPLETE);
        else
            throw new TransportException(MessageFormat.format(JGitText.get().cannotReadBlob, obj.name()),
                    new MissingObjectException(obj, Constants.TYPE_BLOB));
    } catch (IOException error) {
        throw new TransportException(MessageFormat.format(JGitText.get().cannotReadBlob, obj.name()), error);
    }
}

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

License:Eclipse Distribution License

private void processTree(final RevObject obj) throws TransportException {
    try {//from   www  .java  2  s.  co  m
        treeWalk.reset(obj);
        while (treeWalk.next()) {
            final FileMode mode = treeWalk.getFileMode(0);
            final int sType = mode.getObjectType();

            switch (sType) {
            case Constants.OBJ_BLOB:
            case Constants.OBJ_TREE:
                treeWalk.getObjectId(idBuffer, 0);
                needs(revWalk.lookupAny(idBuffer, sType));
                continue;

            default:
                if (FileMode.GITLINK.equals(mode))
                    continue;
                treeWalk.getObjectId(idBuffer, 0);
                throw new CorruptObjectException(MessageFormat.format(JGitText.get().invalidModeFor, mode,
                        idBuffer.name(), treeWalk.getPathString(), obj.getId().name()));
            }
        }
    } catch (IOException ioe) {
        throw new TransportException(MessageFormat.format(JGitText.get().cannotReadTree, obj.name()), ioe);
    }
    obj.add(COMPLETE);
}

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

License:Eclipse Distribution License

private boolean alreadyHave(final AnyObjectId id) throws TransportException {
    try {//w  ww.j av a 2  s  . co  m
        return reader.has(id);
    } catch (IOException error) {
        throw new TransportException(MessageFormat.format(JGitText.get().cannotReadObject, id.name()), error);
    }
}

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

License:Eclipse Distribution License

private boolean downloadPackedObject(final ProgressMonitor monitor, final AnyObjectId id)
        throws TransportException {
    // Search for the object in a remote pack whose index we have,
    // but whose pack we do not yet have.
    ///*from  w w  w  .jav  a 2  s  .  co  m*/
    final Iterator<RemotePack> packItr = unfetchedPacks.iterator();
    while (packItr.hasNext() && !monitor.isCancelled()) {
        final RemotePack pack = packItr.next();
        try {
            pack.openIndex(monitor);
        } catch (IOException err) {
            // If the index won't open its either not found or
            // its a format we don't recognize. In either case
            // we may still be able to obtain the object from
            // another source, so don't consider it a failure.
            //
            recordError(id, err);
            packItr.remove();
            continue;
        }

        if (monitor.isCancelled()) {
            // If we were cancelled while the index was opening
            // the open may have aborted. We can't search an
            // unopen index.
            //
            return false;
        }

        if (!pack.index.hasObject(id)) {
            // Not in this pack? Try another.
            //
            continue;
        }

        // It should be in the associated pack. Download that
        // and attach it to the local repository so we can use
        // all of the contained objects.
        //
        try {
            pack.downloadPack(monitor);
        } catch (IOException err) {
            // If the pack failed to download, index correctly,
            // or open in the local repository we may still be
            // able to obtain this object from another pack or
            // an alternate.
            //
            recordError(id, err);
            continue;
        } finally {
            // If the pack was good its in the local repository
            // and Repository.hasObject(id) will succeed in the
            // future, so we do not need this data anymore. If
            // it failed the index and pack are unusable and we
            // shouldn't consult them again.
            //
            try {
                if (pack.tmpIdx != null)
                    FileUtils.delete(pack.tmpIdx);
            } catch (IOException e) {
                throw new TransportException(e.getMessage(), e);
            }
            packItr.remove();
        }

        if (!alreadyHave(id)) {
            // What the hell? This pack claimed to have
            // the object, but after indexing we didn't
            // actually find it in the pack.
            //
            recordError(id, new FileNotFoundException(
                    MessageFormat.format(JGitText.get().objectNotFoundIn, id.name(), pack.packName)));
            continue;
        }

        // Complete any other objects that we can.
        //
        final Iterator<ObjectId> pending = swapFetchQueue();
        while (pending.hasNext()) {
            final ObjectId p = pending.next();
            if (pack.index.hasObject(p)) {
                pending.remove();
                process(p);
            } else {
                workQueue.add(p);
            }
        }
        return true;

    }
    return false;
}