Skip to content

Commit

Permalink
EASY-2475: configurable timeout for call to easy-deposit-agreement-ge…
Browse files Browse the repository at this point in the history
…nerator (#65)
  • Loading branch information
rvanheest authored Jan 7, 2020
1 parent af02fd4 commit 8ff48a1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/assembly/dist/cfg/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ auth.ldap.user=cn=ldapadmin,dc=dans,dc=knaw,dc=nl
auth.ldap.password=changeme
daemon.http.port=20130
pdf-gen.url=http://localhost:20210/agreement
pdf-gen.conn-timeout-ms=5000
pdf-gen.read-timeout-ms=5000
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package nl.knaw.dans.easy.agreement

import java.net.URL

import nl.knaw.dans.easy.agreement.AgreementGenerator.PdfGenConfiguration
import nl.knaw.dans.easy.agreement.datafetch.{ Dataset, EasyUser }
import nl.knaw.dans.lib.logging.DebugEnhancedLogging
import org.apache.commons.io.IOUtils
Expand All @@ -29,18 +30,22 @@ import scalaj.http.{ BaseHttp, HttpResponse }
import scala.io.Source
import scala.util.{ Failure, Success, Try }

class AgreementGenerator(http: BaseHttp, url: URL) extends DebugEnhancedLogging {
class AgreementGenerator(http: BaseHttp, config: PdfGenConfiguration) extends DebugEnhancedLogging {

private implicit val jsonFormats: Formats = DefaultFormats

def generate(dataset: Dataset, isSample: Boolean)(outputStreamProvider: OutputStreamProvider): Try[Unit] = {
val json = datasetToJSON(dataset, isSample)
val jsonString = Serialization.write(json)
debug(s"calling easy-deposit-agreement-generator with body: $jsonString")
val response = http(url.toString).postData(jsonString).header("content-type", "application/json").exec {
case (OK_200, _, is) => IOUtils.copyLarge(is, outputStreamProvider())
case (_, _, is) => Source.fromInputStream(is).mkString
}
val response = http(config.url.toString)
.postData(jsonString)
.header("content-type", "application/json")
.timeout(config.connTimeout, config.readTimeout)
.exec {
case (OK_200, _, is) => IOUtils.copyLarge(is, outputStreamProvider())
case (_, _, is) => Source.fromInputStream(is).mkString
}
if (response.code == OK_200) Success(())
else Failure(GeneratorError(s"Could not generate agreement for dataset ${ dataset.datasetId }", HttpResponse(response.body.asInstanceOf[String], response.code, response.headers)))
}
Expand Down Expand Up @@ -69,3 +74,9 @@ class AgreementGenerator(http: BaseHttp, url: URL) extends DebugEnhancedLogging
("email" -> user.email)
}
}
object AgreementGenerator {
case class PdfGenConfiguration(url: URL,
connTimeout: Int,
readTimeout: Int,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import better.files.File
import better.files.File.root
import com.yourmediashelf.fedora.client.{ FedoraClient, FedoraCredentials }
import javax.naming.Context
import nl.knaw.dans.easy.agreement.AgreementGenerator.PdfGenConfiguration
import org.apache.commons.configuration.PropertiesConfiguration

case class Configuration(version: String,
serverPort: Int,
fedoraClient: FedoraClient,
ldapEnv: LdapEnv,
pdfGenerator: URL,
pdfGenerator: PdfGenConfiguration,
)

object Configuration {
Expand Down Expand Up @@ -57,7 +58,11 @@ object Configuration {
put(Context.SECURITY_CREDENTIALS, properties.getString("auth.ldap.password"))
put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory")
},
pdfGenerator = new URL(properties.getString("pdf-gen.url")),
pdfGenerator = PdfGenConfiguration(
url = new URL(properties.getString("pdf-gen.url")),
connTimeout = properties.getInt("pdf-gen.conn-timeout-ms"),
readTimeout = properties.getInt("pdf-gen.read-timeout-ms"),
)
)
}
}
2 changes: 2 additions & 0 deletions src/test/resources/debug-config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ auth.ldap.user=cn=ldapadmin,dc=dans,dc=knaw,dc=nl
auth.ldap.password=ldapadmin
daemon.http.port=20130
pdf-gen.url=http://localhost:20210/agreement
pdf-gen.conn-timeout-ms=5000
pdf-gen.read-timeout-ms=5000
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream
import java.nio.charset.StandardCharsets

import nl.knaw.dans.common.lang.dataset.AccessCategory
import nl.knaw.dans.easy.agreement.AgreementGenerator.PdfGenConfiguration
import nl.knaw.dans.easy.agreement.datafetch.{ Dataset, EasyUser }
import nl.knaw.dans.easy.agreement.fixture.{ FixedDateTime, TestSupportFixture }
import nl.knaw.dans.pf.language.emd.Term.Name
Expand Down Expand Up @@ -55,7 +56,7 @@ class AgreementGeneratorSpec extends TestSupportFixture with FixedDateTime with
private val date = mock[EmdDate]
private val rights = mock[EmdRights]

private val generator = new AgreementGenerator(Http, baseURL.url())
private val generator = new AgreementGenerator(Http, PdfGenConfiguration(baseURL.url(), 5000, 5000))

override protected def afterAll(): Unit = {
server.shutdown()
Expand Down

0 comments on commit 8ff48a1

Please sign in to comment.