Example usage for org.eclipse.jgit.util HttpSupport response

List of usage examples for org.eclipse.jgit.util HttpSupport response

Introduction

In this page you can find the example usage for org.eclipse.jgit.util HttpSupport response.

Prototype

public static int response(java.net.HttpURLConnection c) throws IOException 

Source Link

Document

Get the HTTP response code from the request.

Usage

From source file:com.google.gerrit.pgm.init.LibraryDownloader.java

License:Apache License

private static InputStream openHttpStream(String urlStr) throws IOException {
    ProxySelector proxySelector = ProxySelector.getDefault();
    URL url = new URL(urlStr);
    Proxy proxy = HttpSupport.proxyFor(proxySelector, url);
    HttpURLConnection c = (HttpURLConnection) url.openConnection(proxy);

    switch (HttpSupport.response(c)) {
    case HttpURLConnection.HTTP_OK:
        return c.getInputStream();

    case HttpURLConnection.HTTP_NOT_FOUND:
        throw new FileNotFoundException(url.toString());

    default:/*from   w  w w  .  java  2 s . c om*/
        throw new IOException(url.toString() + ": " + HttpSupport.response(c) + " " + c.getResponseMessage());
    }
}

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 {/*from  w w  w.  ja va2  s .  c om*/
        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 {/*w  w w  .  ja  va2  s .  c  o  m*/
        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);
    }
}