Thu, 13 May 2021 19:31:09 +0200
fixes wrong handling of feeds - only one channel per feed is allowed
1 /*
2 * Copyright 2021 Mike Becker. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
26 package de.uapcore.lightpit.servlet
28 import de.uapcore.lightpit.AbstractServlet
29 import de.uapcore.lightpit.HttpRequest
30 import de.uapcore.lightpit.dao.DataAccessObject
31 import de.uapcore.lightpit.entities.*
32 import de.uapcore.lightpit.types.IssueCategory
33 import de.uapcore.lightpit.types.IssueStatus
34 import de.uapcore.lightpit.types.VersionStatus
35 import de.uapcore.lightpit.types.WebColor
36 import de.uapcore.lightpit.util.AllFilter
37 import de.uapcore.lightpit.util.IssueFilter
38 import de.uapcore.lightpit.util.IssueSorter.Companion.DEFAULT_ISSUE_SORTER
39 import de.uapcore.lightpit.util.SpecificFilter
40 import de.uapcore.lightpit.viewmodel.*
41 import java.sql.Date
42 import javax.servlet.annotation.WebServlet
44 @WebServlet(urlPatterns = ["/projects/*"])
45 class ProjectServlet : AbstractServlet() {
47 init {
48 get("/", this::projects)
49 get("/%project", this::project)
50 get("/%project/issues/%version/%component/", this::project)
51 get("/%project/edit", this::projectForm)
52 get("/-/create", this::projectForm)
53 post("/-/commit", this::projectCommit)
55 get("/%project/versions/", this::versions)
56 get("/%project/versions/%version/edit", this::versionForm)
57 get("/%project/versions/-/create", this::versionForm)
58 post("/%project/versions/-/commit", this::versionCommit)
60 get("/%project/components/", this::components)
61 get("/%project/components/%component/edit", this::componentForm)
62 get("/%project/components/-/create", this::componentForm)
63 post("/%project/components/-/commit", this::componentCommit)
65 get("/%project/issues/%version/%component/%issue", this::issue)
66 get("/%project/issues/%version/%component/%issue/edit", this::issueForm)
67 post("/%project/issues/%version/%component/%issue/comment", this::issueComment)
68 get("/%project/issues/%version/%component/-/create", this::issueForm)
69 post("/%project/issues/%version/%component/-/commit", this::issueCommit)
70 }
72 private fun projects(http: HttpRequest, dao: DataAccessObject) {
73 val projects = dao.listProjects()
74 val projectInfos = projects.map {
75 ProjectInfo(
76 project = it,
77 versions = dao.listVersions(it),
78 components = emptyList(), // not required in this view
79 issueSummary = dao.collectIssueSummary(it)
80 )
81 }
83 with(http) {
84 view = ProjectsView(projectInfos)
85 navigationMenu = projectNavMenu(projects)
86 styleSheets = listOf("projects")
87 render("projects")
88 }
89 }
91 private fun activeProjectNavMenu(
92 projects: List<Project>,
93 projectInfo: ProjectInfo,
94 selectedVersion: Version? = null,
95 selectedComponent: Component? = null
96 ) =
97 projectNavMenu(
98 projects,
99 projectInfo.versions,
100 projectInfo.components,
101 projectInfo.project,
102 selectedVersion,
103 selectedComponent
104 )
106 sealed class LookupResult<T> {
107 class NotFound<T> : LookupResult<T>()
108 data class Found<T>(val elem: T?) : LookupResult<T>()
109 }
111 private fun <T : HasNode> HttpRequest.lookupPathParam(paramName: String, list: List<T>): LookupResult<T> {
112 val node = pathParams[paramName]
113 return if (node == null || node == "-") {
114 LookupResult.Found(null)
115 } else {
116 val result = list.find { it.node == node }
117 if (result == null) {
118 LookupResult.NotFound()
119 } else {
120 LookupResult.Found(result)
121 }
122 }
123 }
125 private fun obtainProjectInfo(http: HttpRequest, dao: DataAccessObject): ProjectInfo? {
126 val project = dao.findProjectByNode(http.pathParams["project"] ?: "") ?: return null
128 val versions: List<Version> = dao.listVersions(project)
129 val components: List<Component> = dao.listComponents(project)
131 return ProjectInfo(
132 project,
133 versions,
134 components,
135 dao.collectIssueSummary(project)
136 )
137 }
139 private fun sanitizeNode(name: String): String {
140 val san = name.replace(Regex("[/\\\\]"), "-")
141 return if (san.startsWith(".")) {
142 "v$san"
143 } else {
144 san
145 }
146 }
148 private fun feedPath(project: Project) = "feed/${project.node}/issues.rss"
150 data class PathInfos(
151 val projectInfo: ProjectInfo,
152 val version: Version?,
153 val component: Component?
154 ) {
155 val project = projectInfo.project
156 val issuesHref by lazyOf("projects/${project.node}/issues/${version?.node ?: "-"}/${component?.node ?: "-"}/")
157 }
159 private fun withPathInfo(http: HttpRequest, dao: DataAccessObject): PathInfos? {
160 val projectInfo = obtainProjectInfo(http, dao)
161 if (projectInfo == null) {
162 http.response.sendError(404)
163 return null
164 }
166 val version = when (val result = http.lookupPathParam("version", projectInfo.versions)) {
167 is LookupResult.NotFound -> {
168 http.response.sendError(404)
169 return null
170 }
171 is LookupResult.Found -> {
172 result.elem
173 }
174 }
175 val component = when (val result = http.lookupPathParam("component", projectInfo.components)) {
176 is LookupResult.NotFound -> {
177 http.response.sendError(404)
178 return null
179 }
180 is LookupResult.Found -> {
181 result.elem
182 }
183 }
185 return PathInfos(projectInfo, version, component)
186 }
188 private fun project(http: HttpRequest, dao: DataAccessObject) {
189 withPathInfo(http, dao)?.run {
191 val issues = dao.listIssues(IssueFilter(
192 project = SpecificFilter(project),
193 version = version?.let { SpecificFilter(it) } ?: AllFilter(),
194 component = component?.let { SpecificFilter(it) } ?: AllFilter()
195 )).sortedWith(DEFAULT_ISSUE_SORTER)
197 with(http) {
198 view = ProjectDetails(projectInfo, issues, version, component)
199 feedPath = feedPath(project)
200 navigationMenu = activeProjectNavMenu(
201 dao.listProjects(),
202 projectInfo,
203 version,
204 component
205 )
206 styleSheets = listOf("projects")
207 render("project-details")
208 }
209 }
210 }
212 private fun projectForm(http: HttpRequest, dao: DataAccessObject) {
213 val projectInfo = obtainProjectInfo(http, dao)
214 if (projectInfo == null) {
215 http.response.sendError(404)
216 return
217 }
219 with(http) {
220 view = ProjectEditView(projectInfo.project, dao.listUsers())
221 navigationMenu = activeProjectNavMenu(
222 dao.listProjects(),
223 projectInfo
224 )
225 styleSheets = listOf("projects")
226 render("project-form")
227 }
228 }
230 private fun projectCommit(http: HttpRequest, dao: DataAccessObject) {
231 // TODO: replace defaults with throwing validator exceptions
232 val project = Project(http.param("id")?.toIntOrNull() ?: -1).apply {
233 name = http.param("name") ?: ""
234 node = http.param("node") ?: ""
235 description = http.param("description") ?: ""
236 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
237 repoUrl = http.param("repoUrl") ?: ""
238 owner = (http.param("owner")?.toIntOrNull() ?: -1).let {
239 if (it < 0) null else dao.findUser(it)
240 }
241 // intentional defaults
242 if (node.isBlank()) node = name
243 // sanitizing
244 node = sanitizeNode(node)
245 }
247 if (project.id < 0) {
248 dao.insertProject(project)
249 } else {
250 dao.updateProject(project)
251 }
253 http.renderCommit("projects/${project.node}")
254 }
256 private fun versions(http: HttpRequest, dao: DataAccessObject) {
257 val projectInfo = obtainProjectInfo(http, dao)
258 if (projectInfo == null) {
259 http.response.sendError(404)
260 return
261 }
263 with(http) {
264 view = VersionsView(
265 projectInfo,
266 dao.listVersionSummaries(projectInfo.project)
267 )
268 feedPath = feedPath(projectInfo.project)
269 navigationMenu = activeProjectNavMenu(
270 dao.listProjects(),
271 projectInfo
272 )
273 styleSheets = listOf("projects")
274 render("versions")
275 }
276 }
278 private fun versionForm(http: HttpRequest, dao: DataAccessObject) {
279 val projectInfo = obtainProjectInfo(http, dao)
280 if (projectInfo == null) {
281 http.response.sendError(404)
282 return
283 }
285 val version: Version
286 when (val result = http.lookupPathParam("version", projectInfo.versions)) {
287 is LookupResult.NotFound -> {
288 http.response.sendError(404)
289 return
290 }
291 is LookupResult.Found -> {
292 version = result.elem ?: Version(-1, projectInfo.project.id)
293 }
294 }
296 with(http) {
297 view = VersionEditView(projectInfo, version)
298 feedPath = feedPath(projectInfo.project)
299 navigationMenu = activeProjectNavMenu(
300 dao.listProjects(),
301 projectInfo,
302 selectedVersion = version
303 )
304 styleSheets = listOf("projects")
305 render("version-form")
306 }
307 }
309 private fun versionCommit(http: HttpRequest, dao: DataAccessObject) {
310 val id = http.param("id")?.toIntOrNull()
311 val projectid = http.param("projectid")?.toIntOrNull() ?: -1
312 val project = dao.findProject(projectid)
313 if (id == null || project == null) {
314 http.response.sendError(400)
315 return
316 }
318 // TODO: replace defaults with throwing validator exceptions
319 val version = Version(id, projectid).apply {
320 name = http.param("name") ?: ""
321 node = http.param("node") ?: ""
322 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
323 status = http.param("status")?.let(VersionStatus::valueOf) ?: VersionStatus.Future
324 // intentional defaults
325 if (node.isBlank()) node = name
326 // sanitizing
327 node = sanitizeNode(node)
328 }
330 if (id < 0) {
331 dao.insertVersion(version)
332 } else {
333 dao.updateVersion(version)
334 }
336 http.renderCommit("projects/${project.node}/versions/")
337 }
339 private fun components(http: HttpRequest, dao: DataAccessObject) {
340 val projectInfo = obtainProjectInfo(http, dao)
341 if (projectInfo == null) {
342 http.response.sendError(404)
343 return
344 }
346 with(http) {
347 view = ComponentsView(
348 projectInfo,
349 dao.listComponentSummaries(projectInfo.project)
350 )
351 feedPath = feedPath(projectInfo.project)
352 navigationMenu = activeProjectNavMenu(
353 dao.listProjects(),
354 projectInfo
355 )
356 styleSheets = listOf("projects")
357 render("components")
358 }
359 }
361 private fun componentForm(http: HttpRequest, dao: DataAccessObject) {
362 val projectInfo = obtainProjectInfo(http, dao)
363 if (projectInfo == null) {
364 http.response.sendError(404)
365 return
366 }
368 val component: Component
369 when (val result = http.lookupPathParam("component", projectInfo.components)) {
370 is LookupResult.NotFound -> {
371 http.response.sendError(404)
372 return
373 }
374 is LookupResult.Found -> {
375 component = result.elem ?: Component(-1, projectInfo.project.id)
376 }
377 }
379 with(http) {
380 view = ComponentEditView(projectInfo, component, dao.listUsers())
381 feedPath = feedPath(projectInfo.project)
382 navigationMenu = activeProjectNavMenu(
383 dao.listProjects(),
384 projectInfo,
385 selectedComponent = component
386 )
387 styleSheets = listOf("projects")
388 render("component-form")
389 }
390 }
392 private fun componentCommit(http: HttpRequest, dao: DataAccessObject) {
393 val id = http.param("id")?.toIntOrNull()
394 val projectid = http.param("projectid")?.toIntOrNull() ?: -1
395 val project = dao.findProject(projectid)
396 if (id == null || project == null) {
397 http.response.sendError(400)
398 return
399 }
401 // TODO: replace defaults with throwing validator exceptions
402 val component = Component(id, projectid).apply {
403 name = http.param("name") ?: ""
404 node = http.param("node") ?: ""
405 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
406 color = WebColor(http.param("color") ?: "#000000")
407 description = http.param("description")
408 lead = (http.param("lead")?.toIntOrNull() ?: -1).let {
409 if (it < 0) null else dao.findUser(it)
410 }
411 // intentional defaults
412 if (node.isBlank()) node = name
413 // sanitizing
414 node = sanitizeNode(node)
415 }
417 if (id < 0) {
418 dao.insertComponent(component)
419 } else {
420 dao.updateComponent(component)
421 }
423 http.renderCommit("projects/${project.node}/components/")
424 }
426 private fun issue(http: HttpRequest, dao: DataAccessObject) {
427 withPathInfo(http, dao)?.run {
428 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1)
429 if (issue == null) {
430 http.response.sendError(404)
431 return
432 }
434 val comments = dao.listComments(issue)
436 with(http) {
437 view = IssueDetailView(issue, comments, project, version, component)
438 // TODO: feed path for this particular issue
439 feedPath = feedPath(projectInfo.project)
440 navigationMenu = activeProjectNavMenu(
441 dao.listProjects(),
442 projectInfo,
443 version,
444 component
445 )
446 styleSheets = listOf("projects")
447 render("issue-view")
448 }
449 }
450 }
452 private fun issueForm(http: HttpRequest, dao: DataAccessObject) {
453 withPathInfo(http, dao)?.run {
454 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1) ?: Issue(
455 -1,
456 project,
457 )
459 // pre-select component, if available in the path info
460 issue.component = component
462 // pre-select version, if available in the path info
463 if (version != null) {
464 if (version.status.isReleased) {
465 issue.affectedVersions = listOf(version)
466 } else {
467 issue.resolvedVersions = listOf(version)
468 }
469 }
471 with(http) {
472 view = IssueEditView(
473 issue,
474 projectInfo.versions,
475 projectInfo.components,
476 dao.listUsers(),
477 project,
478 version,
479 component
480 )
481 feedPath = feedPath(projectInfo.project)
482 navigationMenu = activeProjectNavMenu(
483 dao.listProjects(),
484 projectInfo,
485 version,
486 component
487 )
488 styleSheets = listOf("projects")
489 render("issue-form")
490 }
491 }
492 }
494 private fun issueComment(http: HttpRequest, dao: DataAccessObject) {
495 withPathInfo(http, dao)?.run {
496 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1)
497 if (issue == null) {
498 http.response.sendError(404)
499 return
500 }
502 // TODO: throw validator exception instead of using a default
503 val comment = IssueComment(-1, issue.id).apply {
504 author = http.remoteUser?.let { dao.findUserByName(it) }
505 comment = http.param("comment") ?: ""
506 }
508 dao.insertComment(comment)
510 http.renderCommit("${issuesHref}${issue.id}")
511 }
512 }
514 private fun issueCommit(http: HttpRequest, dao: DataAccessObject) {
515 withPathInfo(http, dao)?.run {
516 // TODO: throw validator exception instead of using defaults
517 val issue = Issue(
518 http.param("id")?.toIntOrNull() ?: -1,
519 project
520 ).apply {
521 component = dao.findComponent(http.param("component")?.toIntOrNull() ?: -1)
522 category = IssueCategory.valueOf(http.param("category") ?: "")
523 status = IssueStatus.valueOf(http.param("status") ?: "")
524 subject = http.param("subject") ?: ""
525 description = http.param("description") ?: ""
526 assignee = http.param("assignee")?.toIntOrNull()?.let {
527 when (it) {
528 -1 -> null
529 -2 -> component?.lead
530 else -> dao.findUser(it)
531 }
532 }
533 eta = http.param("eta")?.let { if (it.isBlank()) null else Date.valueOf(it) }
535 affectedVersions = http.paramArray("affected")
536 .mapNotNull { param -> param.toIntOrNull()?.let { Version(it, project.id) } }
537 resolvedVersions = http.paramArray("resolved")
538 .mapNotNull { param -> param.toIntOrNull()?.let { Version(it, project.id) } }
539 }
541 val openId = if (issue.id < 0) {
542 dao.insertIssue(issue)
543 } else {
544 dao.updateIssue(issue)
545 issue.id
546 }
548 if (http.param("more") != null) {
549 http.renderCommit("${issuesHref}-/create")
550 } else {
551 http.renderCommit("${issuesHref}${openId}")
552 }
553 }
554 }
555 }