unfiltered
A toolkit for servicing HTTP requests in Scala
Unfiltered — Unfiltered
I often run a dispatcher server in sbt ~run
.
unfiltered.netty.Http(port)
.handler(plan)
.run()
Http.shutdown()
When I start up my server, it says:
Embedded server running on port 8080. Press any key to stop.
However, only ENTER will stop the server. Unfortunately, ENTER also stops sbt
. How can I set up unfiltered so that it actually stops on any key within sbt
? I would like pressing a key to stop the server and cause sbt
to re-compile and re-run the application.
Source: (StackOverflow)
I need to show operating unit number look up filtered by department in excel ad-in to populate one table in AX. The look up seems to be fine except it doesn't filter department only values, rather it shows business as well as cost center. I have added relation to OMOperatingUnit
with OMOperatingNumber
as well as OMOperatingType
as 1 (department). It shows correct and filtered by department lookup if we look into the field of the table from AOT. In excel Ad-in, it shows unfiltered data including department, cost center etc.
How can I achieve it?
Source: (StackOverflow)
In the following example:
object AsyncPlan extends unfiltered.filter.async.Plan {
def intent = {
case GET(UFPath("/pass")) => Pass
case req@GET(UFPath("/async")) =>
// sleep for a bit
req.respond(ResponseString("test") ~> Ok)
}
}
There is the chance that the client closes the connection before respond is called. In that scenario Unfiltered allows the underlying ClosedChannelException to bubble through.
What is the intended pattern for gracefully dealing with this scenario?
Source: (StackOverflow)
when writing a CGI script or a basic servlet you can send part of the response to the client before the entire response has been generated. This is advantageous when, for example, performing a large SQL query and displaying the results in an HTML table. Showing the results as they come in makes the application much more responsive. Is this possible in a basic REST framework like Scalatra or Unfiltered--where the return value is often the response text?
Source: (StackOverflow)
With unfiltered, how to produce servlets instead of filters? Or turn filters into servlets?
Motivation is that Google App Engine's development server has poor support (read bugs) for filters.
For clarity, my filters look like this:
class Filter extends unfiltered.filter.Plan {
def intent = {...}
}
Source: (StackOverflow)
Does anyone have an example how to deserialize request json with the Unfiltered framework?
Spray has an 'extract' keyword which hides the deserialization. Does Unfiltered have something similar? Preferably using Json4s.
Source: (StackOverflow)
I did a quick test in unfiltered, comparing throughput of both jetty and netty as underlying connection handlers. Just serving a (memory-cached) image and running a load test on that. My findings are that there is no significant difference in performance.
Apart from that, I am under the impression that both have similar scaling features aswell, like suspending connections.
Also, unfiltered is kind enough to give us very similar (if not the same) interfaces to both frameworks, so that you can't really say one is easier to use that the other.
So I wonder, why does unfiltered give us those two choices? is there any scenario when you would choose netty over jetty (or the other way around)?
Source: (StackOverflow)
Using Unfiltered, respecting a request's Accept-Encoding
header is made trivial by the GZip
kit.
What I cannot seem to find, however, is how to accept a gzipped request entity. It's perfectly legal for a client to declare an HTTP header of Content-Encoding: gzip
, and servers should honour it. This doesn't appear to be the default unfiltered behaviour, nor can I find a built-in solution.
Am I missing an obvious solution, or do I need to manually handle this case? If the later, I'd be interested in pointers in the right direction.
Source: (StackOverflow)
I found scalate-console on the Scalate website. I think it would help significantly with debugging. Is there a way to use scalate-console with unfiltered? The site says that it uses "WAR overlay" to integrate the feature. Should it also work with mvn jetty:run
?
Source: (StackOverflow)
I'm working with Unfiltered 0.6.8 (using the Jetty connector) and have encountered an odd behaviour: path segments aren't URL decoded.
The following bit of code is my minimal test case:
import unfiltered.request._
import unfiltered.response._
object Test extends App with unfiltered.filter.Plan {
def intent = {
case reg @ Path(Seg(test :: Nil)) =>
println(test)
ResponseString(test)
}
unfiltered.jetty.Http.local(8080).filter(Test).run()
}
Querying http://localhost:8080/some_string
yields the expected result: some_string
, both on the client and server side.
On the other hand, http://localhost:8080/some%20string
yields some%20string
on both client and server, rather than the some string
I was expecting.
Working around the issue is trivial (java.net.URLDecoder#decode(String, String)
), but I'd like to know if:
- I'm forgetting something trivial and making a fool of myself.
- unfiltered has a kit to deal with the hassle automatically.
- if none of the above, is there a particular reason for this behaviour, or should I file a bug report?
As a side note, the unfiltered
tag doesn't exist and I do not have enough reputation to create it, which is why I defaulted to scala
.
Source: (StackOverflow)
I'm new to Scala and trying to understand the syntax the pattern matching constructs, specifically from examples in Unfiltered (http://unfiltered.databinder.net/Try+Unfiltered.html).
Here's a simple HTTP server that echos back Hello World! and 2 parts of the path if the path is 2 parts long:
package com.hello
import unfiltered.request.GET
import unfiltered.request.Path
import unfiltered.request.Seg
import unfiltered.response.ResponseString
object HelloWorld {
val sayhello = unfiltered.netty.cycle.Planify {
case GET(Path(Seg(p :: q :: Nil))) => {
ResponseString("Hello World! " + p + " " + q);
}
};
def main(args: Array[String]) {
unfiltered.netty.Http(10000).plan(sayhello).run();
}
}
Also for reference the source code for the Path, Seg, and GET/Method objects:
package unfiltered.request
object Path {
def unapply[T](req: HttpRequest[T]) = Some(req.uri.split('?')(0))
def apply[T](req: HttpRequest[T]) = req.uri.split('?')(0)
}
object Seg {
def unapply(path: String): Option[List[String]] = path.split("/").toList match {
case "" :: rest => Some(rest) // skip a leading slash
case all => Some(all)
}
}
class Method(method: String) {
def unapply[T](req: HttpRequest[T]) =
if (req.method.equalsIgnoreCase(method)) Some(req)
else None
}
object GET extends Method("GET")
I was able to break down how most of it works, but this line leaves me baffled:
case GET(Path(Seg(p :: q :: Nil))) => {
I understand the purpose of the code, but not how it gets applied. I'm very interested in learning the ins and outs of Scala rather than simply implementing an HTTP server with it, so I've been digging into this for a couple hours. I understand that it has something to do with extractors and the unapply
method on the GET
, Path
, and Seg
objects, I also knows that when I debug it hits unapply
in GET
before Path
and Path
before Seg
.
I don't understand the following things:
Why can't I write GET.unapply(req)
, but I can write GET(req)
or GET()
and it will match any HTTP GET?
Why or how does the compiler know what values get passed to each extractor's unapply
method? It seems that it will just chain them together unless one of them returns a None
instead of an Some
?
How does it bind the variables p and q? It knows they are Strings, it must infer that from the return type of Seg.unapply
, but I don't understand the mechanism that assigns p the value of the first part of the list and q the value of the second part of the list.
Is there a way to rewrite it to make it more clear what's happening? When I first looked at this example, I was confused by the line
val sayhello = unfiltered.netty.cycle.Planify {
, I dug around and rewrote it and found out that it was implicitly creating a PartialFunction and passing it to Planify.apply.
Source: (StackOverflow)
Unfiltered offers an easy way to specify routes.
case GET(Path("/ask")) =>
It also offers an easy way to extract parameters.
case Params(params) =>
What if I want to do both? What is good style for this? I realize I could:
- use
case req @ GET(Path("/ask"))
and use req.parameterValues
- match a second time on
req
- call
Params.unapply
directly
What should I do?
Source: (StackOverflow)
I have an Unfiltered Netty server that I need to shutdown and restart after every test.
val mockService = unfiltered.netty.Server.http(mockServicePort).handler(mockServicePlan)
before {
proxyServer.start()
}
after {
proxyServer.stop()
}
Currently, this is not working, and I am fairly certain that is because the stop()
function is non-blocking and so the following start()
function gets called to early.
I looked for a way to block or get notified on server closure, but it would not appear to be surfaced through the current API.
Is there a better way of achieving what I am trying to achieve?
Source: (StackOverflow)
I am trying use the Scala Unfiltered framework. One of the main benefits of Scala is the async nature which makes use of futures. However, I am unable to get these to work when creating async web request/responses.
I have the following minimal Main.scala
in order to illustrate my issue:
import unfiltered.filter._
import unfiltered.request._
import unfiltered.response._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.Future
import scala.concurrent.duration.DurationInt
class HelloWorldApp extends Plan {
def intent = {
case GET(Path("/")) => Future {
PlainTextContent ~> ResponseString( "Ok" )
}
}
}
object Server {
def main(args: Array[String]) {
unfiltered.jetty.Http.local(8080).filter(new HelloWorldApp).run
}
}
There must be a specific way of getting async requests working, but I'm not seeing how.
Source: (StackOverflow)