0
\$\begingroup\$

Following is an https call to retrieve the token, once it's retrieved, it's cached.

Callers call the addTokenObserver method that sets a timer based on the token expiry as well as to retrieve the token.

Once token is retrieved, the class automatically renews the token hence isolating callers having to renew the token manually.

Code is meant to be in plain java language for backward compatibility.

public class Authentication {

    private SecurityToken securityToken;

    public Authentication() {}

    private SecurityToken requestToken() throws IOException, InvalidClientException, ParserConfigurationException, SAXException {
        URL url = new URL("https://example.com/AuthenticationService/AuthenticationMgmt.svc");

        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("content-type", "text/xml;charset=UTF-8");
        connection.setRequestProperty("SOAPAction", "\"http://example.com/Services/Authenticate\"");
        connection.setRequestProperty("Authorization", "Basic Bhc3N3b3bVBBOlBhc3N3b3JkNSQ=");
        connection.setDoOutput(true);
        connection.getOutputStream().write("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/><soapenv:Body/></soapenv:Envelope>".getBytes("UTF8"));

        StringBuilder result = new StringBuilder();
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getResponseCode() == 200 ? connection.getInputStream() : connection.getErrorStream()))) {
            String line;
            while((line = bufferedReader.readLine()) != null) {result.append(line);}
        }

        if(connection.getResponseCode() == 200) {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(new ByteArrayInputStream(result.toString().getBytes("UTF8")));
            return securityToken = new SecurityToken(document.getDocumentElement());
        } else {
            throw new InvalidClientException(result.toString());
        }
    }

    public SecurityToken addTokenObserver() throws IOException, InvalidClientException, SAXException, ParserConfigurationException {
        requestToken();
        new Timer().schedule(
            new TimerTask() {
                public void run() {
                    try {
                        addTokenObserver();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } catch (InvalidClientException e) {
                        e.printStackTrace();
                    }
                }
            }, securityToken.expiresIn()
        );
        return securityToken;
    }

    public SecurityToken getSecurityToken() {
        return securityToken;
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.