Class BasicLTIUtil
- java.lang.Object
-
- org.tsugi.basiclti.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.xmlSample 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>
-
-
Field Summary
Fields Modifier and Type Field Description static StringEXTRA_BUTTON_HTMLstatic StringEXTRA_ERROR_TIMEOUTstatic StringEXTRA_FORM_IDstatic StringEXTRA_HTTP_POPUPstatic StringEXTRA_HTTP_POPUP_FALSEstatic StringEXTRA_JAVASCRIPTstatic StringISO_8601_FORMAT
-
Constructor Summary
Constructors Constructor Description BasicLTIUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static StringadaptToCustomPropertyName(String propertyName)A simple utility method which implements the specified semantics of custom properties.static booleancheckProperties(Map<String,String> postProp, String url, String method, String oauth_consumer_key, String oauth_consumer_secret)Check if the fields are properly signedstatic booleancheckProperties(Properties postProp, String url, String method, String oauth_consumer_key, String oauth_consumer_secret)Deprecated.static Map<String,String>convertToMap(Properties properties)Simple utility method to help with the migration from Properties to Map. static PropertiesconvertToProperties(Map<String,String> map)Deprecated.Should migrate to Mapsignatures. static booleanequals(String str1, String str2)Compares two Strings, returningtrueif they are equal.static booleanequalsIgnoreCase(String str1, String str2)Compares two Strings, returningtrueif they are equal ignoring the case.static org.json.simple.JSONArrayforceArray(Object obj)static org.json.simple.JSONArraygetArray(org.json.simple.JSONObject obj, String key)static StringgetBrowserSignature(HttpServletRequest request)static DoublegetDouble(org.json.simple.JSONObject obj, String key)static StringgetISO8601()Return a ISO 8601 formatted datestatic StringgetISO8601(Date date)Return a ISO 8601 formatted datestatic LonggetLong(org.json.simple.JSONObject obj, String key)static StringgetOAuthURL(String method, String url, String oauth_consumer_key, String oauth_secret)getOAuthURL - Form a GET request signed by OAuthstatic StringgetOAuthURL(String method, String url, String oauth_consumer_key, String oauth_secret, String signature)getOAuthURL - Form a GET request signed by OAuthstatic org.json.simple.JSONObjectgetObject(org.json.simple.JSONObject obj, String key)static StringgetRealPath(String servletUrl, String extUrl)Simple utility method deal with a request that has the wrong URL when behind a proxy.static StringgetRealPath(HttpServletRequest request, String extUrl)static intgetResponseCode(HttpURLConnection connection)getResponseCode - Read the HTTP Responsestatic StringgetString(org.json.simple.JSONObject obj, String key)static Stringhtmlspecialchars(String input)static booleanisBlank(String str)Checks if a String is whitespace, empty ("") or null.static booleanisNotBlank(String str)Checks if a String is not empty (""), not null and not whitespace only.static booleanisRequest(HttpServletRequest request)static StringmapKeyName(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".static StringmergeCSV(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 onestatic booleanparseDescriptor(Map<String,String> launch_info, Map<String,String> postProp, String descriptor)static booleanparseDescriptor(Properties launch_info, Properties postProp, String descriptor)Deprecated.static DateparseIMS8601(String timestamp)Parse an IMS 8601 date the strict ISO8601 but at the same time be flexible about it...static org.json.simple.JSONObjectparseJSONObject(String str)static StringpostLaunchHTML(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.static StringpostLaunchHTML(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.static StringpostLaunchHTML(Properties cleanProperties, String endpoint, String launchtext, boolean autosubmit, boolean debug, Map<String,String> extra)Deprecated.static StringpostLaunchHTML(Properties cleanProperties, String endpoint, String launchtext, boolean debug, Map<String,String> extra)Deprecated.static StringprepareForExport(String descriptor)static StringreadHttpResponse(HttpURLConnection connection)readHttpResponse - Read the HTTP Responsestatic voidsendHTMLPage(HttpServletResponse res, String body)static HttpURLConnectionsendOAuthURL(String method, String url, String oauth_consumer_key, String oauth_secret)getOAuthURL - Form a GET request signed by OAuthstatic voidsetProperty(Map<String,String> map, String key, String value)Mutates the passed Mapmap variable. static voidsetProperty(Properties props, String key, String value)Deprecated.static DateshiftJVMDateToTimeZone(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 transportstatic Map<String,String>signProperties(Map<String,String> postProp, String url, String method, String oauth_consumer_key, String oauth_consumer_secret, String tool_consumer_instance_guid, String tool_consumer_instance_description, String tool_consumer_instance_url, String tool_consumer_instance_name, String tool_consumer_instance_contact_email, Map<String,String> extra)Add the necessary fields and sign.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.static PropertiessignProperties(Properties postProp, String url, String method, String oauth_consumer_key, String oauth_consumer_secret, String org_id, String org_desc, String org_url, Map<String,String> extra)static PropertiessignProperties(Properties postProp, String url, String method, String oauth_consumer_key, String oauth_consumer_secret, Map<String,String> extra)static StringtoNull(String str)static StringvalidateDescriptor(String descriptor)static ObjectvalidateMessage(HttpServletRequest request, String URL, String oauth_secret, String expected_oauth_key)
-
-
-
Field Detail
-
ISO_8601_FORMAT
public static final String ISO_8601_FORMAT
- See Also:
- Constant Field Values
-
EXTRA_BUTTON_HTML
public static final String EXTRA_BUTTON_HTML
- See Also:
- Constant Field Values
-
EXTRA_ERROR_TIMEOUT
public static final String EXTRA_ERROR_TIMEOUT
- See Also:
- Constant Field Values
-
EXTRA_HTTP_POPUP
public static final String EXTRA_HTTP_POPUP
- See Also:
- Constant Field Values
-
EXTRA_HTTP_POPUP_FALSE
public static final String EXTRA_HTTP_POPUP_FALSE
- See Also:
- Constant Field Values
-
EXTRA_JAVASCRIPT
public static final String EXTRA_JAVASCRIPT
- See Also:
- Constant Field Values
-
EXTRA_FORM_ID
public static final String EXTRA_FORM_ID
- See Also:
- Constant Field Values
-
-
Method Detail
-
isRequest
public static boolean isRequest(HttpServletRequest request)
-
validateMessage
public static Object validateMessage(HttpServletRequest request, String URL, String oauth_secret, String expected_oauth_key)
-
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 Properties signProperties(Properties postProp, String url, String method, String oauth_consumer_key, String oauth_consumer_secret, Map<String,String> extra)
Deprecated.Add the necessary fields and sign.- Parameters:
postProp-url-method-oauth_consumer_key-oauth_consumer_secret-extra-- Returns:
-
signProperties
public static Properties signProperties(Properties postProp, String url, String method, String oauth_consumer_key, String oauth_consumer_secret, String org_id, String org_desc, String org_url, Map<String,String> extra)
Deprecated.Add the necessary fields and sign.- Parameters:
postProp-url-method-oauth_consumer_key-oauth_consumer_secret-org_id- See:BasicLTIConstants.TOOL_CONSUMER_INSTANCE_GUIDorg_desc- See:BasicLTIConstants.TOOL_CONSUMER_INSTANCE_DESCRIPTIONorg_url- See:BasicLTIConstants.TOOL_CONSUMER_INSTANCE_URLextra-- Returns:
-
signProperties
public static Map<String,String> signProperties(Map<String,String> postProp, String url, String method, String oauth_consumer_key, String oauth_consumer_secret, String tool_consumer_instance_guid, String tool_consumer_instance_description, String tool_consumer_instance_url, String tool_consumer_instance_name, String tool_consumer_instance_contact_email, Map<String,String> extra)
Add the necessary fields and sign.- Parameters:
postProp-url-method-oauth_consumer_key-oauth_consumer_secret-tool_consumer_instance_guid- See:BasicLTIConstants.TOOL_CONSUMER_INSTANCE_GUIDtool_consumer_instance_description- See:BasicLTIConstants.TOOL_CONSUMER_INSTANCE_DESCRIPTIONtool_consumer_instance_url- See:BasicLTIConstants.TOOL_CONSUMER_INSTANCE_URLtool_consumer_instance_name- See:BasicLTIConstants.TOOL_CONSUMER_INSTANCE_NAMEtool_consumer_instance_contact_email- See:BasicLTIConstants.TOOL_CONSUMER_INSTANCE_CONTACT_EMAILextra-- 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(Properties postProp, String url, String method, String oauth_consumer_key, String oauth_consumer_secret)
Deprecated.Check if the properties are properly signed- Parameters:
postProp-url-method-oauth_consumer_key-oauth_consumer_secret-- 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)
Deprecated.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)
Deprecated.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 autosubmitteddebug- 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 autosubmittedextra- 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)
Deprecated.- 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:
-
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 Mapmap 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)
Deprecated.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-
-
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.
-
getRealPath
public static String getRealPath(HttpServletRequest request, String extUrl)
-
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 Mapsignatures. Simple utility method to help with the migration from Mapto 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:
trueif 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:
trueif 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
trueif they are equal.nulls are handled without exceptions. Twonullreferences 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 nullstr2- the second String, may be null- Returns:
trueif the Strings are equal, case sensitive, or bothnull- See Also:
String.equals(Object)
-
equalsIgnoreCase
public static boolean equalsIgnoreCase(String str1, String str2)
Compares two Strings, returning
trueif they are equal ignoring the case.nulls are handled without exceptions. Twonullreferences 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 nullstr2- the second String, may be null- Returns:
trueif the Strings are equal, case insensitive, or bothnull- See Also:
String.equalsIgnoreCase(String)
-
getISO8601
public static String getISO8601()
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)
-
getBrowserSignature
public static String getBrowserSignature(HttpServletRequest request)
-
sendHTMLPage
public static void sendHTMLPage(HttpServletResponse res, String body)
-
-