Esempio n. 1
0
  public void handle(final AbstractActiveNetModule aOwner, @Nonnull final Socket aSocket) {
    final String sClientInfo = getClientInfo(aSocket);
    s_aLogger.info("Incoming connection " + sClientInfo);

    final AS2Message aMsg = createMessage(aSocket);

    final IAS2HttpResponseHandler aResponseHandler = new AS2HttpResponseHandlerSocket(aSocket);

    // Time the transmission
    final StopWatch aSW = StopWatch.createdStarted();
    byte[] aMsgData = null;
    try {
      // Read in the message request, headers, and data
      aMsgData =
          readAndDecodeHttpRequest(
              new AS2InputStreamProviderSocket(aSocket), aResponseHandler, aMsg);
    } catch (final Exception ex) {
      final NetException ne = new NetException(aSocket.getInetAddress(), aSocket.getPort(), ex);
      ne.terminate();
    }

    aSW.stop();

    if (aMsgData != null) {
      s_aLogger.info(
          "received "
              + IOHelper.getTransferRate(aMsgData.length, aSW)
              + " from "
              + sClientInfo
              + aMsg.getLoggingText());

      handleIncomingMessage(sClientInfo, aMsgData, aMsg, aResponseHandler);
    }
  }
  private void _sendAsyncMDN(@Nonnull final AS2Message aMsg) throws OpenAS2Exception {
    s_aLogger.info("Async MDN submitted" + aMsg.getLoggingText());
    final DispositionType aDisposition = DispositionType.createSuccess();

    try {
      final IMessageMDN aMdn = aMsg.getMDN();

      // Create a HTTP connection
      final String sUrl = aMsg.getAsyncMDNurl();
      final boolean bOutput = true;
      final boolean bInput = true;
      final boolean bUseCaches = false;
      final HttpURLConnection aConn =
          getConnection(sUrl, bOutput, bInput, bUseCaches, "POST", getSession().getHttpProxy());

      try {
        s_aLogger.info("connected to " + sUrl + aMsg.getLoggingText());

        aConn.setRequestProperty(CAS2Header.HEADER_CONNECTION, CAS2Header.DEFAULT_CONNECTION);
        aConn.setRequestProperty(CAS2Header.HEADER_USER_AGENT, CAS2Header.DEFAULT_USER_AGENT);
        // Copy all the header from mdn to the RequestProperties of conn
        final Enumeration<?> aHeaders = aMdn.getHeaders().getAllHeaders();
        while (aHeaders.hasMoreElements()) {
          final Header aHeader = (Header) aHeaders.nextElement();
          final String sHeaderValue =
              aHeader.getValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ');
          aConn.setRequestProperty(aHeader.getName(), sHeaderValue);
        }

        // Note: closing this stream causes connection abort errors on some AS2
        // servers
        final OutputStream aMessageOS = aConn.getOutputStream();

        // Transfer the data
        final InputStream aMessageIS = aMdn.getData().getInputStream();
        final StopWatch aSW = StopWatch.createdStarted();
        final long nBytes = IOHelper.copy(aMessageIS, aMessageOS);
        aSW.stop();
        s_aLogger.info(
            "transferred " + IOHelper.getTransferRate(nBytes, aSW) + aMsg.getLoggingText());

        // Check the HTTP Response code
        final int nResponseCode = aConn.getResponseCode();
        if (nResponseCode != HttpURLConnection.HTTP_OK
            && nResponseCode != HttpURLConnection.HTTP_CREATED
            && nResponseCode != HttpURLConnection.HTTP_ACCEPTED
            && nResponseCode != HttpURLConnection.HTTP_PARTIAL
            && nResponseCode != HttpURLConnection.HTTP_NO_CONTENT) {
          s_aLogger.error(
              "sent AsyncMDN [" + aDisposition.getAsString() + "] Fail " + aMsg.getLoggingText());
          throw new HttpResponseException(sUrl, nResponseCode, aConn.getResponseMessage());
        }

        s_aLogger.info(
            "sent AsyncMDN [" + aDisposition.getAsString() + "] OK " + aMsg.getLoggingText());

        // log & store mdn into backup folder.
        try {
          getSession()
              .getMessageProcessor()
              .handle(IProcessorStorageModule.DO_STOREMDN, aMsg, null);
        } catch (final ComponentNotFoundException ex) {
          // May be
        }
      } finally {
        aConn.disconnect();
      }
    } catch (final HttpResponseException ex) {
      // Resend if the HTTP Response has an error code
      ex.terminate();
      _resend(aMsg, ex);
    } catch (final IOException ex) {
      // Resend if a network error occurs during transmission
      final OpenAS2Exception wioe = WrappedOpenAS2Exception.wrap(ex);
      wioe.addSource(OpenAS2Exception.SOURCE_MESSAGE, aMsg);
      wioe.terminate();

      _resend(aMsg, wioe);
    } catch (final Exception ex) {
      // Propagate error if it can't be handled by a resend
      throw WrappedOpenAS2Exception.wrap(ex);
    }
  }