You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stanbol.apache.org by Reto Bachmann-Gmür <re...@apache.org> on 2013/03/17 16:33:20 UTC

Simplifying jaxrs resource methods

Hi Danny

I just wanted to point a possible simplification of JAX-RS methods.

For example from commit 1457449:

     @GET
     @Path("users/{username}")
-    @Produces(SupportedFormat.TURTLE)
-    public Response getUserTurtle(@PathParam("username") String userName)
-          throws UnsupportedEncodingException {
+    public TripleCollection getUserContext(@PathParam("username") String
userName) {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
-        serializer.serialize(baos, getUser(userName).getNodeContext(),
-                SupportedFormat.TURTLE);
-        String serialized = new String(baos.toByteArray(), "utf-8");
-        // System.out.println("User = "+serialized);
-        return Response.ok(serialized).build();
+        return getUser(userName).getNodeContext();
     }

9 lines of code less and additionally the advantage that not just turtle
but any supported RDF serialization. The trick is not to care about
serialization but just return a triple collection and let the serialization
be the business of the installed MessageBodyWriterS. NOte that if you want
to enforce turtle you could just leave the @Produces annotation there. The
same is also possible for the submitted content, I'll now look at this.

Cheer,
Reto

Re: Simplifying jaxrs resource methods

Posted by Danny Ayers <da...@gmail.com>.
Thanks Rupert!

D'oh!

On 20 March 2013 06:08, Rupert Westenthaler
<ru...@gmail.com> wrote:
> Hi
>
> On Tue, Mar 19, 2013 at 11:29 PM, Danny Ayers <da...@gmail.com> wrote:
>> The approach makes sense, but I've just hit what appears to be a bug
>> in how the serializer is chosen -
>>
>> curl --user admin:admin -H "Content-Type: text/turtle"
>> http://localhost:8080/user-management/users/anonymous
>>
>
> You need to set the Accept header and not the Content-Type header in
> the request. The Content-Type header defines the type of the data sent
> in the request (applies only to POST, PUT requests). The Accept header
> determines the content type of the data the server sends back to you.
> When using
>
>     curl --user admin:admin -H "Accept: text/turtle" \
>         http://localhost:8080/user-management/users/anonymous
>
> you should get the response serialized as Turtle
>
> best
> Rupert
>
>> returns RDF/JSON instead of Turtle
>>
>> this is calling against:
>>
>>     @GET
>>     @Path("users/{username}")
>>     public TripleCollection getUserContext(@PathParam("username")
>> String userName)
>>             throws UnsupportedEncodingException {
>>         return getUser(userName).getNodeContext();
>>     }
>>
>> I'm not 100% sure, my build isn't totally fresh from svn (I've just
>> updated, waiting for the install as I type...), but it's easy enough
>> to check with the curl line above.
>>
>> Where is the magic that turns that TripleCollection into a Response?
>>
>> (This isn't a blocker for me, I can put in temporary forced-turtle
>> methods for now).
>>
>> Cheers,
>> Danny.
>>
>>
>>
>> --
>> http://dannyayers.com
>>
>> http://webbeep.it  - text to tones and back again
>
>
>
> --
> | Rupert Westenthaler             rupert.westenthaler@gmail.com
> | Bodenlehenstraße 11                             ++43-699-11108907
> | A-5500 Bischofshofen



-- 
http://dannyayers.com

http://webbeep.it  - text to tones and back again

Re: Simplifying jaxrs resource methods

Posted by Rupert Westenthaler <ru...@gmail.com>.
Hi

On Tue, Mar 19, 2013 at 11:29 PM, Danny Ayers <da...@gmail.com> wrote:
> The approach makes sense, but I've just hit what appears to be a bug
> in how the serializer is chosen -
>
> curl --user admin:admin -H "Content-Type: text/turtle"
> http://localhost:8080/user-management/users/anonymous
>

You need to set the Accept header and not the Content-Type header in
the request. The Content-Type header defines the type of the data sent
in the request (applies only to POST, PUT requests). The Accept header
determines the content type of the data the server sends back to you.
When using

    curl --user admin:admin -H "Accept: text/turtle" \
        http://localhost:8080/user-management/users/anonymous

you should get the response serialized as Turtle

best
Rupert

> returns RDF/JSON instead of Turtle
>
> this is calling against:
>
>     @GET
>     @Path("users/{username}")
>     public TripleCollection getUserContext(@PathParam("username")
> String userName)
>             throws UnsupportedEncodingException {
>         return getUser(userName).getNodeContext();
>     }
>
> I'm not 100% sure, my build isn't totally fresh from svn (I've just
> updated, waiting for the install as I type...), but it's easy enough
> to check with the curl line above.
>
> Where is the magic that turns that TripleCollection into a Response?
>
> (This isn't a blocker for me, I can put in temporary forced-turtle
> methods for now).
>
> Cheers,
> Danny.
>
>
>
> --
> http://dannyayers.com
>
> http://webbeep.it  - text to tones and back again



--
| Rupert Westenthaler             rupert.westenthaler@gmail.com
| Bodenlehenstraße 11                             ++43-699-11108907
| A-5500 Bischofshofen

Re: Simplifying jaxrs resource methods

Posted by Danny Ayers <da...@gmail.com>.
The approach makes sense, but I've just hit what appears to be a bug
in how the serializer is chosen -

curl --user admin:admin -H "Content-Type: text/turtle"
http://localhost:8080/user-management/users/anonymous

returns RDF/JSON instead of Turtle

this is calling against:

    @GET
    @Path("users/{username}")
    public TripleCollection getUserContext(@PathParam("username")
String userName)
            throws UnsupportedEncodingException {
        return getUser(userName).getNodeContext();
    }

I'm not 100% sure, my build isn't totally fresh from svn (I've just
updated, waiting for the install as I type...), but it's easy enough
to check with the curl line above.

Where is the magic that turns that TripleCollection into a Response?

(This isn't a blocker for me, I can put in temporary forced-turtle
methods for now).

Cheers,
Danny.



-- 
http://dannyayers.com

http://webbeep.it  - text to tones and back again

Re: Simplifying jaxrs resource methods

Posted by Reto Bachmann-Gmür <re...@apache.org>.
A similar simplification is possible when the body of the request is a
graph.

So that instead of
   public Response addUser(@Context UriInfo uriInfo, String userData) {
        Graph inputGraph = parseGraphFromString(userData);

One can just use
   public Response addUser(@Context UriInfo uriInfo, Graph inputGraph) {

As stanbol has currently no such MessageBodyReader to support this I've
created STANBOL-988 to add a small bundle from Clerezza. With this bundle
we have however two MessageBodyWriters for TripleCollections. The one in
org.apache.stanbol.commons.web.base.writers also supports
application/octet-stream and text/plain as format for serializing them.
More important I think would be to add JSON_LD.

Cheers,
Reto


On Sun, Mar 17, 2013 at 4:33 PM, Reto Bachmann-Gmür <re...@apache.org> wrote:

> Hi Danny
>
> I just wanted to point a possible simplification of JAX-RS methods.
>
> For example from commit 1457449:
>
>      @GET
>      @Path("users/{username}")
> -    @Produces(SupportedFormat.TURTLE)
> -    public Response getUserTurtle(@PathParam("username") String userName)
> -          throws UnsupportedEncodingException {
> +    public TripleCollection getUserContext(@PathParam("username") String
> userName) {
> -        ByteArrayOutputStream baos = new ByteArrayOutputStream();
> -
> -        serializer.serialize(baos, getUser(userName).getNodeContext(),
> -                SupportedFormat.TURTLE);
> -        String serialized = new String(baos.toByteArray(), "utf-8");
> -        // System.out.println("User = "+serialized);
> -        return Response.ok(serialized).build();
> +        return getUser(userName).getNodeContext();
>      }
>
> 9 lines of code less and additionally the advantage that not just turtle
> but any supported RDF serialization. The trick is not to care about
> serialization but just return a triple collection and let the serialization
> be the business of the installed MessageBodyWriterS. NOte that if you want
> to enforce turtle you could just leave the @Produces annotation there. The
> same is also possible for the submitted content, I'll now look at this.
>
> Cheer,
> Reto
>