ServiceUnavailableException.kt 1.5 KB
Newer Older
Ricki Hirner's avatar
Ricki Hirner committed
1
/*
2
 * Copyright © Ricki Hirner (bitfire web engineering).
Ricki Hirner's avatar
Ricki Hirner committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

package at.bitfire.dav4android.exception

import at.bitfire.dav4android.Constants
import okhttp3.Response
import okhttp3.internal.http.HttpDate
import java.net.HttpURLConnection
import java.util.*

class ServiceUnavailableException: HttpException {

    var retryAfter: Date? = null

    constructor(message: String?): super(HttpURLConnection.HTTP_UNAVAILABLE, message) {
        retryAfter = null
    }

    constructor(response: Response): super(response) {
        // Retry-After  = "Retry-After" ":" ( HTTP-date | delta-seconds )
        // HTTP-date    = rfc1123-date | rfc850-date | asctime-date

        response.header("Retry-After")?.let {
            retryAfter = HttpDate.parse(it) ?:
                    // not a HTTP-date, must be delta-seconds
                    try {
                        val seconds = Integer.parseInt(it)

                        val cal = Calendar.getInstance()
                        cal.add(Calendar.SECOND, seconds)
                        cal.time

                    } catch (ignored: NumberFormatException) {
                        Constants.log.warning("Received Retry-After which was not a HTTP-date nor delta-seconds")
                        null
                    }
        }
    }

}