Below are Apex code blocks that can be used to:

  • Get an OAuth token from CampusLogic's STS
  • Retrieve student requirements for a given student and award/aid year

Note: The following code is sample code and must be refined (error handling, logging, etc.) prior to use in a production environment.

// Get OAuth token from STS

public String getToken(String username, String password, String realm, String endpoint) {   

    HttpRequest req = new HttpRequest();

    req.setMethod('POST');

    req.setEndpoint(endpoint);

    req.setHeader('content-type', FORM_URL_ENCODED);       

    req.setBody(formatRequest(username, password, realm));

    req.setTimeout(2000);

     

    Http http = new Http();

    String result = '';

     

    try {

        HttpResponse res = http.send(req);

        Integer statusCode = res.getStatusCode();

        if (statusCode != 200) {

            throw new myException('Request failed; Http Status Code = ' + statusCode);

        }

         

        result = res.getBody();

        statusCode = res.getStatusCode();

    catch(System.CalloutException e) {       

        // log and/or handle exception

        System.debug(e.getStackTraceString());

        // re-throw exception

        throw(e);

    }

    return result;             

}

 

// format body/message of OAuth token request

private String formatRequest(String username, String password, String realm) {

    String[] args = new String[] {EncodingUtil.urlEncode(username,'UTF-8'), EncodingUtil.urlEncode(password,'UTF-8'), EncodingUtil.urlEncode(realm,'UTF-8')};

    String format = 'wrap_name={0}&wrap_password={1}&wrap_scope={2}';               

    return String.format(format, args);

}

 

// format raw response from OAuth token request

private String formatToken(string rawResponse) {

    String temp = 'wrap_access_token=';

    Integer index = temp.Length();

     

    String rawToken = EncodingUtil.urlDecode(rawResponse.split('&')[0], 'UTF-8');

    String token = rawToken.substring(index, rawToken.length());

    return 'WRAP access_token="' + token + '"';

}

 

// Get student requirements

public String getStudentRequirements(String token, String studentId, String awardYear) {

    HttpRequest req = new HttpRequest();

    req.setMethod('GET');

     

    // Note: retrieve root URL from configuration

    req.setEndpoint('[root url]/studentrequirements/' + studentId + '?awardyear=' + awardYear);

    req.setHeader('Authorization', token);

    req.setHeader('Accept''application/json');

    req.setTimeout(2000);

     

    Http http = new Http();     

             

    String result = '';

    try {

        HttpResponse res = http.send(req);

        Integer statusCode = res.getStatusCode();           

        if (statusCode != 200) {

            throw new myException('Request failed; Http Status Code = ' + statusCode);           

        }

        result = res.getBody();

    catch(System.CalloutException e) {

        // log and/or handle exception

        System.debug(e);       

        // re-throw exception

        throw(e);

    }

    return result;     

}