Skip to content

Instantly share code, notes, and snippets.

@pmedcraft
Last active November 14, 2020 22:32
Show Gist options
  • Save pmedcraft/570dcc22dede7c301c9819397a301e9f to your computer and use it in GitHub Desktop.
Save pmedcraft/570dcc22dede7c301c9819397a301e9f to your computer and use it in GitHub Desktop.
Connects to SDL WorldServer via invoking the /login REST API call
/**
* Connects to SDL WorldServer via invoking the /login REST API call.
*
* @param wsBaseUrl
* @param username
* @param password
* @return a security token.
* @throws IOException
*/
private String login(String wsBaseUrl, String username, String password) throws IOException {
log.info("Connecting to SDL WorldServer");
StringBuilder loginJson = new StringBuilder();
loginJson.append("{");
loginJson.append("\"username\":\"" + username + "\",");
loginJson.append("\"password\":\"" + password + "\"");
loginJson.append("}");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPostRequest = new HttpPost(wsBaseUrl + "/ws-api/v1/login");
httpPostRequest.setHeader(new BasicHeader("Content-Type", "application/json"));
httpPostRequest.setHeader(new BasicHeader("Accept", "application/json"));
httpPostRequest.setEntity(new StringEntity(loginJson.toString()));
HttpResponse response = httpClient.execute(httpPostRequest);
HttpEntity entity = response.getEntity();
Gson gson = new GsonBuilder().create();
JsonObject jsonLoginResponse = gson.fromJson(new InputStreamReader(entity.getContent()), JsonObject.class);
String token = jsonLoginResponse.getAsJsonPrimitive("sessionId").getAsString();
log.info("Security token: " + token);
return token;
}
@InaSong
Copy link

InaSong commented Aug 26, 2019

Hi Phil,

Great to see the try on WorldServer REST APIs, are you from WorldServer team, and do you know if the WorldServer REST API support AIS files handling in 11.x version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment