Search in sources :

Example 1 with RestClientDefinitionException

use of org.eclipse.microprofile.rest.client.RestClientDefinitionException in project wildfly-swarm by wildfly-swarm.

the class BuilderImpl method verifyInterface.

private <T> void verifyInterface(Class<T> typeDef) {
    Method[] methods = typeDef.getMethods();
    // multiple verbs
    for (Method method : methods) {
        boolean hasHttpMethod = false;
        for (Annotation annotation : method.getAnnotations()) {
            boolean isHttpMethod = (annotation.annotationType().getAnnotation(HttpMethod.class) != null);
            if (!hasHttpMethod && isHttpMethod) {
                hasHttpMethod = true;
            } else if (hasHttpMethod && isHttpMethod) {
                throw new RestClientDefinitionException("Ambiguous @Httpmethod defintion on type " + typeDef);
            }
        }
    }
    // invalid parameter
    Path classPathAnno = typeDef.getAnnotation(Path.class);
    final Set<String> classLevelVariables = new HashSet<>();
    ResteasyUriBuilder classTemplate = null;
    if (classPathAnno != null) {
        classTemplate = (ResteasyUriBuilder) UriBuilder.fromUri(classPathAnno.value());
        classLevelVariables.addAll(classTemplate.getPathParamNamesInDeclarationOrder());
    }
    ResteasyUriBuilder template;
    for (Method method : methods) {
        Path methodPathAnno = method.getAnnotation(Path.class);
        if (methodPathAnno != null) {
            template = classPathAnno == null ? (ResteasyUriBuilder) UriBuilder.fromUri(methodPathAnno.value()) : (ResteasyUriBuilder) UriBuilder.fromUri(classPathAnno.value() + "/" + methodPathAnno.value());
        } else {
            template = classTemplate;
        }
        if (template == null) {
            continue;
        }
        // it's not executed, so this can be anything - but a hostname needs to present
        template.host("localhost");
        Set<String> allVariables = new HashSet<>(template.getPathParamNamesInDeclarationOrder());
        Map<String, Object> paramMap = new HashMap<>();
        for (Parameter p : method.getParameters()) {
            PathParam pathParam = p.getAnnotation(PathParam.class);
            if (pathParam != null) {
                paramMap.put(pathParam.value(), "foobar");
            }
        }
        if (allVariables.size() != paramMap.size()) {
            throw new RestClientDefinitionException("Parameters and variables don't match on " + typeDef + "::" + method.getName());
        }
        try {
            template.resolveTemplates(paramMap, false).build();
        } catch (IllegalArgumentException ex) {
            throw new RestClientDefinitionException("Parameter names don't match variable names on " + typeDef + "::" + method.getName(), ex);
        }
    }
}
Also used : Path(javax.ws.rs.Path) HashMap(java.util.HashMap) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method) RestClientDefinitionException(org.eclipse.microprofile.rest.client.RestClientDefinitionException) Annotation(java.lang.annotation.Annotation) ResteasyUriBuilder(org.jboss.resteasy.specimpl.ResteasyUriBuilder) Parameter(java.lang.reflect.Parameter) PathParam(javax.ws.rs.PathParam) HashSet(java.util.HashSet)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 HttpMethod (javax.ws.rs.HttpMethod)1 Path (javax.ws.rs.Path)1 PathParam (javax.ws.rs.PathParam)1 RestClientDefinitionException (org.eclipse.microprofile.rest.client.RestClientDefinitionException)1 ResteasyUriBuilder (org.jboss.resteasy.specimpl.ResteasyUriBuilder)1