Permalink
Browse files

Merge branch 'master' of github.com:shafirov/kara

  • Loading branch information...
2 parents 170a23f + 7533667 commit 3410ecd91e7da009274df9783e1d7a4e672d4b12 @JuliusKunze JuliusKunze committed Jan 20, 2017
Showing with 318 additions and 222 deletions.
  1. +1 −0 samples/KaraDemo/src/kara/demo/routes/Home.kt
  2. +1 −2 samples/KaraDemo/src/kara/demo/styles/StyleClasses.kt
  3. +5 −4 samples/KaraDemo/src/kara/demo/views/DetailsTemplate.kt
  4. +3 −3 samples/KaraDemo/src/kara/demo/views/Index.kt
  5. +5 −5 samples/KaraDemo/src/kara/demo/views/Show.kt
  6. +3 −2 src/ConfigReader/src/kara/config/Config.kt
  7. +3 −7 src/HTMLBuilder/src/kotlinx/html/AttributeTypes.kt
  8. +16 −16 src/HTMLBuilder/src/kotlinx/html/Color.kt
  9. +3 −3 src/HTMLBuilder/src/kotlinx/html/CssDSL.kt
  10. +3 −3 src/HTMLBuilder/src/kotlinx/html/Dimensions.kt
  11. +5 −7 src/HTMLBuilder/src/kotlinx/html/HtmlBuilder.kt
  12. +17 −18 src/KaraExec/src/kara/server/JettyRunner.kt
  13. +3 −2 src/KaraLib/src/kara/ActionContext.kt
  14. +16 −14 src/KaraLib/src/kara/ActionResult.kt
  15. +2 −2 src/KaraLib/src/kara/Application.kt
  16. +2 −2 src/KaraLib/src/kara/ApplicationConfig.kt
  17. +1 −1 src/KaraLib/src/kara/ApplicationContext.kt
  18. +20 −5 src/KaraLib/src/kara/AsyncActionResult.kt
  19. +1 −1 src/KaraLib/src/kara/CustomClassloaderObjectInputStream.java
  20. +1 −1 src/KaraLib/src/kara/FlashScope.kt
  21. +4 −4 src/KaraLib/src/kara/FormBuilder.kt
  22. +1 −1 src/KaraLib/src/kara/Less.kt
  23. +4 −5 src/KaraLib/src/kara/Minify.kt
  24. +2 −3 src/KaraLib/src/kara/Resources.kt
  25. +16 −7 src/KaraLib/src/kara/RouteParameters.kt
  26. +2 −2 src/KaraLib/src/kara/Template.kt
  27. +17 −18 src/KaraLib/src/kara/internal/ResourceDescriptor.kt
  28. +1 −1 src/KaraLib/src/kara/internal/ResourceDispatcher.kt
  29. +3 −3 src/KaraLib/src/kara/internal/StringUtils.kt
  30. +10 −0 src/KaraTests/src/kara.tests/controllers/DispatchTests.kt
  31. +6 −1 src/KaraTests/src/kara.tests/controllers/Routes.kt
  32. +12 −4 src/KaraTests/src/kara.tests/mock/MockHttpServletRequest.kt
  33. +1 −1 src/KaraTests/src/kara.tests/mock/MockHttpSession.kt
  34. +1 −1 src/KaraTests/src/kara.tests/styles/CSSSelectorsTest.kt
  35. +4 −3 src/KaraTests/src/kara.tests/views/HomeView.kt
  36. +3 −3 src/KaraTests/src/kara.tests/views/TemplateTests.kt
  37. +0 −2 src/KaraTests/src/kara.tests/views/ViewTests.kt
  38. +8 −2 src/Kootstrap/src/content/sausa.js
  39. +3 −3 src/Kootstrap/src/kotlinx/html/bootstrap/Modal.kt
  40. +1 −0 src/ReflectionUtil/ReflectionUtil.iml
  41. +92 −45 src/ReflectionUtil/src/kotlinx/reflection/ParamDeserializer.kt
  42. +16 −15 src/ReflectionUtil/src/kotlinx/reflection/ReflectionUtils.kt
@@ -4,6 +4,7 @@ import kara.*
import kara.demo.models.Book
import java.util.*
+@Suppress("unused")
object Home {
@Get("/")
class Index() : Request({ kara.demo.views.Index() })
@@ -1,7 +1,6 @@
package kara.demo.styles
-import kara.*
-import kotlinx.html.*
+import kotlinx.html.StyleClass
enum class StyleClasses : StyleClass {
fields, cLabel, top,
@@ -1,10 +1,11 @@
package kara.demo.views
-import kara.*
-import kara.demo.styles.DefaultStyles
-import kara.demo.styles.StyleClasses.*
+import kara.Placeholder
+import kara.Template
+import kara.demo.styles.StyleClasses.body
+import kara.demo.styles.StyleClasses.title
+import kara.insert
import kotlinx.html.*
-import javax.swing.text.DefaultEditorKit.InsertBreakAction
class DetailsTemplate : Template<HTML>() {
val caption = Placeholder<HtmlBodyTag>()
@@ -1,9 +1,9 @@
package kara.demo.views
-import kara.*
+import kara.HtmlTemplateView
import kotlinx.html.*
-fun Index() = HtmlTemplateView<DefaultTemplate>(DefaultTemplate()) {
+fun Index() = HtmlTemplateView(DefaultTemplate()) {
content {
p {
+"This is the demo site for Kara. Below you'll find all of the html content that can be expressed in Kara views."
@@ -31,7 +31,7 @@ fun Index() = HtmlTemplateView<DefaultTemplate>(DefaultTemplate()) {
li { +"List Item 1" }
li { +"List Item 2" }
}
- fieldset() {
+ fieldset {
label { +"Text Input" }
input {
inputType = InputType.text
@@ -1,11 +1,11 @@
package kara.demo.views
-import kara.*
-import kara.demo.models.*
-import kara.demo.styles.StyleClasses.*
-import kara.demo.styles.StyleClasses
-import kotlinx.html.*
+import kara.HtmlTemplateView
import kara.demo.models.Post
+import kara.demo.styles.StyleClasses.date
+import kotlinx.html.div
+import kotlinx.html.h3
+import kotlinx.html.label
fun Show(post: Post) = HtmlTemplateView(DetailsTemplate()) {
caption {
@@ -2,6 +2,7 @@ package kara.config
import org.apache.log4j.Logger
import java.io.File
+import java.lang.RuntimeException
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.regex.Pattern
@@ -117,13 +118,13 @@ open class Config() {
else -> {
val eq = line.indexOf('=')
if (eq <= 0) error("Cannot parse line '$line' in file '${file.absolutePath}'")
- config.set(line.substring(0, eq).trim(), evalVars(line.substring(eq + 1).trim(), ::eval))
+ config[line.substring(0, eq).trim()] = evalVars(line.substring(eq + 1).trim(), ::eval)
}
}
}
}
- val varPattern = Pattern.compile("\\$\\{([^\\}]*)\\}")
+ private val varPattern = Pattern.compile("\\$\\{([^\\}]*)\\}")
fun evalVars(line: String, eval: (String) -> String) = buildString {
val matcher = varPattern.matcher(line)
var lastAppend = 0
@@ -4,10 +4,10 @@ import kotlin.reflect.KProperty
abstract class Attribute<T>(val name: String) {
operator fun getValue(tag: HtmlTag, property: KProperty<*>): T {
- return decode(tag[name]);
+ return decode(tag[name])
}
operator open fun setValue(tag: HtmlTag, property: KProperty<*>, value: T) {
- tag[name] = encode(value);
+ tag[name] = encode(value)
}
abstract fun encode(t: T): String?
@@ -93,11 +93,7 @@ class EnumAttribute<T>(name: String, val klass: Class<T>) : Attribute<T>(name)
}
override fun decode(s: String?): T {
- for (c in klass.enumConstants!!) {
- if (encode(c) == s) return c
- }
-
- throw RuntimeException("Can't decode '$s' as value of '${klass.name}'")
+ return klass.enumConstants!!.firstOrNull { encode(it) == s } ?: throw RuntimeException("Can't decode '$s' as value of '${klass.name}'")
}
}
@@ -58,7 +58,7 @@ class Color(var red: Double, var green: Double, var blue: Double, var alpha: Dou
val a = Integer.parseInt(s.substring(7, 9), 16)
return Color.fromRgb(r, g, b, a)
}
- throw Exception("Invalid color hex string: $s");
+ throw Exception("Invalid color hex string: $s")
}
/** Creates a color from a set of HSL values. */
@@ -153,22 +153,22 @@ class Color(var red: Double, var green: Double, var blue: Double, var alpha: Dou
if(t > 1.0)
t -= 1.0
if(t < 1.0 / 6.0)
- return p + (q - p) * 6.0 * t;
+ return p + (q - p) * 6.0 * t
if(t < 0.5)
- return q;
+ return q
if(t < 2.0 / 3.0)
- return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
- return p;
+ return p + (q - p) * (2.0 / 3.0 - t) * 6.0
+ return p
}
- var q: Double
- if (hsl.lightness < 0.5)
- q = hsl.lightness * (1 + hsl.saturation)
+ val q = if (hsl.lightness < 0.5)
+ hsl.lightness * (1 + hsl.saturation)
else
- q = hsl.lightness + hsl.saturation - hsl.lightness * hsl.saturation
- var p = 2.0 * hsl.lightness - q;
- red = hue2rgb(p, q, hsl.hue + 1.0 / 3.0);
- green = hue2rgb(p, q, hsl.hue);
- blue = hue2rgb(p, q, hsl.hue - 1.0 / 3.0);
+ hsl.lightness + hsl.saturation - hsl.lightness * hsl.saturation
+
+ val p = 2.0 * hsl.lightness - q
+ red = hue2rgb(p, q, hsl.hue + 1.0 / 3.0)
+ green = hue2rgb(p, q, hsl.hue)
+ blue = hue2rgb(p, q, hsl.hue - 1.0 / 3.0)
}
}
@@ -214,14 +214,14 @@ class Color(var red: Double, var green: Double, var blue: Double, var alpha: Dou
* - a 6 digit hex string for RGB: #FE395A
* - a 8 digit hex string for RGBA: #FE395A88
*/
-inline fun color(s: String): Color {
+fun color(s: String): Color {
if (s.startsWith("#"))
return Color.fromHex(s)
- throw Exception("Invalid color string: $s");
+ throw Exception("Invalid color string: $s")
}
/** Returns true if the string is a valid color literal. */
-inline fun isColor(s: String): Boolean {
+fun isColor(s: String): Boolean {
return s.startsWith("#") || s.startsWith("rgb")
}
@@ -20,7 +20,7 @@ interface StyleClass : SelectorTrait, Selector {
val name: String
override fun toExternalForm(): String {
- return ".${name}"
+ return ".$name"
}
}
@@ -47,7 +47,7 @@ enum class PseudoClass : StyleClass {
empty, link, visited, active, focus, hover, target, enabled, disabled, checked;
override fun toExternalForm(): String {
- return ":${name}"
+ return ":$name"
}
}
@@ -231,7 +231,7 @@ class StyledElement(val selector: String) : CssElement() {
builder.append("$thisSelector {\n")
for (a in attributes.keys) {
val attr = attributes[a]!!
- builder.append(" $a: ${attr.toString()};\n")
+ builder.append(" $a: $attr;\n")
}
builder.append("}\n")
for (child in children) {
@@ -74,16 +74,16 @@ class BoxDimensions(var top: LinearDimension, var right: LinearDimension, var bo
}
/** Convenience function for making a BoxDimensions with all dimensions the same. */
-inline fun box(all: LinearDimension): BoxDimensions {
+fun box(all: LinearDimension): BoxDimensions {
return BoxDimensions(all, all, all, all)
}
/** Convenience function for making a BoxDimensions with top/bottom and left/right values. */
-inline fun box(topBottom: LinearDimension, leftRight: LinearDimension): BoxDimensions {
+fun box(topBottom: LinearDimension, leftRight: LinearDimension): BoxDimensions {
return BoxDimensions(topBottom, leftRight, topBottom, leftRight)
}
/** Convenience function for making a BoxDimensions with all four dimensions. */
-inline fun box(top: LinearDimension, right: LinearDimension, bottom: LinearDimension, left: LinearDimension): BoxDimensions {
+fun box(top: LinearDimension, right: LinearDimension, bottom: LinearDimension, left: LinearDimension): BoxDimensions {
return BoxDimensions(top, right, bottom, left)
}
@@ -1,5 +1,6 @@
package kotlinx.html
+import java.lang.RuntimeException
import java.util.*
abstract class HtmlElement(val containingElement: HtmlElement?, val contentStyle: ContentStyle = ContentStyle.block) {
@@ -9,7 +10,7 @@ abstract class HtmlElement(val containingElement: HtmlElement?, val contentStyle
private fun appendTo(element: HtmlElement?) = element?.children?.add(this)
- val children: MutableList<HtmlElement> = ArrayList<HtmlElement>()
+ val children: MutableList<HtmlElement> = ArrayList()
abstract fun renderElement(builder: StringBuilder, indent: String)
@@ -44,16 +45,13 @@ fun html(content: HtmlBodyTag.() -> Unit) = buildString {
}
fun String.htmlEscapeTo(builder: StringBuilder) {
- val len = length
-
- for (i in 0..len - 1) {
- val c = this[i]
- when (c) {
+ this.forEach {
+ when(it) {
'<' -> builder.append("&lt;")
'>' -> builder.append("&gt;")
'\"' -> builder.append("&quot;")
'&' -> builder.append("&amp;")
- else -> builder.append(c)
+ else -> builder.append(it)
}
}
}
@@ -22,7 +22,7 @@ import javax.servlet.http.HttpSession
*/
class JettyRunner(val applicationConfig: ApplicationConfig) {
val logger = Logger.getLogger(this.javaClass)!!
- var server: Server? = null
+ lateinit var server: Server
val resourceHandlers = ArrayList<ResourceHandler>()
val apps = HashMap<String, Application>()
@@ -39,7 +39,7 @@ class JettyRunner(val applicationConfig: ApplicationConfig) {
val CONFIG = MultipartConfigElement(System.getProperty("java.io.tmpdir"))
override fun handle(target: String?, baseRequest: Request?, request: HttpServletRequest?, response: HttpServletResponse?) {
- if (baseRequest?.contentType?.let { it.contains("multipart/form-data", ignoreCase = true) } ?: false) {
+ if (baseRequest?.contentType?.contains("multipart/form-data", ignoreCase = true) ?: false) {
baseRequest?.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, CONFIG)
}
@@ -60,7 +60,7 @@ class JettyRunner(val applicationConfig: ApplicationConfig) {
resourceHandler.handle(targ, baseRequest, request.removeContext(appContext), response)
if (baseRequest!!.isHandled) {
logger.info("$method -- ${request.requestURL}${if (query != null) "?" + query else ""} -- OK @${resourceHandler.resourceBase}")
- break;
+ break
}
}
}
@@ -69,7 +69,7 @@ class JettyRunner(val applicationConfig: ApplicationConfig) {
}
}
catch(ex: Throwable) {
- logger.error("dispatch error: ${ex.message}", ex);
+ logger.error("dispatch error: ${ex.message}", ex)
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorDescr(ex, request, request.session!!))
}
}
@@ -84,9 +84,8 @@ class JettyRunner(val applicationConfig: ApplicationConfig) {
fun start() {
logger.info("Starting server...")
- var port: Int
- try {
- port = applicationConfig.port.toInt()
+ val port = try {
+ applicationConfig.port.toInt()
}
catch (ex: Exception) {
throw RuntimeException("${applicationConfig.port} is not a valid port number")
@@ -99,40 +98,40 @@ class JettyRunner(val applicationConfig: ApplicationConfig) {
isDirectoriesListed = false
resourceBase = "./$it"
welcomeFiles = arrayOf("index.html")
+ // Prevent Jetty from locking static files on windows 9
+ // see http://www.eclipse.org/jetty/documentation/current/troubleshooting-locked-files-on-windows.html
+ if (System.getProperty("os.name").startsWith("Windows", true)) {
+ minMemoryMappedContentLength = -1
+ }
})
}
- server?.handler = SessionHandler().apply {
+ server.handler = SessionHandler().apply {
sessionManager = HashSessionManager().apply {
storeDirectory = java.io.File("tmp/sessions")
httpOnly = true
}
handler = Handler()
}
- server?.start()
+ server.start()
logger.info("Server running.")
if (applicationConfig.tryGet("kara.jetty.dontJoinServer") != "true") {
- server?.join()
+ server.join()
}
}
fun stop() {
- if (server != null) {
- server?.stop()
- server = null
- }
+ server.stop()
}
fun restart() {
- this.stop()
- this.start()
+ stop()
+ start()
}
-
}
-
fun Throwable.getStackTraceString(): String {
val os = ByteArrayOutputStream()
this.printStackTrace(PrintStream(os))
@@ -4,6 +4,7 @@ import kotlinx.html.Link
import java.io.ByteArrayOutputStream
import java.io.ObjectOutputStream
import java.io.Serializable
+import java.lang.Exception
import java.math.BigInteger
import java.security.SecureRandom
import java.util.*
@@ -125,7 +126,7 @@ class RequestScope<T:Any>(): ReadWriteProperty<Any?, T?> {
}
}
-class LazyRequestScope<T:Any>(val initial: () -> T): ReadOnlyProperty<Any?, T> {
+class LazyRequestScope<out T:Any>(val initial: () -> T): ReadOnlyProperty<Any?, T> {
@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: Any?, property: KProperty<*>): T = ActionContext.current().data.getOrPut(thisRef to property, { initial() }) as T
}
@@ -141,7 +142,7 @@ class SessionScope<T:Any>(): ReadWriteProperty<Any?, T?> {
}
}
-class LazySessionScope<T:Any>(val initial: () -> T): ReadOnlyProperty<Any?, T> {
+class LazySessionScope<out T:Any>(val initial: () -> T): ReadOnlyProperty<Any?, T> {
private val store = SessionScope<T>()
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
Oops, something went wrong.

0 comments on commit 3410ecd

Please sign in to comment.