|
1 | 1 | package org.imsglobal.pox; |
2 | 2 |
|
3 | 3 | import java.io.ByteArrayInputStream; |
| 4 | +import java.io.IOException; |
4 | 5 | import java.io.Reader; |
5 | 6 | import java.net.URLDecoder; |
6 | 7 | import java.security.MessageDigest; |
|
27 | 28 | import net.oauth.SimpleOAuthValidator; |
28 | 29 | import net.oauth.server.OAuthServlet; |
29 | 30 | import net.oauth.signature.OAuthSignatureMethod; |
| 31 | +import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; |
| 32 | +import oauth.signpost.exception.OAuthException; |
30 | 33 |
|
| 34 | +import org.apache.commons.codec.binary.Base64; |
31 | 35 | import org.apache.commons.lang3.StringEscapeUtils; |
32 | | -import org.apache.commons.httpclient.util.Base64; |
| 36 | +import org.apache.http.HttpResponse; |
| 37 | +import org.apache.http.client.HttpResponseException; |
| 38 | +import org.apache.http.client.methods.HttpPost; |
| 39 | +import org.apache.http.entity.StringEntity; |
| 40 | +import org.apache.http.impl.client.DefaultHttpClient; |
33 | 41 | import org.imsglobal.basiclti.XMLMap; |
34 | 42 | import org.w3c.dom.Document; |
35 | 43 | import org.w3c.dom.Element; |
@@ -224,7 +232,7 @@ public void loadFromRequest(HttpServletRequest request) |
224 | 232 | try { |
225 | 233 | MessageDigest md = MessageDigest.getInstance("SHA1"); |
226 | 234 | md.update(postBody.getBytes()); |
227 | | - byte[] output = Base64.encode(md.digest()); |
| 235 | + byte[] output = Base64.encodeBase64(md.digest()); |
228 | 236 | String hash = new String(output); |
229 | 237 | // System.out.println("HASH="+hash); |
230 | 238 | if ( ! hash.equals(oauth_body_hash) ) { |
@@ -481,35 +489,72 @@ public String getResponse(String description, String major, String severity, |
481 | 489 | bodyString, newLine); |
482 | 490 |
|
483 | 491 | } |
| 492 | + |
| 493 | + static final String replaceResultMessage = |
| 494 | + "<?xml version = \"1.0\" encoding = \"UTF-8\"?>"+ |
| 495 | + "<imsx_POXEnvelopeRequest xmlns=\"http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0\">"+ |
| 496 | + " <imsx_POXHeader>"+ |
| 497 | + " <imsx_POXRequestHeaderInfo>"+ |
| 498 | + " <imsx_version>V1.0</imsx_version>"+ |
| 499 | + " </imsx_POXRequestHeaderInfo>"+ |
| 500 | + " </imsx_POXHeader>"+ |
| 501 | + " <imsx_POXBody>"+ |
| 502 | + " <replaceResultRequest>"+ |
| 503 | + " <resultRecord>"+ |
| 504 | + " <sourcedGUID>"+ |
| 505 | + " <sourcedId>%s</sourcedId>"+ |
| 506 | + " </sourcedGUID>"+ |
| 507 | + " <result>"+ |
| 508 | + " <resultScore>"+ |
| 509 | + " <textString>%s</textString>"+ |
| 510 | + " </resultScore>"+ |
| 511 | + " %s"+ |
| 512 | + " </result>"+ |
| 513 | + " </resultRecord>"+ |
| 514 | + " </replaceResultRequest>"+ |
| 515 | + " </imsx_POXBody>"+ |
| 516 | + "</imsx_POXEnvelopeRequest>"; |
| 517 | + |
| 518 | + static final String resultDataText = "<resultData><text>%s</text></resultData>"; |
| 519 | + |
| 520 | + static final String resultDataUrl = "<resultData><text>%s</text></resultData>"; |
| 521 | + |
| 522 | + public static void sendReplaceResult(String url, String key, String secret, String sourcedid, String score) throws IOException, OAuthException { |
| 523 | + sendReplaceResult(url, key, secret, sourcedid, score, null); |
| 524 | + } |
| 525 | + |
| 526 | + public static void sendReplaceResult(String url, String key, String secret, String sourcedid, String score, String resultData) throws IOException, OAuthException { |
| 527 | + sendReplaceResult(url, key, secret, sourcedid, score, resultData, false); |
| 528 | + } |
| 529 | + |
| 530 | + public static void sendReplaceResult(String url, String key, String secret, String sourcedid, String score, String resultData, Boolean isUrl) throws IOException, OAuthException { |
| 531 | + String dataXml = ""; |
| 532 | + if (resultData != null) { |
| 533 | + if (isUrl) { |
| 534 | + dataXml = String.format(resultDataUrl, StringEscapeUtils.escapeXml(resultData)); |
| 535 | + } else { |
| 536 | + dataXml = String.format(resultDataText, StringEscapeUtils.escapeXml(resultData)); |
| 537 | + } |
| 538 | + } |
| 539 | + String xml = String.format(replaceResultMessage, StringEscapeUtils.escapeXml(sourcedid), |
| 540 | + StringEscapeUtils.escapeXml(score), dataXml); |
| 541 | + |
| 542 | + CommonsHttpOAuthConsumer signer = new CommonsHttpOAuthConsumer(key, secret); |
| 543 | + HttpPost request = new HttpPost(url); |
| 544 | + request.setHeader("Content-Type", "application/xml"); |
| 545 | + request.setEntity(new StringEntity(xml, "UTF-8")); |
| 546 | + signer.sign(request); |
| 547 | + DefaultHttpClient client = new DefaultHttpClient(); |
| 548 | + HttpResponse response = client.execute(request); |
| 549 | + if (response.getStatusLine().getStatusCode() >= 400) { |
| 550 | + throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); |
| 551 | + } |
| 552 | + } |
484 | 553 |
|
485 | 554 | /** Unit Tests */ |
486 | | - static final String inputTestData = "<?xml version = \"1.0\" encoding = \"UTF-8\"?>\n" + |
487 | | - "<imsx_POXEnvelopeRequest xmlns = \"http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0\">\n" + |
488 | | - "<imsx_POXHeader>\n" + |
489 | | - "<imsx_POXRequestHeaderInfo>\n" + |
490 | | - "<imsx_version>V1.0</imsx_version>\n" + |
491 | | - "<imsx_messageIdentifier>999999123</imsx_messageIdentifier>\n" + |
492 | | - "</imsx_POXRequestHeaderInfo>\n" + |
493 | | - "</imsx_POXHeader>\n" + |
494 | | - "<imsx_POXBody>\n" + |
495 | | - "<replaceResultRequest>\n" + |
496 | | - "<resultRecord>\n" + |
497 | | - "<sourcedGUID>\n" + |
498 | | - "<sourcedId>3124567</sourcedId>\n" + |
499 | | - "</sourcedGUID>\n" + |
500 | | - "<result>\n" + |
501 | | - "<resultScore>\n" + |
502 | | - "<language>en-us</language>\n" + |
503 | | - "<textString>A</textString>\n" + |
504 | | - "</resultScore>\n" + |
505 | | - "</result>\n" + |
506 | | - "</resultRecord>\n" + |
507 | | - "</replaceResultRequest>\n" + |
508 | | - "</imsx_POXBody>\n" + |
509 | | - "</imsx_POXEnvelopeRequest>"; |
510 | | - |
511 | 555 | public static void runTest() { |
512 | 556 | System.out.println("Runnig test."); |
| 557 | + String inputTestData = String.format(replaceResultMessage, "3124567", "A", ""); |
513 | 558 | IMSPOXRequest pox = new IMSPOXRequest(inputTestData); |
514 | 559 | System.out.println("Version = "+pox.getHeaderVersion()); |
515 | 560 | System.out.println("Operation = "+pox.getOperation()); |
|
0 commit comments