Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
RestAssuredResponseImpl: getting body as String results in NPE
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  16 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
bjello  
View profile  
 More options Sep 28 2012, 2:54 pm
From: bjello <devnull2...@gmx.de>
Date: Fri, 28 Sep 2012 11:54:43 -0700 (PDT)
Local: Fri, Sep 28 2012 2:54 pm
Subject: RestAssuredResponseImpl: getting body as String results in NPE
Hi!

I'm new to RestAssured (using 1.6.2). When I tried to read the content
of the response's body as a String, I got a NullPointerException.
However, during debugging the REST server logs successful messages and
the content length of the response is as expected, the result is
there, I just can not read it. Then I tried to read the body as
InputStream and it worked. So, is there a problem with my code?

Response response = RestAssured.get(path);
String token = response.getBody().asString();

I also tried to use print() or prettyPrint() but I got the same
results.
The exception gets thrown in RestAssuredResponseImpl in

private String convertStreamToString(InputStream is) throws
IOException {
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    Reader reader;
    try {
        reader = new BufferedReader(new InputStreamReader(is,
findCharset()));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
        reader.close();
    }
    return writer.toString();

}

When I reach finally, the reader is already null again:
java.lang.NullPointerException: Cannot invoke method close() on null
object
Any idea why I can't use asString or prettyPrint?

Cheers!
Bjello


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Haleby  
View profile  
 More options Sep 29 2012, 6:17 am
From: Johan Haleby <johan.hal...@gmail.com>
Date: Sat, 29 Sep 2012 12:17:25 +0200
Local: Sat, Sep 29 2012 6:17 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

Hmm it sounds like a bug indeed but it's very hard to tell what's going on.
Can you provide some details on the data (body) your resource returns?

/Johan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
devnull2...@gmx.de  
View profile  
 More options Sep 30 2012, 6:40 pm
From: devnull2...@gmx.de
Date: Mon, 01 Oct 2012 00:40:39 +0200
Local: Sun, Sep 30 2012 6:40 pm
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE
Hi again,

> Hmm it sounds like a bug indeed but it's very hard to tell what's going
> on.
> Can you provide some details on the data (body) your resource returns?

I tested two methods. The first one returns a String of length 32 plus newline:
return Response.ok( token + "\n" ).build();
The annotation on the method:
@Produces("text/plain;charset=UTF-8")

The second method returns a list in json of users from a database:
@Produces("application/json;charset=UTF-8")
....
SelectQuery query = new SelectQuery(cls);
List<?> result = (List<?>) context.performQuery(query);
return Response.ok(result).build();

I'm using tapestry-resteasy and cayenne as OR-Mapper.

With both methods I got a NullPointerException when I tried to use asString(). For now I'm using asInputStream() and Apache IOUtils to convert the stream to a String and it works.

-Bjello


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Haleby  
View profile  
 More options Oct 1 2012, 3:05 am
From: Johan Haleby <johan.hal...@gmail.com>
Date: Mon, 1 Oct 2012 09:05:18 +0200
Local: Mon, Oct 1 2012 3:05 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

Hmm I can't reproduce this. Could you create a small sample that
demonstrates the issue?

Regards,
/Johan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bjello  
View profile  
 More options Oct 1 2012, 8:55 am
From: bjello <devnull2...@gmx.de>
Date: Mon, 1 Oct 2012 05:55:24 -0700 (PDT)
Local: Mon, Oct 1 2012 8:55 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

I created a simple tapestry project with maven in eclipse. Hope this works
for you.

Right click on the RATests.java in src/test/java and choose run as Junit
Test. This starts a Jetty server, runs 4 tests and stops the server.
The tests aren't really tests, the methods just try to read the response.
The first two tests should work, the other two produce the
NullPointerException.
I'm not sure if you'd need the RunJettyRun plugin for eclipse.

Cheers!

  testproject.zip
130K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Haleby  
View profile  
 More options Oct 1 2012, 9:14 am
From: Johan Haleby <johan.hal...@gmail.com>
Date: Mon, 1 Oct 2012 15:14:04 +0200
Local: Mon, Oct 1 2012 9:14 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

Thanks for your help! I'm not using Eclipse (I'm using Intellij) but I'll
see if I can get it to work (hopefully later this evening).

Regards,
/Johan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bjello  
View profile  
 More options Oct 1 2012, 10:05 am
From: bjello <devnull2...@gmx.de>
Date: Mon, 1 Oct 2012 07:05:00 -0700 (PDT)
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

I tried to run it without eclipse:

> cd testproject
> mvn jetty:run

Entering http://localhost:8080/testproject/rest/testtoken in firefox
produced the fake token
and
http://localhost:8080/testproject/rest/testuser?email=t...@example.org&pw=password
produced the user info in json.
I just don't know how to run junit tests with maven but I could try some
more on Wednesday.

Regards,
Bjello


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Haleby  
View profile  
 More options Oct 1 2012, 10:15 am
From: Johan Haleby <johan.hal...@gmail.com>
Date: Mon, 1 Oct 2012 16:15:55 +0200
Local: Mon, Oct 1 2012 10:15 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

I have the project up and running in Intellij (no problems at all) now and
I also get the same error that you get. I'll try to investigate it soon.

Regards,
/Johan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Haleby  
View profile  
 More options Oct 1 2012, 10:36 am
From: Johan Haleby <johan.hal...@gmail.com>
Date: Mon, 1 Oct 2012 16:36:46 +0200
Local: Mon, Oct 1 2012 10:36 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

Alright I've fixed the bug. The problem was that your server returns an
unrecognized charset in the content-type header response (which made RA
throw NPE). You actually return "UTF-8" (in quotation) and not UTF-8. I'm
not sure if this is valid according to the HTTP spec. If it is I'll fix it
so that it's unescaped automatically. So what I've fixed is the NPE bug so
that you now get the real exception instead.

Regards,
/Johan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bjello  
View profile  
 More options Oct 2 2012, 10:50 am
From: bjello <devnull2...@gmx.de>
Date: Tue, 2 Oct 2012 07:50:35 -0700 (PDT)
Local: Tues, Oct 2 2012 10:50 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

> You actually return "UTF-8" (in quotation) and not UTF-8. I'm not sure if
> this is valid according to the HTTP spec.

I think you're right, it should be  

Content-Type: text/plain; charset=UTF-8

without quotes. I haven't figured out yet how to make Resteasy to not add
them. When I don't set the encoding at all, i. e. I change the annotation
in MyResource.java from
@Produces("text/plain;charset=UTF-8") to
@Produces("text/plain") it works, there's no exception.

To get the RestAssured version with the "real" exception I guess I should
check out the source from the git repository, right?

Anyway, thanks a lot for the fast help. :-)

Regards,
Bjello


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Haleby  
View profile  
 More options Oct 2 2012, 12:30 pm
From: Johan Haleby <johan.hal...@gmail.com>
Date: Tue, 2 Oct 2012 18:30:41 +0200
Local: Tues, Oct 2 2012 12:30 pm
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

If Resteasy add quotations then perhaps it's valid? In that case I should
fix that in RA but I'm not sure if it's correct. Reading
https://bugzilla.mozilla.org/show_bug.cgi?id=700589 I interpret it that
it's not allowed to use quotations around the charset, what do you think?

Yes you would have to build from trunk for now :/

/Johan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Haleby  
View profile  
 More options Oct 2 2012, 3:35 pm
From: Johan Haleby <johan.hal...@gmail.com>
Date: Tue, 2 Oct 2012 21:35:53 +0200
Local: Tues, Oct 2 2012 3:35 pm
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

I've deployed the latest snapshot to sonatype if you want to try it out.
Depend on version 1.6.3-SNAPSHOT after having added the following repo:

<repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/
</url>
            <snapshots />
        </repository>
</repositories>

Regards,
/Johan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bjello  
View profile  
 More options Oct 2 2012, 7:53 pm
From: bjello <devnull2...@gmx.de>
Date: Tue, 2 Oct 2012 16:53:11 -0700 (PDT)
Local: Tues, Oct 2 2012 7:53 pm
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

Thanks for the snapshot.

If Resteasy add quotations then perhaps it's valid? In that case I should

> fix that in RA but I'm not sure if it's correct. Reading
> https://bugzilla.mozilla.org/show_bug.cgi?id=700589 I interpret it that
> it's not allowed to use quotations around the charset, what do you think?

From RFC 2616 HTTP (http://www.ietf.org/rfc/rfc2616.txt)

14.17 Content-Type [...]

Content-Type   = "Content-Type" ":" media-type

Media types are defined in section 3.7. An example of the field is
    Content-Type: text/html; charset=ISO-8859-4

There are no quotes in this example.

From section 3.7 Media Types:

    media-type     = type "/" subtype *( ";" parameter )

From 3.6:
Parameters are in  the form of attribute/value pairs.
    parameter               = attribute "=" value

They say nothing about quotes for the value here. Hmm.

In 3.4 Character Sets:
[...] HTTP character sets are identified by case-insensitive tokens. The
complete set of tokens is defined by the IANA Character Set registry
[19].
    charset = token

I looked around some more on the IANA website but I couldn't find anything
explicit about quotes for the charset token. The charsets and their names
are listed without quotes on http://www.iana.org/assignments/character-sets.
In RFC 2046 (MIME) there's another example for the type text/plain:

4.1.2.  Charset Parameter
A critical parameter that may be specified in the Content-Type field
for "text/plain" data is the character set.  This is specified with a
"charset" parameter, as in:
    Content-type: text/plain; charset=iso-8859-1
Again, no quotes. Well, I'm no expert but I'd say RestEasy is wrong and there's nothing to fix in RA. :-)

Regards
Bjello


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Haleby  
View profile  
 More options Oct 3 2012, 1:54 am
From: Johan Haleby <johan.hal...@gmail.com>
Date: Wed, 3 Oct 2012 07:54:04 +0200
Local: Wed, Oct 3 2012 1:54 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

Thanks for your investigation, really helpful! :) I'll leave it as it is
then.

If you have any other problems or questions in the future just shoot!

Regards,
/Johan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bjello  
View profile  
 More options Oct 4 2012, 5:47 am
From: bjello <devnull2...@gmx.de>
Date: Thu, 4 Oct 2012 02:47:36 -0700 (PDT)
Local: Thurs, Oct 4 2012 5:47 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

Just for the record: I tried it with tomcat instead of jetty and the
response's content type was text/plain;charset=UTF-8 or
application/json;charset=UTF-8, there weren't any quotes. So RestEasy isn't
doing anything wrong either, it looks like it's actually Jetty.

Regards,
Bjello


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Haleby  
View profile  
 More options Oct 4 2012, 6:26 am
From: Johan Haleby <johan.hal...@gmail.com>
Date: Thu, 4 Oct 2012 12:26:40 +0200
Local: Thurs, Oct 4 2012 6:26 am
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String results in NPE

Alright, thanks for sharing.

/Johan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »