You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "artur.chyzy" <ar...@cybercom.com> on 2011/05/12 15:11:44 UTC
Accessing JAXRS JSON content directly
Hello.
I have a JAX RS service configured like this
<jaxrs:server id="someServer" address="/someAddress">
<jaxrs:serviceBeans>
<ref bean="someEndpoint" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider" />
</jaxrs:providers>
</jaxrs:server>
The service correctly handles IN and OUT messages and converts them to
objects.
But I have problem with accessing JSON content as a String (raw format). I
need this to correctly implement security as content is needed to calculate
MAC for each message (Message Authentication Code).
I tried to do it like this:
@Context
MessageContext mc;
....
IOUtils.toString(mc.getContent(InputStream.class), "utf-8");
but empty String is returned as input stream has no content (checked on
debug)
On the other hand mc.getContent(String.class) returns null
It is strange as LogginInIntercetor from CXF is using the same technique.
Is there any way to get JSON message content as String ?
Artur
--
View this message in context: http://cxf.547215.n5.nabble.com/Accessing-JAXRS-JSON-content-directly-tp4390185p4390185.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: Accessing JAXRS JSON content directly
Posted by "artur.chyzy" <ar...@cybercom.com>.
Hello.
Thanks a lot. Finally it is working
Here's my code if in the future someone will have similar issue (partially
taken from LoggingInInterceptor from CXF)
public class StringContentRequestHandler implements RequestHandler {
private static final Logger logger =
LoggerFactory.getLogger(RawContentRequestHandler.class);
public Response handleRequest(Message m, ClassResourceInfo
resourceClass) {
InputStream is = m.getContent(InputStream.class);
try {
CachedOutputStream bos = new CachedOutputStream();
IOUtils.copy(is, bos);
bos.flush();
is.close();
m.setContent(InputStream.class, bos.getInputStream());
StringBuilder builder = new StringBuilder();
bos.writeCacheTo(builder, "utf-8");
m.setContent(String.class, builder.toString());
return null;
} catch (IOException ex) {
logger.error("IOException on getting raw content", ex);
return null;
}
}
}
Artur
Sergey Beryozkin-5 wrote:
>
> Hi
>
> On Thu, May 12, 2011 at 2:11 PM, artur.chyzy
> <artur.chyzy@cybercom.com> wrote:
>> Hello.
>>
>> I have a JAX RS service configured like this
>>
>> <jaxrs:server id="someServer" address="/someAddress">
>> <jaxrs:serviceBeans>
>> <ref bean="someEndpoint" />
>> </jaxrs:serviceBeans>
>> <jaxrs:providers>
>> <ref bean="jsonProvider" />
>> </jaxrs:providers>
>> </jaxrs:server>
>>
>> The service correctly handles IN and OUT messages and converts them to
>> objects.
>>
>> But I have problem with accessing JSON content as a String (raw format).
>> I
>> need this to correctly implement security as content is needed to
>> calculate
>> MAC for each message (Message Authentication Code).
>> I tried to do it like this:
>>
>> @Context
>> MessageContext mc;
>> ....
>> IOUtils.toString(mc.getContent(InputStream.class), "utf-8");
>>
>> but empty String is returned as input stream has no content (checked on
>> debug)
>> On the other hand mc.getContent(String.class) returns null
>>
>> It is strange as LogginInIntercetor from CXF is using the same technique.
>> Is there any way to get JSON message content as String ?
>>
> I'm presuming that you are trying to get InputStream after the IN
> parameter has already been populated ?
> If yes then you will need to duplicate current InputStream in
> RequestHandler filter and save it or the calculated MAC as a custom
> property on the message, and then do mc.get("json.mac") or similar...
>
> Cheers, Sergey
>
>> Artur
>>
>> --
>> View this message in context:
>> http://cxf.547215.n5.nabble.com/Accessing-JAXRS-JSON-content-directly-tp4390185p4390185.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
>>
>
--
View this message in context: http://cxf.547215.n5.nabble.com/Accessing-JAXRS-JSON-content-directly-tp4390185p4390428.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: Accessing JAXRS JSON content directly
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On Thu, May 12, 2011 at 2:11 PM, artur.chyzy <ar...@cybercom.com> wrote:
> Hello.
>
> I have a JAX RS service configured like this
>
> <jaxrs:server id="someServer" address="/someAddress">
> <jaxrs:serviceBeans>
> <ref bean="someEndpoint" />
> </jaxrs:serviceBeans>
> <jaxrs:providers>
> <ref bean="jsonProvider" />
> </jaxrs:providers>
> </jaxrs:server>
>
> The service correctly handles IN and OUT messages and converts them to
> objects.
>
> But I have problem with accessing JSON content as a String (raw format). I
> need this to correctly implement security as content is needed to calculate
> MAC for each message (Message Authentication Code).
> I tried to do it like this:
>
> @Context
> MessageContext mc;
> ....
> IOUtils.toString(mc.getContent(InputStream.class), "utf-8");
>
> but empty String is returned as input stream has no content (checked on
> debug)
> On the other hand mc.getContent(String.class) returns null
>
> It is strange as LogginInIntercetor from CXF is using the same technique.
> Is there any way to get JSON message content as String ?
>
I'm presuming that you are trying to get InputStream after the IN
parameter has already been populated ?
If yes then you will need to duplicate current InputStream in
RequestHandler filter and save it or the calculated MAC as a custom
property on the message, and then do mc.get("json.mac") or similar...
Cheers, Sergey
> Artur
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Accessing-JAXRS-JSON-content-directly-tp4390185p4390185.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>