Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.