All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.outbrain.ob1k.client.endpoints.StreamClientEndpoint Maven / Gradle / Ivy

The newest version!
package com.outbrain.ob1k.client.endpoints;

import com.outbrain.ob1k.client.ctx.DefaultStreamClientRequestContext;
import com.outbrain.ob1k.client.ctx.StreamClientRequestContext;
import com.outbrain.ob1k.client.targets.TargetProvider;
import com.outbrain.ob1k.common.marshalling.RequestMarshaller;
import com.outbrain.ob1k.common.marshalling.RequestMarshallerRegistry;
import com.outbrain.ob1k.http.HttpClient;

import com.outbrain.ob1k.common.filters.StreamFilter;
import com.outbrain.ob1k.http.RequestBuilder;
import com.outbrain.ob1k.http.Response;
import com.outbrain.ob1k.http.marshalling.MarshallingStrategy;
import org.apache.commons.codec.EncoderException;
import rx.Observable;
import static rx.Observable.error;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Handle the stream based invocation chain of filters and remote target on the client side
 *
 * @author aronen
 */
public class StreamClientEndpoint extends AbstractClientEndpoint {

  private final StreamFilter[] filters;
  private final MarshallingStrategy marshallingStrategy = new MarshallingStrategy() {
    @Override
    public  T unmarshall(final Type type, final Response response) throws IOException {
      final RequestMarshaller marshaller = marshallerRegistry.getMarshaller(response.getContentType());
      return marshaller.unmarshallStreamResponse(response, type);
    }
    @Override
    public byte[] marshall(final Object value) throws IOException {
      return marshallObject(value);
    }
  };

  public StreamClientEndpoint(final HttpClient httpClient, final RequestMarshallerRegistry marshallerRegistry,
                              final Endpoint endpoint, final StreamFilter[] filters) {

    super(httpClient, marshallerRegistry, endpoint);
    this.filters = filters;
  }

  @SuppressWarnings("unchecked")
  public  Observable invokeStream(final StreamClientRequestContext ctx) {

    if (filters != null && ctx.getExecutionIndex() < filters.length) {

      final StreamFilter filter = filters[ctx.getExecutionIndex()];
      return (Observable) filter.handleStream(ctx.nextPhase());

    } else {

      final RequestBuilder requestBuilder;

      try {
        requestBuilder = buildEndpointRequestBuilder(ctx, marshallingStrategy);
      } catch (final EncoderException | IOException e) {
        return Observable.error(e);
      }

      final Type responseType = extractResponseType();

      // If the client requested to get the response object
      if (responseType == Response.class) {
        return (Observable) requestBuilder.asStream();
      }

      // If the client requested to get the , together with the whole response object
      if (isTypedResponse(responseType)) {
        final Type type = ((ParameterizedType) responseType).getActualTypeArguments()[0];
        return (Observable) requestBuilder.asTypedStream(type);
      }

      return requestBuilder.asStreamValue(responseType);
    }
  }

  @Override
  public Object invoke(final TargetProvider targetProvider, final Object[] params) throws Throwable {
    final String remoteTarget;
    try{
      remoteTarget = targetProvider.provideTarget();
    } catch (final RuntimeException e) {
      return error(e);
    }
    final DefaultStreamClientRequestContext ctx = new DefaultStreamClientRequestContext(remoteTarget, params, this);
    return invokeStream(ctx);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy