List of usage examples for com.squareup.okhttp.internal.http StatusLine parse
public static StatusLine parse(String statusLine) throws IOException
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;//from w ww .java 2 s.c o m StatusLine status = null; 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:at.bitfire.dav4android.DavResource.java
License:Open Source License
private PropertyCollection parseMultiStatus_PropStat(XmlPullParser parser) throws IOException, XmlPullParserException { // <!ELEMENT propstat (prop, status, error?, responsedescription?) > final int depth = parser.getDepth(); StatusLine status = null;/*from w w w . ja v a 2 s . c o m*/ PropertyCollection prop = null; 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 "prop": prop = parseMultiStatus_Prop(parser); 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"); } } } eventType = parser.next(); } if (status != null && status.code / 100 != 2) // not successful, null out property values so that they can be removed when merging in parseMultiStatus_Response prop.nullAllValues(); return prop; }