Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Read response of attempted Sword
Browse files Browse the repository at this point in the history
This handles both non-responses and non-success responses when
attempting a Sword transfer.

closes #114
  • Loading branch information
JPrevost committed Jun 25, 2015
1 parent 558feac commit 468319b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app/models/Transfer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ object Transfer {
def make(subscriberId: Int, subscriptionId: Int, itemId: Int, action: String) = {
findById(create(subscriberId, subscriptionId, itemId, action).get.toInt).get
}

def delete(id: Int) = {
DB.withConnection { implicit c =>
SQL("DELETE FROM transfer WHERE id = {id}")
.on('id -> id).executeUpdate
}
}
}
18 changes: 18 additions & 0 deletions app/views/email/sword_transfer_failure.scala.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@*****************************************************************************
* Email template used to notify sysadmins of harvest start failures *
* Copyright (c) 2015 MIT Libraries *
*****************************************************************************@
@(item: Item, channel: Channel, trans: Transfer, error: String)
An error occurred attempting to deliver a Sword package:

Channel:
@channel.channelUrl
@channel.description

Subscriber:
@Subscriber.findById(trans.subscriberId).get.name

Link to Item: http://scoap3.topichub.org/item/@item.id

Exception text:
@error
39 changes: 34 additions & 5 deletions app/workers/Conveyor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ package workers
import java.util.Date

import akka.actor.Actor

import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Success, Failure}
import play.api._
import play.api.Play.current
import play.api.libs.ws._
import play.api.mvc._
import play.api.http.HeaderNames._

import models.{Agent, Channel, Hold, Interest, Item, Plan, Scheme, Subscriber,
Subscription, Topic, TopicPick, Transfer}
Subscription, Topic, TopicPick, Transfer, User}
import services.Emailer

/** Conveyor is the worker responsible for transmitting notifications
Expand Down Expand Up @@ -225,7 +226,6 @@ object Conveyor {
case "sword" => swordTransfer(item, chan, trans)
case _ => println("Don't know how to transfer via: " + chan.protocol)
}
chan.recordTransfer
}
}

Expand All @@ -236,8 +236,37 @@ object Conveyor {
.withAuth(channel.userId, channel.password, WSAuthScheme.BASIC)
println("About to deposit: " + req)
val resp = req.post(Packager.packageItemAsFile(item))
// TODO: need to read response, and determine success, also
// update transfer object to indicate we are OK

resp onComplete {
case Success(response) => readSwordResponse(response)
case Failure(t) => failedSwordTransferAttempt(t.getMessage)
}

def failedSwordTransferAttempt(t: String) = {
println("An error occurred attempting to submit a Sword package")
val sysadminEmails = User.allByRole("sysadmin").map(x => x.email).mkString(",")
val msg = views.txt.email.sword_transfer_failure(item, channel, trans, t).body
sendSwordFailureEmail(sysadminEmails, msg)
}

def sendSwordFailureEmail(addresses: String, msg: String) = {
println(msg)
Emailer.notify(addresses, "SCOAP3Hub: failure of sword delivery detected", msg)
Transfer.delete(trans.id)
}

def readSwordResponse(response: play.api.libs.ws.WSResponse) = {
if (response.status == 201) {
println("Successful Transfer of " + item.objKey)
channel.recordTransfer
} else {
println("The SWORD server did not accept the transfer. Response was " + response.toString)
// email admin details
val sysadminEmails = User.allByRole("sysadmin").map(x => x.email).mkString(",")
val msg = views.txt.email.sword_transfer_failure(item, channel, trans, response.toString).body
sendSwordFailureEmail(sysadminEmails, msg)
}
}
}

private def notify(item: Item, sub: Subscription) = {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/TransferSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,27 @@ class TransferSpec extends Specification {
Transfer.findById(tran.id) must equalTo(Some(tran))
}
}

"#delete" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val u = User.make("bob", "bob@example.com", "pwd", "role1")
val subscriber = Subscriber.make(u.id, "Sub Name", "cat", "contact", Some("link"), Some("logo"))
val scheme = Scheme.make("tag", "gentype", "cat", "desc", Some("link"), Some("logo"))
val t = Topic.make(scheme.id, "tag", "name")
val subscription = Subscription.make(subscriber.id, t.id, "deliver", subscriber.created, subscriber.created)
val pub = Publisher.make(u.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", Some(""), Some(""))
val ct = ContentType.make("cttag", "ctlabel", "ctdesc", Some(""))
val rmap = ResourceMap.make("rmaptag", "rmapdesc", Some(""))
var col = Collection.make(pub.id, ct.id, rmap.id, "coll", "desc", "open")
val item = Item.make(col.id, ct.id, "location", "scoap:abc:123")

Transfer.findById(1) must equalTo(None)
val tran = Transfer.make(subscriber.id, subscription.id, item.id, "doit")
Transfer.findById(tran.id) must equalTo(Some(tran))

Transfer.delete(tran.id)
Transfer.findById(1) must equalTo(None)
}
}
}
}

0 comments on commit 468319b

Please sign in to comment.