Example usage for com.squareup.okhttp HttpUrl scheme

List of usage examples for com.squareup.okhttp HttpUrl scheme

Introduction

In this page you can find the example usage for com.squareup.okhttp HttpUrl scheme.

Prototype

String scheme

To view the source code for com.squareup.okhttp HttpUrl scheme.

Click Source Link

Document

Either "http" or "https".

Usage

From source file:at.bitfire.dav4android.DavResource.java

License:Open Source License

private void parseMultiStatus_Response(XmlPullParser parser)
        throws IOException, XmlPullParserException, HttpException, UnsupportedDavException {
    /* <!ELEMENT response (href, ((href*, status)|(propstat+)),
                               error?, responsedescription? , location?) > */
    final int depth = parser.getDepth();

    HttpUrl href = null;
    StatusLine status = null;/*w ww  .  j  a va  2 s.c  o  m*/
    PropertyCollection properties = new PropertyCollection();

    int eventType = parser.getEventType();
    while (!(eventType == XmlPullParser.END_TAG && parser.getDepth() == depth)) {
        if (eventType == XmlPullParser.START_TAG && parser.getDepth() == depth + 1) {
            String ns = parser.getNamespace(), name = parser.getName();
            if (XmlUtils.NS_WEBDAV.equals(ns))
                switch (name) {
                case "href":
                    String sHref = parser.nextText();
                    if (!sHref.startsWith("/")) {
                        /* According to RFC 4918 8.3 URL Handling, only absolute paths are allowed as relative
                           URLs. However, some servers reply with relative paths. */
                        int firstColon = sHref.indexOf(':');
                        if (firstColon != -1) {
                            /* There are some servers which return not only relative paths, but relative paths like "a:b.vcf",
                               which would be interpreted as scheme: "a", scheme-specific part: "b.vcf" normally.
                               For maximum compatibility, we prefix all relative paths which contain ":" (but not "://"),
                               with "./" to allow resolving by HttpUrl. */
                            boolean hierarchical = false;
                            try {
                                if ("://".equals(sHref.substring(firstColon, firstColon + 3)))
                                    hierarchical = true;
                            } catch (IndexOutOfBoundsException e) {
                                // no "://"
                            }
                            if (!hierarchical)
                                sHref = "./" + sHref;
                        }
                    }
                    href = location.resolve(sHref);
                    break;
                case "status":
                    try {
                        status = StatusLine.parse(parser.nextText());
                    } catch (ProtocolException e) {
                        log.warn("Invalid status line, treating as 500 Server Error");
                        status = new StatusLine(Protocol.HTTP_1_1, 500, "Invalid status line");
                    }
                    break;
                case "propstat":
                    PropertyCollection prop = parseMultiStatus_PropStat(parser);
                    if (prop != null)
                        properties.merge(prop, false);
                    break;
                case "location":
                    throw new UnsupportedDavException("Redirected child resources are not supported yet");
                }
        }
        eventType = parser.next();
    }

    if (href == null) {
        log.warn("Ignoring <response> without valid <href>");
        return;
    }

    // if we know this resource is a collection, make sure href has a trailing slash (for clarity and resolving relative paths)
    ResourceType type = (ResourceType) properties.get(ResourceType.NAME);
    if (type != null && type.types.contains(ResourceType.COLLECTION))
        href = UrlUtils.withTrailingSlash(href);

    log.debug("Received <response> for " + href + ", status: " + status + ", properties: " + properties);

    if (status != null)
        // treat an HTTP error of a single response (i.e. requested resource or a member) like an HTTP error of the requested resource
        checkStatus(status);

    // Which resource does this <response> represent?
    DavResource target = null;
    if (UrlUtils.equals(UrlUtils.omitTrailingSlash(href), UrlUtils.omitTrailingSlash(location))) {
        // it's about ourselves
        target = this;
    } else if (location.scheme().equals(href.scheme()) && location.host().equals(href.host())
            && location.port() == href.port()) {
        List<String> locationSegments = location.pathSegments(), hrefSegments = href.pathSegments();

        // don't compare trailing slash segment ("")
        int nBasePathSegments = locationSegments.size();
        if ("".equals(locationSegments.get(nBasePathSegments - 1)))
            nBasePathSegments--;

        /* example:   locationSegments  = [ "davCollection", "" ]
                  nBasePathSegments = 1
                  hrefSegments      = [ "davCollection", "aMember" ]
        */

        if (hrefSegments.size() > nBasePathSegments) {
            boolean sameBasePath = true;
            for (int i = 0; i < nBasePathSegments; i++) {
                if (!locationSegments.get(i).equals(hrefSegments.get(i))) {
                    sameBasePath = false;
                    break;
                }
            }

            if (sameBasePath)
                members.add(target = new DavResource(log, httpClient, href));
        }
    }

    // set properties for target
    if (target != null)
        target.properties.merge(properties, true);
    else
        log.warn("Received <response> not for self and not for member resource");
}

From source file:com.baidu.oped.apm.plugin.okhttp.interceptor.HttpEngineSendRequestMethodInterceptor.java

License:Apache License

private String getDestinationId(HttpUrl httpUrl) {
    if (httpUrl == null || httpUrl.host() == null) {
        return "UnknownHttpClient";
    }/*from   w  ww . j  av  a2  s . co m*/
    if (httpUrl.port() == HttpUrl.defaultPort(httpUrl.scheme())) {
        return httpUrl.host();
    }
    final StringBuilder sb = new StringBuilder();
    sb.append(httpUrl.host());
    sb.append(':');
    sb.append(httpUrl.port());
    return sb.toString();
}

From source file:com.codenote.tikal.example.client.HttpLoggingInterceptor.java

License:Apache License

private static String requestPath(HttpUrl url) {
    String domain = url.scheme() + "://" + url.host();
    String path = url.encodedPath();
    String query = url.encodedQuery();
    return query != null ? domain.concat(path + '?' + query) : domain.concat(path);
}

From source file:com.navercorp.pinpoint.plugin.okhttp.interceptor.RequestBuilderBuildMethodInterceptor.java

License:Apache License

private String getDestinationId(HttpUrl httpUrl) {
    if (httpUrl == null || httpUrl.host() == null) {
        return "UnknownHttpClient";
    }//from   w  w w. jav a2  s.  c  o  m
    if (httpUrl.port() <= 0 || httpUrl.port() == HttpUrl.defaultPort(httpUrl.scheme())) {
        return httpUrl.host();
    }
    final StringBuilder sb = new StringBuilder();
    sb.append(httpUrl.host());
    sb.append(':');
    sb.append(httpUrl.port());
    return sb.toString();
}

From source file:com.navercorp.pinpoint.plugin.okhttp.v2.interceptor.RequestBuilderBuildMethodInterceptor.java

License:Apache License

private String getDestinationId(HttpUrl httpUrl) {
    if (httpUrl == null || httpUrl.host() == null) {
        return "UnknownHttpClient";
    }/*from ww w . j  a  v a 2 s.  c  o  m*/
    final int port = EndPointUtils.getPort(httpUrl.port(), HttpUrl.defaultPort(httpUrl.scheme()));
    return HostAndPort.toHostAndPortString(httpUrl.host(), port);
}

From source file:net.yatomiya.e4.util.HttpUtils.java

License:Open Source License

public static String getHostUrl(String url) {
    try {//from www.  j a  va 2s. c  o m
        HttpUrl hurl = HttpUrl.parse(url);
        return hurl.scheme() + "://" + hurl.host();
    } catch (Throwable e) {
        return null;
    }
}