Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change http status code on interceptor #1035

Open
nykolaslima opened this issue Dec 5, 2015 · 7 comments
Open

Change http status code on interceptor #1035

nykolaslima opened this issue Dec 5, 2015 · 7 comments
Labels

Comments

@nykolaslima
Copy link
Contributor

I have a simple controller:

@Post("/user")
public void add(User user) {
 userDao.add(user);
 result.use(status()).created();
}

But there is a HibernateTransactionInterceptor that commits the database transaction at the end of the request. But if there is some error commiting the request, I want to change the response status:

@AroundCall
    public void intercept(SimpleInterceptorStack stack) {
        try {
            stack.next();
        } catch(Exception e) {
            logger.error("Unexpected exception", e);
            result.use(status()).internalServerError();
        }
    }

But the response was already commited and then the returned http code is the created(201) that was configured in the controller.

Is there any way to fix it? @Turini

@nykolaslima
Copy link
Contributor Author

I got it fixed here by overriding GsonSerializer class to avoid flushQuietly(writer); method to be called. This call commits the HttpServletResponse before it should be committed, because interceptors could still make changes in it.

@csokol @Turini let's change those classes to do not flush the HttpServletResponse's OutputStream?

@Turini
Copy link
Member

Turini commented Dec 7, 2015

@nykolaslima that sounds reasonable to me... i'm just not sure if it'll break any expected
behaviour. Can we just remove the flushQuietly call and everything will work just fine?

@nykolaslima
Copy link
Contributor Author

From the tests I've made everything works fine. I believe that flush is already called somewhere, maybe it should be called from application server and not from application code. @Turini

@csokol
Copy link
Contributor

csokol commented Dec 7, 2015

@nykolaslima, I guess overriding GsonSerializer only fixes the case in which you're using Results#json, right? Does it solve the issue with Result#status?

@nykolaslima
Copy link
Contributor Author

I guess it solves the Result#status problem too. Because the response isn't forced to flush in the GsonSerializer.

@csokol
Copy link
Contributor

csokol commented Dec 7, 2015

Huum, strange, why is Result#status related with json serialization?

On Mon, Dec 7, 2015 at 12:01 PM Nykolas Laurentino de Lima <
[email protected]> wrote:

I guess it solves the Result#status problem too. Because the response
isn't forced to flush in the GsonSerializer.


Reply to this email directly or view it on GitHub
#1035 (comment).

@nykolaslima
Copy link
Contributor Author

Sorry @csokol, you are right.
Just using #Result.status works fine. The problem is in flush.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants