Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » Resource loading with HttpService
Resource loading with HttpService [message #553322] Tue, 17 August 2010 03:41 Go to next message
Bob  is currently offline Bob
Messages: 6
Registered: August 2010
Junior Member
Hi,

I am using HttpService to embbed a WebServer into Eclipse. But I am facing some issue when trying to read resources by using ServletContext#getResource.

I have following setup:

0. internal bundle structure

+ Root
+--+ bin
+--+ META-INF
+--+ resources
   +--+ WebContent
      +-- index.html


1. bundle classpath
Bundle-ClassPath: ./bin, ./resources


2. register resource
httpService.registerResource("/", "/WebContent", myHttpContext);


I want to get load resources by calling:

servletContext.getResource("/index.html");


which returns null.

Instead I need to call the following to get the InputStream:

servletContext.getResource("/WebContent/index.html");


I expect the first one to work since I explicitly set the ressource mapping for alias "/" to "/WebContent". When I use ServletContext#getResource in plain WAR-enviroments like Tomcat this is working. So I am wondering whether there is any difference or whether I am missing something.

Thanks,
Bob

Do I miss something?
Re: Resource loading with HttpService [message #553456 is a reply to message #553322] Tue, 17 August 2010 11:15 Go to previous messageGo to next message
Gunnar Wagenknecht is currently offline Gunnar Wagenknecht
Messages: 451
Registered: July 2009
Senior Member
Am 17.08.2010 09:41, schrieb Bob:
> 1. bundle classpath
>
> Bundle-ClassPath: ./bin, ./resources

../resources shouldn't be on the classpath.

> 2. register resource
>
> httpService.registerResource("/", "/WebContent", myHttpContext);

Try:

httpService.registerResource("/", "/resources/WebContent", null);

How does "myHttpContext" look like?

-Gunnar

--
Gunnar Wagenknecht
gunnar@wagenknecht.org
http://wagenknecht.org/
Re: Resource loading with HttpService [message #553593 is a reply to message #553322] Wed, 18 August 2010 03:04 Go to previous messageGo to next message
Bob  is currently offline Bob
Messages: 6
Registered: August 2010
Junior Member
I found out that the resource mapping is not resolved when using ServletContext#getResource. Nevertheless it is done when calling the resource via Browser URL by org.eclipse.equinox.http.servlet.internal.HttpServiceServlet .

Even when I create a custom HttpContext I cannot properly resolve the mapping since both cases are handled by HttpContext in the end and it is not possible to determine whether to resolve the mapping or not.

I would consider this as a bug. What do you think?
Re: Resource loading with HttpService [message #553611 is a reply to message #553593] Wed, 18 August 2010 04:14 Go to previous messageGo to next message
Gunnar Wagenknecht is currently offline Gunnar Wagenknecht
Messages: 451
Registered: July 2009
Senior Member
Am 18.08.2010 09:04, schrieb Bob:
> I found out that the resource mapping is not resolved when using
> ServletContext#getResource. Nevertheless it is done when calling the
> resource via Browser URL by
> org.eclipse.equinox.http.servlet.internal.HttpServiceServlet .

When doing #registerResource you make a resource available to the
HttpService for accessing it via URLs. There is nothing in the spec that
implies that those mappings also work with ServletContext#getResource.
You need to implement your own HttpContext in order to allow such a
mapping facility.

> Even when I create a custom HttpContext I cannot properly resolve the
> mapping since both cases are handled by HttpContext in the end and it
> is not possible to determine whether to resolve the mapping or not.

What you mean by both cases? I think you are mixing resource
registrations with context mappings.

That's what you have:
> + Root
> +--+ bin
> +--+ META-INF
> +--+ resources
> +--+ WebContent
> +-- index.html

That's what you want:
> I want to get load resources by calling:
> servletContext.getResource("/index.html");

In order to do that you need to implement your own HttpContext which
knows that resources should be looked up from "/resource/WebContent".

If you also want to make stuff from "/resource/WebContent" public
available so that it can be accessed via "http://myhost/index.html" you
need to call registerResource the following way:

httpService.registerResource("/", "", theImplementedHttpContext);

Alternatively, you could also call:

httpService.registerResource("/", "/resource/WebContent", null);

This will create a default context that looks up resources from your
bundle. However, the context is just a default with no special mapping
logic.

-Gunnar

--
Gunnar Wagenknecht
gunnar@wagenknecht.org
http://wagenknecht.org/
Re: Resource loading with HttpService [message #553686 is a reply to message #553611] Wed, 18 August 2010 09:48 Go to previous messageGo to next message
Bob  is currently offline Bob
Messages: 6
Registered: August 2010
Junior Member
Hi Gunnar,

thanks for your comments.

Quote:

In order to do that you need to implement your own HttpContext which
knows that resources should be looked up from "/resource/WebContent".



I already tried implementing my own HttpContext with specific getResource handling. This context will be called in both cases:

a) ServletContext#getResource

b) and in case the resource is accessed by Browser URL

But the String argument passed in is different for both scenarios.

a) mapping is not resolved

b) mapping is resolved


See following sample implementation:

	public URL getResource(String name) {

		// --> debug here
		if ("/index.html".equals(name)) {
			// case 1: called by browser
			LOG.log(Level.INFO, "called by browser: " + name);
			
			
		} else if ("/WebContent/index.html".equals(name)) {
			// case 2: called by ServletContext#getResource
			LOG.log(Level.INFO, "called by ServletContext#getResource: " + name);
			
		}

		// will call Bundle#getResource()
		URL url = defaultContext.getResource(name);

		return url;
	}


Thanks,
Bob
Re: Resource loading with HttpService [message #553711 is a reply to message #553686] Wed, 18 August 2010 10:30 Go to previous messageGo to next message
Gunnar Wagenknecht is currently offline Gunnar Wagenknecht
Messages: 451
Registered: July 2009
Senior Member
Bob,

Am 18.08.2010 15:48, schrieb Bob:
> But the String argument passed in is different for both scenarios.

Please see my previous posting. It's because of the parameter you
specify when calling #registerResource.

-Gunnar


--
Gunnar Wagenknecht
gunnar@wagenknecht.org
http://wagenknecht.org/
Re: Resource loading with HttpService [message #553926 is a reply to message #553711] Thu, 19 August 2010 08:44 Go to previous messageGo to next message
Bob  is currently offline Bob
Messages: 6
Registered: August 2010
Junior Member
Hi Gunnar,

I see that your proposal of not setting a resource name at all is working:

Quote:

httpService.registerResource("/", "", theImplementedHttpContext);



This would mean the custom HttpContext has to know about the resource name.

If this is the way how it is intended to work I would consider the API for registering resources of the HttpService as not designed very well because:

1. you can either set the resource name or a custom HttpContext

2. in case you use the default HttpContext and specify a resource name the method ServletContext#getResource is not working as expected as it is not resolving the mapping.

What should the resource name be good for when it is not resolved correctly by default context and can you need to write your own context which takes care of the resource name instead?

But still I consider this behavior as a bug for which your proposal is a working workarround.

I can also provide a demo applciation, but I am not able to upload to as attachment to this thread.

Thanks,
Bob
Re: Resource loading with HttpService [message #553937 is a reply to message #553926] Thu, 19 August 2010 09:17 Go to previous messageGo to next message
Gunnar Wagenknecht is currently offline Gunnar Wagenknecht
Messages: 451
Registered: July 2009
Senior Member
Am 19.08.2010 14:44, schrieb Bob:
> This would mean the custom HttpContext has to know about the resource name.

Yes. That's the reason you have a _custom_ HttpContext (the other one
being security). Otherwise you would simply be happy with the mapping
and use the default one.

> What should the resource name be good for when it is not resolved correctly by default context and can you need to write your own context which takes care of the resource name instead?

This is what the JavaDoc says:

For example, suppose the resource name /tmp is registered to the alias
/files. A request for /files/foo.txt will map to the resource name
/tmp/foo.txt.

And this is exactly how it's implemented. The JavaDoc does not say
anything about the mapping being applied to "ServletContext" as well.
"registerResources" works for HTTP access only.

What you want is your own extensible HttpContext. You register your
servlets using this context and you register your resources with this
context as well. The extension registry version of the Equinox
HttpService does something like that.

-Gunnar

--
Gunnar Wagenknecht
gunnar@wagenknecht.org
http://wagenknecht.org/
Re: Resource loading with HttpService [message #554001 is a reply to message #553937] Thu, 19 August 2010 11:59 Go to previous messageGo to next message
Bob  is currently offline Bob
Messages: 6
Registered: August 2010
Junior Member
Last try to convince you Wink

The Servlet spec says:

Quote:

The getResource and getResourceAsStream methods take a String with a
leading "/" as an argument that gives the path of the resource relative to the root of
the context.




So I expect the default context to behave as defined in the Servlet spec without writing my own HttpContext.

Of course I will write my own context for security or classloading reasons when resources are registered from other bundles.
Re: Resource loading with HttpService [message #554030 is a reply to message #554001] Thu, 19 August 2010 14:17 Go to previous messageGo to next message
Gunnar Wagenknecht is currently offline Gunnar Wagenknecht
Messages: 451
Registered: July 2009
Senior Member
Am 19.08.2010 17:59, schrieb Bob:
> Last try to convince you ;)

;)

> So I expect the default context to behave as defined in the Servlet spec without writing my own HttpContext.

The thing is that HttpContext != ServletContext. At least, I'm not aware
that the OSGi HttpService spec implies any guarantees on ServletContext
& Co. To my knowledge this was always something left to the container.
Another examples would be welcome files. The servlet spec talks about
them but not the OSGi HttpService. It really was not designed as a full
Servlet spec replacement.

FWIW, I also put some effort into writing my own stuff to compensate
such limitations. Another thing is, that everything registered with
HttpServices shares the same HttpSession. That might be ok if you end up
deploying your stuff as a WAR using the servlet bridge. However, it's
definitely not acceptable when running with different bundles in a
shared system.

-Gunnar

--
Gunnar Wagenknecht
gunnar@wagenknecht.org
http://wagenknecht.org/
Re: Resource loading with HttpService [message #554087 is a reply to message #554030] Fri, 20 August 2010 02:57 Go to previous messageGo to next message
Bob  is currently offline Bob
Messages: 6
Registered: August 2010
Junior Member
Hi Gunnar,

thank you very much for this discussion.

Do you know whether there is an official list of limitations comparing to Servlet spec? I think something like this would be helpful to see what should reliable work and what is not working. I found some limitations but burried in powerpoints from some conventions.

Thanks,
Bob
Re: Resource loading with HttpService [message #554166 is a reply to message #554087] Fri, 20 August 2010 07:39 Go to previous message
Gunnar Wagenknecht is currently offline Gunnar Wagenknecht
Messages: 451
Registered: July 2009
Senior Member
Am 20.08.2010 08:57, schrieb Bob:
> Do you know whether there is an official list of limitations comparing to Servlet spec?

Unfortunately not. I think it's also implementation specific. FWIW,
please don't hesitate to start a wiki page!

-Gunnar


--
Gunnar Wagenknecht
gunnar@wagenknecht.org
http://wagenknecht.org/
Previous Topic:Implications of Contributions from Multiple Developers to OSGI
Next Topic:Automaticlly Updates pref checkbox default enable
Goto Forum:
  


Current Time: Thu Aug 21 18:05:44 EDT 2014

Powered by FUDForum. Page generated in 0.01830 seconds