Refine search
SentinelInvocationHandler(Target<?> target, Map<Method, MethodHandler> dispatch) { this.target = checkNotNull(target, "target"); this.dispatch = checkNotNull(dispatch, "dispatch"); }
private void parseConsumes(MethodMetadata md, Method method, RequestMapping annotation) { String[] serverConsumes = annotation.consumes(); String clientProduces = serverConsumes.length == 0 ? null : emptyToNull(serverConsumes[0]); if (clientProduces != null) { md.template().header(CONTENT_TYPE, clientProduces); } }
@Override public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) { int parameterIndex = context.getParameterIndex(); Class<?> parameterType = method.getParameterTypes()[parameterIndex]; MethodMetadata data = context.getMethodMetadata(); if (Map.class.isAssignableFrom(parameterType)) { checkState(data.headerMapIndex() == null, "Header map can only be present once."); data.headerMapIndex(parameterIndex); return true; } String name = ANNOTATION.cast(annotation).value(); checkState(emptyToNull(name) != null, "RequestHeader.value() was empty on parameter %s", parameterIndex); context.setParameterName(name); Collection<String> header = context.setTemplateParameter(name, data.template().headers().get(name)); data.template().header(name, header); return true; } }
private void handleProducesAnnotation(MethodMetadata data, Produces produces, String name) { String[] serverProduces = produces.value(); String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]); checkState(clientAccepts != null, "Produces.value() was empty on %s", name); data.template().header(ACCEPT, (String) null); // remove any previous produces data.template().header(ACCEPT, clientAccepts); }
@Override public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) { String name = ANNOTATION.cast(annotation).value(); checkState(emptyToNull(name) != null, "PathVariable annotation was empty on param %s.", context.getParameterIndex()); context.setParameterName(name); MethodMetadata data = context.getMethodMetadata(); String varName = '{' + name + '}'; if (!data.template().url().contains(varName) && !searchMapValues(data.template().queries(), varName) && !searchMapValues(data.template().headers(), varName)) { data.formParams().add(name); } return true; }
public PublisherClientMethodHandler(Target target, MethodMetadata methodMetadata, PublisherHttpClient publisherClient) { this.target = checkNotNull(target, "target must be not null"); this.methodMetadata = checkNotNull(methodMetadata, "methodMetadata must be not null"); this.publisherClient = checkNotNull(publisherClient, "client must be not null"); this.pathExpander = buildExpandFunction(methodMetadata.template().url()); this.headerExpanders = buildExpanders(methodMetadata.template().headers()); this.queriesAll = new HashMap<>(methodMetadata.template().queries()); if (methodMetadata.formParams() != null) { methodMetadata.formParams() .forEach(param -> add(queriesAll, param, "{" + param + "}")); } this.queryExpanders = buildExpanders(queriesAll); }
@Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (bodyType.equals(InputStream.class)) { try { template.body(Util.toByteArray((InputStream) object), StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e); } } else { delegate.encode(object, bodyType, template); } } }
private ReactiveClientMethodHandler(Target target, MethodMetadata methodMetadata, ReactiveHttpClient reactiveClient) { this.target = checkNotNull(target, "target must be not null"); this.methodMetadata = checkNotNull(methodMetadata, "methodMetadata must be not null"); this.reactiveClient = checkNotNull(reactiveClient, "client must be not null"); this.defaultUriBuilderFactory = new DefaultUriBuilderFactory(target.url()); Stream<AbstractMap.SimpleImmutableEntry<String, String>> simpleImmutableEntryStream = methodMetadata .template().headers().entrySet().stream() .flatMap(e -> e.getValue().stream() .map(v -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(), v))); this.headerExpanders = simpleImmutableEntryStream.collect(groupingBy( entry -> entry.getKey(), mapping(entry -> buildExpandHeaderFunction(entry.getValue()), toList()))); }
public static Type getBodyActualType(Type bodyType) { return ofNullable(bodyType).map(type -> { if (type instanceof ParameterizedType) { Class<?> bodyClass = (Class<?>) ((ParameterizedType) type).getRawType(); if (Publisher.class.isAssignableFrom(bodyClass)) { return resolveLastTypeParameter(bodyType, bodyClass); } else { return type; } } else { return type; } }).orElse(null); }
public MockClient ok(RequestKey requestKey, InputStream responseBody) throws IOException { return ok(requestKey, Util.toByteArray(responseBody)); }
@Override protected void processAnnotationOnClass(MethodMetadata data, Class<?> targetType) { if (targetType.isAnnotationPresent(Headers.class)) { String[] headersOnType = targetType.getAnnotation(Headers.class).value(); checkState(headersOnType.length > 0, "Headers annotation was empty on type %s.", targetType.getName()); Map<String, Collection<String>> headers = toMap(headersOnType); headers.putAll(data.template().headers()); data.template().headers(null); // to clear data.template().headers(headers); } }
@Override public List<MethodMetadata> parseAndValidatateMetadata(final Class<?> targetType) { checkNotNull(targetType, "Argument targetType must be not null"); final List<MethodMetadata> metadatas = delegate.parseAndValidatateMetadata(targetType); for (final MethodMetadata metadata : metadatas) { final Type type = metadata.returnType(); if (type instanceof ParameterizedType && ((ParameterizedType) type).getRawType().equals(Future.class)) { final Type actualType = resolveLastTypeParameter(type, Future.class); metadata.returnType(actualType); } else { throw new IllegalStateException(String.format( "Method %s of contract %s doesn't returns io.vertx.core.Future", metadata.configKey(), targetType.getSimpleName())); } } return metadatas; } }
public HardCodedTarget(Class<T> type, String name, String url) { this.type = checkNotNull(type, "type"); this.name = checkNotNull(emptyToNull(name), "name"); this.url = checkNotNull(emptyToNull(url), "url"); }
private void checkOne(Method method, Object[] values, String fieldName) { checkState(values != null && values.length == 1, "Method %s can only contain 1 %s field. Found: %s", method.getName(), fieldName, values == null ? null : Arrays.asList(values)); }
@Override protected void processAnnotationOnClass(MethodMetadata data, Class<?> clz) { if (clz.getInterfaces().length == 0) { RequestMapping classAnnotation = findMergedAnnotation(clz, RequestMapping.class); if (classAnnotation != null) { // Prepend path from class annotation if specified if (classAnnotation.value().length > 0) { String pathValue = emptyToNull(classAnnotation.value()[0]); pathValue = resolve(pathValue); if (!pathValue.startsWith("/")) { pathValue = "/" + pathValue; } data.template().insert(0, pathValue); } } } }
@Override @SuppressWarnings("unchecked") public <T> T newInstance(final Target<T> target) { checkNotNull(target, "Argument target must be not null"); final Map<String, MethodHandler> nameToHandler = targetToHandlersByName.apply(target); final Map<Method, MethodHandler> methodToHandler = new HashMap<>(); final List<DefaultMethodHandler> defaultMethodHandlers = new ArrayList<>(); for (final Method method : target.type().getMethods()) { if (isDefault(method)) { final DefaultMethodHandler handler = new DefaultMethodHandler(method); defaultMethodHandlers.add(handler); methodToHandler.put(method, handler); } else { methodToHandler.put(method, nameToHandler.get(Feign.configKey(target.type(), method))); } } final InvocationHandler handler = factory.create(target, methodToHandler); final T proxy = (T) Proxy.newProxyInstance( target.type().getClassLoader(), new Class<?>[] { target.type() }, handler); for (final DefaultMethodHandler defaultMethodHandler : defaultMethodHandlers) { defaultMethodHandler.bindTo(proxy); } return proxy; }
public RequestTemplate method(String method) { this.method = checkNotNull(method, "method"); checkArgument(method.matches("^[A-Z]+$"), "Invalid HTTP Method: %s", method); return this; }
private RequestTemplate doQuery(boolean encoded, String name, String... values) { checkNotNull(name, "name"); String paramName = encoded ? name : encodeIfNotVariable(name); queries.remove(paramName); if (values != null && values.length > 0 && values[0] != null) { ArrayList<String> paramValues = new ArrayList<String>(); for (String value : values) { paramValues.add(encoded ? value : encodeIfNotVariable(value)); } this.queries.put(paramName, paramValues); } return this; }
@Override public Object decode(Response response, Type type) throws IOException { if (response.status() == 404) return Util.emptyValueOf(type); if (response.body() == null) return null; if (byte[].class.equals(type)) { return Util.toByteArray(response.body().asInputStream()); } return super.decode(response, type); } }
@Override public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) { checkState(targetType.getTypeParameters().length == 0, "Parameterized types unsupported: %s", targetType.getSimpleName()); checkState(targetType.getInterfaces().length <= 1, "Only single inheritance supported: %s", targetType.getSimpleName()); if (targetType.getInterfaces().length == 1) { checkState(targetType.getInterfaces()[0].getInterfaces().length == 0, "Only single-level inheritance supported: %s", targetType.getSimpleName()); } Map<String, MethodMetadata> result = new LinkedHashMap<String, MethodMetadata>(); for (Method method : targetType.getMethods()) { if (method.getDeclaringClass() == Object.class || (method.getModifiers() & Modifier.STATIC) != 0 || Util.isDefault(method)) { continue; } MethodMetadata metadata = parseAndValidateMetadata(targetType, method); checkState(!result.containsKey(metadata.configKey()), "Overrides unsupported: %s", metadata.configKey()); result.put(metadata.configKey(), metadata); } return new ArrayList<MethodMetadata>(result.values()); }