Skip to content

Commit 2b41498

Browse files
authored
Directly use java.util.Logger instead of keeping an instance in Dav4jvm object (#50)
1 parent e5450fa commit 2b41498

23 files changed

+139
-113
lines changed

src/main/kotlin/at/bitfire/dav4jvm/BasicDigestAuthHandler.kt

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import java.util.LinkedList
2121
import java.util.Locale
2222
import java.util.UUID
2323
import java.util.concurrent.atomic.AtomicInteger
24+
import java.util.logging.Logger
2425

2526
/**
2627
* Handler to manage authentication against a given service (may be limited to one domain).
@@ -33,13 +34,13 @@ import java.util.concurrent.atomic.AtomicInteger
3334
* Usage: Set as authenticator *and* as network interceptor.
3435
*/
3536
class BasicDigestAuthHandler(
36-
/** Authenticate only against hosts ending with this domain (may be null, which means no restriction) */
37-
val domain: String?,
37+
/** Authenticate only against hosts ending with this domain (may be null, which means no restriction) */
38+
val domain: String?,
3839

39-
val username: String,
40-
val password: String,
40+
val username: String,
41+
val password: String,
4142

42-
val insecurePreemptive: Boolean = false
43+
val insecurePreemptive: Boolean = false
4344
): Authenticator, Interceptor {
4445

4546
companion object {
@@ -64,13 +65,15 @@ class BasicDigestAuthHandler(
6465
// cached authentication schemes
6566
private var basicAuth: Challenge? = null
6667
private var digestAuth: Challenge? = null
68+
69+
private val logger = Logger.getLogger(javaClass.name)
6770

6871

6972
fun authenticateRequest(request: Request, response: Response?): Request? {
7073
domain?.let {
7174
val host = request.url.host
7275
if (!domain.equals(UrlUtils.hostToDomain(host), true)) {
73-
Dav4jvm.log.warning("Not authenticating against $host because it doesn't belong to $domain")
76+
logger.warning("Not authenticating against $host because it doesn't belong to $domain")
7477
return null
7578
}
7679
}
@@ -79,7 +82,7 @@ class BasicDigestAuthHandler(
7982
// we're not processing a 401 response
8083

8184
if (basicAuth == null && digestAuth == null && (request.isHttps || insecurePreemptive)) {
82-
Dav4jvm.log.fine("Trying Basic auth preemptively")
85+
logger.fine("Trying Basic auth preemptively")
8386
basicAuth = Challenge("Basic", "")
8487
}
8588

@@ -92,15 +95,15 @@ class BasicDigestAuthHandler(
9295
when {
9396
"Basic".equals(challenge.scheme, true) -> {
9497
basicAuth?.let {
95-
Dav4jvm.log.warning("Basic credentials didn't work last time -> aborting")
98+
logger.warning("Basic credentials didn't work last time -> aborting")
9699
basicAuth = null
97100
return null
98101
}
99102
newBasicAuth = challenge
100103
}
101104
"Digest".equals(challenge.scheme, true) -> {
102105
if (digestAuth != null && !"true".equals(challenge.authParams["stale"], true)) {
103-
Dav4jvm.log.warning("Digest credentials didn't work last time and server nonce has not expired -> aborting")
106+
logger.warning("Digest credentials didn't work last time and server nonce has not expired -> aborting")
104107
digestAuth = null
105108
return null
106109
}
@@ -115,12 +118,12 @@ class BasicDigestAuthHandler(
115118
// we MUST prefer Digest auth [https://tools.ietf.org/html/rfc2617#section-4.6]
116119
when {
117120
digestAuth != null -> {
118-
Dav4jvm.log.fine("Adding Digest authorization request for ${request.url}")
121+
logger.fine("Adding Digest authorization request for ${request.url}")
119122
return digestRequest(request, digestAuth)
120123
}
121124

122125
basicAuth != null -> {
123-
Dav4jvm.log.fine("Adding Basic authorization header for ${request.url}")
126+
logger.fine("Adding Basic authorization header for ${request.url}")
124127

125128
/* In RFC 2617 (obsolete), there was no encoding for credentials defined, although
126129
one can interpret it as "use ISO-8859-1 encoding". This has been clarified by RFC 7617,
@@ -133,7 +136,7 @@ class BasicDigestAuthHandler(
133136
}
134137

135138
response != null ->
136-
Dav4jvm.log.warning("No supported authentication scheme")
139+
logger.warning("No supported authentication scheme")
137140
}
138141

139142
return null
@@ -158,13 +161,13 @@ class BasicDigestAuthHandler(
158161
if (realm != null)
159162
params.add("realm=${quotedString(realm)}")
160163
else {
161-
Dav4jvm.log.warning("No realm provided, aborting Digest auth")
164+
logger.warning("No realm provided, aborting Digest auth")
162165
return null
163166
}
164167
if (nonce != null)
165168
params.add("nonce=${quotedString(nonce)}")
166169
else {
167-
Dav4jvm.log.warning("No nonce provided, aborting Digest auth")
170+
logger.warning("No nonce provided, aborting Digest auth")
168171
return null
169172
}
170173
if (opaque != null)
@@ -193,7 +196,7 @@ class BasicDigestAuthHandler(
193196
else ->
194197
null
195198
}
196-
Dav4jvm.log.finer("A1=$a1")
199+
logger.finer("A1=$a1")
197200

198201
val a2: String? = when (qop) {
199202
Protection.Auth ->
@@ -203,18 +206,18 @@ class BasicDigestAuthHandler(
203206
val body = request.body
204207
"$method:$digestURI:" + (if (body != null) h(body) else h(""))
205208
} catch(e: IOException) {
206-
Dav4jvm.log.warning("Couldn't get entity-body for hash calculation")
209+
logger.warning("Couldn't get entity-body for hash calculation")
207210
null
208211
}
209212
}
210213
}
211-
Dav4jvm.log.finer("A2=$a2")
214+
logger.finer("A2=$a2")
212215

213216
if (a1 != null && a2 != null)
214217
response = kd(h(a1), "$nonce:$ncValue:$clientNonce:${qop.qop}:${h(a2)}")
215218

216219
} else {
217-
Dav4jvm.log.finer("Using legacy Digest auth")
220+
logger.finer("Using legacy Digest auth")
218221

219222
// legacy (backwards compatibility with RFC 2069)
220223
if (algorithm == Algorithm.MD5) {
@@ -235,7 +238,7 @@ class BasicDigestAuthHandler(
235238

236239

237240
private enum class Algorithm(
238-
val algorithm: String
241+
val algorithm: String
239242
) {
240243
MD5("MD5"),
241244
MD5_SESSION("MD5-sess");
@@ -248,7 +251,8 @@ class BasicDigestAuthHandler(
248251
MD5_SESSION.algorithm.equals(paramValue, true) ->
249252
MD5_SESSION
250253
else -> {
251-
Dav4jvm.log.warning("Ignoring unknown hash algorithm: $paramValue")
254+
val logger = Logger.getLogger(Algorithm::javaClass.name)
255+
logger.warning("Ignoring unknown hash algorithm: $paramValue")
252256
null
253257
}
254258
}

src/main/kotlin/at/bitfire/dav4jvm/Dav4jvm.kt

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/main/kotlin/at/bitfire/dav4jvm/DavAddressBook.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import java.io.IOException
2323
import java.io.StringWriter
2424
import java.util.logging.Logger
2525

26+
@Suppress("unused")
2627
class DavAddressBook @JvmOverloads constructor(
27-
httpClient: OkHttpClient,
28-
location: HttpUrl,
29-
log: Logger = Dav4jvm.log
30-
): DavCollection(httpClient, location, log) {
28+
httpClient: OkHttpClient,
29+
location: HttpUrl,
30+
logger: Logger = Logger.getLogger(DavAddressBook::javaClass.name)
31+
): DavCollection(httpClient, location, logger) {
3132

3233
companion object {
3334
val MIME_JCARD = "application/vcard+json".toMediaType()

src/main/kotlin/at/bitfire/dav4jvm/DavCalendar.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ import java.time.format.DateTimeFormatter
2929
import java.util.Locale
3030
import java.util.logging.Logger
3131

32+
@Suppress("unused")
3233
class DavCalendar @JvmOverloads constructor(
33-
httpClient: OkHttpClient,
34-
location: HttpUrl,
35-
log: Logger = Dav4jvm.log
36-
): DavCollection(httpClient, location, log) {
34+
httpClient: OkHttpClient,
35+
location: HttpUrl,
36+
logger: Logger = Logger.getLogger(DavCalendar::javaClass.name)
37+
): DavCollection(httpClient, location, logger) {
3738

3839
companion object {
3940
val MIME_ICALENDAR = "text/calendar".toMediaType()

src/main/kotlin/at/bitfire/dav4jvm/DavCollection.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ import at.bitfire.dav4jvm.exception.DavException
1111
import at.bitfire.dav4jvm.exception.HttpException
1212
import at.bitfire.dav4jvm.property.webdav.NS_WEBDAV
1313
import at.bitfire.dav4jvm.property.webdav.SyncToken
14-
import java.io.StringWriter
15-
import java.util.logging.Logger
1614
import okhttp3.HttpUrl
1715
import okhttp3.OkHttpClient
1816
import okhttp3.Request
1917
import okhttp3.RequestBody.Companion.toRequestBody
18+
import java.io.StringWriter
19+
import java.util.logging.Logger
2020

2121
/**
2222
* Represents a WebDAV collection.
2323
*/
2424
open class DavCollection @JvmOverloads constructor(
25-
httpClient: OkHttpClient,
26-
location: HttpUrl,
27-
log: Logger = Dav4jvm.log
28-
): DavResource(httpClient, location, log) {
25+
httpClient: OkHttpClient,
26+
location: HttpUrl,
27+
logger: Logger = Logger.getLogger(DavCollection::class.java.name)
28+
): DavResource(httpClient, location, logger) {
2929

3030
companion object {
3131
val SYNC_COLLECTION = Property.Name(NS_WEBDAV, "sync-collection")

src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ import at.bitfire.dav4jvm.property.caldav.NS_CALDAV
2020
import at.bitfire.dav4jvm.property.carddav.NS_CARDDAV
2121
import at.bitfire.dav4jvm.property.webdav.NS_WEBDAV
2222
import at.bitfire.dav4jvm.property.webdav.SyncToken
23-
import java.io.EOFException
24-
import java.io.IOException
25-
import java.io.Reader
26-
import java.io.StringWriter
27-
import java.net.HttpURLConnection
28-
import java.util.logging.Level
29-
import java.util.logging.Logger
3023
import okhttp3.Headers
3124
import okhttp3.HttpUrl
3225
import okhttp3.MediaType.Companion.toMediaType
@@ -37,6 +30,13 @@ import okhttp3.RequestBody.Companion.toRequestBody
3730
import okhttp3.Response
3831
import org.xmlpull.v1.XmlPullParser
3932
import org.xmlpull.v1.XmlPullParserException
33+
import java.io.EOFException
34+
import java.io.IOException
35+
import java.io.Reader
36+
import java.io.StringWriter
37+
import java.net.HttpURLConnection
38+
import java.util.logging.Level
39+
import java.util.logging.Logger
4040
import at.bitfire.dav4jvm.Response as DavResponse
4141

4242
/**
@@ -52,12 +52,12 @@ import at.bitfire.dav4jvm.Response as DavResponse
5252
*
5353
* @param httpClient [OkHttpClient] to access this object (must not follow redirects)
5454
* @param location location of the WebDAV resource
55-
* @param log will be used for logging
55+
* @param logger will be used for logging
5656
*/
5757
open class DavResource @JvmOverloads constructor(
58-
val httpClient: OkHttpClient,
59-
location: HttpUrl,
60-
val log: Logger = Dav4jvm.log
58+
val httpClient: OkHttpClient,
59+
location: HttpUrl,
60+
val logger: Logger = Logger.getLogger(DavResource::class.java.name)
6161
) {
6262

6363
companion object {
@@ -680,7 +680,7 @@ open class DavResource @JvmOverloads constructor(
680680
response.use {
681681
val target = it.header("Location")?.let { location.resolve(it) }
682682
if (target != null) {
683-
log.fine("Redirected, new location = $target")
683+
logger.fine("Redirected, new location = $target")
684684

685685
if (location.isHttps && !target.isHttps)
686686
throw DavException("Received redirect from HTTPS to HTTP")
@@ -718,18 +718,18 @@ open class DavResource @JvmOverloads constructor(
718718
val firstBytes = ByteArray(XML_SIGNATURE.size)
719719
body.source().peek().readFully(firstBytes)
720720
if (XML_SIGNATURE.contentEquals(firstBytes)) {
721-
Dav4jvm.log.warning("Received 207 Multi-Status that seems to be XML but has MIME type $mimeType")
721+
logger.warning("Received 207 Multi-Status that seems to be XML but has MIME type $mimeType")
722722

723723
// response is OK, return and do not throw Exception below
724724
return
725725
}
726726
} catch (e: Exception) {
727-
Dav4jvm.log.log(Level.WARNING, "Couldn't scan for XML signature", e)
727+
logger.log(Level.WARNING, "Couldn't scan for XML signature", e)
728728
}
729729

730730
throw DavException("Received non-XML 207 Multi-Status", httpResponse = response)
731731
}
732-
} ?: log.warning("Received 207 Multi-Status without Content-Type, assuming XML")
732+
} ?: logger.warning("Received 207 Multi-Status without Content-Type, assuming XML")
733733
}
734734

735735

src/main/kotlin/at/bitfire/dav4jvm/HttpUtils.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import java.time.ZonedDateTime
1515
import java.time.format.DateTimeFormatter
1616
import java.time.format.DateTimeParseException
1717
import java.util.Locale
18+
import java.util.logging.Logger
1819

1920
object HttpUtils {
2021

@@ -24,6 +25,10 @@ object HttpUtils {
2425
private const val httpDateFormatStr = "EEE, dd MMM yyyy HH:mm:ss ZZZZ"
2526
private val httpDateFormat = DateTimeFormatter.ofPattern(httpDateFormatStr, Locale.US)
2627

28+
private val logger
29+
get() = Logger.getLogger(javaClass.name)
30+
31+
2732
/**
2833
* Gets the resource name (the last segment of the path) from an URL.
2934
* Empty if the resource is the base directory.
@@ -91,7 +96,7 @@ object HttpUtils {
9196
}
9297

9398
// no success in parsing
94-
Dav4jvm.log.warning("Couldn't parse HTTP date: $dateStr, ignoring")
99+
logger.warning("Couldn't parse HTTP date: $dateStr, ignoring")
95100
return null
96101
}
97102

src/main/kotlin/at/bitfire/dav4jvm/Property.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
package at.bitfire.dav4jvm
88

9-
import at.bitfire.dav4jvm.Dav4jvm.log
109
import at.bitfire.dav4jvm.exception.InvalidPropertyException
1110
import org.xmlpull.v1.XmlPullParser
1211
import java.io.Serializable
1312
import java.util.LinkedList
1413
import java.util.logging.Level
14+
import java.util.logging.Logger
1515

1616
/**
1717
* Represents a WebDAV property.
@@ -33,6 +33,8 @@ interface Property {
3333
companion object {
3434

3535
fun parse(parser: XmlPullParser): List<Property> {
36+
val logger = Logger.getLogger(Property::javaClass.name)
37+
3638
// <!ELEMENT prop ANY >
3739
val depth = parser.depth
3840
val properties = LinkedList<Property>()
@@ -50,9 +52,9 @@ interface Property {
5052
if (property != null) {
5153
properties.add(property)
5254
} else
53-
log.fine("Ignoring unknown property $name")
55+
logger.fine("Ignoring unknown property $name")
5456
} catch (e: InvalidPropertyException) {
55-
log.log(Level.WARNING, "Ignoring invalid property", e)
57+
logger.log(Level.WARNING, "Ignoring invalid property", e)
5658
}
5759
}
5860

0 commit comments

Comments
 (0)