users@grizzly.java.net

Re: grizzly http embed questions

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Mon, 01 Dec 2008 11:53:47 -0500

Salut,

rama wrote:
> Dear friends of grizzly community, i need some help to better understand
> grizzly http embed.
>
> so, let's start :)
>
> Question 1:
> i want to setup a webserver, port 7777 that server a certain path
>
> String root1 = "/Users/ramarama/root";
> String root2 = "/Users/ramarama/root2";
>
>
> GrizzlyWebServer ws = new GrizzlyWebServer(8080,10,root1);
>
> when a file is not present on /Users/ramarama/root
> i want that webserver try to take a look on root2.
> Seems quite easy to do, but i don't get if there is a good way to do it :)
> (btw, don't propose solution like "do a ls -n or whatever" this is just
> a learing session of grizzly http embed library)

I would have not recommended that :-)

>
> I can identify the file, in request method, then i can read it and wrote
> the answare on response...
> but, if i do that, i will not be able to use caching system or all fancy
> things like that isn't it?

The file cache is invoked by an instance of:

https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/util/Interceptor.html

which is associated with an StreamAlgorithm:

https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/util/StreamAlgorithm.html

By default, the file cache is associated with a
GrizzlyWebServer/SelectorThread

st.setAlgorithmClassName(StaticStreamAlgorithm.class.getName());

as an example, the StaticStreamAlgorithm:

> public StaticStreamAlgorithm() {
> handler = new StaticHandler();
> }
>
>
> /**
> * Do nothing, as the ByteBufferInputStream will take care of reading the
> * missing bytes.
> */
> @Override
> public ByteBuffer preParse(ByteBuffer byteBuffer){
> return byteBuffer;
> }
>
>
> /**
> * Do not parse the bytes and automatically flip the {_at_link ByteBuffer}
> */
> public boolean parse(ByteBuffer byteBuffer){
> byteBuffer.flip();
> return true;
> }
>
>
> /**
> * Return the {_at_link Interceptor} used by this algorithm.
> */
> public Interceptor getHandler(){
> return handler;
> }

So you will need to write your own StreamAlgorithm and also your own
Interceptor.

>
> --
> ws.addGrizzlyAdapter(new GrizzlyAdapter(){
> public void service(GrizzlyRequest request,
> GrizzlyResponse response){
> try {
> //should come here for file not found
> } catch (IOException ex) {
>
>
>
> }
> }
> });
>
> Question 2:
> The easy one, to take a bit of relax before question number 3.
> how do i change the default filename?
> right now is index.html, i want to change it with foobar.xhtml just
> because i am strange :)

Can you elaborate a little more here? What do you want to do exactly?
Redirect request from /index.html to /foobar.xhtml?

>
>
> Question 3:
> Suppose that i want to handle a particular situation.
> I want that all request do at localhost/virtual
> return a "hello guy" and bypass the static file services.

(1) Invoke the GrizlyAdapter.setHandleStaticResource(false);
https://grizzly.dev.java.net/nonav/apidocs/com/sun/grizzly/tcp/http11/GrizzlyAdapter.html#setHandleStaticResources(boolean)

(2) Do something like:

> 195 response.setContentType(MimeType.get("foo"));
> 197
> 198 long length = YourFile.length();
> 199 response.setContentLengthLong(length);
> 200
> 201 response.getWriter().println(YouFoBarContent);
> 204
> 205

Take a look at:

https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/tcp/StaticResourcesAdapter.html


>
> Question 4:
> Suppose that i want to handle a particular extension (similar to case of
> above)
> i want that all request do at localhost/file.exotic
> will be handled in a certain way.

Same as above. You decide what is returned from the GrizzlyAdapter.

>
>
> my bet for 3-4 is using an async filter, that parse for the name of the
> file or for the extension (ie, a reqular expression?)
> then, if there is a match, simply write "heay" to response, and return
> false (returning false, will be block the grizzlyadapter sync block right?)
>

You don't need AsyncFilter here unless I've misunderstood you.

>
>
> Question 5:
> How can i start a grizzlyWebserver on port 443 with ssl support, using
> certificate stuff?

Nice catch. You can create an instance of SSLSelectorThread, but the
current GrizzlyWebServer isn't yet supporting SSL. I've filled:

https://grizzly.dev.java.net/issues/show_bug.cgi?id=343

and will fix that today.


>
>
> Not a real question, but
> import com.sun.grizzly.http.AsyncFilter;
> is deprecated?

yes, use c.s.g.arp.AsyncFilter. The class has just been moved from a new
pacakge.


> if yes, and i want to use the asyncfilter (for question3 and 4) how i
> can proceed? it's normal that is deprecated, and i should use it
> without any problem, or there is something that i miss?
>
> Tnx to all for the patience, and sorry for my terrible english, i hope
> that i haven't do too much mistake :)

You are welcome. Just continue asking questions :)

A+

-- Jeanfrancois



>
> Matteo
>