Example usage for org.springframework.security.oauth2.client OAuth2RestTemplate postForLocation

List of usage examples for org.springframework.security.oauth2.client OAuth2RestTemplate postForLocation

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.client OAuth2RestTemplate postForLocation.

Prototype

@Override
    @Nullable
    public URI postForLocation(URI url, @Nullable Object request) throws RestClientException 

Source Link

Usage

From source file:org.apereo.lap.services.output.handlers.SSPEarlyAlertOutputHandler.java

@Override
public OutputResult writeOutput(Output output) {
    logger.debug(output.toString());/*from w  ww. jav  a2  s .c  o  m*/

    SSPConfigPersistentStorage sspConfigPersistentStorage = storageFactory.getSSPConfigPersistentStorage();
    SSPConfig sspConfig = sspConfigPersistentStorage.get();

    if (sspConfig == null) {
        throw new RuntimeException("No SSP Configuration");
    }

    ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
    resourceDetails.setClientId(sspConfig.getKey());
    resourceDetails.setClientSecret(sspConfig.getSecret());

    String baseUrl = sspConfig.getUrl();
    if (!baseUrl.endsWith("/")) {
        baseUrl = baseUrl.concat("/");
    }

    resourceDetails.setAccessTokenUri(baseUrl + "ssp/api/1/oauth2/token");
    DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();

    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setSupportedMediaTypes(
            Arrays.asList(MediaType.APPLICATION_JSON, MediaType.valueOf("text/javascript")));
    restTemplate.setMessageConverters(Arrays.<HttpMessageConverter<?>>asList(converter));

    OutputResult result = new OutputResult(output);

    String selectSQL = output.makeTempDBSelectSQL();

    SqlRowSet rowSet;
    try {
        rowSet = storage.getTempJdbcTemplate().queryForRowSet(selectSQL);

    } catch (Exception e) {
        throw new RuntimeException("Failure while trying to retrieve the output data set: " + selectSQL);
    }

    Map<String, Integer> riskMap = new HashMap<String, Integer>();
    riskMap.put("NO RISK", 0);
    riskMap.put("LOW RISK", 1);
    riskMap.put("MEDIUM RISK", 2);
    riskMap.put("HIGH RISK", 3);

    Integer riskThreshold = riskMap.get(sspConfig.getRiskRule());

    List<EarlyAlert> earlyAlertList = new ArrayList<SSPEarlyAlertOutputHandler.EarlyAlert>();
    while (rowSet.next()) {
        if (!rowSet.wasNull()) {

            String student = rowSet.getString(1);
            String course = rowSet.getString(2);
            String risk = rowSet.getString(3);

            Integer riskScore = riskMap.get(risk);

            if (riskScore >= riskThreshold) {
                EarlyAlert earlyAlert = new EarlyAlert(course, student,
                        "Automated early alert due to risk score above acceptable limit", risk, null);
                earlyAlertList.add(earlyAlert);
            }

            logger.debug(String.format("student: %s, course: %s, risk:%s", student, course, risk));
        }
    }

    if (earlyAlertList.size() > 0) {
        EarlyAlertMessage message = new EarlyAlertMessage("test.com", "test", earlyAlertList);
        restTemplate.postForLocation(baseUrl + "ssp/api/1/bulkEarlyAlerts", message);
    }

    return result;
}