On Mar 4, 2008, at 3:40 PM, Gabor Szokoli wrote:
> Hi there,
>
> Unrelated smallish questions I have using Jersey:
>
> 1, How do people usually pass configuration options to their code in
> jeresy? if I run it as a servlet, can I pass config options in the
> web.xml?
>
Any init-param name and value automatically gets added to the
ResourceConfig properties map, the values get added as String
instances.
You can then inject a ResourceConfig onto your class:
@Path("/")
public class Resource {
@Context ResourceConfig rc;
@GET public String get() {
return rc.getProperties().get("my-servlet-init-param-property");
}
}
I think using Spring/Guice is another more elegant and flexible way.
> 2, @GET methods may return InputStream, as we have established here
> earlyer. How about Reader?
>
Ah, good point. You know you can easily create your own :-) but we
should add this to the default set. Could you log an issue?
> 3, I have an external executeble I must run with
> Runtime.getRuntime().exec to generate one of my resources.
> The XML is generated on STDOUT.
> Is there a pattern for doing this? Preferably without buffering the
> output, I'd like to have the XML streamed out directly.
> But if my @GET method just returns the InputStream without waiting for
> the process to finish, then the closure where process resides in is
> left too early. So I guess I'd have to start a thread for it, but
> that's usually forbidden in EE...
>
There is some discussion in the EG about a standard way to get an
OutputStream to write to directly.
You can do this in Jersey as follows:
@ProduceMime("text/plain")
@GET
public void get(HttpRequestContext requestContext,
HttpResponseContext responseContext) throws
IOException {
assertEquals("GET", requestContext.getHttpMethod());
responseContext.getOutputStream().
write("RESOURCE".getBytes());
}
The method signature has to be as above, namely returning void and
with two method parameters, HttpRequestContext and
HttpResponseContext respectively. It is currently not possible to
intermix Http*Context with other parameters.
So if you wrap the OutputStream in a OutputStreamWriter or whatever
the STDOUT requires you should be able to stream the output from what
you executed.
Paul.