Skip to content

Commit

Permalink
refactoring forms
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Dec 5, 2016
1 parent 177188e commit dacf3a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
21 changes: 10 additions & 11 deletions core/src/main/scala/io/fintrospect/parameters/FormBody.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ import scala.util.{Failure, Success, Try}
class FormBody(val fields: Seq[FormField[_] with Extractor[Form, _]], encodeDecode: FormCodec)
extends Body[Form] {

private val spec = BodySpec.string(None, APPLICATION_FORM_URLENCODED).map(b => encodeDecode.decode(fields, b), (f: Form) => encodeDecode.encode(f))

override val contentType = spec.contentType
override val contentType = APPLICATION_FORM_URLENCODED

override def iterator = fields.iterator

def -->(value: Form): Seq[RequestBinding] =
Seq(new RequestBinding(null, t => {
val content = spec.serialize(value)
t.headerMap.add(Names.CONTENT_TYPE, spec.contentType.value)
t.headerMap.add(Names.CONTENT_LENGTH, content.length.toString)
t.content = content
t
Seq(new RequestBinding(null, req => {
val contentString = encodeDecode.encode(value)
req.headerMap.add(Names.CONTENT_TYPE, contentType.value)
req.headerMap.add(Names.CONTENT_LENGTH, contentString.length.toString)
req.contentString = contentString
req
})) ++ fields.map(f => new FormFieldBinding(f, ""))

override def <--?(message: Message): Extraction[Form] =
Try(spec.deserialize(message.content)) match {
override def <--?(message: Message): Extraction[Form] = {
Try(encodeDecode.decode(fields, message)) match {
case Success(form) => encodeDecode.extract(fields, form)
case Failure(e) => ExtractionFailed(fields.filter(_.required).map(param => ExtractionError(param, "Could not parse")))
}
}
}
10 changes: 5 additions & 5 deletions core/src/main/scala/io/fintrospect/parameters/FormCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package io.fintrospect.parameters

import java.net.{URLDecoder, URLEncoder}

import com.twitter.finagle.http.Message
import io.fintrospect.util._

/**
* Represents different strategies for decoding and encoding HTML forms.
*/
trait FormCodec {

protected def decodeFields(content: String): Map[String, Set[String]] = {
content
.split("&")
Expand All @@ -26,7 +26,7 @@ trait FormCodec {
case (name, values) => values.map(value => URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8"))
}.mkString("&")

def decode(fields: Seq[Extractor[Form, _]], s: String): Form
def decode(fields: Seq[Extractor[Form, _]], msg: Message): Form

def extract(fields: Seq[Extractor[Form, _]], f: Form): Extraction[Form]
}
Expand All @@ -37,8 +37,8 @@ trait FormCodec {
* As such, extracting an invalid webform from a request will not fail unless the body encoding itself is invalid.
*/
class WebFormCodec(messages: Map[Parameter, String]) extends FormCodec {
override def decode(fields: Seq[Extractor[Form, _]], content: String): Form = {
val rawForm = new Form(decodeFields(content), Map.empty , Nil)
override def decode(fields: Seq[Extractor[Form, _]], msg: Message): Form = {
val rawForm = new Form(decodeFields(msg. contentString), Map.empty , Nil)
new Form(rawForm.fields, Map.empty, fields.flatMap {
_ <--? rawForm match {
case ExtractionFailed(e) => e.map(er => ExtractionError(er.param, messages.getOrElse(er.param, er.reason)))
Expand All @@ -56,7 +56,7 @@ class WebFormCodec(messages: Map[Parameter, String]) extends FormCodec {
* will auto-reject requests with a BadRequest.
*/
class StrictFormCodec() extends FormCodec {
override def decode(fields: Seq[Extractor[Form, _]], content: String): Form = new Form(decodeFields(content), Map.empty, Nil)
override def decode(fields: Seq[Extractor[Form, _]], msg: Message): Form = new Form(decodeFields(msg.contentString), Map.empty, Nil)

override def extract(fields: Seq[Extractor[Form, _]], form: Form): Extraction[Form] = Extraction.combine(fields.map(_.extract(form))) match {
case failed@ExtractionFailed(_) => failed
Expand Down

0 comments on commit dacf3a6

Please sign in to comment.