@Override public HttpRequest filter(HttpRequest request) throws HttpException { checkArgument(request instanceof GeneratedHttpRequest, "This filter can only be applied to GeneratedHttpRequest objects"); GeneratedHttpRequest generatedRequest = (GeneratedHttpRequest) request; // Look if there is a custom api version for the current method String commandName = config.getCommandName(generatedRequest.getInvocation()); String customApiVersion = versions.get(commandName); if (customApiVersion == null) { // No custom config for the specific method. Let's look for custom // config for the class Invokable<?, ?> invoked = generatedRequest.getInvocation().getInvokable(); String className = invoked.getOwnerType().getRawType().getSimpleName(); customApiVersion = versions.get(className); } if (customApiVersion != null) { return request.toBuilder().replaceQueryParam("api-version", customApiVersion).build(); } return request; }
@SuppressWarnings("unchecked") @BeforeMethod void createMocks() { http = createMock(HttpCommandExecutorService.class); timeLimiter = createMock(TimeLimiter.class); fallback = createMock(org.jclouds.Fallback.class); config = createMock(InvocationConfig.class); future = createMock(ListenableFuture.class); invokeHttpMethod = new InvokeSyncToAsyncHttpMethod(sync2async, toRequest, http, transformerForRequest, timeLimiter, config, userThreads); expect(config.getCommandName(asyncGet)).andReturn("ns:get"); expect(config.getFallback(asyncGet)).andReturn(fallback); }
@Override public Object apply(Invocation in) { Optional<Long> timeoutNanos = config.getTimeoutNanos(in); if (timeoutNanos.isPresent()) { return invokeWithTimeout(in, timeoutNanos.get()); } return invoke(in); }
private org.jclouds.Fallback<?> getFallback(String commandName, Invocation invocation, HttpCommand command) { HttpRequest request = command.getCurrentRequest(); org.jclouds.Fallback<?> fallback = config.getFallback(invocation); if (fallback instanceof InvocationContext) InvocationContext.class.cast(fallback).setContext(request); logger.trace("<< exceptions from %s are parsed by %s", commandName, fallback.getClass().getSimpleName()); return fallback; }
private org.jclouds.Fallback<?> getFallback(String commandName, Invocation invocation, HttpCommand command) { HttpRequest request = command.getCurrentRequest(); org.jclouds.Fallback<?> fallback = config.getFallback(invocation); if (fallback instanceof InvocationContext) InvocationContext.class.cast(fallback).setContext(request); logger.trace("<< exceptions from %s are parsed by %s", commandName, fallback.getClass().getSimpleName()); return fallback; }
@Override public HttpRequest filter(HttpRequest request) throws HttpException { checkArgument(request instanceof GeneratedHttpRequest, "This filter can only be applied to GeneratedHttpRequest objects"); GeneratedHttpRequest generatedRequest = (GeneratedHttpRequest) request; // Look if there is a custom api version for the current method String commandName = config.getCommandName(generatedRequest.getInvocation()); String customApiVersion = versions.get(commandName); if (customApiVersion == null) { // No custom config for the specific method. Let's look for custom // config for the class Invokable<?, ?> invoked = generatedRequest.getInvocation().getInvokable(); String className = invoked.getOwnerType().getRawType().getSimpleName(); customApiVersion = versions.get(className); } if (customApiVersion != null) { return request.toBuilder().replaceQueryParam("api-version", customApiVersion).build(); } return request; }
@Override public Object apply(Invocation in) { Optional<Long> timeoutNanos = config.getTimeoutNanos(in); if (timeoutNanos.isPresent()) { return invokeWithTimeout(in, timeoutNanos.get()); } return invoke(in); }
@SuppressWarnings("unchecked") @BeforeMethod void createMocks() { http = createMock(HttpCommandExecutorService.class); timeLimiter = createMock(TimeLimiter.class); fallback = createMock(org.jclouds.Fallback.class); config = createMock(InvocationConfig.class); invokeHttpMethod = new InvokeHttpMethod(toRequest, http, transformerForRequest, timeLimiter, config); expect(config.getCommandName(get)).andReturn("ns:get"); expect(config.getFallback(get)).andReturn(fallback); }
private org.jclouds.Fallback<?> getFallback(String commandName, Invocation invocation, HttpCommand command) { HttpRequest request = command.getCurrentRequest(); org.jclouds.Fallback<?> fallback = config.getFallback(invocation); if (fallback instanceof InvocationContext) InvocationContext.class.cast(fallback).setContext(request); logger.trace("<< exceptions from %s are parsed by %s", commandName, fallback.getClass().getSimpleName()); return fallback; }
@BeforeMethod public void setup() { noName = Invocation.create(method(VersionedApi.class, "noName"), ImmutableList.of()); named = Invocation.create(method(VersionedApi.class, "named"), ImmutableList.of()); config = createMock(InvocationConfig.class); expect(config.getCommandName(noName)).andReturn("VersionedApi.noName"); expect(config.getCommandName(named)).andReturn("named:get"); replay(config); }
@Override public Object apply(Invocation in) { Optional<Long> timeoutNanos = config.getTimeoutNanos(in); if (timeoutNanos.isPresent()) { return invokeWithTimeout(in, timeoutNanos.get()); } return invoke(in); }
@SuppressWarnings("unchecked") @BeforeMethod void createMocks() { http = createMock(HttpCommandExecutorService.class); timeLimiter = createMock(TimeLimiter.class); fallback = createMock(org.jclouds.Fallback.class); config = createMock(InvocationConfig.class); future = createMock(ListenableFuture.class); invokeHttpMethod = new InvokeHttpMethod(toRequest, http, transformerForRequest, timeLimiter, config); expect(config.getCommandName(get)).andReturn("ns:get"); expect(config.getFallback(get)).andReturn(fallback); }
private org.jclouds.Fallback<?> getFallback(String commandName, Invocation invocation, HttpCommand command) { HttpRequest request = command.getCurrentRequest(); org.jclouds.Fallback<?> fallback = config.getFallback(invocation); if (fallback instanceof InvocationContext) InvocationContext.class.cast(fallback).setContext(request); logger.trace("<< exceptions from %s are parsed by %s", commandName, fallback.getClass().getSimpleName()); return fallback; }
/** * invokes the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. */ public Object invoke(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> invoking %s", commandName); try { return transformer.apply(http.invoke(command)); } catch (Throwable t) { try { return fallback.createOrPropagate(t); } catch (Exception e) { throw propagate(e); } } }
@Override public Object apply(Invocation in) { Optional<Long> timeoutNanos = config.getTimeoutNanos(in); if (timeoutNanos.isPresent()) { return invokeWithTimeout(in, timeoutNanos.get()); } return invoke(in); }
private org.jclouds.Fallback<?> getFallback(String commandName, Invocation invocation, HttpCommand command) { HttpRequest request = command.getCurrentRequest(); org.jclouds.Fallback<?> fallback = config.getFallback(invocation); if (fallback instanceof InvocationContext) InvocationContext.class.cast(fallback).setContext(request); logger.trace("<< exceptions from %s are parsed by %s", commandName, fallback.getClass().getSimpleName()); return fallback; }
/** * submits the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. Parsing and Fallback occur on the * {@code userExecutor} thread. */ public ListenableFuture<?> submit(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> submitting %s", commandName); return withFallback(transform(http.submit(command), transformer, userExecutor), fallback); }
@Override public Object apply(Invocation in) { Optional<Long> timeoutNanos = config.getTimeoutNanos(in); if (timeoutNanos.isPresent()) { return invokeWithTimeout(in, timeoutNanos.get()); } return invoke(in); }
private org.jclouds.Fallback<?> getFallback(String commandName, Invocation invocation, HttpCommand command) { HttpRequest request = command.getCurrentRequest(); org.jclouds.Fallback<?> fallback = config.getFallback(invocation); if (fallback instanceof InvocationContext) InvocationContext.class.cast(fallback).setContext(request); logger.trace("<< exceptions from %s are parsed by %s", commandName, fallback.getClass().getSimpleName()); return fallback; }
/** * invokes the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. */ public Object invoke(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> invoking %s", commandName); try { return transformer.apply(http.invoke(command)); } catch (Throwable t) { try { return fallback.createOrPropagate(t); } catch (Exception e) { throw propagate(e); } } }