org.imsglobal.lti2.LTI2Servlet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of basiclti-util Show documentation
Show all versions of basiclti-util Show documentation
BasicLTI Utilities are a set of utility classes to aid in the development of BasicLTI consumers and
providers. They deal with much of the heavy lifting and make the process more opaque to the developer.
The newest version!
/**
* Copyright (c) 2013 IMS GLobal Learning Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Author: Charles Severance
*/
package org.imsglobal.lti2;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.imsglobal.lti.BasicLTIConstants;
import org.imsglobal.lti.BasicLTIUtil;
import org.imsglobal.lti.launch.LtiVerificationResult;
import org.imsglobal.json.IMSJSONRequest;
import org.imsglobal.lti2.objects.consumer.ServiceOffered;
import org.imsglobal.lti2.objects.consumer.StandardServices;
import org.imsglobal.lti2.objects.consumer.ToolConsumer;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
/**
* Notes:
*
* This is a sample "Hello World" servlet for LTI2. It is a simple UI - mostly
* intended to exercise the APIs and show the way for servlet-based LTI2 code.
*
* Here are the web.xml entries:
*
* {@code
*
* SampleServlet
* org.imsglobal.lti2.LTI2Servlet
*
*
* SampleServlet
* /sample/*
*
* }
*
*
* Then navigate to:
* http://localhost/testservlet/sample/register
*
* A PHP endpoint is available at:
*
* https://source.sakaiproject.org/svn/basiclti/trunk/basiclti-docs/resources/docs/sakai-api-test
*
* The tp.php script is the Tool Provider registration endpoint in the PHP code
*
*/
@SuppressWarnings("deprecation")
public class LTI2Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Logger M_log = Logger.getLogger(LTI2Servlet.class.getName());
protected ServiceOffered LTI2ResultItem = null;
protected ServiceOffered LTI2LtiLinkSettings = null;
protected ServiceOffered LTI2ToolProxyBindingSettings = null;
protected ServiceOffered LTI2ToolProxySettings = null;
private static final String SVC_tc_profile = "tc_profile";
private static final String SVC_tc_registration = "tc_registration";
private static final String SVC_Settings = "Settings";
private static final String SVC_Result = "Result";
@SuppressWarnings("unused")
private static final String EMPTY_JSON_OBJECT = "{\n}\n";
private static final String APPLICATION_JSON = "application/json";
// Normally these would be in a database
private static String TEST_KEY = "42";
private static String TEST_SECRET = "zaphod";
// Pretending to be a database row :)
private static Map PERSIST = new TreeMap ();
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try {
doRequest(request, response);
} catch (Exception e) {
String ipAddress = request.getRemoteAddr();
String uri = request.getRequestURI();
M_log.log(Level.WARNING, "General LTI2 Failure URI="+uri+" IP=" + ipAddress);
e.printStackTrace();
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
doErrorJSON(request, response, null, "General failure", e);
}
}
@SuppressWarnings("unused")
protected void doRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("getServiceURL="+getServiceURL(request));
String ipAddress = request.getRemoteAddr();
System.out.println("LTI Service request from IP=" + ipAddress);
String rpi = request.getPathInfo();
String uri = request.getRequestURI();
String [] parts = uri.split("/");
if ( parts.length < 4 ) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
doErrorJSON(request, response, null, "Incorrect url format", null);
return;
}
String controller = parts[3];
if ( "register".equals(controller) ) {
doRegister(request,response);
return;
} else if ( "launch".equals(controller) ) {
doLaunch(request,response);
return;
} else if ( SVC_tc_profile.equals(controller) && parts.length == 5 ) {
String profile_id = parts[4];
getToolConsumerProfile(request,response,profile_id);
return;
} else if ( SVC_tc_registration.equals(controller) && parts.length == 5 ) {
String profile_id = parts[4];
registerToolProviderProfile(request, response, profile_id);
return;
} else if ( SVC_Result.equals(controller) && parts.length == 5 ) {
String sourcedid = parts[4];
handleResultRequest(request, response, sourcedid);
return;
} else if ( SVC_Settings.equals(controller) && parts.length >= 6 ) {
handleSettingsRequest(request, response, parts);
return;
}
IMSJSONRequest jsonRequest = new IMSJSONRequest(request);
if ( jsonRequest.valid ) {
System.out.println(jsonRequest.getPostBody());
}
response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
M_log.log(Level.WARNING, "Unknown request="+uri);
doErrorJSON(request, response, null, "Unknown request="+uri, null);
}
protected void doRegister(HttpServletRequest request, HttpServletResponse response)
{
// Reset our database
PERSIST.clear();
String launch_url = request.getParameter("launch_url");
response.setContentType("text/html");
String output = null;
if ( launch_url != null ) {
Properties ltiProps = new Properties();
ltiProps.setProperty(BasicLTIConstants.LTI_VERSION, LTI2Constants.LTI2_VERSION_STRING);
ltiProps.setProperty(LTI2Constants.REG_KEY,TEST_KEY);
ltiProps.setProperty(LTI2Constants.REG_PASSWORD,TEST_SECRET);
ltiProps.setProperty(BasicLTIUtil.BASICLTI_SUBMIT, "Press to Launch External Tool");
ltiProps.setProperty(BasicLTIConstants.LTI_MESSAGE_TYPE, BasicLTIConstants.LTI_MESSAGE_TYPE_TOOLPROXYREGISTRATIONREQUEST);
String serverUrl = getServiceURL(request);
ltiProps.setProperty(LTI2Constants.TC_PROFILE_URL,serverUrl + SVC_tc_profile + "/" + TEST_KEY);
ltiProps.setProperty(BasicLTIConstants.LAUNCH_PRESENTATION_RETURN_URL, serverUrl + "launch");
System.out.println("ltiProps="+ltiProps);
boolean dodebug = true;
output = BasicLTIUtil.postLaunchHTML(ltiProps, launch_url, dodebug);
} else {
output = "