java - OncePerRequestFilter doesn't include last character from response body -
i trying implement content digest header in rest api. when creating digest response omits last character.
public class contentdigestfilter extends onceperrequestfilter {
@override protected boolean shouldnotfilterasyncdispatch() { return false; } @override protected void dofilterinternal(httpservletrequest request, httpservletresponse response, filterchain filterchain) throws servletexception, ioexception { httpservletresponse responsetouse = response; if (!isasyncdispatch(request) && !(response instanceof contentcachingresponsewrapper)) { responsetouse = new httpstreamingawarecontentcachingresponsewrapper(response, request); } filterchain.dofilter(request, responsetouse); updateresponse(request, responsetouse); } private void updateresponse(httpservletrequest request, httpservletresponse response) throws ioexception { contentcachingresponsewrapper responsewrapper = webutils.getnativeresponse(response, contentcachingresponsewrapper.class); assert.notnull(responsewrapper, "contentcachingresponsewrapper not found"); httpservletresponse rawresponse = (httpservletresponse) responsewrapper.getresponse(); int statuscode = responsewrapper.getstatuscode(); if (rawresponse.iscommitted()) { responsewrapper.copybodytoresponse(); } else{ string digest = getcontentdigest(responsewrapper.getcontentinputstream()); rawresponse.setheader("x-digest",digest); responsewrapper.copybodytoresponse(); } } protected string getcontentdigest(inputstream inputstream) throws ioexception { try { byte[] dataarray = streamutils.copytobytearray(inputstream); //dataarray doesn't include last character of response body. system.out.println("response body: " + new string(dataarray,"utf-8")); string digest = ""; //generate digest hmac-sha512 return digest; } catch (nosuchalgorithmexception e) { throw new runtimeexception(e); } catch (invalidkeyexception e) { e.printstacktrace(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } return "nodigest"; } private static class httpstreamingawarecontentcachingresponsewrapper extends contentcachingresponsewrapper { private final httpservletrequest request; public httpstreamingawarecontentcachingresponsewrapper(httpservletresponse response, httpservletrequest request) { super(response); this.request = request; } @override public servletoutputstream getoutputstream() throws ioexception { return super.getoutputstream(); } @override public printwriter getwriter() throws ioexception { return super.getwriter(); } }
}
in method getcontentdigest if print dataarray doesn't include last character of response
Comments
Post a Comment