public Invocation apply(Invocation in) { return Invocation.create( checkNotNull(cache.getIfPresent(in.getInvokable()), "invokable %s not in %s", in.getInvokable(), cache), in.getArgs()); } };
/** * looks up the corresponding {@code Invocation} that returns a * {@code Future}. Only Invokables that return {@code Futures} are annotated * in a way that can be parsed into an {@linkplain HttpRequest}. */ private Invocation toAsync(Invocation in) { Invocation async = sync2async.apply(in); checkState(isFuture(async.getInvokable()), "not a future: %s", async); return async; }
@Override public Set<String> apply(Invocation invocation) { Optional<Consumes> accept = Optional.fromNullable(invocation.getInvokable().getAnnotation(Consumes.class)).or( Optional.fromNullable(invocation.getInvokable().getOwnerType().getRawType().getAnnotation(Consumes.class))); return (accept.isPresent()) ? ImmutableSet.copyOf(accept.get().value()) : ImmutableSet.<String> of(); } }
private boolean checkPresentOrNullable(Invocation invocation, String paramKey, int argIndex, Object arg) { if (arg == null && !invocation.getInvokable().getParameters().get(argIndex).isAnnotationPresent(Nullable.class)) throw new NullPointerException(format("param{%s} for invocation %s.%s", paramKey, invocation.getInvokable() .getOwnerType().getRawType().getSimpleName(), invocation.getInvokable().getName())); return true; }
private void addHeaderIfAnnotationPresentOnMethod(Multimap<String, String> headers, Invocation invocation, Multimap<String, ?> tokenValues) { if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(Headers.class)) { Headers header = invocation.getInvokable().getOwnerType().getRawType().getAnnotation(Headers.class); addHeader(headers, header, tokenValues); } if (invocation.getInvokable().isAnnotationPresent(Headers.class)) { Headers header = invocation.getInvokable().getAnnotation(Headers.class); addHeader(headers, header, tokenValues); } }
@Override public String toString() { String callerString = caller != null ? String.format("%s.%s%s", caller.getInvokable().getOwnerType().getRawType().getSimpleName(), caller.getInvokable().getName(), caller.getArgs()) : null; return Objects.toStringHelper("").omitNullValues().add("caller", callerString).toString(); } }
private void addProducesIfPresentOnTypeOrMethod(Multimap<String, String> headers, Invocation invocation) { if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(Produces.class)) { Produces header = invocation.getInvokable().getOwnerType().getRawType().getAnnotation(Produces.class); headers.replaceValues(CONTENT_TYPE, asList(header.value())); } if (invocation.getInvokable().isAnnotationPresent(Produces.class)) { Produces header = invocation.getInvokable().getAnnotation(Produces.class); headers.replaceValues(CONTENT_TYPE, asList(header.value())); } }
private void addProducesIfPresentOnTypeOrMethod(Multimap<String, String> headers, Invocation invocation) { if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(Produces.class)) { Produces header = invocation.getInvokable().getOwnerType().getRawType().getAnnotation(Produces.class); headers.replaceValues(CONTENT_TYPE, asList(header.value())); } if (invocation.getInvokable().isAnnotationPresent(Produces.class)) { Produces header = invocation.getInvokable().getAnnotation(Produces.class); headers.replaceValues(CONTENT_TYPE, asList(header.value())); } }
@Override public org.jclouds.Fallback<?> getFallback(Invocation invocation) { Fallback fallback = invocation.getInvokable().getAnnotation(Fallback.class); if (fallback != null) { return injector.getInstance(fallback.value()); } return defaultFallback; }
@Override public String getCommandName(Invocation invocation) { Invokable<?, ?> invoked = invocation.getInvokable(); if (invoked.isAnnotationPresent(Named.class)) { return invoked.getAnnotation(Named.class).value(); } else { // TODO: remove old logic once Named annotations are on all methods String className = invoked.getOwnerType().getRawType().getSimpleName().replace("AsyncClient", "Client") .replace("AsyncApi", "Api"); return className + "." + invoked.getName(); } }
private void overridePathEncoding(UriBuilder uriBuilder, Invocation invocation) { if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(SkipEncoding.class)) { uriBuilder.skipPathEncoding(Chars.asList(invocation.getInvokable().getOwnerType().getRawType() .getAnnotation(SkipEncoding.class).value())); } if (invocation.getInvokable().isAnnotationPresent(SkipEncoding.class)) { uriBuilder.skipPathEncoding(Chars.asList(invocation.getInvokable().getAnnotation(SkipEncoding.class).value())); } }
private Multimap<String, Object> getFormParamKeyValues(Invocation invocation) { Multimap<String, Object> formParamValues = LinkedHashMultimap.create(); for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), FormParam.class)) { FormParam formParam = param.getAnnotation(FormParam.class); String paramKey = formParam.value(); Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); if (paramValue.isPresent()) formParamValues.put(paramKey, paramValue.get().toString()); } return formParamValues; }
private Multimap<String, Object> getFormParamKeyValues(Invocation invocation) { Multimap<String, Object> formParamValues = LinkedHashMultimap.create(); for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), FormParam.class)) { FormParam formParam = param.getAnnotation(FormParam.class); String paramKey = formParam.value(); Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); if (paramValue.isPresent()) formParamValues.put(paramKey, paramValue.get().toString()); } return formParamValues; }
private Multimap<String, Object> getPathParamKeyValues(Invocation invocation) { Multimap<String, Object> pathParamValues = LinkedHashMultimap.create(); for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PathParam.class)) { PathParam pathParam = param.getAnnotation(PathParam.class); String paramKey = pathParam.value(); Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); if (paramValue.isPresent()) pathParamValues.put(paramKey, paramValue.get().toString()); } return pathParamValues; }
private Multimap<String, Object> getFormParamKeyValues(Invocation invocation) { Multimap<String, Object> formParamValues = LinkedHashMultimap.create(); for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), FormParam.class)) { FormParam formParam = param.getAnnotation(FormParam.class); String paramKey = formParam.value(); Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); if (paramValue.isPresent()) formParamValues.put(paramKey, paramValue.get().toString()); } return formParamValues; }
private Map<String, Object> buildPayloadParams(Invocation invocation) { Map<String, Object> payloadParamValues = Maps.newLinkedHashMap(); for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PayloadParam.class)) { PayloadParam payloadParam = param.getAnnotation(PayloadParam.class); String paramKey = payloadParam.value(); Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); if (paramValue.isPresent()) payloadParamValues.put(paramKey, paramValue.get()); } return payloadParamValues; }
private Map<String, Object> buildPayloadParams(Invocation invocation) { Map<String, Object> payloadParamValues = Maps.newLinkedHashMap(); for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PayloadParam.class)) { PayloadParam payloadParam = param.getAnnotation(PayloadParam.class); String paramKey = payloadParam.value(); Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); if (paramValue.isPresent()) payloadParamValues.put(paramKey, paramValue.get()); } return payloadParamValues; }
private Map<String, Object> buildPayloadParams(Invocation invocation) { Map<String, Object> payloadParamValues = Maps.newLinkedHashMap(); for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PayloadParam.class)) { PayloadParam payloadParam = param.getAnnotation(PayloadParam.class); String paramKey = payloadParam.value(); Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); if (paramValue.isPresent()) payloadParamValues.put(paramKey, paramValue.get()); } return payloadParamValues; }
@Override public Object apply(Invocation in) { if (isFuture(in.getInvokable())) { return submit(in); } Invocation async = toAsync(in); Optional<Long> timeoutNanos = config.getTimeoutNanos(async); if (timeoutNanos.isPresent()) { return invokeWithTimeout(async, timeoutNanos.get()); } return invoke(async); }
protected GeneratedHttpRequest requestForArgs(List<Object> args) { return GeneratedHttpRequest.builder().method("POST").endpoint("http://localhost/key") .invocation(Invocation.create(toString.getInvokable(), args)).build(); } }