List of usage examples for com.squareup.okhttp HttpUrl pathSegments
List pathSegments
To view the source code for com.squareup.okhttp HttpUrl pathSegments.
Click Source Link
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;//from w w w. j av a2 s.c om 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:io.instacount.client.decoders.InstacountJacksonDecoder.java
License:Apache License
/** * Helper to construct an optionally present instance of {@link CounterLocationInfo} from the Feign {@link Response} * .//w w w .j ava2s.c o m * * @param response * @return */ private Optional<CounterLocationInfo> constructCounterLocationInfo(final Response response) { final Optional<CounterLocationInfo> optCounterInfo; final Collection<String> locationHeaders = response.headers().get("Location"); if (!locationHeaders.isEmpty()) { final String location = locationHeaders.iterator().next(); final HttpUrl httpUrl = HttpUrl.parse(location); Preconditions.checkNotNull(httpUrl); final String counterName = httpUrl.pathSegments().get(1); final CounterLocationInfo counterLocationHeaderInfo = new CounterLocationInfo(location, counterName); optCounterInfo = Optional.fromNullable(counterLocationHeaderInfo); } else { optCounterInfo = Optional.absent(); } return optCounterInfo; }
From source file:net.yatomiya.e4.util.HttpUtils.java
License:Open Source License
public static String getPathName(HttpUrl url) { List<String> segments = url.pathSegments(); if (segments.size() >= 1) { return segments.get(segments.size() - 1); }//from w w w . j ava2 s .co m return ""; }
From source file:org.mariotaku.twidere.util.OAuthPasswordAuthenticator.java
License:Open Source License
public OAuthPasswordAuthenticator(final TwitterOAuth oauth, final LoginVerificationCallback loginVerificationCallback, final String userAgent) { final RestClient restClient = RestAPIFactory.getRestClient(oauth); this.oauth = oauth; this.client = (OkHttpRestClient) restClient.getRestClient(); final OkHttpClient okhttp = client.getClient(); okhttp.setCookieHandler(new CookieManager()); okhttp.networkInterceptors().add(new Interceptor() { @Override//from ww w .j ava 2 s .co m public Response intercept(Chain chain) throws IOException { final Response response = chain.proceed(chain.request()); if (!response.isRedirect()) { return response; } final String location = response.header("Location"); final Response.Builder builder = response.newBuilder(); if (!TextUtils.isEmpty(location) && !endpoint.checkEndpoint(location)) { final HttpUrl originalLocation = HttpUrl .get(URI.create("https://api.twitter.com/").resolve(location)); final HttpUrl.Builder locationBuilder = HttpUrl.parse(endpoint.getUrl()).newBuilder(); for (String pathSegments : originalLocation.pathSegments()) { locationBuilder.addPathSegment(pathSegments); } for (int i = 0, j = originalLocation.querySize(); i < j; i++) { final String name = originalLocation.queryParameterName(i); final String value = originalLocation.queryParameterValue(i); locationBuilder.addQueryParameter(name, value); } final String encodedFragment = originalLocation.encodedFragment(); if (encodedFragment != null) { locationBuilder.encodedFragment(encodedFragment); } final HttpUrl newLocation = locationBuilder.build(); builder.header("Location", newLocation.toString()); } return builder.build(); } }); this.endpoint = restClient.getEndpoint(); this.loginVerificationCallback = loginVerificationCallback; this.userAgent = userAgent; }