Skip to content

Commit

Permalink
SAK-30372 - Switch the implementation to Jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
csev committed Feb 27, 2016
1 parent 5020404 commit 13d21cc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* $URL$
* $Id$
*
* Copyright (c) 2006-2009 The Sakai Foundation
* Copyright (c) 2016- Charles R. Severance
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,8 +19,16 @@

package org.sakaiproject.basiclti.util;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import java.util.ArrayList;

import org.tsugi.casa.objects.Launch;
import org.tsugi.casa.objects.Use;
import org.tsugi.casa.objects.Contact;
import org.tsugi.casa.objects.Original;
import org.tsugi.casa.objects.Identity;
import org.tsugi.casa.objects.Application;

import org.tsugi.jackson.JacksonUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -46,7 +54,7 @@ public class SakaiCASAUtil {

private static Log M_log = LogFactory.getLog(SakaiCASAUtil.class);

public static JSONObject getCASAEntry(String toolRegistration)
public static Application getCASAEntry(String toolRegistration)
{
Tool theTool = ToolManager.getTool(toolRegistration);
if ( theTool == null ) return null;
Expand All @@ -57,36 +65,27 @@ public static JSONObject getCASAEntry(String toolRegistration)
sample = true;
}

JSONObject jsonResponse = new JSONObject();
JSONObject identity = new JSONObject();
identity.put("product_instance_guid", cnf.getService_owner_id());
identity.put("originator_id", cnf.getService_owner_id());
identity.put("id", toolRegistration);
jsonResponse.put("identity", identity);
JSONObject use = new JSONObject();
use.put(CASAUtil.TITLE_SCHEMA,theTool.getTitle());
use.put(CASAUtil.TEXT_SCHEMA,theTool.getDescription());
JSONArray contact = new JSONArray();
JSONObject name = new JSONObject();
name.put("name", cnf.getService_owner_owner_name());
name.put("email", cnf.getService_owner_support_email());
contact.add(name);
use.put(CASAUtil.CONTACT_SCHEMA, contact);
use.put(CASAUtil.ICON_SCHEMA, "https://www.apereo.org/sites/all/themes/apereo/images/apereo-logo-white-bg.png");

JSONObject launch = new JSONObject();
launch.put("launch_url", ServerConfigurationService.getServerUrl() + "/imsblis/provider/"+toolRegistration);
use.put(CASAUtil.LAUNCH_SCHEMA, launch);

JSONObject original = new JSONObject();
original.put("use", use);
original.put("timestamp", "2015-01-02T22:17:00.371Z");
original.put("uri", ServerConfigurationService.getServerUrl());
original.put("share", Boolean.TRUE);
original.put("propagate", Boolean.TRUE);
jsonResponse.put("original", original);

return jsonResponse;
Launch launch = new Launch();
launch.setLaunch_url(ServerConfigurationService.getServerUrl() + "/imsblis/provider/"+toolRegistration);

Use use = new Use(launch);
// TODO: Fix this
use.setIcon_url("https://www.apereo.org/sites/all/themes/apereo/images/apereo-logo-white-bg.png");
use.setTitle(theTool.getTitle());
use.setText(theTool.getDescription());
use.addContact(new Contact(cnf.getService_owner_owner_name(), cnf.getService_owner_support_email()));

Original orig = new Original(use);
orig.setUri(ServerConfigurationService.getServerUrl());
orig.setPropagate(Boolean.TRUE);
orig.setShare(Boolean.TRUE);
// TODO: Fix this when I know the rules
orig.setTimestamp("2015-01-02T22:17:00.371Z");

Identity identity = new Identity(cnf.getService_owner_id(), toolRegistration);

Application app = new Application(identity,orig);
return app;
}

}
12 changes: 12 additions & 0 deletions basiclti/basiclti-portlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@
<artifactId>gradebook-service-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
import org.tsugi.basiclti.BasicLTIUtil;
import org.tsugi.basiclti.BasicLTIProviderUtil;

import org.tsugi.casa.objects.Application;

import org.tsugi.jackson.JacksonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;

import org.sakaiproject.authz.api.SecurityAdvisor;
import org.sakaiproject.authz.cover.SecurityService;
import org.sakaiproject.lti.api.BLTIProcessor;
Expand Down Expand Up @@ -260,8 +265,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}

if ( "/casa.json".equals(request.getPathInfo()) ) {
handleCASAList(request, response);
return;
if ( ServerConfigurationService.getBoolean("casa.provider", true)) {
handleCASAList(request, response);
return;
} else {
M_log.warn("CASA Provider is Disabled IP=" + ipAddress);
response.sendError(HttpServletResponse.SC_FORBIDDEN,
"CASA Provider is Disabled");
return;
}
}

if (M_log.isDebugEnabled()) {
Expand Down Expand Up @@ -926,23 +938,23 @@ public void run() {
ltiService.insertMembershipsJob(siteId, membershipsId, membershipsUrl, oauth_consumer_key, callbackType);
}

private void handleCASAList(HttpServletRequest request, HttpServletResponse response) {

JSONArray retval = new JSONArray();
String allowedToolsConfig = ServerConfigurationService.getString("basiclti.provider.allowedtools", "");
private void handleCASAList(HttpServletRequest request, HttpServletResponse response)
{
ArrayList<Application> apps = new ArrayList<Application>();

String allowedToolsConfig = ServerConfigurationService.getString("basiclti.provider.allowedtools", "");
String[] allowedTools = allowedToolsConfig.split(":");
List<String> allowedToolsList = Arrays.asList(allowedTools);

for (String toolId : allowedToolsList) {
JSONObject entry = SakaiCASAUtil.getCASAEntry(toolId);
retval.add(entry);
Application app = SakaiCASAUtil.getCASAEntry(toolId);
apps.add(app);
}

// System.out.println("retval="+retval);
try {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.write(retval.toString());
out.write(JacksonUtil.prettyPrint(apps));
}
catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit 13d21cc

Please sign in to comment.