forked from codahale/jersey-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ScalaCollectionFormParamInjectable{,Provider,ProviderSpec}
Split methods between providers into into AbstractScalaCollectionsParamInjectableProvider
- Loading branch information
Showing
7 changed files
with
167 additions
and
24 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...in/scala/com/codahale/jersey/inject/AbstractScalaCollectionsParamInjectableProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.codahale.jersey.inject | ||
|
||
import com.sun.jersey.core.spi.component.ComponentScope | ||
import com.sun.jersey.server.impl.model.parameter.multivalued.MultivaluedParameterExtractor | ||
|
||
abstract class AbstractScalaCollectionsParamInjectableProvider { | ||
def getScope = ComponentScope.PerRequest | ||
protected def buildExtractor(name: String, default: String, klass: Class[_]): MultivaluedParameterExtractor = { | ||
if (klass == classOf[Seq[String]]) { | ||
new ScalaCollectionStringReaderExtractor[Seq](name, default, Seq) | ||
} else if (klass == classOf[List[String]]) { | ||
new ScalaCollectionStringReaderExtractor[List](name, default, List) | ||
} else if (klass == classOf[Vector[String]]) { | ||
new ScalaCollectionStringReaderExtractor[Vector](name, default, Vector) | ||
} else if (klass == classOf[IndexedSeq[String]]) { | ||
new ScalaCollectionStringReaderExtractor[IndexedSeq](name, default, IndexedSeq) | ||
} else if (klass == classOf[Set[String]]) { | ||
new ScalaCollectionStringReaderExtractor[Set](name, default, Set) | ||
} else if (klass == classOf[Option[String]]) { | ||
new ScalaOptionStringExtractor(name, default) | ||
} else null | ||
} | ||
} | ||
|
||
|
||
|
19 changes: 19 additions & 0 deletions
19
src/main/scala/com/codahale/jersey/inject/ScalaCollectionFormParamInjectable.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.codahale.jersey.inject | ||
|
||
import com.sun.jersey.api.ParamException | ||
import com.sun.jersey.api.core.HttpContext | ||
import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable | ||
import com.sun.jersey.server.impl.model.parameter.multivalued.{MultivaluedParameterExtractor, ExtractorContainerException} | ||
|
||
class ScalaCollectionFormParamInjectable(extractor: MultivaluedParameterExtractor) | ||
extends AbstractHttpContextInjectable[Object] { | ||
|
||
def getValue(c: HttpContext) = try { | ||
extractor.extract(c.getRequest.getFormParameters) | ||
} catch { | ||
case e: ExtractorContainerException => | ||
throw new ParamException.FormParamException(e.getCause, | ||
extractor.getName, | ||
extractor.getDefaultStringValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/scala/com/codahale/jersey/inject/ScalaCollectionsFormParamInjectableProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.codahale.jersey.inject | ||
|
||
import javax.ws.rs.ext.Provider | ||
import com.sun.jersey.api.model.Parameter | ||
import com.sun.jersey.core.spi.component.{ComponentScope, ComponentContext} | ||
import com.sun.jersey.spi.inject.{Injectable, InjectableProvider} | ||
import com.sun.jersey.server.impl.model.parameter.multivalued.MultivaluedParameterExtractor | ||
import javax.ws.rs.{FormParam, QueryParam} | ||
|
||
@Provider | ||
class ScalaCollectionsFormParamInjectableProvider | ||
extends AbstractScalaCollectionsParamInjectableProvider | ||
with InjectableProvider[FormParam, Parameter] | ||
{ | ||
def getInjectable(ic: ComponentContext, a: FormParam, c: Parameter): Injectable[_] = { | ||
val parameterName = c.getSourceName() | ||
if (parameterName != null && !parameterName.isEmpty) { | ||
buildInjectable(parameterName, c.getDefaultValue, c.getParameterClass) | ||
} else null | ||
} | ||
|
||
private def buildInjectable(name: String, default: String, klass: Class[_]): Injectable[_ <: Object] = { | ||
val extractor = buildExtractor(name, default, klass) | ||
if (extractor != null) { | ||
new ScalaCollectionFormParamInjectable(extractor) | ||
} else null | ||
} | ||
} | ||
|
||
|
||
|
31 changes: 10 additions & 21 deletions
31
src/main/scala/com/codahale/jersey/inject/ScalaCollectionsQueryParamInjectableProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,31 @@ | ||
package com.codahale.jersey.inject | ||
|
||
import javax.ws.rs.QueryParam | ||
import javax.ws.rs.ext.Provider | ||
import com.sun.jersey.api.model.Parameter | ||
import com.sun.jersey.core.spi.component.{ComponentScope, ComponentContext} | ||
import com.sun.jersey.spi.inject.{Injectable, InjectableProvider} | ||
import com.sun.jersey.server.impl.model.parameter.multivalued.MultivaluedParameterExtractor | ||
import javax.ws.rs.{FormParam, QueryParam} | ||
|
||
@Provider | ||
class ScalaCollectionsQueryParamInjectableProvider extends InjectableProvider[QueryParam, Parameter] { | ||
def getScope = ComponentScope.PerRequest | ||
class ScalaCollectionsQueryParamInjectableProvider | ||
extends AbstractScalaCollectionsParamInjectableProvider | ||
with InjectableProvider[QueryParam, Parameter] | ||
{ | ||
def getInjectable(ic: ComponentContext, a: QueryParam, c: Parameter): Injectable[_] = { | ||
val parameterName = c.getSourceName() | ||
if (parameterName != null && !parameterName.isEmpty) { | ||
buildInjectable(parameterName, c.getDefaultValue, !c.isEncoded, c.getParameterClass) | ||
} else null | ||
} | ||
|
||
private def buildExtractor(name: String, default: String, klass: Class[_]): MultivaluedParameterExtractor = { | ||
if (klass == classOf[Seq[String]]) { | ||
new ScalaCollectionStringReaderExtractor[Seq](name, default, Seq) | ||
} else if (klass == classOf[List[String]]) { | ||
new ScalaCollectionStringReaderExtractor[List](name, default, List) | ||
} else if (klass == classOf[Vector[String]]) { | ||
new ScalaCollectionStringReaderExtractor[Vector](name, default, Vector) | ||
} else if (klass == classOf[IndexedSeq[String]]) { | ||
new ScalaCollectionStringReaderExtractor[IndexedSeq](name, default, IndexedSeq) | ||
} else if (klass == classOf[Set[String]]) { | ||
new ScalaCollectionStringReaderExtractor[Set](name, default, Set) | ||
} else if (klass == classOf[Option[String]]) { | ||
new ScalaOptionStringExtractor(name, default) | ||
} else null | ||
} | ||
|
||
private def buildInjectable(name: String, default: String, decode: Boolean, klass: Class[_]): Injectable[_ <: Object] = { | ||
private def buildInjectable(name: String, default: String, decoded: Boolean, klass: Class[_]): Injectable[_ <: Object] = { | ||
val extractor = buildExtractor(name, default, klass) | ||
if (extractor != null) { | ||
new ScalaCollectionQueryParamInjectable(extractor, decode) | ||
new ScalaCollectionQueryParamInjectable(extractor, decoded) | ||
} else null | ||
} | ||
} | ||
|
||
|
||
|
78 changes: 78 additions & 0 deletions
78
...la/com/codahale/jersey/inject/specs/ScalaCollectionsFormParamInjectableProviderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.codahale.jersey.inject.specs | ||
|
||
import com.codahale.simplespec.Spec | ||
import org.junit.Test | ||
import com.sun.jersey.api.model.Parameter | ||
import com.sun.jersey.core.spi.component.{ComponentContext, ComponentScope} | ||
import com.sun.jersey.core.util.MultivaluedMapImpl | ||
import javax.ws.rs.FormParam | ||
import com.codahale.jersey.inject.{ScalaCollectionsFormParamInjectableProvider, ScalaCollectionFormParamInjectable} | ||
import com.sun.jersey.api.core.{HttpRequestContext, HttpContext} | ||
import com.sun.jersey.api.representation.Form | ||
|
||
class ScalaCollectionsFormParamInjectableProviderSpec extends Spec { | ||
class `A Scala collections form param injectable provider` { | ||
val httpContext = mock[HttpContext] | ||
val requestContext = mock[HttpRequestContext] | ||
|
||
val params = new Form() | ||
params.add("name", "one") | ||
params.add("name", "two") | ||
params.add("name", "three") | ||
|
||
httpContext.getRequest returns requestContext | ||
requestContext.getFormParameters returns params | ||
|
||
val context = mock[ComponentContext] | ||
val formParam = mock[FormParam] | ||
|
||
val provider = new ScalaCollectionsFormParamInjectableProvider | ||
|
||
|
||
@Test def `has a per-request scope` = { | ||
provider.getScope.must(be(ComponentScope.PerRequest)) | ||
} | ||
|
||
@Test def `returns an injectable for Seq instances` = { | ||
val param = new Parameter(Array(), null, null, "name", null, classOf[Seq[String]], false, "default") | ||
val injectable = provider.getInjectable(context, formParam, param).asInstanceOf[ScalaCollectionFormParamInjectable] | ||
|
||
injectable.getValue(httpContext).must(be(Seq("one", "two", "three"))) | ||
} | ||
|
||
@Test def `returns an injectable for List instances` = { | ||
val param = new Parameter(Array(), null, null, "name", null, classOf[List[String]], false, "default") | ||
val injectable = provider.getInjectable(context, formParam, param).asInstanceOf[ScalaCollectionFormParamInjectable] | ||
|
||
injectable.getValue(httpContext).must(be(List("one", "two", "three"))) | ||
} | ||
|
||
@Test def `returns an injectable for Vector instances` = { | ||
val param = new Parameter(Array(), null, null, "name", null, classOf[Vector[String]], false, "default") | ||
val injectable = provider.getInjectable(context, formParam, param).asInstanceOf[ScalaCollectionFormParamInjectable] | ||
|
||
injectable.getValue(httpContext).must(be(Vector("one", "two", "three"))) | ||
} | ||
|
||
@Test def `returns an injectable for IndexedSeq instances` = { | ||
val param = new Parameter(Array(), null, null, "name", null, classOf[IndexedSeq[String]], false, "default") | ||
val injectable = provider.getInjectable(context, formParam, param).asInstanceOf[ScalaCollectionFormParamInjectable] | ||
|
||
injectable.getValue(httpContext).must(be(IndexedSeq("one", "two", "three"))) | ||
} | ||
|
||
@Test def `return an injectable for Set instances` = { | ||
val param = new Parameter(Array(), null, null, "name", null, classOf[Set[String]], false, "default") | ||
val injectable = provider.getInjectable(context, formParam, param).asInstanceOf[ScalaCollectionFormParamInjectable] | ||
|
||
injectable.getValue(httpContext).must(be(Set("one", "two", "three"))) | ||
} | ||
|
||
@Test def `returns an injectable for Option instances` = { | ||
val param = new Parameter(Array(), null, null, "name", null, classOf[Option[String]], false, "default") | ||
val injectable = provider.getInjectable(context, formParam, param).asInstanceOf[ScalaCollectionFormParamInjectable] | ||
|
||
injectable.getValue(httpContext).must(be(Some("one"))) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters