Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 53 additions & 78 deletions src/main/java/com/memetix/mst/MicrosoftTranslatorAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@
* Makes the generic Microsoft Translator API calls. Different service classes then
* extend this to make the specific service calls.
*
* Uses the AJAX Interface V2 - see: http://msdn.microsoft.com/en-us/library/ff512404.aspx
* see: https://www.microsoft.com/en-us/translator/getstarted.aspx
*
* @author Jonathan Griggs
*/
public abstract class MicrosoftTranslatorAPI {
//Encoding type
protected static final String ENCODING = "UTF-8";



private static String MicrosoftCognitiveServicesAuthTokenUri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";

protected static String apiKey;
private static String DatamarketAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
private static String referrer;
private static String clientId;
private static String clientSecret;
private static String token;
private static long tokenExpiration = 0;
private static String contentType = "text/plain";

protected static final String PARAM_APP_ID = "appId=",
protected static final String
PARAM_TO_LANG = "&to=",
PARAM_FROM_LANG = "&from=",
PARAM_TEXT_SINGLE = "&text=",
Expand All @@ -64,89 +64,65 @@ public abstract class MicrosoftTranslatorAPI {

/**
* Sets the API key.
*
* Note: Should ONLY be used with API Keys generated prior to March 31, 2012. All new applications should obtain a ClientId and Client Secret by following
* the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx
*
* Note: DataMarket and Data Services are being retired and will stop accepting new orders after 12/31/2016. Existing subscriptions will be retired and cancelled starting 3/31/2017.
* the guide at: https://www.microsoft.com/en-us/translator/getstarted.aspx
*
* @param pKey The API key.
*/
public static void setKey(final String pKey) {
apiKey = pKey;
}

/**
* Sets the API key.
*
* Note: Should ONLY be used with API Keys generated prior to March 31, 2012. All new applications should obtain a ClientId and Client Secret by following
* the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx
* @param pKey The API key.
*/
public static void setContentType(final String pKey) {
contentType = pKey;
}

/**
* Sets the Client ID.
* All new applications should obtain a ClientId and Client Secret by following
* the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx
* @param pKey The Client Id.
*/
public static void setClientId(final String pClientId) {
clientId = pClientId;
}


/**
* Sets the Client Secret.
* All new applications should obtain a ClientId and Client Secret by following
* the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx
* @param pKey The Client Secret.
* Sets the content type.
*
* @param type The content type.
*/
public static void setClientSecret(final String pClientSecret) {
clientSecret = pClientSecret;
public static void setContentType(final String type) {
contentType = type;
}

/**
* Sets the Http Referrer.
* @param pReferrer The HTTP client referrer.
*/
public static void setHttpReferrer(final String pReferrer) {
referrer = pReferrer;
}

/**
* Gets the OAuth access token.
* @param clientId The Client key.
* @param clientSecret The Client Secret
* @param key The Client key.
*/
public static String getToken(final String clientId, final String clientSecret) throws Exception {
final String params = "grant_type=client_credentials&scope=http://api.microsofttranslator.com"
+ "&client_id=" + URLEncoder.encode(clientId,ENCODING)
+ "&client_secret=" + URLEncoder.encode(clientSecret,ENCODING) ;
public static String getToken(final String key) throws Exception {
//final String params = "Subscription-Key=" + URLEncoder.encode(key,ENCODING);

final URL url = new URL(DatamarketAccessUri);
final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
if(referrer!=null)
uc.setRequestProperty("referer", referrer);
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=" + ENCODING);
uc.setRequestProperty("Accept-Charset",ENCODING);
uc.setRequestMethod("POST");
uc.setDoOutput(true);
final URL url = new URL(MicrosoftCognitiveServicesAuthTokenUri);
final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestProperty("Ocp-Apim-Subscription-Key", key);
uc.setRequestProperty("Content-Type","application/json");
uc.setRequestProperty("Accept","application/jwt");
uc.setRequestMethod("POST");
uc.setDoOutput(true);

OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream());
wr.write(params);
wr.flush();
OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream());
//wr.write(params);
wr.flush();

try {
final int responseCode = uc.getResponseCode();
final String result = inputStreamToString(uc.getInputStream());
if(responseCode!=200) {
throw new Exception("Error from Microsoft Translator API: " + result);
}
return result;
} finally {
if(uc!=null) {
uc.disconnect();
}
}
}
try {
final int responseCode = uc.getResponseCode();
final String result = inputStreamToString(uc.getInputStream());
if(responseCode!=200) {
throw new Exception("Error from Microsoft Translator API: " + result);
}
return result;
} finally {
if(uc!=null) {
uc.disconnect();
}
}
}

/**
* Forms an HTTP request, sends it using GET method and returns the result of the request as a String.
Expand All @@ -156,11 +132,12 @@ public static String getToken(final String clientId, final String clientSecret)
* @throws Exception on error.
*/
private static String retrieveResponse(final URL url) throws Exception {
if(clientId!=null&&clientSecret!=null&&System.currentTimeMillis()>tokenExpiration) {
String tokenJson = getToken(clientId,clientSecret);
Integer expiresIn = Integer.parseInt((String)((JSONObject)JSONValue.parse(tokenJson)).get("expires_in"));
tokenExpiration = System.currentTimeMillis()+((expiresIn*1000)-1);
token = "Bearer " + (String)((JSONObject)JSONValue.parse(tokenJson)).get("access_token");

if(apiKey!=null&&System.currentTimeMillis()>tokenExpiration) {
String authToken = getToken(apiKey);
// set expiration time to 8 minutes
tokenExpiration = System.currentTimeMillis()+(8*60*1000);
token = "Bearer " + authToken;
}
final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
if(referrer!=null)
Expand Down Expand Up @@ -196,7 +173,7 @@ private static String retrieveResponse(final URL url) throws Exception {
*/
protected static String retrieveString(final URL url) throws Exception {
try {
final String response = retrieveResponse(url);
final String response = retrieveResponse(url);
return jsonToString(response);
} catch (Exception ex) {
throw new Exception("[microsoft-translator-api] Error retrieving translation : " + ex.getMessage(), ex);
Expand Down Expand Up @@ -317,10 +294,8 @@ private static String inputStreamToString(final InputStream inputStream) throws

//Check if ready to make request, if not, throw a RuntimeException
protected static void validateServiceState() throws Exception {
if(apiKey!=null&&apiKey.length()<16) {
throw new RuntimeException("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key");
} else if (apiKey==null&&(clientId==null||clientSecret==null)) {
throw new RuntimeException("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation");
if(apiKey!=null&&apiKey.length()<32) {
throw new RuntimeException("INVALID_API_KEY - Please set the API Key with your Azure Cognitive Services Developer's Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx");
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/memetix/mst/detect/Detect.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public final class Detect extends MicrosoftTranslatorAPI {
public static Language execute(final String text) throws Exception {
//Run the basic service validations first
validateServiceState(text);
final URL url = new URL(SERVICE_URL
+(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "")
final URL url = new URL(SERVICE_URL
+PARAM_TEXT_SINGLE+URLEncoder.encode(text, ENCODING));

final String response = retrieveString(url);
Expand All @@ -67,8 +66,7 @@ public static String[] execute(final String[] texts) throws Exception {
//Run the basic service validations first
validateServiceState(texts);
final String textArr = buildStringArrayParam(texts);
final URL url = new URL(ARRAY_SERVICE_URL
+(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "")
final URL url = new URL(ARRAY_SERVICE_URL
+PARAM_TEXT_ARRAY+URLEncoder.encode(textArr, ENCODING));
final String[] response = retrieveStringArr(url);
return response;
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/com/memetix/mst/language/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,7 @@ public static void setKey(String pKey) {
LanguageService.setKey(pKey);
}

public static void setClientId(String pId) {
LanguageService.setClientId(pId);
}
public static void setClientSecret(String pSecret) {
LanguageService.setClientSecret(pSecret);
}


/**
* getName()
*
Expand Down Expand Up @@ -221,7 +215,6 @@ public static String[] execute(final Language[] targets, final Language locale)
final String targetString = buildStringArrayParam(Language.values());

final URL url = new URL(SERVICE_URL
+(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "")
+PARAM_LOCALE+URLEncoder.encode(locale.toString(), ENCODING)
+PARAM_LANGUAGE_CODES + URLEncoder.encode(targetString, ENCODING));
localizedNames = retrieveStringArr(url);
Expand All @@ -245,7 +238,7 @@ public static String[] execute() throws Exception {
validateServiceState();
String[] codes = new String[0];

final URL url = new URL(SERVICE_URL +(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : ""));
final URL url = new URL(SERVICE_URL);
codes = retrieveStringArr(url);
return codes;
}
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/com/memetix/mst/language/SpokenDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,7 @@ public static void setKey(String pKey) {
SpokenDialectService.setKey(pKey);
}

public static void setClientId(String pId) {
SpokenDialectService.setClientId(pId);
}

public static void setClientSecret(String pSecret) {
SpokenDialectService.setClientSecret(pSecret);
}


/**
* getName()
*
Expand Down Expand Up @@ -172,7 +165,6 @@ public static String[] execute(final SpokenDialect[] targets, final Language loc
final String targetString = buildStringArrayParam(SpokenDialect.values());

final URL url = new URL(SERVICE_URL
+(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "")
+PARAM_LOCALE+URLEncoder.encode(locale.toString(), ENCODING)
+PARAM_LANGUAGE_CODES + URLEncoder.encode(targetString, ENCODING));
localizedNames = retrieveStringArr(url);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/memetix/mst/sentence/BreakSentences.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public static Integer[] execute(final String text, final Language fromLang) thro
//Run the basic service validations first
validateServiceState(text,fromLang);
final URL url = new URL(SERVICE_URL
+(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "")
+PARAM_SENTENCES_LANGUAGE+URLEncoder.encode(fromLang.toString(), ENCODING)
+PARAM_TEXT_SINGLE+URLEncoder.encode(text, ENCODING));

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/memetix/mst/speak/Speak.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public static String execute(final String text, final SpokenDialect language) th
//Run the basic service validations first
validateServiceState(text);
final URL url = new URL(SERVICE_URL
+(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "")
+PARAM_SPOKEN_LANGUAGE+URLEncoder.encode(language.toString(),ENCODING)
+PARAM_TEXT_SINGLE+URLEncoder.encode(text, ENCODING));
final String response = retrieveString(url);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/memetix/mst/translate/Translate.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ public static String execute(final String text, final Language from, final Langu
//Run the basic service validations first
validateServiceState(text);
final String params =
(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "")
+ PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING)
PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING)
+ PARAM_TO_LANG + URLEncoder.encode(to.toString(),ENCODING)
+ PARAM_TEXT_SINGLE + URLEncoder.encode(text,ENCODING);

final URL url = new URL(SERVICE_URL + params);
final String response = retrieveString(url);
final String response = retrieveString(url);
return response;
}

Expand Down Expand Up @@ -92,8 +91,7 @@ public static String[] execute(final String[] texts, final Language from, final
//Run the basic service validations first
validateServiceState(texts);
final String params =
(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "")
+ PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING)
PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING)
+ PARAM_TO_LANG + URLEncoder.encode(to.toString(),ENCODING)
+ PARAM_TEXT_ARRAY + URLEncoder.encode(buildStringArrayParam(texts),ENCODING);

Expand All @@ -112,7 +110,6 @@ public static String[] execute(final String[] texts, final Language from, final
* execute(texts[],fromLang,toLang)
*
* @param texts The Strings Array to translate.
* @param from The language code to translate from.
* @param to The language code to translate to.
* @return The translated Strings Array[].
* @throws Exception on error.
Expand Down
24 changes: 5 additions & 19 deletions src/test/java/com/memetix/mst/detect/DetectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,10 @@ public void setUp() throws Exception {
p = new Properties();
URL url = ClassLoader.getSystemResource("META-INF/config.properties");
p.load(url.openStream());
String apiKey = p.getProperty("microsoft.translator.api.key");
String apiKey = p.getProperty("microsoft.azure.subscription.key");
if(System.getProperty("test.api.key")!=null) {
apiKey = System.getProperty("test.api.key").split(",")[0];
}
String clientId = p.getProperty("microsoft.translator.api.clientId");
if(System.getProperty("test.api.key")!=null) {
clientId = System.getProperty("test.api.key").split(",")[1];
}
String clientSecret = p.getProperty("microsoft.translator.api.clientSecret");
if(System.getProperty("test.api.key")!=null) {
clientSecret = System.getProperty("test.api.key").split(",")[2];
}
Detect.setClientId(clientId);
Detect.setClientSecret(clientSecret);
Detect.setKey(apiKey);
}

Expand Down Expand Up @@ -102,37 +92,33 @@ public void testDetectArraySingle() throws Exception {
@Test
public void testDetect_WrongKey() throws Exception {
Detect.setKey("wrong_key");
Detect.setClientId(null);
exception.expect(RuntimeException.class);
exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key");
exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Azure Subscription Key");
Detect.execute("전 세계 여러분 안녕하세요");
}

@Test
public void testDetect_NoKey() throws Exception {
Detect.setKey(null);
Detect.setClientId(null);
exception.expect(RuntimeException.class);
exception.expectMessage("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation");
exception.expectMessage("Must provide a Windows Azure Subscription Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx for further documentation");
Detect.execute("전 세계 여러분 안녕하세요");
}
@Test
public void testDetectArray_NoKey() throws Exception {
Detect.setKey(null);
Detect.setClientId(null);
String[] texts = {"Hello world!"};
exception.expect(RuntimeException.class);
exception.expectMessage("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation");
exception.expectMessage("Must provide a Windows Azure Subscription Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx for further documentation");
Detect.execute(texts);
}

@Test
public void testDetectArray_WrongKey() throws Exception {
Detect.setKey("wrong_key");
Detect.setClientId(null);
String[] texts = {"Hello world!"};
exception.expect(RuntimeException.class);
exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key");
exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Azure Subscription Key");
Detect.execute(texts);
}

Expand Down
Loading