Last active
November 14, 2020 22:32
-
-
Save pmedcraft/570dcc22dede7c301c9819397a301e9f to your computer and use it in GitHub Desktop.
Connects to SDL WorldServer via invoking the /login REST API call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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
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?