/
ApplicationsServlet.scala
142 lines (124 loc) · 5.91 KB
/
ApplicationsServlet.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package fi.vm.sade.omatsivut.servlet
import fi.vm.sade.groupemailer.GroupEmailComponent
import fi.vm.sade.hakemuseditori._
import fi.vm.sade.hakemuseditori.hakemus.domain.HakemusMuutos
import fi.vm.sade.hakemuseditori.hakemus.{ApplicationValidatorComponent, Fetch, HakemusInfo, HakemusRepositoryComponent, SpringContextComponent}
import fi.vm.sade.hakemuseditori.json.JsonFormats
import fi.vm.sade.hakemuseditori.localization.TranslationsComponent
import fi.vm.sade.hakemuseditori.lomake.LomakeRepositoryComponent
import fi.vm.sade.hakemuseditori.user.Oppija
import fi.vm.sade.hakemuseditori.valintatulokset.ValintatulosServiceComponent
import fi.vm.sade.omatsivut.config.AppConfig.AppConfig
import fi.vm.sade.omatsivut.hakemuspreview.HakemusPreviewGeneratorComponent
import fi.vm.sade.omatsivut.security.AuthenticationRequiringServlet
import fi.vm.sade.omatsivut.vastaanotto.{Vastaanotto, VastaanottoComponent}
import fi.vm.sade.utils.cas.{CasAuthenticatingClient, CasClient, CasParams}
import org.http4s.client.blaze
import org.json4s.DefaultFormats
import org.json4s.jackson.Serialization
import org.scalatra._
import org.scalatra.json._
import scala.util.{Failure, Success}
trait ApplicationsServletContainer {
this: HakemusEditoriComponent with
LomakeRepositoryComponent with
HakemusRepositoryComponent with
ValintatulosServiceComponent with
ApplicationValidatorComponent with
HakemusPreviewGeneratorComponent with
SpringContextComponent with
GroupEmailComponent with
VastaanottoComponent with
TranslationsComponent =>
class ApplicationsServlet(val appConfig: AppConfig) extends OmatSivutServletBase with JsonFormats with JacksonJsonSupport with AuthenticationRequiringServlet with HakemusEditoriUserContext {
def user = Oppija(personOid())
private val hakemusEditori = newEditor(this)
private val securitySettings = appConfig.settings.securitySettings
private val blazeHttpClient = blaze.defaultClient
private val casClient = new CasClient(securitySettings.casUrl, blazeHttpClient)
private val serviceUrl = appConfig.settings.authenticationServiceConfig.url + "/"
private val casParams = CasParams(serviceUrl, securitySettings.casUsername, securitySettings.casPassword)
private val httpClient = CasAuthenticatingClient(casClient, casParams, blazeHttpClient, Some("omatsivut.omatsivut.backend"), "JSESSIONID")
protected val applicationDescription = "Oppijan henkilökohtaisen palvelun REST API, jolla voi hakea ja muokata hakemuksia ja omia tietoja"
before() {
contentType = formats("json")
}
get("/tuloskirje/:hakuOid") {
val hakuOid = params("hakuOid")
hakemusEditori.fetchTuloskirje(request, personOid(), hakuOid) match {
case Some(tuloskirje) => Ok(tuloskirje, Map(
"Content-Type" -> "application/octet-stream",
"Content-Disposition" -> "attachment; filename=tuloskirje.pdf"))
case None => NotFound("error" -> "Not found")
}
}
get("/") {
implicit val formats = DefaultFormats
val timeout = 1000*30L
val oppijanumero: String = personOid()
val allOids: Iterable[String] = oppijanumerorekisteriService.fetchAllDuplicateOids(oppijanumero)
var allSuccess = true
val allHakemukset: List[HakemusInfo] = allOids.toList.flatMap(oid => {
hakemusEditori.fetchByPersonOid(request, oid, Fetch) match {
case FullSuccess(hakemukset) => hakemukset
case PartialSuccess(partialHakemukset, exceptions) =>
exceptions.foreach(logger.warn(s"Failed to fetch all applications for oid $oid",_))
allSuccess = false
partialHakemukset
case FullFailure(exceptions) =>
exceptions.foreach(logger.error(s"Failed to fetch applications for oid $oid", _))
allSuccess = false
List.empty
}
}).sortBy(_.hakemus.received).reverse
Map("allApplicationsFetched" -> allSuccess, "applications" -> allHakemukset)
}
put("/:oid") {
val content: String = request.body
val updated = Serialization.read[HakemusMuutos](content)
hakemusEditori.updateHakemus(request, updated) match {
case Success(body) => ActionResult(ResponseStatus(200), body, Map.empty)
case Failure(e: ForbiddenException) => ActionResult(ResponseStatus(403), "error" -> "Forbidden", Map.empty)
case Failure(e: ValidationException) => ActionResult(ResponseStatus(400), e.validationErrors, Map.empty)
case Failure(e) => InternalServerError("error" -> "Internal service unavailable")
}
}
post("/validate/:oid") {
val muutos = Serialization.read[HakemusMuutos](request.body)
hakemusEditori.validateHakemus(muutos) match {
case Some(hakemusInfo) => hakemusInfo
case _ => InternalServerError("error" -> "Internal service unavailable")
}
}
get("/preview/:oid") {
newHakemusPreviewGenerator(language).generatePreview(request, personOid(), params("oid")) match {
case Some(previewHtml) =>
contentType = formats("html")
Ok(previewHtml)
case None =>
NotFound("error" -> "Not found")
}
}
post("/vastaanota/:hakemusOid/hakukohde/:hakukohdeOid") {
val hakemusOid = params("hakemusOid")
val hakukohdeOid = params("hakukohdeOid")
val henkiloOid = personOid()
val vastaanotto = Serialization.read[Vastaanotto](request.body)
hakemusEditori.fetchByHakemusOid(request, henkiloOid, hakemusOid, Fetch) match {
case Some(hakemus) => {
vastaanottoService.vastaanota(
request,
hakemusOid,
hakukohdeOid,
henkiloOid,
vastaanotto,
hakemus
)
}
case None =>
logger.error(s"Vastaanotto failed because no application found for: henkiloOid $henkiloOid, hakemusOid $hakemusOid")
NotFound("error" -> "Not found")
}
}
}
}