will
will

Reputation: 5061

Method to have both HTTPS and HTTP with embedded Spark server?

I have small embedded server supporting HTTPS using Spark. I also want to support/catch some HTTP calls; e.g. like a help page and some redirections.

For example a help page:

  http://localhost:8088/help .... renders .... http::8088//localhost/help.html

or a login page:

  http://localhost::8088/login ... renders .... https::8089//localhost/login.html

I have progressed to the stage where I have an embedded server that responds to either HTTP xor HTTPS -- One or the other, not both together. Is it possible to respond to both protocols with the same embedded Jetty (via Spark) server? If not, is it feasible to run two Jetty servers in the same embedded application? Can I run two Spark servers in the same application? (I'm taking it as 'read' that if Jetty won't do it asking Spark for that service will be a dead-end).

My preferred answer would be if I can use my Spark server to listen and handle both protocols. My investigation so far suggest that only one mode is possible, largely because Spark is actually a static library you can only set-one port; and you may only have one 'Spark' server. Has anyone used the same port for both HTTP and HTTPS? (That just doesn't seem 'good' to me).

I did find Unit Test code that looked like it was using both protocols, TestSparkUtil here:

However that's testing the server and uses lots of deprecated API-s. One final thought is, can I use a second (embedded) Jetty-only servlet to redirect HTTP calls to HTTPS? Would you recommend that?

I'm sure there's an elegant solution here. Thanks in advance for your time to consider the questions framed herewithin.

Upvotes: 4

Views: 2839

Answers (2)

Islam Abdalla
Islam Abdalla

Reputation: 1514

For anyone's interested, the pull request in George answer is no longer available in the master branch. The current proper way to this is by using instance API. Documentations are not available in the official docs, but the details and an example can be found in the news page below.

http://sparkjava.com/news#spark-25-released

Upvotes: 1

George
George

Reputation: 7543

There is a pull request on GitHub addressing this question.

https://github.com/perwendel/spark/pull/329

Update: All the changes are merged with the master branch in the commit here

Upvotes: 2

Related Questions