Class BasicLTIUtil


  • public class BasicLTIUtil
    extends Object
    Some Utility code for IMS LTI http://www.anyexample.com/programming/java /java_simple_class_to_compute_sha_1_hash.xml

    Sample Descriptor

     <?xml version="1.0" encoding="UTF-8"?>
     <basic_lti_link xmlns="http://www.imsglobal.org/xsd/imsbasiclti_v1p0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <title>generated by tp+user</title>
       <description>generated by tp+user</description>
       <custom>
             <parameter key="keyname">value</parameter>
       </custom>
       <extensions platform="www.lms.com">
             <parameter key="keyname">value</parameter>
       </extensions>
       <launch_url>url to the basiclti launch URL</launch_url>
       <secure_launch_url>url to the basiclti launch URL</secure_launch_url>
       <icon>url to an icon for this tool (optional)</icon>
       <secure_icon>url to an icon for this tool (optional)</secure_icon>
       <cartridge_icon identifierref="BLTI001_Icon"/>
       <vendor>
             <code>vendor.com</code>
             <name>Vendor Name</name>
             <description>
                This is a Grade Book that supports many column types.
              </description>
             <contact>
               <email>support@vendor.com</email>
             </contact>
             <url>http://www.vendor.com/product</url>
       </vendor>
     </basic_lti_link>
     
    • Constructor Detail

      • BasicLTIUtil

        public BasicLTIUtil()
    • Method Detail

      • validateDescriptor

        public static String validateDescriptor​(String descriptor)
      • adaptToCustomPropertyName

        public static String adaptToCustomPropertyName​(String propertyName)
        A simple utility method which implements the specified semantics of custom properties.

        i.e. The parameter names are mapped to lower case and any character that is neither a number nor letter in a parameter name is replaced with an "underscore".

        e.g. Review:Chapter=1.2.56 would map to custom_review_chapter=1.2.56.

        Parameters:
        propertyName -
        Returns:
      • signProperties

        public static Map<String,​String> signProperties​(Map<String,​String> postProp,
                                                              String url,
                                                              String method,
                                                              String oauth_consumer_key,
                                                              String oauth_consumer_secret,
                                                              Map<String,​String> extra)
        Add the necessary fields and sign.
        Parameters:
        postProp -
        url -
        method -
        oauth_consumer_key -
        oauth_consumer_secret -
        extra -
        Returns:
      • checkProperties

        public static boolean checkProperties​(Map<String,​String> postProp,
                                              String url,
                                              String method,
                                              String oauth_consumer_key,
                                              String oauth_consumer_secret)
        Check if the fields are properly signed
        Parameters:
        postProp -
        url -
        method -
        oauth_consumer_key -
        oauth_consumer_secret -
        Returns:
      • postLaunchHTML

        public static String postLaunchHTML​(Properties cleanProperties,
                                            String endpoint,
                                            String launchtext,
                                            boolean debug,
                                            Map<String,​String> extra)
        Create the HTML to render a POST form and then automatically submit it.
        Parameters:
        cleanProperties -
        endpoint - The LTI launch url.
        launchtext - The LTI launch text. Used if javascript is turned off.
        debug - Useful for viewing the HTML before posting to end point.
        extra -
        Returns:
        the HTML ready for IFRAME src = inclusion.
      • postLaunchHTML

        public static String postLaunchHTML​(Properties cleanProperties,
                                            String endpoint,
                                            String launchtext,
                                            boolean autosubmit,
                                            boolean debug,
                                            Map<String,​String> extra)
        Create the HTML to render a POST form and then automatically submit it.
        Parameters:
        cleanProperties -
        endpoint - The LTI launch url.
        launchtext - The LTI launch text. Used if javascript is turned off.
        autosubmit - Whether or not we want the form autosubmitted
        debug - Useful for viewing the HTML before posting to end point.
        extra -
        Returns:
        the HTML ready for IFRAME src = inclusion.
      • postLaunchHTML

        public static String postLaunchHTML​(Map<String,​String> cleanProperties,
                                            String endpoint,
                                            String launchtext,
                                            boolean debug,
                                            Map<String,​String> extra)
        Create the HTML to render a POST form and then automatically submit it.
        Parameters:
        cleanProperties -
        endpoint - The LTI launch url.
        launchtext - The LTI launch text. Used if javascript is turned off.
        debug - Useful for viewing the HTML before posting to end point.
        extra - Useful for viewing the HTML before posting to end point.
        Returns:
        the HTML ready for IFRAME src = inclusion.
      • postLaunchHTML

        public static String postLaunchHTML​(Map<String,​String> cleanProperties,
                                            String endpoint,
                                            String launchtext,
                                            boolean autosubmit,
                                            boolean debug,
                                            Map<String,​String> extra)
        Create the HTML to render a POST form and then automatically submit it.
        Parameters:
        cleanProperties -
        endpoint - The LTI launch url.
        launchtext - The LTI launch text. Used if javascript is turned off.
        autosubmit - Whether or not we want the form autosubmitted
        extra - Useful for viewing the HTML before posting to end point.
        Returns:
        the HTML ready for IFRAME src = inclusion.
      • getOAuthURL

        public static String getOAuthURL​(String method,
                                         String url,
                                         String oauth_consumer_key,
                                         String oauth_secret)
        getOAuthURL - Form a GET request signed by OAuth
        Parameters:
        method -
        url -
        oauth_consumer_key -
        oauth_secret -
      • getOAuthURL

        public static String getOAuthURL​(String method,
                                         String url,
                                         String oauth_consumer_key,
                                         String oauth_secret,
                                         String signature)
        getOAuthURL - Form a GET request signed by OAuth
        Parameters:
        method -
        url -
        oauth_consumer_key -
        oauth_secret -
        signature -
      • sendOAuthURL

        public static HttpURLConnection sendOAuthURL​(String method,
                                                     String url,
                                                     String oauth_consumer_key,
                                                     String oauth_secret)
        getOAuthURL - Form a GET request signed by OAuth
        Parameters:
        method -
        url -
        oauth_consumer_key -
        oauth_secret - HttpURLConnection connection = sendOAuthURL('GET', url, oauth_consumer_key, oauth_secret) int responseCode = connection.getResponseCode(); String data = readHttpResponse(connection)
      • getResponseCode

        public static int getResponseCode​(HttpURLConnection connection)
        getResponseCode - Read the HTTP Response
        Parameters:
        connection -
      • readHttpResponse

        public static String readHttpResponse​(HttpURLConnection connection)
        readHttpResponse - Read the HTTP Response
        Parameters:
        connection -
      • parseDescriptor

        public static boolean parseDescriptor​(Properties launch_info,
                                              Properties postProp,
                                              String descriptor)
        Parameters:
        launch_info - Variable is mutated by this method.
        postProp - Variable is mutated by this method.
        descriptor -
        Returns:
      • parseDescriptor

        public static boolean parseDescriptor​(Map<String,​String> launch_info,
                                              Map<String,​String> postProp,
                                              String descriptor)
        Parameters:
        launch_info - Variable is mutated by this method.
        postProp - Variable is mutated by this method.
        descriptor -
        Returns:
      • prepareForExport

        public static String prepareForExport​(String descriptor)
      • mapKeyName

        public static String mapKeyName​(String keyname)
        The parameter name is mapped to lower case and any character that is neither a number or letter is replaced with an "underscore". So if a custom entry was as follows: 1.2.56 Would map to: custom_vendor_chapter=1.2.56
      • setProperty

        public static void setProperty​(Map<String,​String> map,
                                       String key,
                                       String value)
        Mutates the passed Map map variable. Puts the key,value into the Map if the value is not null and is not empty.
        Parameters:
        map - Variable is mutated by this method.
        key -
        value -
      • setProperty

        public static void setProperty​(Properties props,
                                       String key,
                                       String value)
        Mutates the passed Properties props variable. Puts the key,value into the Map if the value is not null and is not empty.
        Parameters:
        props - Variable is mutated by this method.
        key -
        value -
      • htmlspecialchars

        public static String htmlspecialchars​(String input)
      • mergeCSV

        public static String mergeCSV​(String old_id_history,
                                      String new_id_history,
                                      String old_resource_link_id)
        Merge two resource_link_id or context_id history strings and add a new one
      • getRealPath

        public static String getRealPath​(String servletUrl,
                                         String extUrl)
        Simple utility method deal with a request that has the wrong URL when behind a proxy.
        Parameters:
        servletUrl -
        extUrl - The url that the external world sees us as responding to. This needs to be up to but not including the last slash like and not include any path information https://www.sakailms.org/ - although we do compensate for extra stuff at the end.
        Returns:
        The full path of the request with extUrl in place of whatever the request thinks is the current URL.
      • convertToMap

        public static Map<String,​String> convertToMap​(Properties properties)
        Simple utility method to help with the migration from Properties to Map.
        Parameters:
        properties -
        Returns:
      • convertToProperties

        public static Properties convertToProperties​(Map<String,​String> map)
        Deprecated.
        Should migrate to Map signatures.
        Simple utility method to help with the migration from Map to Properties.
        Parameters:
        map -
        Returns:
      • isBlank

        public static boolean isBlank​(String str)

        Checks if a String is whitespace, empty ("") or null.

         StringUtils.isBlank(null)        = true
         StringUtils.isBlank("")                = true
         StringUtils.isBlank(" ")          = true
         StringUtils.isBlank("bob")      = false
         StringUtils.isBlank("  bob  ") = false
         
        Parameters:
        str - the String to check, may be null
        Returns:
        true if the String is null, empty or whitespace
        Since:
        2.0
      • isNotBlank

        public static boolean isNotBlank​(String str)

        Checks if a String is not empty (""), not null and not whitespace only.

         StringUtils.isNotBlank(null)     = false
         StringUtils.isNotBlank("")             = false
         StringUtils.isNotBlank(" ")       = false
         StringUtils.isNotBlank("bob")   = true
         StringUtils.isNotBlank("  bob  ") = true
         
        Parameters:
        str - the String to check, may be null
        Returns:
        true if the String is not empty and not null and not whitespace
        Since:
        2.0
      • equals

        public static boolean equals​(String str1,
                                     String str2)

        Compares two Strings, returning true if they are equal.

        nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

         StringUtils.equals(null, null)   = true
         StringUtils.equals(null, "abc")  = false
         StringUtils.equals("abc", null)  = false
         StringUtils.equals("abc", "abc") = true
         StringUtils.equals("abc", "ABC") = false
         
        Parameters:
        str1 - the first String, may be null
        str2 - the second String, may be null
        Returns:
        true if the Strings are equal, case sensitive, or both null
        See Also:
        String.equals(Object)
      • equalsIgnoreCase

        public static boolean equalsIgnoreCase​(String str1,
                                               String str2)

        Compares two Strings, returning true if they are equal ignoring the case.

        nulls are handled without exceptions. Two null references are considered equal. Comparison is case insensitive.

         StringUtils.equalsIgnoreCase(null, null)   = true
         StringUtils.equalsIgnoreCase(null, "abc")  = false
         StringUtils.equalsIgnoreCase("abc", null)  = false
         StringUtils.equalsIgnoreCase("abc", "abc") = true
         StringUtils.equalsIgnoreCase("abc", "ABC") = true
         
        Parameters:
        str1 - the first String, may be null
        str2 - the second String, may be null
        Returns:
        true if the Strings are equal, case insensitive, or both null
        See Also:
        String.equalsIgnoreCase(String)
      • getISO8601

        public static String getISO8601()
        Return a ISO 8601 formatted date
      • getISO8601

        public static String getISO8601​(Date date)
        Return a ISO 8601 formatted date
      • parseIMS8601

        public static Date parseIMS8601​(String timestamp)
        Parse an IMS 8601 date the strict ISO8601 but at the same time be flexible about it... We keep this in our own litte corner in case special adjustments are needed as we gain experience with the variatios in date formats in actual LTI Advantage tool practice. All the IMS examples use the most common ISO8601/UTC format as in: "startDateTime": "2018-03-06T20:05:02Z", "endDateTime": "2018-04-06T22:05:03Z" So we make particular effort to make sure this works and gives the right kind of date. And then for any other reasonable format, we try our best.
      • shiftJVMDateToTimeZone

        public static Date shiftJVMDateToTimeZone​(Date date,
                                                  String timeZone)
        Shift a date from the JVM Timezone to Another Timezone Usually the resulting date is then usually moved into UTC for transport
      • parseJSONObject

        public static org.json.simple.JSONObject parseJSONObject​(String str)
      • forceArray

        public static org.json.simple.JSONArray forceArray​(Object obj)
      • getArray

        public static org.json.simple.JSONArray getArray​(org.json.simple.JSONObject obj,
                                                         String key)
      • getObject

        public static org.json.simple.JSONObject getObject​(org.json.simple.JSONObject obj,
                                                           String key)
      • getString

        public static String getString​(org.json.simple.JSONObject obj,
                                       String key)
      • getLong

        public static Long getLong​(org.json.simple.JSONObject obj,
                                   String key)
      • getDouble

        public static Double getDouble​(org.json.simple.JSONObject obj,
                                       String key)