Example usage for com.squareup.okhttp HttpUrl resolve

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

Introduction

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

Prototype

public HttpUrl resolve(String link) 

Source Link

Document

Returns the URL that would be retrieved by following link from this URL.

Usage

From source file:com.granita.contacticloudsync.resource.DavResourceFinder.java

License:Open Source License

public void findResources(Service service) throws URISyntaxException {
    URI baseURI = serverInfo.getBaseURI();
    String domain = null;//  ww w  .  j a  v  a 2  s.  c o m

    HttpUrl principalUrl = null;
    Set<HttpUrl> homeSets = new HashSet<>();

    if (service == Service.CALDAV) {
        calendars.clear();
        taskLists.clear();
    } else if (service == Service.CARDDAV)
        addressbooks.clear();

    log.info("*** STARTING COLLECTION DISCOVERY FOR SERVICE " + service.name.toUpperCase(Locale.US) + "***");
    if ("http".equals(baseURI.getScheme()) || "https".equals(baseURI.getScheme())) {
        HttpUrl userURL = HttpUrl.get(baseURI);

        /* check whether:
        1. user-given URL is a calendar
        2. user-given URL has a calendar-home-set property (i.e. is a principal URL)
         */
        log.info(
                "Check whether user-given URL is a calendar collection and/or contains <calendar-home-set> and/or has <current-user-principal>");
        DavResource davBase = new DavResource(log, httpClient, userURL);
        try {
            if (service == Service.CALDAV) {
                davBase.propfind(0, CalendarHomeSet.NAME, SupportedCalendarComponentSet.NAME, ResourceType.NAME,
                        DisplayName.NAME, CalendarColor.NAME, CalendarDescription.NAME, CalendarTimezone.NAME,
                        CurrentUserPrivilegeSet.NAME, CurrentUserPrincipal.NAME);
                addIfCalendar(davBase);
            } else if (service == Service.CARDDAV) {
                davBase.propfind(0, AddressbookHomeSet.NAME, ResourceType.NAME, DisplayName.NAME,
                        AddressbookDescription.NAME, CurrentUserPrivilegeSet.NAME, CurrentUserPrincipal.NAME);
                addIfAddressBook(davBase);
            }
        } catch (IOException | HttpException | DavException e) {
            log.debug("PROPFIND on user-given URL failed", e);
        }

        if (service == Service.CALDAV) {
            CalendarHomeSet calendarHomeSet = (CalendarHomeSet) davBase.properties.get(CalendarHomeSet.NAME);
            if (calendarHomeSet != null) {
                log.info("Found <calendar-home-set> at user-given URL");
                for (String href : calendarHomeSet.hrefs) {
                    HttpUrl url = userURL.resolve(href);
                    if (url != null)
                        homeSets.add(url);
                }
            }
        } else if (service == Service.CARDDAV) {
            AddressbookHomeSet addressbookHomeSet = (AddressbookHomeSet) davBase.properties
                    .get(AddressbookHomeSet.NAME);
            if (addressbookHomeSet != null) {
                log.info("Found <addressbook-home-set> at user-given URL");
                for (String href : addressbookHomeSet.hrefs) {
                    HttpUrl url = userURL.resolve(href);
                    if (url != null)
                        homeSets.add(url);
                }
            }
        }

        /* When home sets haven already been found, skip further searching.
         * Otherwise (no home sets found), treat the user-given URL as "initial context path" for service discovery.
         *
         * Keep in mind that the CalDAV principal URL must not be the CardDAV principal URL! */
        if (homeSets.isEmpty())
            try {
                log.info("No home sets found, looking for <current-user-principal>");

                davBase.options();
                if ((service == Service.CALDAV && davBase.capabilities.contains("calendar-access"))
                        || (service == Service.CARDDAV && davBase.capabilities.contains("addressbook"))) {
                    CurrentUserPrincipal currentUserPrincipal = (CurrentUserPrincipal) davBase.properties
                            .get(CurrentUserPrincipal.NAME);
                    if (currentUserPrincipal != null && currentUserPrincipal.href != null)
                        principalUrl = davBase.location.resolve(currentUserPrincipal.href);
                }
            } catch (IOException | HttpException | DavException e) {
                log.debug("Couldn't find <current-user-principal> at user-given URL", e);
            }

        if (principalUrl == null)
            try {
                log.info("User-given URL doesn't contain <current-user-principal>, trying /.well-known/"
                        + service.name);
                principalUrl = getCurrentUserPrincipal(userURL.resolve("/.well-known/" + service.name));
            } catch (IOException | HttpException | DavException e) {
                log.debug("Couldn't determine <current-user-principal> from well-known " + service + " path",
                        e);
            }

        if (principalUrl == null)
            // still no principal URL, try service discovery with "domain" = user-given host name
            domain = baseURI.getHost();

    } else if ("mailto".equals(baseURI.getScheme())) {
        String mailbox = baseURI.getSchemeSpecificPart();

        // determine service FQDN
        int posAt = mailbox.lastIndexOf("@");
        if (posAt == -1)
            throw new URISyntaxException(mailbox, "Missing @ sign");

        domain = mailbox.substring(posAt + 1);
    }

    if (principalUrl == null && domain != null) {
        log.info("No principal URL yet, trying SRV/TXT records with domain " + domain);
        try {
            principalUrl = discoverPrincipalUrl(domain, service);
        } catch (IOException | HttpException | DavException e) {
            log.info("Couldn't find principal URL using service discovery");
        }
    }

    // principal URL has been found, get addressbook-home-set/calendar-home-set
    if (principalUrl != null) {
        log.info("Principal URL=" + principalUrl + ", getting <calendar-home-set>");
        try {
            DavResource principal = new DavResource(log, httpClient, principalUrl);

            if (service == Service.CALDAV) {
                principal.propfind(0, CalendarHomeSet.NAME);
                CalendarHomeSet calendarHomeSet = (CalendarHomeSet) principal.properties
                        .get(CalendarHomeSet.NAME);
                if (calendarHomeSet != null) {
                    log.info("Found <calendar-home-set> at principal URL");
                    for (String href : calendarHomeSet.hrefs) {
                        HttpUrl url = principal.location.resolve(href);
                        if (url != null)
                            homeSets.add(url);
                    }
                }
            } else if (service == Service.CARDDAV) {
                principal.propfind(0, AddressbookHomeSet.NAME);
                AddressbookHomeSet addressbookHomeSet = (AddressbookHomeSet) principal.properties
                        .get(AddressbookHomeSet.NAME);
                if (addressbookHomeSet != null) {
                    log.info("Found <addressbook-home-set> at principal URL");
                    for (String href : addressbookHomeSet.hrefs) {
                        HttpUrl url = principal.location.resolve(href);
                        if (url != null)
                            homeSets.add(url);
                    }
                }
            }

        } catch (IOException | HttpException | DavException e) {
            log.debug("PROPFIND on " + principalUrl + " failed", e);
        }
    }

    // now query all home sets
    for (HttpUrl url : homeSets)
        if (service == Service.CALDAV)
            try {
                log.info("Listing calendar collections in home set " + url);
                DavResource homeSet = new DavResource(log, httpClient, url);
                homeSet.propfind(1, SupportedCalendarComponentSet.NAME, ResourceType.NAME, DisplayName.NAME,
                        CurrentUserPrivilegeSet.NAME, CalendarColor.NAME, CalendarDescription.NAME,
                        CalendarTimezone.NAME);

                // home set should not be a calendar, but some servers have only one calendar and it's the home set
                addIfCalendar(homeSet);

                // members of the home set can be calendars, too
                for (DavResource member : homeSet.members)
                    addIfCalendar(member);
            } catch (IOException | HttpException | DavException e) {
                log.debug("PROPFIND on " + url + " failed", e);
            }
        else if (service == Service.CARDDAV)
            try {
                log.info("Listing address books in home set " + url);
                DavResource homeSet = new DavResource(log, httpClient, url);
                homeSet.propfind(1, ResourceType.NAME, DisplayName.NAME, CurrentUserPrivilegeSet.NAME,
                        AddressbookDescription.NAME);

                // home set should not be an address book, but some servers have only one address book and it's the home set
                addIfAddressBook(homeSet);

                // members of the home set can be calendars, too
                for (DavResource member : homeSet.members)
                    addIfAddressBook(member);
            } catch (IOException | HttpException | DavException e) {
                log.debug("PROPFIND on " + url + " failed", e);
            }

    if (service == Service.CALDAV) {
        serverInfo.setCalendars(calendars.values().toArray(new ServerInfo.ResourceInfo[calendars.size()]));
        serverInfo.setTaskLists(taskLists.values().toArray(new ServerInfo.ResourceInfo[taskLists.size()]));
    } else if (service == Service.CARDDAV)
        serverInfo.setAddressBooks(
                addressbooks.values().toArray(new ServerInfo.ResourceInfo[addressbooks.size()]));
}

From source file:com.granita.contacticloudsync.resource.DavResourceFinder.java

License:Open Source License

/**
 * Queries a given URL for current-user-principal
 * @param url   URL to query with PROPFIND (Depth: 0)
 * @return      current-user-principal URL, or null if none
 *//*  www  .java  2  s  . c  om*/
protected HttpUrl getCurrentUserPrincipal(HttpUrl url) throws IOException, HttpException, DavException {
    DavResource dav = new DavResource(log, httpClient, url);
    dav.propfind(0, CurrentUserPrincipal.NAME);
    CurrentUserPrincipal currentUserPrincipal = (CurrentUserPrincipal) dav.properties
            .get(CurrentUserPrincipal.NAME);
    if (currentUserPrincipal != null && currentUserPrincipal.href != null) {
        HttpUrl principal = url.resolve(currentUserPrincipal.href);
        if (principal != null) {
            log.info("Found current-user-principal: " + principal);
            return principal;
        }
    }
    return null;
}

From source file:de.luhmer.owncloudnewsreader.reader.owncloud.OwnCloudReaderMethods.java

License:Open Source License

public static String GetVersionNumber(HttpUrl basePath) throws Exception {
    //Try APIv2/*from   w ww  .j  ava 2s . c  o m*/
    try {
        HttpUrl requestUrl = basePath.resolve(OwnCloudConstants.ROOT_PATH_APIv2).newBuilder()
                .addPathSegment(OwnCloudConstants.VERSION_PATH).build();

        InputStream is = HttpJsonRequest.getInstance().PerformJsonRequest(requestUrl);

        try {
            GetVersion_v2 gv = new GetVersion_v2();
            readJsonStreamSimple(is, gv);
            return gv.getVersion();
        } finally {
            is.close();
        }
    } catch (Exception ex) {
        HttpUrl requestUrl = basePath.resolve(OwnCloudConstants.ROOT_PATH_APIv1).newBuilder()
                .addPathSegment(OwnCloudConstants.VERSION_PATH).addQueryParameter("format", "json").build();

        InputStream is = HttpJsonRequest.getInstance().PerformJsonRequest(requestUrl);

        try {
            GetVersion_v1 gv = new GetVersion_v1();
            readJsonStreamSimple(is, gv);
            return gv.getVersion();
        } finally {
            is.close();
        }
    }
}

From source file:jonas.tool.saveForOffline.FaviconFetcher.java

License:Open Source License

public List<String> getPotentialFaviconUrls(Document document) {
    List<String> iconUrls = new ArrayList<String>();
    HttpUrl base = HttpUrl.parse(document.baseUri());

    for (String cssQuery : htmlIconCssQueries) {
        for (Element e : document.select(cssQuery)) {
            if (e.hasAttr("href")) {
                iconUrls.add(e.attr("href"));
            }/* www . ja  v  a 2  s. com*/

            if (e.hasAttr("content")) {
                iconUrls.add(e.attr("content"));
            }

            if (e.hasAttr("src")) {
                iconUrls.add(e.attr("src"));
            }
        }
    }

    for (String path : hardcodedIconPaths) {
        HttpUrl url = HttpUrl.parse("http://" + HttpUrl.parse(document.baseUri()).host() + path);
        iconUrls.add(url.toString());
    }

    for (ListIterator<String> i = iconUrls.listIterator(); i.hasNext();) {
        HttpUrl httpUrl = base.resolve(i.next());
        if (httpUrl != null) {
            i.set(httpUrl.toString());
        } else {
            i.remove();
        }
    }

    return iconUrls;

}