package org.realityforge.replicant.client.gwt;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import java.util.Objects;
import javax.annotation.Nonnull;
import org.realityforge.replicant.shared.SharedConstants;

class ReplicantRequestCallback
  implements RequestCallback
{
  private final replicant.Request _r;
  private final RequestCallback _callback;

  ReplicantRequestCallback( @Nonnull final replicant.Request r, @Nonnull final RequestCallback callback )
  {
    _r = Objects.requireNonNull( r );
    _callback = Objects.requireNonNull( callback );
  }

  @Override
  public void onResponseReceived( final Request request, final Response response )
  {
    final int statusCode = response.getStatusCode();
    if ( Response.SC_OK == statusCode )
    {
      final boolean messageComplete = "1".equals( response.getHeader( SharedConstants.REQUEST_COMPLETE_HEADER ) );
      _r.onSuccess( messageComplete, () -> {
        if ( null != _callback )
        {
          _callback.onResponseReceived( request, response );
        }
      } );
    }
    else
    {
      _r.onFailure( () -> {
        if ( null != _callback )
        {
          _callback.onError( request, new InvalidHttpResponseException( statusCode, response.getStatusText() ) );
        }
      } );
    }
  }

  @Override
  public void onError( final Request request, final Throwable exception )
  {
    _r.onFailure( () -> {
      if ( null != _callback )
      {
        _callback.onError( request, exception );
      }
    } );
  }
}

@@ -297,16 +297,22 @@
 tants;%0A%0A
+final 
 class Re
@@ -365,16 +365,27 @@
 lback%0A%7B%0A
+  @Nonnull%0A
   privat
@@ -414,16 +414,27 @@
 est _r;%0A
+  @Nonnull%0A
   privat
@@ -1033,63 +1033,8 @@
 ) -%3E
- %7B%0A        if ( null != _callback )%0A        %7B%0A         
  _ca
@@ -1079,35 +1079,16 @@
 sponse )
-;%0A        %7D%0A      %7D
  );%0A    
@@ -1114,110 +1114,58 @@
     
-_r.onFailure( () -%3E %7B%0A        if ( null != _callback )%0A        %7B%0A          _callback.onError( request,
+final InvalidHttpResponseException error =%0A       
  new
@@ -1231,18 +1231,16 @@
 usText()
- )
  );%0A    
@@ -1245,19 +1245,63 @@
     
-  %7D%0A      %7D
+_r.onFailure( () -%3E _callback.onError( request, error )
  );%0A
@@ -1428,57 +1428,8 @@
 ) -%3E
- %7B%0A      if ( null != _callback )%0A      %7B%0A       
  _ca
@@ -1468,23 +1468,8 @@
 on )
-;%0A      %7D%0A    %7D
  );%0A

Add Nullability annotations, simplify code by removing unnecessary null checks