@Test (expected = IOException.class) public void testNoLengthMessageStream () throws Exception { final NonBlockingByteArrayInputStream is = new NonBlockingByteArrayInputStream (m_sNoLengthMessage.getBytes (StandardCharsets.UTF_8)); final AS2Message aMsg = new AS2Message (); final IAS2InputStreamProvider mockStreamProvider = new MockAS2InputStreamProvider (is); HTTPHelper.readHttpRequest (mockStreamProvider, MOCK_RH, aMsg, INCOMING_DUMPER); }
@Nonnull public static DataSource readHttpRequest (@Nonnull final IAS2InputStreamProvider aISP, @Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnull final IMessage aMsg, @Nullable final IHTTPIncomingDumper aIncomingDumper) throws IOException, MessagingException { final InputStream aIS = aISP.getInputStream (); final String [] aRequest = _readRequestInfo (aIS); aMsg.attrs ().putIn (MA_HTTP_REQ_TYPE, aRequest[0]); aMsg.attrs ().putIn (MA_HTTP_REQ_URL, aRequest[1]); aMsg.attrs ().putIn (MA_HTTP_REQ_VERSION, aRequest[2]); final InternetHeaders aHeaders = new InternetHeaders (aIS); final Enumeration <Header> aEnum = aHeaders.getAllHeaders (); while (aEnum.hasMoreElements ()) { final Header aHeader = aEnum.nextElement (); aMsg.headers ().addHeader (aHeader.getName (), aHeader.getValue ()); } final String sReceivedContentType = AS2HttpHelper.getCleanContentType (aMsg.getHeader (CHttpHeader.CONTENT_TYPE)); byte [] aBytePayLoad = null; final DataSource aPayload; final String sContentLength = aMsg.getHeader (CHttpHeader.CONTENT_LENGTH); if (sContentLength == null) { final InputStream aRealIS; final String sTransferEncoding = aMsg.getHeader (CHttpHeader.TRANSFER_ENCODING); if (sTransferEncoding != null) { if (AS2Helper.getWithoutSpaces (sTransferEncoding).equalsIgnoreCase ("chunked")) { @WillNotClose final TempSharedFileInputStream aSharedIS = TempSharedFileInputStream.getTempSharedFileInputStream (new ChunkedInputStream (aIS), aMsg.getMessageID ()); aRealIS = aSharedIS; aMsg.setTempSharedFileInputStream (aSharedIS); } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Transfer-Encoding unimplemented: " + sTransferEncoding); } } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length missing"); } aPayload = new InputStreamDataSource (aRealIS, aMsg.getAS2From () == null ? "" : aMsg.getAS2From (), sReceivedContentType, true); } else { final long nContentLength = StringParser.parseLong (sContentLength, -1); if (nContentLength < 0 || nContentLength > Integer.MAX_VALUE) { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length '" + sContentLength + "' is invalid. Only values between 0 and " + Integer.MAX_VALUE + " are allowed."); } aBytePayLoad = new byte [(int) nContentLength]; try (final DataInputStream aDataIS = new DataInputStream (aIS)) { aDataIS.readFully (aBytePayLoad); } aPayload = new ByteArrayDataSource (aBytePayLoad, sReceivedContentType, null); } if (aIncomingDumper != null) aIncomingDumper.dumpIncomingRequest (getAllHTTPHeaderLines (aHeaders), aBytePayLoad != null ? aBytePayLoad : "Payload body was not read yet, and therefore it cannot be dumped (yet) - sorry".getBytes (StandardCharsets.ISO_8859_1), aMsg); return aPayload; }
HTTPHelper { @Nonnull public static DataSource readHttpRequest (@Nonnull final IAS2InputStreamProvider aISP, @Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnull final IMessage aMsg, @Nullable final IHTTPIncomingDumper aIncomingDumper) throws IOException, MessagingException { final InputStream aIS = aISP.getInputStream (); final String [] aRequest = _readRequestInfo (aIS); aMsg.attrs ().putIn (MA_HTTP_REQ_TYPE, aRequest[0]); aMsg.attrs ().putIn (MA_HTTP_REQ_URL, aRequest[1]); aMsg.attrs ().putIn (MA_HTTP_REQ_VERSION, aRequest[2]); final InternetHeaders aHeaders = new InternetHeaders (aIS); final Enumeration <Header> aEnum = aHeaders.getAllHeaders (); while (aEnum.hasMoreElements ()) { final Header aHeader = aEnum.nextElement (); aMsg.headers ().addHeader (aHeader.getName (), aHeader.getValue ()); } final String sReceivedContentType = AS2HttpHelper.getCleanContentType (aMsg.getHeader (CHttpHeader.CONTENT_TYPE)); byte [] aBytePayLoad = null; final DataSource aPayload; final String sContentLength = aMsg.getHeader (CHttpHeader.CONTENT_LENGTH); if (sContentLength == null) { final InputStream aRealIS; final String sTransferEncoding = aMsg.getHeader (CHttpHeader.TRANSFER_ENCODING); if (sTransferEncoding != null) { if (AS2Helper.getWithoutSpaces (sTransferEncoding).equalsIgnoreCase ("chunked")) { @WillNotClose final TempSharedFileInputStream aSharedIS = TempSharedFileInputStream.getTempSharedFileInputStream (new ChunkedInputStream (aIS), aMsg.getMessageID ()); aRealIS = aSharedIS; aMsg.setTempSharedFileInputStream (aSharedIS); } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Transfer-Encoding unimplemented: " + sTransferEncoding); } } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length missing"); } aPayload = new InputStreamDataSource (aRealIS, aMsg.getAS2From () == null ? "" : aMsg.getAS2From (), sReceivedContentType, true); } else { final long nContentLength = StringParser.parseLong (sContentLength, -1); if (nContentLength < 0 || nContentLength > Integer.MAX_VALUE) { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length '" + sContentLength + "' is invalid. Only values between 0 and " + Integer.MAX_VALUE + " are allowed."); } aBytePayLoad = new byte [(int) nContentLength]; try (final DataInputStream aDataIS = new DataInputStream (aIS)) { aDataIS.readFully (aBytePayLoad); } aPayload = new ByteArrayDataSource (aBytePayLoad, sReceivedContentType, null); } if (aIncomingDumper != null) aIncomingDumper.dumpIncomingRequest (getAllHTTPHeaderLines (aHeaders), aBytePayLoad != null ? aBytePayLoad : "Payload body was not read yet, and therefore it cannot be dumped (yet) - sorry".getBytes (StandardCharsets.ISO_8859_1), aMsg); return aPayload; } }
HTTPHelper { @Nonnull public static DataSource readHttpRequest (@Nonnull final IAS2InputStreamProvider aISP, @Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnull final IMessage aMsg, @Nullable final IHTTPIncomingDumper aIncomingDumper) throws IOException, MessagingException { final InputStream aIS = aISP.getInputStream (); final String [] aRequest = _readRequestInfo (aIS); aMsg.attrs ().putIn (MA_HTTP_REQ_TYPE, aRequest[0]); aMsg.attrs ().putIn (MA_HTTP_REQ_URL, aRequest[1]); aMsg.attrs ().putIn (MA_HTTP_REQ_VERSION, aRequest[2]); final InternetHeaders aHeaders = new InternetHeaders (aIS); final Enumeration <Header> aEnum = aHeaders.getAllHeaders (); while (aEnum.hasMoreElements ()) { final Header aHeader = aEnum.nextElement (); aMsg.headers ().addHeader (aHeader.getName (), aHeader.getValue ()); } final String sReceivedContentType = AS2HttpHelper.getCleanContentType (aMsg.getHeader (CHttpHeader.CONTENT_TYPE)); byte [] aBytePayLoad = null; final DataSource aPayload; final String sContentLength = aMsg.getHeader (CHttpHeader.CONTENT_LENGTH); if (sContentLength == null) { final InputStream aRealIS; final String sTransferEncoding = aMsg.getHeader (CHttpHeader.TRANSFER_ENCODING); if (sTransferEncoding != null) { if (AS2Helper.getWithoutSpaces (sTransferEncoding).equalsIgnoreCase ("chunked")) { @WillNotClose final TempSharedFileInputStream aSharedIS = TempSharedFileInputStream.getTempSharedFileInputStream (new ChunkedInputStream (aIS), aMsg.getMessageID ()); aRealIS = aSharedIS; aMsg.setTempSharedFileInputStream (aSharedIS); } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Transfer-Encoding unimplemented: " + sTransferEncoding); } } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length missing"); } aPayload = new InputStreamDataSource (aRealIS, aMsg.getAS2From () == null ? "" : aMsg.getAS2From (), sReceivedContentType, true); } else { final long nContentLength = StringParser.parseLong (sContentLength, -1); if (nContentLength < 0 || nContentLength > Integer.MAX_VALUE) { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length '" + sContentLength + "' is invalid. Only values between 0 and " + Integer.MAX_VALUE + " are allowed."); } aBytePayLoad = new byte [(int) nContentLength]; try (final DataInputStream aDataIS = new DataInputStream (aIS)) { aDataIS.readFully (aBytePayLoad); } aPayload = new ByteArrayDataSource (aBytePayLoad, sReceivedContentType, null); } if (aIncomingDumper != null) aIncomingDumper.dumpIncomingRequest (getAllHTTPHeaderLines (aHeaders), aBytePayLoad != null ? aBytePayLoad : "Payload body was not read yet, and therefore it cannot be dumped (yet) - sorry".getBytes (StandardCharsets.ISO_8859_1), aMsg); return aPayload; } private  HTTPHelper(); }
HTTPHelper { @Nonnull public static DataSource readHttpRequest (@Nonnull final IAS2InputStreamProvider aISP, @Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnull final IMessage aMsg, @Nullable final IHTTPIncomingDumper aIncomingDumper) throws IOException, MessagingException { final InputStream aIS = aISP.getInputStream (); final String [] aRequest = _readRequestInfo (aIS); aMsg.attrs ().putIn (MA_HTTP_REQ_TYPE, aRequest[0]); aMsg.attrs ().putIn (MA_HTTP_REQ_URL, aRequest[1]); aMsg.attrs ().putIn (MA_HTTP_REQ_VERSION, aRequest[2]); final InternetHeaders aHeaders = new InternetHeaders (aIS); final Enumeration <Header> aEnum = aHeaders.getAllHeaders (); while (aEnum.hasMoreElements ()) { final Header aHeader = aEnum.nextElement (); aMsg.headers ().addHeader (aHeader.getName (), aHeader.getValue ()); } final String sReceivedContentType = AS2HttpHelper.getCleanContentType (aMsg.getHeader (CHttpHeader.CONTENT_TYPE)); byte [] aBytePayLoad = null; final DataSource aPayload; final String sContentLength = aMsg.getHeader (CHttpHeader.CONTENT_LENGTH); if (sContentLength == null) { final InputStream aRealIS; final String sTransferEncoding = aMsg.getHeader (CHttpHeader.TRANSFER_ENCODING); if (sTransferEncoding != null) { if (AS2Helper.getWithoutSpaces (sTransferEncoding).equalsIgnoreCase ("chunked")) { @WillNotClose final TempSharedFileInputStream aSharedIS = TempSharedFileInputStream.getTempSharedFileInputStream (new ChunkedInputStream (aIS), aMsg.getMessageID ()); aRealIS = aSharedIS; aMsg.setTempSharedFileInputStream (aSharedIS); } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Transfer-Encoding unimplemented: " + sTransferEncoding); } } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length missing"); } aPayload = new InputStreamDataSource (aRealIS, aMsg.getAS2From () == null ? "" : aMsg.getAS2From (), sReceivedContentType, true); } else { final long nContentLength = StringParser.parseLong (sContentLength, -1); if (nContentLength < 0 || nContentLength > Integer.MAX_VALUE) { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length '" + sContentLength + "' is invalid. Only values between 0 and " + Integer.MAX_VALUE + " are allowed."); } aBytePayLoad = new byte [(int) nContentLength]; try (final DataInputStream aDataIS = new DataInputStream (aIS)) { aDataIS.readFully (aBytePayLoad); } aPayload = new ByteArrayDataSource (aBytePayLoad, sReceivedContentType, null); } if (aIncomingDumper != null) aIncomingDumper.dumpIncomingRequest (getAllHTTPHeaderLines (aHeaders), aBytePayLoad != null ? aBytePayLoad : "Payload body was not read yet, and therefore it cannot be dumped (yet) - sorry".getBytes (StandardCharsets.ISO_8859_1), aMsg); return aPayload; } private  HTTPHelper(); @Nonnull @ReturnsMutableCopy static ICommonsList <String> getAllHTTPHeaderLines(@Nonnull final InternetHeaders aHeaders); @Nullable static IHTTPIncomingDumper getHTTPIncomingDumper(); @Nonnull static ISupplier <? extends IHTTPIncomingDumper> getHTTPIncomingDumperFactory(); static void setHTTPIncomingDumperFactory(@Nonnull final ISupplier <? extends IHTTPIncomingDumper> aHttpDumperFactory); @Nonnull static DataSource readHttpRequest(@Nonnull final IAS2InputStreamProvider aISP,
                                            @Nonnull final IAS2HttpResponseHandler aResponseHandler,
                                            @Nonnull final IMessage aMsg,
                                            @Nullable final IHTTPIncomingDumper aIncomingDumper); static void sendSimpleHTTPResponse(@Nonnull final IAS2HttpResponseHandler aResponseHandler,
                                             @Nonnegative final int nResponseCode); static int readChunkLen(@Nonnull @WillNotClose final InputStream aIS); static void readTillNexLine(@Nonnull @WillNotClose final InputStream aIS); }
HTTPHelper { @Nonnull public static DataSource readHttpRequest (@Nonnull final IAS2InputStreamProvider aISP, @Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnull final IMessage aMsg, @Nullable final IHTTPIncomingDumper aIncomingDumper) throws IOException, MessagingException { final InputStream aIS = aISP.getInputStream (); final String [] aRequest = _readRequestInfo (aIS); aMsg.attrs ().putIn (MA_HTTP_REQ_TYPE, aRequest[0]); aMsg.attrs ().putIn (MA_HTTP_REQ_URL, aRequest[1]); aMsg.attrs ().putIn (MA_HTTP_REQ_VERSION, aRequest[2]); final InternetHeaders aHeaders = new InternetHeaders (aIS); final Enumeration <Header> aEnum = aHeaders.getAllHeaders (); while (aEnum.hasMoreElements ()) { final Header aHeader = aEnum.nextElement (); aMsg.headers ().addHeader (aHeader.getName (), aHeader.getValue ()); } final String sReceivedContentType = AS2HttpHelper.getCleanContentType (aMsg.getHeader (CHttpHeader.CONTENT_TYPE)); byte [] aBytePayLoad = null; final DataSource aPayload; final String sContentLength = aMsg.getHeader (CHttpHeader.CONTENT_LENGTH); if (sContentLength == null) { final InputStream aRealIS; final String sTransferEncoding = aMsg.getHeader (CHttpHeader.TRANSFER_ENCODING); if (sTransferEncoding != null) { if (AS2Helper.getWithoutSpaces (sTransferEncoding).equalsIgnoreCase ("chunked")) { @WillNotClose final TempSharedFileInputStream aSharedIS = TempSharedFileInputStream.getTempSharedFileInputStream (new ChunkedInputStream (aIS), aMsg.getMessageID ()); aRealIS = aSharedIS; aMsg.setTempSharedFileInputStream (aSharedIS); } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Transfer-Encoding unimplemented: " + sTransferEncoding); } } else { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length missing"); } aPayload = new InputStreamDataSource (aRealIS, aMsg.getAS2From () == null ? "" : aMsg.getAS2From (), sReceivedContentType, true); } else { final long nContentLength = StringParser.parseLong (sContentLength, -1); if (nContentLength < 0 || nContentLength > Integer.MAX_VALUE) { sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_LENGTH_REQUIRED); throw new IOException ("Content-Length '" + sContentLength + "' is invalid. Only values between 0 and " + Integer.MAX_VALUE + " are allowed."); } aBytePayLoad = new byte [(int) nContentLength]; try (final DataInputStream aDataIS = new DataInputStream (aIS)) { aDataIS.readFully (aBytePayLoad); } aPayload = new ByteArrayDataSource (aBytePayLoad, sReceivedContentType, null); } if (aIncomingDumper != null) aIncomingDumper.dumpIncomingRequest (getAllHTTPHeaderLines (aHeaders), aBytePayLoad != null ? aBytePayLoad : "Payload body was not read yet, and therefore it cannot be dumped (yet) - sorry".getBytes (StandardCharsets.ISO_8859_1), aMsg); return aPayload; } private  HTTPHelper(); @Nonnull @ReturnsMutableCopy static ICommonsList <String> getAllHTTPHeaderLines(@Nonnull final InternetHeaders aHeaders); @Nullable static IHTTPIncomingDumper getHTTPIncomingDumper(); @Nonnull static ISupplier <? extends IHTTPIncomingDumper> getHTTPIncomingDumperFactory(); static void setHTTPIncomingDumperFactory(@Nonnull final ISupplier <? extends IHTTPIncomingDumper> aHttpDumperFactory); @Nonnull static DataSource readHttpRequest(@Nonnull final IAS2InputStreamProvider aISP,
                                            @Nonnull final IAS2HttpResponseHandler aResponseHandler,
                                            @Nonnull final IMessage aMsg,
                                            @Nullable final IHTTPIncomingDumper aIncomingDumper); static void sendSimpleHTTPResponse(@Nonnull final IAS2HttpResponseHandler aResponseHandler,
                                             @Nonnegative final int nResponseCode); static int readChunkLen(@Nonnull @WillNotClose final InputStream aIS); static void readTillNexLine(@Nonnull @WillNotClose final InputStream aIS); static final String MA_HTTP_REQ_TYPE; static final String MA_HTTP_REQ_URL; static final String MA_HTTP_REQ_VERSION; }