Skip to content

Commit

Permalink
Added support for HTTP OPTIONS method
Browse files Browse the repository at this point in the history
  • Loading branch information
SeunAdelekan committed Jun 8, 2017
1 parent ad750a4 commit a8a8ec3
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 136 deletions.
276 changes: 161 additions & 115 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.iyanuadelekan</groupId>
<artifactId>kanary</artifactId>
<packaging>jar</packaging>
<version>0.9.0</version>
<version>0.9.1</version>

<properties>
<kotlin.version>1.1.2-4</kotlin.version>
Expand Down
14 changes: 14 additions & 0 deletions src/main/com/iyanuadelekan/kanary/core/KanaryRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class KanaryRouter(var basePath: String?= null, var routeController: KanaryContr
val putRouteList: RouteList = RouteList()
val patchRouteList: RouteList = RouteList()
val deleteRouteList: RouteList = RouteList()
val optionsRouteList: RouteList = RouteList()

/**
* Router function handling GET requests
Expand Down Expand Up @@ -85,6 +86,18 @@ class KanaryRouter(var basePath: String?= null, var routeController: KanaryContr
return this
}

/**
* Router function handling PATCH requests
* @param path Specified route path
* @param action Action to handle requests targeting specified route path
* @param controller Controller handling the route
* @return current instance of [KanaryRouter]
*/
override fun options(path: String, action: (Request, HttpServletRequest, HttpServletResponse) -> Unit, controller: KanaryController?): KanaryRouter {
assembleAndQueueRoute(HttpConstants.OPTIONS.name, path, action, controller)
return this
}

/**
* Function used for route queuing
* adds a [route] based on its [method] to its appropriate route list
Expand All @@ -98,6 +111,7 @@ class KanaryRouter(var basePath: String?= null, var routeController: KanaryContr
HttpConstants.PUT.name -> putRouteList.add(route)
HttpConstants.DELETE.name -> deleteRouteList.add(route)
HttpConstants.PATCH.name -> patchRouteList.add(route)
HttpConstants.OPTIONS.name -> optionsRouteList.add(route)
else -> {
println("Unrecognized HTTP method: '$method'")
}
Expand Down
34 changes: 16 additions & 18 deletions src/main/com/iyanuadelekan/kanary/handlers/AppHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,19 @@ class AppHandler(val app: KanaryApp): AbstractHandler() {

if (request != null) {
if(isMethodSupported(request.method)) {
if(request.method == HttpConstants.OPTIONS.name) {
response?.addHeader("Allow", "${HttpConstants.OPTIONS.name}, ${HttpConstants.GET.name}, " +
"${HttpConstants.POST.name}, ${HttpConstants.PUT.name}, ${HttpConstants.DELETE.name}, " +
HttpConstants.PATCH.name)
val route: Route? = resolveTargetRoute(request.method, "$target")

response?. withStatus(200)?. send("")
} else {
val route: Route? = resolveTargetRoute(request.method, "$target")

if (route != null) {
val action = route.action
executeBeforeAction(route, request, response)

if(baseRequest != null && response != null) {
action.invoke(baseRequest, request, response)
}
if (route != null) {
val action = route.action
executeBeforeAction(route, request, response)

executeAfterAction(route, request, response)
} else {
response?. withStatus(404)?. send("Method not found.")
if(baseRequest != null && response != null) {
action.invoke(baseRequest, request, response)
}

executeAfterAction(route, request, response)
} else {
response?. withStatus(404)?. send("Method not found.")
}
} else {
response?.addHeader("Allow", "${HttpConstants.OPTIONS.name}, ${HttpConstants.GET.name}, " +
Expand Down Expand Up @@ -119,6 +111,12 @@ class AppHandler(val app: KanaryApp): AbstractHandler() {
route = matchedRoutes[0]
}
}
HttpConstants.OPTIONS.name -> {
matchedRoutes = router.optionsRouteList.filter { it.path == formattedTarget }
if(matchedRoutes.isNotEmpty()) {
route = matchedRoutes[0]
}
}
}
} }
return route
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ interface RouterInterface {
fun patch(path: String, action: (Request, HttpServletRequest, HttpServletResponse) -> Unit,
controller: KanaryController?=null): KanaryRouter

fun options(path: String, action: (Request, HttpServletRequest, HttpServletResponse) -> Unit,
controller: KanaryController?=null): KanaryRouter

}
4 changes: 2 additions & 2 deletions target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven
#Thu Jun 01 20:20:29 WAT 2017
version=0.9.0
#Thu Jun 08 20:52:58 WAT 2017
version=0.9.1
groupId=com.iyanuadelekan
artifactId=kanary

0 comments on commit a8a8ec3

Please sign in to comment.