diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d5545..e0ceb68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v2.0.2 + +### Fixed +- User agent header version resolution ## v2.0.1 ### Fixed - Maven source control url - ## v2.0.0 ### Added diff --git a/pom.xml b/pom.xml index 3c26b71..8432280 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ detectlanguage jar detectlanguage-java - 2.0.1 + 2.0.2 Language Detection API Java client. http://detectlanguage.com @@ -35,6 +35,18 @@ UTF-8 UTF-8 + + + + src/main/resources + true + + + src/main/java + false + + + org.sonatype.oss oss-parent diff --git a/src/main/java/com/detectlanguage/Client.java b/src/main/java/com/detectlanguage/Client.java index a621e46..ed2711b 100644 --- a/src/main/java/com/detectlanguage/Client.java +++ b/src/main/java/com/detectlanguage/Client.java @@ -17,11 +17,12 @@ import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import java.util.Scanner; import java.lang.reflect.Type; public class Client { - private static final String AGENT = "detectlanguage-java"; + private static final String AGENT = "detectlanguage-java/" + getVersionFromProperties(); private static final String CHARSET = "UTF-8"; public Client() { @@ -36,7 +37,7 @@ public T post(String path, String payload, Type responseType) throws APIErro } private T execute(String method, String path, Map params, - String payload, Type responseType) throws APIError { + String payload, Type responseType) throws APIError { URL url = buildUrl(path, params); try { @@ -104,9 +105,8 @@ private URL buildUrl(String path, Map params) { DetectLanguage.apiVersion, path); - if (params != null && params.size() > 0) - url+= '?' + buildQuery(params); + url += '?' + buildQuery(params); try { return new URL(url); @@ -121,9 +121,7 @@ private HttpURLConnection createConnection(URL url) throws IOException { conn.setReadTimeout(DetectLanguage.timeout); conn.setUseCaches(false); - String version = getClass().getPackage().getImplementationVersion(); - - conn.setRequestProperty("User-Agent", AGENT + '/' + version); + conn.setRequestProperty("User-Agent", AGENT); conn.setRequestProperty("Accept", "application/json"); conn.setRequestProperty("Authorization", "Bearer " + DetectLanguage.apiKey); @@ -183,7 +181,7 @@ private static Map flattenParams(Map params) { private static String getResponseBody(InputStream responseStream) throws IOException { - //\A is the beginning of + // \A is the beginning of // the stream boundary String rBody = new Scanner(responseStream, CHARSET) .useDelimiter("\\A") @@ -192,4 +190,19 @@ private static String getResponseBody(InputStream responseStream) responseStream.close(); return rBody; } + + private static String getVersionFromProperties() { + try { + Properties props = new Properties(); + InputStream input = Client.class.getClassLoader().getResourceAsStream("version.properties"); + if (input != null) { + props.load(input); + input.close(); + return props.getProperty("version", "unknown"); + } + } catch (IOException e) { + // Fallback to unknown version + } + return "unknown"; + } } diff --git a/src/main/resources/version.properties b/src/main/resources/version.properties new file mode 100644 index 0000000..a9ca5c5 --- /dev/null +++ b/src/main/resources/version.properties @@ -0,0 +1 @@ +version=@project.version@ diff --git a/src/test/java/com/detectlanguage/DetectLanguageTest.java b/src/test/java/com/detectlanguage/DetectLanguageTest.java index fa37cfc..29d7fbd 100644 --- a/src/test/java/com/detectlanguage/DetectLanguageTest.java +++ b/src/test/java/com/detectlanguage/DetectLanguageTest.java @@ -37,7 +37,7 @@ public void testDetectError() throws APIError { @Test public void testBatchDetect() throws APIError { - String[] texts = {"Hello world", "Kabo kabikas, žiūri žiūrikas"}; + String[] texts = { "Hello world", "Kabo kabikas, žiūri žiūrikas" }; List> results = DetectLanguage.detect(texts); Result result; @@ -57,7 +57,7 @@ public void testBatchDetect() throws APIError { public void testBatchDetectError() throws APIError { DetectLanguage.apiKey = "INVALID"; - String[] texts = {"Hello world", "Kabo kabikas, žiūri žiūrikas"}; + String[] texts = { "Hello world", "Kabo kabikas, žiūri žiūrikas" }; DetectLanguage.detect(texts); }