BUG Community

Welcome! Log In

Forums BUG SDK Servlet: static files?

Subscribe to Servlet: static files?  31 posts, 7 voices

Log in to reply to this topic
 
Oct 14, 2007 7:39pm
Medium Bug Labs team finsprings 268 posts

How can I map static files to they are available via the browser? The default working directory for the VirtualBUG when I run it is my eclipse directory, likely since that’s the folder I’m in when I start eclipse. This is where my ‘security system’ log file and camera snapshots get written to right now, as I’m not specifying a path.

I have a servlet running but when I write an IMG tag I don’t know how to point it at the image file that’s in my working directory. If I put something like:

<img src="/12345.jpg" alt="whatever"/>

it doesn’t find that jpg file when I go to my servlet in the browser.

Oct 15, 2007 9:44am
Medium Bug Labs team kgilmer 215 posts

finsprings,

Take a look at the OSGi HTTP Service in the R3 specification. However currently our servlet container does not support serving resources, only Servlets.

Oct 15, 2007 10:18am
Medium Bug Labs team finsprings 268 posts

So, I have my security thing capturing an image to file when motion is detected. But how can I present that image via the servlet interface without access to static resources? I could load the image data back into memory but you can’t inline IMG tags like that AFAIK.

Oct 15, 2007 2:06pm
Medium Bug Labs team kgilmer 215 posts

Good point finsprings. I hadn’t considered this case. We’ll have to get an update of the HTTP bundle out before this functionality can be used.

Nov 14, 2007 12:02am
Medium Bug Labs team kgilmer 215 posts

The HTTP Service (com.buglabs.osgi.http) in the SVN repository now supports static resources. I’ve also checked in the test bundle (com.buglabs.osgi.http.test) that includes a test case (ResourceTestCases.java) that demonstrate how static resources are used. We are working ways of getting development code out to beta users more quickly but for now you’ll have to compile these sources yourself to get the functionality.

Nov 14, 2007 12:06am
Medium Bug Labs team finsprings 268 posts

nice! so the security app will be able serve up pics now.

Nov 16, 2007 1:50pm
Medium Bug Labs team kgilmer 215 posts

Er there was a defect in the code that broke Servlets. Now it should work better.

Nov 16, 2007 8:54pm
Medium Bug Labs team finsprings 268 posts

How do I build from source? I've been farting around with it for a while now.

I couldn't get Eclipse to take it, then I noticed the build.xml files, but there's no top-level build.xml in trunk, so I tried running ant on com.buglabs.bug.base and it failed because it couldn't find .../trunk/com.buglabs.bug.kernel.external:

dfindlay-macbook:trunk dave$ cd com.buglabs.bug.base
dfindlay-macbook:com.buglabs.bug.base dave$ ant
Buildfile: build.xml

clean:
[delete] Deleting directory /Users/dave/svn/bug/trunk/com.buglabs.bug.base/build

create_dirs:
[mkdir] Created dir: /Users/dave/svn/bug/trunk/com.buglabs.bug.base/build

build:
[echo] BUILDING: com.buglabs.bug.base
[javac] Compiling 4 source files to /Users/dave/svn/bug/trunk/com.buglabs.bug.base/build

BUILD FAILED
/Users/dave/svn/bug/trunk/com.buglabs.bug.base/build.xml:45: /Users/dave/svn/bug/trunk/com.buglabs.bug.kernel.external not found.


dfindlay-macbook:com.buglabs.bug.base dave$ java -version
java version "1.5.0_13"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05-237)
Java HotSpot(TM) Client VM (build 1.5.0_13-119, mixed mode, sharing)
dfindlay-macbook:com.buglabs.bug.base dave$ ant -version
Apache Ant version 1.7.0 compiled on December 13 2006


This is on OS X 10.5.1.

I didn't find anything on the wiki about building from source but I may have missed it.

Thanks,
Dave
Nov 19, 2007 2:27pm
Medium Bug Labs team kgilmer 215 posts

Good catch Dave :). Sorry, I didn’t really think about the build scripts when I checked it in. I’ve added a project to SVN that exists internally, com.buglabs.bug.kernel.external (for externally referenced dependencies). If you change the variable distDirectory to something valid it should build. You’ll need to check out both …osgi.http and …kernel.external in the same directory and run the build script from …osgi.http.

Nov 19, 2007 2:57pm
Medium Bug Labs team finsprings 268 posts

I’m getting further, but I don’t know what order to build all those subdirs in. Do you have a top-level build.xml or a script that does it?

Nov 19, 2007 2:58pm
Medium Bug Labs team finsprings 268 posts

Alternatively, since this is presumably just temporary until a new SDK is released, any chance of you posting the JARs somewhere (or maybe putting them in a dist dir in SVN or something)?

Cheers,
Dave

Nov 21, 2007 10:38am
Medium Bug Labs team kgilmer 215 posts

All that needs to be built is build.xml in com.buglabs.osgi.http. That will produce an HTTP bundle. I’m looking into the best way of getting files to beta users…

Nov 21, 2007 3:13pm
Medium Bug Labs team finsprings 268 posts

I think I was able to build it, but still didn’t see my images. I’m not sure how file system dirs map to static URLs.

Nov 27, 2007 6:14pm
Medium Bug Labs team kgilmer 215 posts

Glad to hear you were able to build it Dave. We’re still working on the best strategy to get builds and files ASAP to users, and I think we’re getting pretty close. IRT mapping, in my test case (in com.buglabs.osgi.http.test) I have a resource in the Bundle that is served. This is the core use case (at least is in the OSGi R3 specification). LMK if that helps or if I should provide further details.

Nov 27, 2007 9:46pm
Medium Bug Labs team finsprings 268 posts

Thanks, I see your ‘buck’ test resource now. However, in the security app case, the image files don’t exist until they are created by a motion trigger, so they can’t be packaged with the application. I am calling ICameraDevice.getImage and saving the resulting byte array to a file in the default working folder, which is also where I am saving the security log file. For me that ends up being the folder where the Eclipse binary is (yuk); this was because I didn’t know where else I should be saving them.

Is your buck test page unzipped on the Bug file system by the servlet container? I’m just wondering if there’s a servlet->resource-folder mapping that exists. If there is then I can save my image (and log) files in that folder instead of the current working folder. That way it seems like they would be served up properly when referred to by my servlet.

Jan 27, 2008 11:16pm
Medium Bug Labs team finsprings 268 posts

Any update on this Ken? Basically, how would one server up image files that didn’t exist at module deployment?

Also, on a related matter, any further thoughts to file system access from modules? In the emulator, the BUGer log and image files get written to the current working directory, in my case where the eclipse binary is. Some means of exposing module-specific permanent storage in a a file-system agnostic fashion would be nice.

Jan 28, 2008 12:11pm
Medium Bug Labs team kgilmer 215 posts

Dave,

Very sorry, let this one slip through the cracks. In the future if you don’t get an answer from me within a day or two feel free to email me…

IRT serving dynamic content, my first approach would be to implement doGet() on the servlet and not use a resource at all. This is how the Camera bundle works…see com.buglabs.bug.module.camera.CameraModlet. This is a little more comples though because it’s using the PublicWSAdmin service to expose the image via the BUG Services WS API. Another approach would be to implement your own HttpContext class that handles the getResource() method. In this case you can do custom logic to retrieve your dynamic files.

In regards to file system locations, yes this is a very good question, and there is a solution in the OSGi framework already. There is a method on BundleContext that returns a path to a place in the filesystem where files can be written to. See BundleContext.getDataFile(). This should be used in order to be assured that a specific point in the filesystem is not read-only.

Jan 29, 2008 11:25pm
Medium Bug Labs team finsprings 268 posts

Thanks Ken. I got the BundleContext stuff working, so I’m no longer pooping in the Eclipse directory, which is great. I looked at CameraModlet but didn’t see a doGet so I will need to do some research on the WSAdmin stuff to get up to speed.

Jan 31, 2008 11:04pm
Medium Bug Labs team finsprings 268 posts

Ken, I managed to get a static image to work, in a similar manner to your 'buck' test case. I had to make a subdir under my Resources dir in the app jar that matched the alias I registered the resource at, but other than that it wasn't too tricky.

So now it looks a little bit prettier:


I've still got to get to the dynamic image part. That's next. For now, I pushed out v1.0.5 of BUGer to bugnet with the buglogo in it as above.
Feb 1, 2008 4:54pm
Medium Bug Labs team kgilmer 215 posts

Dave,

Nice work! For dynamic images you could create a PublicWSProvider that serves up images or you could have just another servlet. I haven’t tried this but as I mentioned earlier you should also be able to provide a custom HttpContext class.

Feb 1, 2008 8:11pm
Medium Bug Labs team finsprings 268 posts

Thanks Ken. Since I already had to use an HttpContext to get the static resource, it was fairly easy to create another one to get the dynamic images.

So now BUGer finally displays the images that are captured when motion is detected:
Feb 1, 2008 11:48pm
Medium Bug Labs team finsprings 268 posts

Although it's working, I'm not sure I'm making use of the HttpContexts properly. For my static image I set the alias to /bugerresources and mapped it to /Resources when I registered the context. I couldn't make the alias /buger or it would just be subsumed by my servlet. But to find the static image resource (the bug logo), I had to put it at Resources/bugerresources/buglogo.jpg and refer to it in the HTML as /bugerresources/buglogo.jpg. That seemed a little clunky to me: if I'm registering a mapping from /bugerresources to /Resources, why do I need the subfolder under Resources to get the mapping to work?

Then, for the captured images I registered another HttpContext for /bugercapture mapped to "". I referred to the images as /bugercapture/<timestamp>.jpg in the HTML. In the getResource() method of my HttpContext I stripped off the leading /bugercapture/ part and called return context.getDataFile( fileName ).toURL() to return a URL for the image file where it resides in the persistent storage area. Again, having to mess with the string made it seem like I wasn't doing something quite right.

So, it's working, but am I doing it the right way?
Feb 4, 2008 4:29pm
Medium Bug Labs team finsprings 268 posts

While, I’m at it with the questions, is there a more official way of linking the servlet jar? I’m still including a copy in my bug app, which works but is presumably not correct.

Feb 12, 2008 12:13pm
Medium Bug Labs team kgilmer 215 posts

Hi Dave,

Regarding the proper usage of HttpContext and servlets, yeah it does seem kind of clunky. I would suggest emailing the OSGI-DEV mailing list regarding the proper use of this. They won’t know anything about BUG but the HTTP Service is an OSGi spec. You can find out more about that at http://osgi.org.

Regarding the servlet JAR. it’s now being bundled inside com.buglabs.osgi.http. I’m not sure what version you are using but if don’t find the classes there (zipinfo com.buglabs.osgi.http.jar) then grab a newer version from our website.

hope that helps

Feb 12, 2008 4:10pm
Medium Bug Labs team finsprings 268 posts

Thanks Ken. I removed the inclusion of servlet.jar from BUGer and it still works, so it must now be picking that app from the SDK - way cool. I also had to make a fix so that if you unplug a module BUGer cares about (camera or motion), it now unregisters the 2 HTTP resources it was registering. Without doing that, it would fail to reregister those resources when you plugged the module back in. Pushed v1.0.7 to bugnet with the changes.

As to the OSGi thing: I’m okay with it being clunky as long as that’s the ‘approved’ way to do it on the bug. I just didn’t want to set a bad example when posting this app to bugnet.

Cheers,
Dave

Log in to reply to this topic
Forums BUG SDK Servlet: static files?

Powered by Community E