Exemple #1
0
  /**
   * Read headers and payload from the passed input stream provider.
   *
   * @param aISP The abstract input stream provider to use. May not be <code>null</code>.
   * @param aResponseHandler The HTTP response handler to be used. May not be <code>null</code>.
   * @param aMsg The Message to be filled. May not be <code>null</code>.
   * @return The payload of the HTTP request.
   * @throws IOException In case of error reading from the InputStream
   * @throws MessagingException In case header line parsing fails
   */
  @Nonnull
  public static byte[] readHttpRequest(
      @Nonnull final IAS2InputStreamProvider aISP,
      @Nonnull final IAS2HttpResponseHandler aResponseHandler,
      @Nonnull final IMessage aMsg)
      throws IOException, MessagingException {
    // Get the stream to read from
    final InputStream aIS = aISP.getInputStream();
    if (aIS == null) throw new IllegalStateException("Failed to open InputStream from " + aISP);

    // Read the HTTP meta data
    final String[] aRequest = _readRequestInfo(aIS);
    // Request method (e.g. "POST")
    aMsg.setAttribute(MA_HTTP_REQ_TYPE, aRequest[0]);
    // Request URL (e.g. "/as2")
    aMsg.setAttribute(MA_HTTP_REQ_URL, aRequest[1]);
    // HTTP version (e.g. "HTTP/1.1")
    aMsg.setAttribute(MA_HTTP_REQ_VERSION, aRequest[2]);

    // Parse all HTTP headers from stream
    final InternetHeaders aHeaders = new InternetHeaders(aIS);
    aMsg.setHeaders(aHeaders);

    // Read the message body - no Content-Transfer-Encoding handling
    final byte[] aPayload = readHttpPayload(aIS, aResponseHandler, aMsg);

    // Dump on demand
    if (isHTTPIncomingDumpEnabled())
      dumpIncomingHttpRequest(getAllHTTPHeaderLines(aHeaders), aPayload, aMsg);

    return aPayload;

    // Don't close the IS here!
  }