forked from valentjn/ltex-ls
-
Notifications
You must be signed in to change notification settings - Fork 3
/
LanguageToolHttpInterface.kt
202 lines (167 loc) · 6.3 KB
/
LanguageToolHttpInterface.kt
1
2
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Copyright (C) 2019-2023 Julian Valentin, LTeX Development Community
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package org.bsplines.ltexls.languagetool
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import org.bsplines.ltexls.parsing.AnnotatedTextFragment
import org.bsplines.ltexls.tools.I18n
import org.bsplines.ltexls.tools.Logging
import org.languagetool.markup.AnnotatedText
import org.languagetool.markup.TextPart
import java.io.IOException
import java.io.UnsupportedEncodingException
import java.net.MalformedURLException
import java.net.URI
import java.net.URISyntaxException
import java.net.URLEncoder
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpRequest.BodyPublishers
import java.net.http.HttpResponse
import java.net.http.HttpResponse.BodyHandlers
class LanguageToolHttpInterface(
uriString: String,
private val languageShortCode: String,
private val motherTongueShortCode: String,
) : LanguageToolInterface() {
private val enabledRules: MutableList<String> = ArrayList()
private val httpClient: HttpClient = HttpClient.newHttpClient()
private val uri: URI?
init {
var exception: Exception? = null
this.uri =
try {
URI(uriString + "/v2/check").toURL().toURI()
} catch (e: MalformedURLException) {
exception = e
null
} catch (e: URISyntaxException) {
exception = e
null
}
if (exception != null) {
Logging.LOGGER.severe(I18n.format("couldNotParseHttpServerUri", exception, uriString))
}
}
override fun isInitialized(): Boolean = (this.uri != null)
override fun checkInternal(
annotatedTextFragment: AnnotatedTextFragment,
): List<LanguageToolRuleMatch> {
if (!isInitialized()) return emptyList()
val requestBody: String = createRequestBody(annotatedTextFragment) ?: return emptyList()
val httpRequest: HttpRequest =
HttpRequest
.newBuilder(this.uri)
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Accept", "application/json")
.POST(BodyPublishers.ofString(requestBody))
.build()
val httpResponse: HttpResponse<String> =
try {
this.httpClient.send(httpRequest, BodyHandlers.ofString())
} catch (e: InterruptedException) {
Logging.LOGGER.severe(I18n.format("couldNotSendHttpRequestToLanguageTool", e))
return emptyList()
} catch (e: IOException) {
Logging.LOGGER.severe(I18n.format("couldNotSendHttpRequestToLanguageTool", e))
return emptyList()
}
val statusCode: Int = httpResponse.statusCode()
if (statusCode != STATUS_CODE_SUCCESS) {
Logging.LOGGER.severe(I18n.format("languageToolFailedWithStatusCode", statusCode))
return emptyList()
}
val responseBody: String = httpResponse.body()
val jsonResponse: JsonObject = JsonParser.parseString(responseBody).asJsonObject
val jsonMatches: JsonArray = jsonResponse.get("matches").asJsonArray
val result = ArrayList<LanguageToolRuleMatch>()
for (jsonElement: JsonElement in jsonMatches) {
result.add(
LanguageToolRuleMatch.fromLanguageTool(jsonElement.asJsonObject, annotatedTextFragment),
)
}
return result
}
private fun createRequestBody(annotatedTextFragment: AnnotatedTextFragment): String? {
val jsonData = JsonObject()
jsonData.add("annotation", convertAnnotatedTextToJson(annotatedTextFragment.annotatedText))
val requestEntries = HashMap<String, String>()
requestEntries["language"] = this.languageShortCode
requestEntries["data"] = jsonData.toString()
Logging.LOGGER.finer("requestEntries[\"data\"].length = " + requestEntries["data"]!!.length)
if (this.languageToolOrgUsername.isNotEmpty()) {
requestEntries["username"] = this.languageToolOrgUsername
}
if (this.languageToolOrgApiKey.isNotEmpty()) {
requestEntries["apiKey"] = this.languageToolOrgApiKey
}
if (annotatedTextFragment.codeFragment.settings.enablePickyRules) {
requestEntries["level"] = "picky"
}
if (this.motherTongueShortCode.isNotEmpty()) {
requestEntries["motherTongue"] = this.motherTongueShortCode
}
if (this.enabledRules.isNotEmpty()) {
requestEntries["enabledRules"] = this.enabledRules.joinToString(",")
}
val builder = StringBuilder()
for ((requestKey: String, requestValue: String) in requestEntries) {
if (builder.isNotEmpty()) builder.append("&")
try {
builder
.append(URLEncoder.encode(requestKey, "utf-8"))
.append("=")
.append(URLEncoder.encode(requestValue, "utf-8"))
} catch (e: UnsupportedEncodingException) {
Logging.LOGGER.severe(I18n.format(e))
return null
}
}
return builder.toString()
}
override fun activateDefaultFalseFriendRules() {
// handled by LanguageTool HTTP server
}
override fun activateLanguageModelRules(languageModelRulesDirectory: String) {
// handled by LanguageTool HTTP server
}
override fun enableRules(ruleIds: Set<String>) {
this.enabledRules.addAll(ruleIds)
}
override fun enableEasterEgg() {
// not possible with LanguageTool HTTP server
}
companion object {
private const val STATUS_CODE_SUCCESS = 200
private fun convertAnnotatedTextToJson(annotatedText: AnnotatedText): JsonElement {
val jsonDataAnnotation = JsonArray()
val parts: List<TextPart> = annotatedText.parts
var i = 0
while (i < parts.size) {
val jsonPart = JsonObject()
if (parts[i].type == TextPart.Type.TEXT) {
jsonPart.addProperty("text", parts[i].part)
} else if (parts[i].type == TextPart.Type.MARKUP) {
jsonPart.addProperty("markup", parts[i].part)
if ((i < parts.size - 1) && (parts[i + 1].type == TextPart.Type.FAKE_CONTENT)) {
i++
jsonPart.addProperty("interpretAs", parts[i].part)
}
} else {
// should not happen
i++
continue
}
jsonDataAnnotation.add(jsonPart)
i++
}
return jsonDataAnnotation
}
}
}