java - HttpMessageNotReadableException: Could not read document: Stream closed; -
i working spring boot , filtering requests using filters. filter being used have check user verification reading data request body (for have used httpservletrequestwrapper implementation). requestwrapper getting data expected , services working fine in filter. but, when on success, filter passes request main controller, getting stream closed exception follows:
o.s.w.s.m.s.defaulthandlerexceptionresolver - failed read http message: org.springframework.http.converter.httpmessagenotreadableexception: not read document: stream closed; nested exception java.io.ioexception: stream closed
here filter class:
@webfilter(urlpatterns = {"/getdocs" }) public class authenticationfilter implements filter{ private static logger logger = logger.getlogger(authenticationfilter.class); @autowired private userverificationservice userverificationservice; @override public void destroy() { // todo auto-generated method stub } @override public void dofilter(servletrequest arg0, servletresponse response, filterchain chain) throws ioexception, servletexception { logger.info("checking token in filter"); httpservletrequest request = (httpservletrequest) arg0; docverificationrequestwrapper myrequestwrapper = new docverificationrequestwrapper((httpservletrequest) request); string body = myrequestwrapper.getbody(); token token = null; try { jsonobject jsonobj = new jsonobject(body); jsonobject tokenobj = (jsonobject) jsonobj.get("token"); gson gson = new gson(); token = gson.fromjson(tokenobj.tostring(), token.class); if(null != token) { if(userverificationservice==null){ servletcontext servletcontext = request.getservletcontext(); webapplicationcontext webapplicationcontext = webapplicationcontextutils.getwebapplicationcontext(servletcontext); userverificationservice = webapplicationcontext.getbean(userverificationservice.class); } string verstatus = userverificationservice.verifyuser(token); logger.info("verstatus = "+verstatus); if(verstatus != null && verstatus.equalsignorecase("success")) { chain.dofilter(request, response); }else logger.error("invalid token"); }else { logger.error("token missing."); } } catch (jsonexception e) { logger.error("exception in authetication filter " + e); } } @override public void init(filterconfig arg0) throws servletexception { // todo auto-generated method stub } }
and here httpservletrequestwrapper implementation class:
public class docverificationrequestwrapper extends httpservletrequestwrapper { private final string body; public docverificationrequestwrapper(httpservletrequest request) throws ioexception { super(request); stringbuilder stringbuilder = new stringbuilder(); bufferedreader bufferedreader = null; try { inputstream inputstream = request.getinputstream(); if (inputstream != null) { bufferedreader = new bufferedreader(new inputstreamreader(inputstream)); char[] charbuffer = new char[128]; int bytesread = -1; while ((bytesread = bufferedreader.read(charbuffer)) > 0) { stringbuilder.append(charbuffer, 0, bytesread); } } else { stringbuilder.append(""); } } catch (ioexception ex) { throw ex; } { if (bufferedreader != null) { try { bufferedreader.close(); } catch (ioexception ex) { throw ex; } } } body = stringbuilder.tostring(); } @override public servletinputstream getinputstream() throws ioexception { final bytearrayinputstream bytearrayinputstream = new bytearrayinputstream(body.getbytes()); servletinputstream servletinputstream = new servletinputstream() { public int read() throws ioexception { return bytearrayinputstream.read(); } @override public boolean isfinished() { // todo auto-generated method stub return false; } @override public boolean isready() { // todo auto-generated method stub return false; } @override public void setreadlistener(readlistener listener) { // todo auto-generated method stub } }; return servletinputstream; } @override public bufferedreader getreader() throws ioexception { return new bufferedreader(new inputstreamreader(this.getinputstream())); } public string getbody() { return this.body; } }
any suggestion, how resolve error?
i guess because instead of passing actual wrapper chain, you're passing original request. try this:
if(verstatus != null && verstatus.equalsignorecase("success")) { chain.dofilter(myrequestwrapper, response); }
obviously, @ time when objectmapper
trying read request body - request input stream closed since has been read wrapper.
wiki
Comments
Post a Comment