Skip to content
This repository has been archived by the owner on Jul 19, 2020. It is now read-only.

Remove Servlet API dependency (#73) #132

Merged
merged 1 commit into from
Mar 1, 2018

Conversation

chkal
Copy link
Contributor

@chkal chkal commented Feb 22, 2018

This pull request addresses #73, which was discussed quite some time ago. JAX-RS does NOT depend on the Servlet API and can therefore be used in other environments. As MVC is built on top of JAX-RS, it makes no sense to introduce a dependency on the Servlet API.

The only class which currently depends on the Servlet API is ViewEngineContext, which exposes the HttpServletRequest and the HttpServletResponse. Beside the fact that this is the only reason for us to depend on Servlet, exposing these Servlet specific classes is bad for other reasons:

  • It allows to write the result of processing the view directly to HttpServletResponse.getOutputStream() which basically completely bypasses JAX-RS.
  • It allows to write to HttpServletResponse.getWriter() which may use a completely different encoding from the one negotiated via @Produces.

This pull request contains the following changes:

  • The Servlet API is removed from our pom.xml.
  • Removed a reference to the Servlet API for the MvcContext javadocs.
  • Remove ViewEngineContext.getRequest() and ViewEngineContext.getResponse().
  • Added ViewEngineContext.getOutputStream() which will be the primary way to obtain the stream to write the view to.
  • Added ViewEngineContext.getResponseHeaders() which exposes a mutable map which can be used to modify the HTTP response headers. I don't really the mutable map approach, but that's the way JAX-RS exposes such headers for MessageBodyWriter, so we should be consistent with JAX-RS here.
  • Added ViewEngineContext.getMediaType() to provide the ViewEngine with all required information about the negotiated media type and character encoding.
  • I understand that there may be reasons for ViewEngine implementation to need the HttpServletRequest or HttpServletResponse. For these cases I added ViewEngineContext.getRequest(Class<T>) and ViewEngineContext.getResponse(Class<T>). These methods don't have a hard dependency on the Servlet API but allow to obtain the request via getRequest(HttpServletRequest.class). It is a bit similar to JSF's ExternalContext.getRequest() but more type safe like EntityManager.unwrap(Class). The nice thing about these methods is that they also support other request/response objects in non-servlet environments.

I already modified Ozark to support this new API on a private branch. It worked great! And I was even able to fix some really serve encoding issues in some of the 3rd party view engines.

This is a very huge change. But it is worth the effort. So any feedback is welcome.

@chkal chkal added this to the 1.0-pfd milestone Feb 22, 2018
@chkal chkal requested review from ivargrimstad, lefloh, kito99 and a team February 22, 2018 10:13
Copy link
Member

@ivargrimstad ivargrimstad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

@Daniel-Dos
Copy link
Contributor

very good.

Copy link
Contributor

@lefloh lefloh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thanks a lot!

Copy link
Contributor

@Daniel-Dos Daniel-Dos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was excellent.

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

Successfully merging this pull request may close these issues.

None yet

4 participants