org.languagetool.remote.RemoteLanguageTool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of languagetool-http-client Show documentation
Show all versions of languagetool-http-client Show documentation
Access to LanguageTool via its HTTP API
/* LanguageTool, a natural language style checker
* Copyright (C) 2016 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.languagetool.remote;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.xml.stream.XMLStreamException;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Check a text using a remote LanguageTool server via HTTP or HTTPS.
* Our public HTTPS API and its restrictions are documented
* here.
* @since 3.4
*/
@SuppressWarnings("unchecked")
public class RemoteLanguageTool {
private static final String V2_CHECK = "/v2/check";
private static final String V2_MAXTEXTLENGTH = "/v2/maxtextlength";
private static final String V2_CONFIGINFO = "/v2/configinfo";
private final ObjectMapper mapper = new ObjectMapper();
private final URL serverBaseUrl;
/**
* @param serverBaseUrl for example {@code https://languagetool.org/api} (not ending in slash)
*/
public RemoteLanguageTool(URL serverBaseUrl) {
if (serverBaseUrl.toString().endsWith("/")) {
throw new IllegalArgumentException("Server base URL must not end with '/': " + serverBaseUrl);
}
this.serverBaseUrl = Objects.requireNonNull(serverBaseUrl);
}
/**
* @param text the text to be checked
* @param langCode the language code like {@code en} or {@code en-US} - note that for some languages (like English) you
* need to specify the country code (like {@code US} or {@code GB}) to get spell checking
*/
public RemoteResult check(String text, String langCode) {
return check(getUrlParams(text, new CheckConfigurationBuilder(langCode).build()));
}
/**
* @param text the text to be checked
*/
public RemoteResult check(String text, CheckConfiguration config) {
return check(getUrlParams(text, config));
}
private String getUrlParams(String text, CheckConfiguration config) {
StringBuilder params = new StringBuilder();
append(params, "text", text);
if (config.getMotherTongueLangCode() != null) {
append(params, "motherTongue", config.getMotherTongueLangCode());
}
if (config.guessLanguage()) {
append(params, "language", "auto");
} else {
append(params, "language", config.getLangCode().orElse("auto"));
}
if (config.getEnabledRuleIds().size() > 0) {
append(params, "enabledRules", String.join(",", config.getEnabledRuleIds()));
}
if (config.enabledOnly()) {
append(params, "enabledOnly", "yes");
}
if (config.getDisabledRuleIds().size() > 0) {
append(params, "disabledRules", String.join(",", config.getDisabledRuleIds()));
}
if (config.getMode() != null) {
append(params, "mode", config.getMode());
}
if (config.getLevel() != null) {
append(params, "level", config.getLevel());
}
if (config.getRuleValues().size() > 0) {
append(params, "ruleValues", String.join(",", config.getRuleValues()));
}
append(params, "useragent", "java-http-client");
return params.toString();
}
private void append(StringBuilder params, String paramName, String paramValue) {
if (params.length() > 0) {
params.append('&');
}
params.append(paramName).append('=').append(encode(paramValue));
}
private String encode(String text) {
try {
return URLEncoder.encode(text, "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
private RemoteResult check(String urlParameters) {
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
URL checkUrl;
try {
checkUrl = new URL(serverBaseUrl + V2_CHECK);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
HttpURLConnection conn = getConnection(postData, checkUrl);
try {
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (InputStream inputStream = conn.getInputStream()) {
return parseJson(inputStream);
}
} else {
try (InputStream inputStream = conn.getErrorStream()) {
String error = readStream(inputStream, "utf-8");
throw new RuntimeException("Got error: " + error + " - HTTP response code " + conn.getResponseCode());
}
}
} catch (ConnectException e) {
throw new RuntimeException("Could not connect to server at " + serverBaseUrl, e);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
conn.disconnect();
}
}
public RemoteConfigurationInfo getConfigurationInfo(String urlParameters) {
if (!urlParameters.startsWith("language=")) {
throw new IllegalArgumentException("'language' parameter missing");
}
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
URL checkUrl;
try {
checkUrl = new URL(serverBaseUrl + V2_CONFIGINFO);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
HttpURLConnection conn = getConnection(postData, checkUrl);
try {
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (InputStream inputStream = conn.getInputStream()) {
RemoteConfigurationInfo configInfo = new RemoteConfigurationInfo(mapper, inputStream);
return configInfo;
}
} else {
try (InputStream inputStream = conn.getErrorStream()) {
String error = readStream(inputStream, "utf-8");
throw new RuntimeException("Got error: " + error + " - HTTP response code " + conn.getResponseCode());
}
}
} catch (ConnectException e) {
throw new RuntimeException("Could not connect to server at " + serverBaseUrl, e);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
conn.disconnect();
}
}
public int getMaxTextLength() {
byte[] postData = { 0 };
URL checkUrl;
try {
checkUrl = new URL(serverBaseUrl + V2_MAXTEXTLENGTH);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
HttpURLConnection conn = getConnection(postData, checkUrl);
try {
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (InputStream inputStream = conn.getInputStream()) {
StringBuilder sb = new StringBuilder();
try (InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
BufferedReader br = new BufferedReader(isr)) {
String line = br.readLine();
return Integer.parseInt(line);
}
}
} else {
try (InputStream inputStream = conn.getErrorStream()) {
String error = readStream(inputStream, "utf-8");
throw new RuntimeException("Got error: " + error + " - HTTP response code " + conn.getResponseCode());
}
}
} catch (ConnectException e) {
throw new RuntimeException("Could not connect to server at " + serverBaseUrl, e);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
conn.disconnect();
}
}
HttpURLConnection getConnection(byte[] postData, URL url) {
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.write(postData);
}
return conn;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private String readStream(InputStream stream, String encoding) throws IOException {
StringBuilder sb = new StringBuilder();
try (InputStreamReader isr = new InputStreamReader(stream, encoding);
BufferedReader br = new BufferedReader(isr)) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append('\n');
}
}
return sb.toString();
}
private RemoteResult parseJson(InputStream inputStream) throws XMLStreamException, IOException {
Map map = mapper.readValue(inputStream, Map.class);
Map languageObj = (Map) map.get("language");
String language = languageObj.get("name");
String languageCode = languageObj.get("code");
Map detectedLanguageObj = (Map) ((Map)languageObj).get("detectedLanguage");
String languageDetectedCode = null, languageDetectedName = null;
if (detectedLanguageObj != null) {
languageDetectedCode = detectedLanguageObj.get("code");
languageDetectedName = detectedLanguageObj.get("name");
}
Map software = (Map) map.get("software");
RemoteServer remoteServer = new RemoteServer(software.get("name"), software.get("version"), software.get("buildDate"));
List matches = (ArrayList) map.get("matches");
List result = new ArrayList<>();
for (Object match : matches) {
RemoteRuleMatch remoteMatch = getMatch((Map)match);
result.add(remoteMatch);
}
return new RemoteResult(language, languageCode, languageDetectedCode, languageDetectedName, result, remoteServer);
}
private RemoteRuleMatch getMatch(Map match) {
Map rule = (Map) match.get("rule");
int offset = (int) getRequired(match, "offset");
int errorLength = (int) getRequired(match, "length");
Map context = (Map) match.get("context");
int contextOffset = (int) getRequired(context, "offset");
RemoteRuleMatch remoteMatch = new RemoteRuleMatch(getRequiredString(rule, "id"), getRequiredString(rule, "description"), getRequiredString(match, "message"),
getRequiredString(context, "text"), contextOffset, offset, errorLength);
remoteMatch.setShortMsg(getOrNull(match, "shortMessage"));
remoteMatch.setRuleSubId(getOrNull(rule, "subId"));
remoteMatch.setLocQualityIssueType(getOrNull(rule, "issueType"));
List urls = getValueList(rule, "urls");
if (urls.size() > 0) {
remoteMatch.setUrl(urls.get(0));
}
Map category = (Map) rule.get("category");
remoteMatch.setCategory(getOrNull(category, "name"));
remoteMatch.setCategoryId(getOrNull(category, "id"));
remoteMatch.setReplacements(getValueList(match, "replacements"));
return remoteMatch;
}
private Object getRequired(Map elem, String propertyName) {
Object val = elem.get(propertyName);
if (val != null) {
return val;
}
throw new RuntimeException("JSON item " + elem + " doesn't contain required property '" + propertyName + "'");
}
private String getRequiredString(Map elem, String propertyName) {
return (String) getRequired(elem, propertyName);
}
private String getOrNull(Map elem, String propertyName) {
Object val = elem.get(propertyName);
if (val != null) {
return (String) val;
}
return null;
}
private List getValueList(Map match, String propertyName) {
List