@@ -38,14 +38,14 @@ import org.owntracks.android.support.interfaces.ConfigurationIncompleteException
3838import timber.log.Timber
3939
4040class HttpMessageProcessorEndpoint (
41- messageProcessor : MessageProcessor ,
42- private val parser : Parser ,
43- private val preferences : Preferences ,
44- private val applicationContext : Context ,
45- private val endpointStateRepo : EndpointStateRepo ,
46- private val caKeyStore : KeyStore ,
47- @ ApplicationScope private val scope : CoroutineScope ,
48- @ CoroutineScopes.IoDispatcher private val ioDispatcher : CoroutineDispatcher
41+ messageProcessor : MessageProcessor ,
42+ private val parser : Parser ,
43+ private val preferences : Preferences ,
44+ private val applicationContext : Context ,
45+ private val endpointStateRepo : EndpointStateRepo ,
46+ private val caKeyStore : KeyStore ,
47+ @param: ApplicationScope private val scope : CoroutineScope ,
48+ @param: CoroutineScopes.IoDispatcher private val ioDispatcher : CoroutineDispatcher
4949) : MessageProcessorEndpoint(messageProcessor), Preferences.OnPreferenceChangeListener {
5050 override val modeId: ConnectionMode = ConnectionMode .HTTP
5151 internal var httpClientAndConfiguration: HttpClientAndConfiguration ? = null
@@ -132,44 +132,35 @@ class HttpMessageProcessorEndpoint(
132132 messageProcessor.onMessageDeliveryFailed(message)
133133 Result .failure(OutgoingMessageSendingException (httpException))
134134 } else {
135- if (response.body != null ) {
136- try {
137- val responseString = response.body!! .string()
138- Timber .d(" HTTP response body: ${responseString.take(1000 )} " )
139- val responseStream = ByteArrayInputStream (responseString.toByteArray())
140- val result = parser.fromJson(responseStream)
141- // TODO apply i18n here
142- scope.launch {
143- endpointStateRepo.setState(
144- EndpointState .IDLE .withMessage(
145- String .format(
146- Locale .ROOT ,
147- " Response %d, (%d msgs received)" ,
148- response.code,
149- result.size,
150- ),
151- ),
152- )
153- result.forEach { onMessageReceived(it) }
154- }
155- return Result .success(Unit )
156- } catch (e: IOException ) {
157- Timber .w(e, " HTTP response body could not be parsed, ignoring" )
135+ try {
136+ val responseString = response.body.string()
137+ Timber .d(" HTTP response body: ${responseString.take(1000 )} " )
138+ val responseStream = ByteArrayInputStream (responseString.toByteArray())
139+ val result = parser.fromJson(responseStream)
140+ // TODO apply i18n here
141+ scope.launch {
158142 endpointStateRepo.setState(
159143 EndpointState .IDLE .withMessage(
160144 String .format(
161- Locale .ROOT , " Response %d (response not parseable)" , response.code),
145+ Locale .ROOT ,
146+ " Response %d, (%d msgs received)" ,
147+ response.code,
148+ result.size,
149+ ),
162150 ),
163151 )
164- return Result .success( Unit )
152+ result.forEach { onMessageReceived(it) }
165153 }
166- } else {
154+ return Result .success(Unit )
155+ } catch (e: IOException ) {
156+ Timber .w(e, " HTTP response body could not be parsed, ignoring" )
167157 endpointStateRepo.setState(
168158 EndpointState .IDLE .withMessage(
169- String .format(Locale .ROOT , " Response %d" , response.code),
159+ String .format(
160+ Locale .ROOT , " Response %d (response not parseable)" , response.code),
170161 ),
171162 )
172- Result .success(Unit )
163+ return Result .success(Unit )
173164 }
174165 }
175166 }
0 commit comments