Discussion:
docroot when using Grizzly2 server and ServletHandler
Farrukh Najmi
2012-01-06 23:09:42 UTC
Permalink
Hi Guys,

This is a dumb question but...

I am using a Grizzly2 embedded server created via
com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory to host a
jersey based REST endpoint using a
org.glassfish.grizzly.servlet.ServletHandler.

Certain requests on that endpoint return a web page that needs to
reference some javascript files.
I am not sure where to place the javascript files nor how to configure
the HttpSerfver or ServletHandler instance so it can find the files.

Any pointers would be terrific. Thanks.
--
Regards,
Farrukh Najmi

Web: http://www.wellfleetsoftware.com
Farrukh Najmi
2012-01-07 15:05:05 UTC
Permalink
It looks like the following thread may answer my questions. Will try it
out. Thanks.

http://jersey.576304.n2.nabble.com/Grizzly-Jersey-REST-and-static-HTML-td7079979.html
Post by Farrukh Najmi
Hi Guys,
This is a dumb question but...
I am using a Grizzly2 embedded server created via
com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory to host a
jersey based REST endpoint using a
org.glassfish.grizzly.servlet.ServletHandler.
Certain requests on that endpoint return a web page that needs to
reference some javascript files.
I am not sure where to place the javascript files nor how to configure
the HttpSerfver or ServletHandler instance so it can find the files.
Any pointers would be terrific. Thanks.
--
Regards,
Farrukh

Web: http://www.wellfleetsoftware.com
Farrukh Najmi
2012-01-09 15:02:47 UTC
Permalink
Hi Guys,

I am still having troubling getting doc root to work with a Grizzly2
HttpServer. I get a 404 when I try a a url in the docroot directory.

Here is the code I am using in a junit test program in my project. Can
any one guide me as to what I am doing wrong. Thanks.

//Begin code fragment for server creation and startup

URI uri = getBaseUri();
ServletHandler servletHandler = getServletHandler();
//See function def further below
org.glassfish.grizzly.http.server.HttpServer server =
GrizzlyServerFactory.createHttpServer(
uri,
servletHandler,
false
);

//Following code was added to enable docroot to be the
webapp dir under target/classes of my maven project
ServerConfiguration serverConfiguration =
server.getServerConfiguration();
String cwd = System.getProperty("user.dir");
String docRootFolder = cwd + "/target/classes/webapp";
serverConfiguration.addHttpHandler(new
StaticHttpHandler(docRootFolder), servletHandler.getContextPath());

server.start();
//End code fragment for server creation and startup

//Begin function that creates ServletHandler for my spring based app
public ServletHandler getServletHandler() {
if (servletHandler == null) {
servletHandler = new ServletHandler();

servletHandler.setServletInstance(getServlet());
servletHandler.setContextPath(contextRoot);

//Context params
servletHandler.addContextParameter("contextConfigLocation",
getAsString(springContextLocations));

//Init params
//jerseyAdapter.addInitParameter("contextConfigLocation",
contextLocations);

servletHandler.addInitParameter("com.sun.jersey.config.property.packages",
getAsString(packagesToScan));

//Servlet listeners

servletHandler.addServletListener("org.springframework.web.context.ContextLoaderListener");

servletHandler.addServletListener("org.springframework.web.context.request.RequestContextListener");
}
return servletHandler;
}
//End function that creates ServletHandler for my spring based app
Post by Farrukh Najmi
It looks like the following thread may answer my questions. Will try
it out. Thanks.
http://jersey.576304.n2.nabble.com/Grizzly-Jersey-REST-and-static-HTML-td7079979.html
Post by Farrukh Najmi
Hi Guys,
This is a dumb question but...
I am using a Grizzly2 embedded server created via
com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory to host a
jersey based REST endpoint using a
org.glassfish.grizzly.servlet.ServletHandler.
Certain requests on that endpoint return a web page that needs to
reference some javascript files.
I am not sure where to place the javascript files nor how to
configure the HttpSerfver or ServletHandler instance so it can find
the files.
Any pointers would be terrific. Thanks.
--
Regards,
Farrukh Najmi

Web: http://www.wellfleetsoftware.com
Oleksiy Stashok
2012-01-09 15:59:29 UTC
Permalink
Hi Farrukh,

can you pls. attach the testcase I can use to reproduce the problem?

Thanks.

WBR,
Alexey.
Post by Farrukh Najmi
Hi Guys,
I am still having troubling getting doc root to work with a Grizzly2
HttpServer. I get a 404 when I try a a url in the docroot directory.
Here is the code I am using in a junit test program in my project. Can
any one guide me as to what I am doing wrong. Thanks.
//Begin code fragment for server creation and startup
URI uri = getBaseUri();
ServletHandler servletHandler = getServletHandler();
//See function def further below
org.glassfish.grizzly.http.server.HttpServer server =
GrizzlyServerFactory.createHttpServer(
uri,
servletHandler,
false
);
//Following code was added to enable docroot to be the
webapp dir under target/classes of my maven project
ServerConfiguration serverConfiguration =
server.getServerConfiguration();
String cwd = System.getProperty("user.dir");
String docRootFolder = cwd + "/target/classes/webapp";
serverConfiguration.addHttpHandler(new
StaticHttpHandler(docRootFolder), servletHandler.getContextPath());
server.start();
//End code fragment for server creation and startup
//Begin function that creates ServletHandler for my spring based app
public ServletHandler getServletHandler() {
if (servletHandler == null) {
servletHandler = new ServletHandler();
servletHandler.setServletInstance(getServlet());
servletHandler.setContextPath(contextRoot);
//Context params
servletHandler.addContextParameter("contextConfigLocation",
getAsString(springContextLocations));
//Init params
//jerseyAdapter.addInitParameter("contextConfigLocation",
contextLocations);
servletHandler.addInitParameter("com.sun.jersey.config.property.packages",
getAsString(packagesToScan));
//Servlet listeners
servletHandler.addServletListener("org.springframework.web.context.ContextLoaderListener");
servletHandler.addServletListener("org.springframework.web.context.request.RequestContextListener");
}
return servletHandler;
}
//End function that creates ServletHandler for my spring based app
Post by Farrukh Najmi
It looks like the following thread may answer my questions. Will try
it out. Thanks.
http://jersey.576304.n2.nabble.com/Grizzly-Jersey-REST-and-static-HTML-td7079979.html
Post by Farrukh Najmi
Hi Guys,
This is a dumb question but...
I am using a Grizzly2 embedded server created via
com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory to host
a jersey based REST endpoint using a
org.glassfish.grizzly.servlet.ServletHandler.
Certain requests on that endpoint return a web page that needs to
reference some javascript files.
I am not sure where to place the javascript files nor how to
configure the HttpSerfver or ServletHandler instance so it can find
the files.
Any pointers would be terrific. Thanks.
Farrukh Najmi
2012-01-09 17:09:09 UTC
Permalink
Hi Alexey,

Thank you for offering to help. Before I do the fair amount of work to
create a standalone test case I thought to share observations below in
case it helps identify my problem:

* If I do not call "serverConfiguration.addHttpHandler" on my
HttpServer's configuration then I can get dynamic resources served
by my jersey rest endpoint. This is the base line that has worked
for a long time before I had need for static resources to be
served by same servlet as jersey endpoint
* If I call "serverConfiguration.addHttpHandler" on my HttpServer then:
o If I specify a context path for the StaticHttpHandler that
is different from the context path for the ServletHandler
for the SpringServlet for my jersey rest endpoint then I am
able to get static resources served by the
StaticHttpHandler. However, then I am no longer able to get
dynamic resources served by my jersey rest endpoint.
o If I specify a context path for the StaticHttpHandler that
is same as the context path for the ServletHandler for the
SpringServlet for my jersey rest endpoint then I am not able
to get static resources served by the StaticHttpHandler nor
am I able to get dynamic resources served by my jersey rest
endpoint.

See code fragment in my previous email for reference. It seems that if I
include it my email gets tagged as spam.

Can you give me a rough sketch of how I create my HttpServer such that:

* The Grizzly2 server serves static resources (in my case javascript
files) from some context path from my servlet
* The Grizzly2 server serves dynamic HTML resources from my jersey
rest endpoint that references the static javascript resources
within <script> tags
* The reference to static resources is relative to the dynamic
resources rather than absolute

Thanks for your help.
--
Regards,
Farrukh Najmi

Web:http://www.wellfleetsoftware.com
Oleksiy Stashok
2012-01-09 17:29:27 UTC
Permalink
Hi Farrukh,

Servlet and Static Handlers should be registered in Grizzly with proper
mapping, and most probably there is an issue.
If you register ServletHandler with some URI, say "/myservlet" - then
try to register static handler with "/myservlet/static" URI. In this
case all the requests to "/myservlet/static/..." will be redirected to
StaticHandler and other requests to "/myservlet/..." will be redirected
to ServletHandler.

Thanks.

WBR,
Alexey.

PS: you can create an issue and attach the testcase to it.
Post by Farrukh Najmi
Hi Alexey,
Thank you for offering to help. Before I do the fair amount of work to
create a standalone test case I thought to share observations below in
* If I do not call "serverConfiguration.addHttpHandler" on my
HttpServer's configuration then I can get dynamic resources served
by my jersey rest endpoint. This is the base line that has worked
for a long time before I had need for static resources to be
served by same servlet as jersey endpoint
o If I specify a context path for the StaticHttpHandler that is
different from the context path for the ServletHandler for the
SpringServlet for my jersey rest endpoint then I am able to
get static resources served by the StaticHttpHandler. However,
then I am no longer able to get dynamic resources served by my
jersey rest endpoint.
o If I specify a context path for the StaticHttpHandler that is
same as the context path for the ServletHandler for the
SpringServlet for my jersey rest endpoint then I am not able
to get static resources served by the StaticHttpHandler nor am
I able to get dynamic resources served by my jersey rest endpoint.
See code fragment in my previous email for reference. It seems that if
I include it my email gets tagged as spam.
* The Grizzly2 server serves static resources (in my case javascript
files) from some context path from my servlet
* The Grizzly2 server serves dynamic HTML resources from my jersey
rest endpoint that references the static javascript resources
within <script> tags
* The reference to static resources is relative to the dynamic
resources rather than absolute
Thanks for your help.
--
Regards,
Farrukh Najmi
Web:http://www.wellfleetsoftware.com
Farrukh Najmi
2012-01-09 17:57:57 UTC
Permalink
Hi Alexey,

I think we are getting closer to resolving this.

I now use the following code to add the StaticHttpHandler to my
ServerConfiguration:

ServerConfiguration serverConfiguration =
server.getServerConfiguration();
String cwd = System.getProperty("user.dir");
String docRootFolder = cwd + "/target/classes/webapp";
serverConfiguration.addHttpHandler(new
StaticHttpHandler(docRootFolder), servletHandler.getContextPath() +
"/static");

The servletHandler.getContextPath() value is "/myserver/rest".

If I comment out the last line of code above then the following URLs to
my jersey REST endpoint work fine:

http://localhost:9070/myserver/rest/search?queryId=urn:oasis:names:tc:ebxml-regrep:query:BasicQuery&name=%25usa%25

But if I uncomment it then the same query returns a 404 as does the
following GET request for the static resource readme.txt

http://localhost:9070/myserver/rest/static/readme.txt

Does it seem like I am doing everything as you would expect? Is there
anything else to try before creating a standalone test case (a lot of
work) and filing an issue?
Can any one tell me if they have gottent Grizzly2 server to serve both
dynamic resource from a jersey endpoint as well as static resources
using com.sun.jersey.spi.spring.container.servlet.SpringServlet which is
what I am using.

Thanks again for your help.
Post by Oleksiy Stashok
Hi Farrukh,
Servlet and Static Handlers should be registered in Grizzly with
proper mapping, and most probably there is an issue.
If you register ServletHandler with some URI, say "/myservlet" - then
try to register static handler with "/myservlet/static" URI. In this
case all the requests to "/myservlet/static/..." will be redirected to
StaticHandler and other requests to "/myservlet/..." will be
redirected to ServletHandler.
Thanks.
WBR,
Alexey.
PS: you can create an issue and attach the testcase to it.
Post by Farrukh Najmi
Hi Alexey,
Thank you for offering to help. Before I do the fair amount of work
to create a standalone test case I thought to share observations
* If I do not call "serverConfiguration.addHttpHandler" on my
HttpServer's configuration then I can get dynamic resources
served by my jersey rest endpoint. This is the base line that
has worked for a long time before I had need for static
resources to be served by same servlet as jersey endpoint
o If I specify a context path for the StaticHttpHandler
that is different from the context path for the
ServletHandler for the SpringServlet for my jersey rest
endpoint then I am able to get static resources served by
the StaticHttpHandler. However, then I am no longer able
to get dynamic resources served by my jersey rest endpoint.
o If I specify a context path for the StaticHttpHandler
that is same as the context path for the ServletHandler
for the SpringServlet for my jersey rest endpoint then I
am not able to get static resources served by the
StaticHttpHandler nor am I able to get dynamic resources
served by my jersey rest endpoint.
See code fragment in my previous email for reference. It seems that
if I include it my email gets tagged as spam.
* The Grizzly2 server serves static resources (in my case
javascript files) from some context path from my servlet
* The Grizzly2 server serves dynamic HTML resources from my
jersey rest endpoint that references the static javascript
resources within <script> tags
* The reference to static resources is relative to the dynamic
resources rather than absolute
Thanks for your help.
--
Regards,
Farrukh Najmi

Web: http://www.wellfleetsoftware.com
Farrukh Najmi
2012-01-09 22:02:19 UTC
Permalink
Hi ALeksey,

Per your suggestion, I have created a reproducable test case and filed
an issue here:

http://java.net/jira/browse/JERSEY-911

I would be very grateful for any quick workaround etc. that you could
provide. I suspect / hope its something really trivial with an easy
workaround and may likley be an error on my part rather than a bug.
Thanks very much for all your help.
Post by Farrukh Najmi
Hi Alexey,
I think we are getting closer to resolving this.
I now use the following code to add the StaticHttpHandler to my
ServerConfiguration serverConfiguration =
server.getServerConfiguration();
String cwd = System.getProperty("user.dir");
String docRootFolder = cwd + "/target/classes/webapp";
serverConfiguration.addHttpHandler(new
StaticHttpHandler(docRootFolder), servletHandler.getContextPath() +
"/static");
The servletHandler.getContextPath() value is "/myserver/rest".
If I comment out the last line of code above then the following URLs
http://localhost:9070/myserver/rest/search?queryId=urn:oasis:names:tc:ebxml-regrep:query:BasicQuery&name=%25usa%25
But if I uncomment it then the same query returns a 404 as does the
following GET request for the static resource readme.txt
http://localhost:9070/myserver/rest/static/readme.txt
Does it seem like I am doing everything as you would expect? Is there
anything else to try before creating a standalone test case (a lot of
work) and filing an issue?
Can any one tell me if they have gottent Grizzly2 server to serve both
dynamic resource from a jersey endpoint as well as static resources
using com.sun.jersey.spi.spring.container.servlet.SpringServlet which
is what I am using.
Thanks again for your help.
Post by Oleksiy Stashok
Hi Farrukh,
Servlet and Static Handlers should be registered in Grizzly with
proper mapping, and most probably there is an issue.
If you register ServletHandler with some URI, say "/myservlet" - then
try to register static handler with "/myservlet/static" URI. In this
case all the requests to "/myservlet/static/..." will be redirected
to StaticHandler and other requests to "/myservlet/..." will be
redirected to ServletHandler.
Thanks.
WBR,
Alexey.
PS: you can create an issue and attach the testcase to it.
Post by Farrukh Najmi
Hi Alexey,
Thank you for offering to help. Before I do the fair amount of work
to create a standalone test case I thought to share observations
* If I do not call "serverConfiguration.addHttpHandler" on my
HttpServer's configuration then I can get dynamic resources
served by my jersey rest endpoint. This is the base line that
has worked for a long time before I had need for static
resources to be served by same servlet as jersey endpoint
* If I call "serverConfiguration.addHttpHandler" on my
o If I specify a context path for the StaticHttpHandler
that is different from the context path for the
ServletHandler for the SpringServlet for my jersey rest
endpoint then I am able to get static resources served
by the StaticHttpHandler. However, then I am no longer
able to get dynamic resources served by my jersey rest
endpoint.
o If I specify a context path for the StaticHttpHandler
that is same as the context path for the ServletHandler
for the SpringServlet for my jersey rest endpoint then I
am not able to get static resources served by the
StaticHttpHandler nor am I able to get dynamic resources
served by my jersey rest endpoint.
See code fragment in my previous email for reference. It seems that
if I include it my email gets tagged as spam.
* The Grizzly2 server serves static resources (in my case
javascript files) from some context path from my servlet
* The Grizzly2 server serves dynamic HTML resources from my
jersey rest endpoint that references the static javascript
resources within <script> tags
* The reference to static resources is relative to the dynamic
resources rather than absolute
Thanks for your help.
--
Regards,
Farrukh Najmi

Web: http://www.wellfleetsoftware.com
Loading...