Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Java Spark after filter to run custom action when a 404 happens

I'll try to keep it short. Here's the problem I'm having while trying to understand Spark filters. I'm trying to create a simple app and one of the things that it should do is to create an error report every time the client is about to see an http error e.g. 404 or 500. Here's how my app looks like:

import static spark.Spark.*;

public class MyApp {
    public static void main(String[] args) {
        get("/hello", (req, res) -> "{\"status\":\"OK\"}");

        after((request, response) -> {
            if (response.raw().getStatus() == 404) {
                // here run the code that will report the error e.g. 
                System.out.println("An error has occurred!!");
            }
        });
    }
}

For some reason, the response parameter has its status attribute set to 0 when I'm checking if it's set to 404. The documentation says "after" filters are evaluated after each request and can read the request and read/modify the response so, I should be able to do it somehow (unless the docs are wrong).

Basically, I'm trying to intercept http errors using an after filter but when I try to check the response I don't get what I expect.

Has anyone an idea what would be a different way of doing the same thing or how to make this work?

Thanks.

like image 612
user43624 Avatar asked Nov 20 '14 23:11

user43624


People also ask

Is it easy to execute a filtering query in spark?

Executing a filtering query is easy… filtering well is difficult. Read the Beautiful Spark book if you want to learn how to create create data lakes that are optimized for performant filtering operations.

How to use filter in Apache spark dataframe?

Apache Spark Spark filter () or where () function is used to filter the rows from DataFrame or Dataset based on the given one or multiple conditions or SQL expression. You can use where () operator instead of the filter if you are coming from SQL background. Both these functions operate exactly the same.

What is the difference between where () and filter () in spark?

Spark filter () or where () function is used to filter the rows from DataFrame or Dataset based on the given one or multiple conditions or SQL expression. You can use where () operator instead of the filter if you are coming from SQL background. Both these functions operate exactly the same.

What is spark Java?

What is Spark Java? Spark is a Java micro framework that allows to quickly create web applications in Java 8. Spark is a lightweight and simple Java web framework designed for quick development. Sinatra, a popular Ruby micro framework, was the inspiration for it.


Video Answer


2 Answers

I solved this one using wildcard routes. Instead of calling the after method, I added a route for each of the HTTP methods that binds the "*" route.

It's important to have them at the bottom of the your Main method so if no route gets resolved these ones always get triggered.

Here's an example:

import static spark.Spark.*;

public class MyApp {
    public static void main(String[] args) {
        get("/hello", (req, res) -> "{\"status\":\"OK\"}");

        get("*", (request, response) -> {
            System.out.println("404 not found!!");
            // email me the request details ...    
        );
    }
}
like image 163
user43624 Avatar answered Sep 22 '22 13:09

user43624


The preferred manner to achieve what you are looking for would look like this.

get("/hello", (request, response) -> {
    // look up your resource using resourceId in request/path/query
    // oh dear, resource not found
    throw new NotFoundException(resourceId);
});

exception(NotFoundException.class, (e, request, response) -> {
    response.status(404);
    response.body(String.format("Resource {%s} not found", e.getResourceId()));
    // doReporting here.
});

public class NotFoundException extends Exception {
    private final String resourceId;
    public NotFoundException(String resourceId) {
        this.resourceId = resourceId;
    }

    public String getResourceId() {
        return resourceId;
    }
}
like image 31
dom farr Avatar answered Sep 20 '22 13:09

dom farr