Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 28th, 2012  |  syntax: None  |  size: 2.58 KB  |  views: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. import com.sun.jersey.api.ParamException.QueryParamException
  3. import com.sun.jersey.api.core.HttpContext
  4. import com.sun.jersey.api.model.Parameter
  5. import com.sun.jersey.core.spi.component.{ComponentContext, ComponentScope}
  6. import com.sun.jersey.spi.inject.{Injectable, InjectableProvider}
  7. import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable
  8. import com.sun.jersey.server.impl.model.parameter.multivalued.MultivaluedParameterExtractor
  9. import com.sun.jersey.server.impl.model.parameter.multivalued.ExtractorContainerException
  10. import javax.ws.rs.QueryParam
  11. import javax.ws.rs.core.MultivaluedMap
  12. import javax.ws.rs.ext.Provider
  13. import org.springframework.stereotype.Component
  14.  
  15. @Provider
  16. @Component
  17. class ScalaQueryParamInjectableProvider extends InjectableProvider[QueryParam, Parameter] {
  18.   def getScope = ComponentScope.PerRequest
  19.  
  20.   def getInjectable(ic: ComponentContext, a: QueryParam, c: Parameter): Injectable[_] = {
  21.     val paramName = c.getSourceName
  22.     if (paramName == null || paramName.isEmpty) {
  23.       null
  24.     } else {
  25.       ScalaQueryParamExtractor(c) match {
  26.         case null => null
  27.         case e => new ScalaQueryParamInjectable(e, !c.isEncoded)
  28.       }
  29.     }
  30.   }
  31. }
  32.  
  33. class ScalaQueryParamInjectable(extractor: MultivaluedParameterExtractor, decode: Boolean)
  34.     extends AbstractHttpContextInjectable[Object] {
  35.   def getValue(c: HttpContext): Object = try {
  36.     extractor.extract(c.getUriInfo.getQueryParameters(decode))
  37.   } catch {
  38.     case e: ExtractorContainerException => throw new QueryParamException(e.getCause, extractor.getName,
  39.       extractor.getDefaultStringValue)
  40.   }
  41. }
  42.  
  43. object ScalaQueryParamExtractor {
  44.   def apply(p: Parameter): MultivaluedParameterExtractor = {
  45.     val klass = p.getParameterClass
  46.     if (klass == classOf[Option[Int]])
  47.       new ScalaIntOptionExtractor(p)
  48.     else
  49.       null
  50.   }
  51. }
  52.  
  53. abstract class BaseScalaOptionExtractor[T](p: Parameter) extends MultivaluedParameterExtractor {
  54.   val defaultValue = convert(p.getDefaultValue, None)
  55.  
  56.   def getName = p.getSourceName
  57.  
  58.   def getDefaultStringValue = p.getDefaultValue
  59.  
  60.   def extract(parameters: MultivaluedMap[String, String]) =
  61.     convert(parameters.getFirst(p.getSourceName), defaultValue)
  62.  
  63.   def convert(original: String, default: Option[T]): Option[T]
  64. }
  65.  
  66. class ScalaIntOptionExtractor(p: Parameter) extends BaseScalaOptionExtractor[Int](p) {
  67.   def convert(original: String, default: Option[Int]): Option[Int] = original match {
  68.     case null => default
  69.     case x => try {
  70.       Some(x.toInt)
  71.     } catch {
  72.       case e: NumberFormatException => throw new ExtractorContainerException("not an int", e)
  73.     }
  74.   }
  75. }