Skip to content

Commit e9aa018

Browse files
committed
Add network access module
Affects: https://ebce-lyrasis.atlassian.net/browse/PP-3688
1 parent 9f521c6 commit e9aa018

22 files changed

Lines changed: 381 additions & 3 deletions

File tree

org.librarysimplified.http.api/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ dependencies {
33

44
compileOnly(libs.osgi.bundle.annotation)
55

6+
implementation(libs.io7m.jattribute.core)
67
implementation(libs.irradia.mime.api)
78
implementation(libs.jackson.core)
89
implementation(libs.joda.time)
910
implementation(libs.kotlin.reflect)
1011
implementation(libs.kotlin.stdlib)
12+
implementation(libs.slf4j)
1113
}

org.librarysimplified.http.api/src/main/java/org/librarysimplified/http/api/LSHTTPClientConfiguration.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ data class LSHTTPClientConfiguration(
3838
*/
3939

4040
val proxy: Proxy = Proxy.NO_PROXY,
41+
42+
/**
43+
* The network access interface.
44+
*/
45+
46+
val networkAccess: LSHTTPNetworkAccessType,
4147
)

org.librarysimplified.http.api/src/main/java/org/librarysimplified/http/api/LSHTTPClientType.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ interface LSHTTPClientType {
2929
*/
3030

3131
fun userAgent(): String
32+
33+
/**
34+
* A view of the current network access.
35+
*/
36+
37+
val networkAccess: LSHTTPNetworkAccessType
3238
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.librarysimplified.http.api
2+
3+
import com.io7m.jattribute.core.AttributeReadableType
4+
import com.io7m.jattribute.core.Attributes
5+
import org.slf4j.LoggerFactory
6+
7+
/**
8+
* The main network access service.
9+
*/
10+
11+
object LSHTTPNetworkAccess : LSHTTPNetworkAccessType {
12+
13+
private val logger =
14+
LoggerFactory.getLogger(LSHTTPNetworkAccess::class.java)
15+
16+
private val attributes =
17+
Attributes.create { x ->
18+
this.logger.debug("Attribute handler raised exception: ", x)
19+
}
20+
21+
private val wifiAvailableProp =
22+
this.attributes.withValue(false)
23+
24+
private val cellularAvailableProp =
25+
this.attributes.withValue(false)
26+
27+
private val wifiPermittedProp =
28+
this.attributes.withValue(true)
29+
30+
private val cellularPermittedProp =
31+
this.attributes.withValue(true)
32+
33+
override val wifiAvailable: AttributeReadableType<Boolean>
34+
get() = this.wifiAvailableProp
35+
36+
override val cellularAvailable: AttributeReadableType<Boolean>
37+
get() = this.cellularAvailableProp
38+
39+
override val wifiPermitted: AttributeReadableType<Boolean>
40+
get() = this.wifiPermittedProp
41+
42+
override val cellularPermitted: AttributeReadableType<Boolean>
43+
get() = this.cellularPermittedProp
44+
45+
override fun setWIFIPermitted(
46+
permitted: Boolean,
47+
) {
48+
this.wifiPermittedProp.set(permitted)
49+
}
50+
51+
override fun setCellularPermitted(
52+
permitted: Boolean,
53+
) {
54+
this.cellularPermittedProp.set(permitted)
55+
}
56+
57+
override fun setWIFIAvailable(
58+
available: Boolean,
59+
) {
60+
this.wifiAvailableProp.set(available)
61+
}
62+
63+
override fun setCellularAvailable(
64+
available: Boolean,
65+
) {
66+
this.cellularAvailableProp.set(available)
67+
}
68+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.librarysimplified.http.api
2+
3+
/**
4+
* An observable interface that indicates the availability of different network types.
5+
*/
6+
7+
interface LSHTTPNetworkAccessReadableType :
8+
LSHTTPNetworkAvailabilityReadableType,
9+
LSHTTPNetworkPolicyReadableType {
10+
/**
11+
* Determine if performing a download should be allowed given the current network availability
12+
* and permissions.
13+
*/
14+
15+
fun canDownload(): Boolean {
16+
if (this.wifiAvailable.get() && this.wifiPermitted.get()) {
17+
return true
18+
}
19+
if (this.cellularAvailable.get() && this.cellularPermitted.get()) {
20+
return true
21+
}
22+
return false
23+
}
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.librarysimplified.http.api
2+
3+
/**
4+
* An observable interface that indicates the availability of different network types.
5+
*/
6+
7+
interface LSHTTPNetworkAccessType :
8+
LSHTTPNetworkAccessReadableType,
9+
LSHTTPNetworkPolicyType,
10+
LSHTTPNetworkAvailabilityType
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.librarysimplified.http.api
2+
3+
import com.io7m.jattribute.core.AttributeReadableType
4+
5+
/**
6+
* An observable interface that indicates the availability of different network types.
7+
*/
8+
9+
interface LSHTTPNetworkAvailabilityReadableType {
10+
11+
val wifiAvailable: AttributeReadableType<Boolean>
12+
13+
val cellularAvailable: AttributeReadableType<Boolean>
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.librarysimplified.http.api
2+
3+
/**
4+
* An observable interface that indicates the availability of different network types.
5+
*/
6+
7+
interface LSHTTPNetworkAvailabilityType : LSHTTPNetworkAvailabilityReadableType {
8+
9+
fun setWIFIAvailable(
10+
available: Boolean,
11+
)
12+
13+
fun setCellularAvailable(
14+
available: Boolean,
15+
)
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.librarysimplified.http.api
2+
3+
import com.io7m.jattribute.core.AttributeReadableType
4+
5+
/**
6+
* An observable interface that indicates the availability of different network types.
7+
*/
8+
9+
interface LSHTTPNetworkPolicyReadableType {
10+
11+
val wifiPermitted: AttributeReadableType<Boolean>
12+
13+
val cellularPermitted: AttributeReadableType<Boolean>
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.librarysimplified.http.api
2+
3+
/**
4+
* An interface that indicates whether it is permitted to use different network types.
5+
*/
6+
7+
interface LSHTTPNetworkPolicyType : LSHTTPNetworkPolicyReadableType {
8+
9+
fun setWIFIPermitted(
10+
permitted: Boolean,
11+
)
12+
13+
fun setCellularPermitted(
14+
permitted: Boolean,
15+
)
16+
}

0 commit comments

Comments
 (0)