Search in sources :

Example 1 with ResteasyUriBuilder

use of org.jboss.resteasy.spi.ResteasyUriBuilder in project resteasy by resteasy.

the class ObjectLinksProvider method buildURI.

private URI buildURI(UriBuilder uriBuilder, LinkResource service, Object entity, Method m) {
    for (ParamBinding binding : service.queryParameters()) {
        uriBuilder.queryParam(binding.name(), evaluateEL(m, getELContext(m, entity), binding.value()));
    }
    for (ParamBinding binding : service.matrixParameters()) {
        uriBuilder.matrixParam(binding.name(), evaluateEL(m, getELContext(m, entity), binding.value()));
    }
    String[] uriTemplates = service.pathParameters();
    if (uriTemplates.length > 0) {
        Object[] values = new Object[uriTemplates.length];
        for (int i = 0; i < values.length; i++) {
            values[i] = evaluateEL(m, getELContext(m, entity), uriTemplates[i]);
        }
        return uriBuilder.build(values);
    }
    // do we need any path parameters?
    List<String> paramNames = ((ResteasyUriBuilder) uriBuilder).getPathParamNamesInDeclarationOrder();
    if (paramNames.isEmpty()) {
        return uriBuilder.build();
    }
    // try to find the IDs
    List<Object> params = findURIParamsFromResource(entity);
    if (params.size() == paramNames.size()) {
        return uriBuilder.build(params.toArray());
    }
    // if we have too many, ignore the last ones
    if (params.size() > paramNames.size()) {
        return uriBuilder.build(params.subList(0, paramNames.size()).toArray());
    }
    throw new ServiceDiscoveryException(m, Messages.MESSAGES.notEnoughtUriParameters(paramNames.size(), params.size()));
}
Also used : ResteasyUriBuilder(org.jboss.resteasy.spi.ResteasyUriBuilder) ParamBinding(org.jboss.resteasy.links.ParamBinding)

Example 2 with ResteasyUriBuilder

use of org.jboss.resteasy.spi.ResteasyUriBuilder in project resteasy by resteasy.

the class ClientWebTarget method queryParamsNoTemplate.

@Override
public ResteasyWebTarget queryParamsNoTemplate(MultivaluedMap<String, Object> parameters) throws IllegalArgumentException, NullPointerException {
    client.abortIfClosed();
    if (parameters == null)
        throw new NullPointerException(Messages.MESSAGES.parametersWasNull());
    ResteasyUriBuilder copy;
    if (uriBuilder instanceof ResteasyUriBuilder) {
        copy = (ResteasyUriBuilder) uriBuilder.clone();
    } else {
        copy = ResteasyUriBuilder.fromTemplate(uriBuilder.toTemplate());
    }
    for (Map.Entry<String, List<Object>> entry : parameters.entrySet()) {
        String[] stringValues = toStringValues(entry.getValue().toArray());
        for (String val : stringValues) {
            copy.clientQueryParam(entry.getKey(), val);
        }
    }
    return newInstance(client, copy, configuration);
}
Also used : ResteasyUriBuilder(org.jboss.resteasy.spi.ResteasyUriBuilder) List(java.util.List) HashMap(java.util.HashMap) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 3 with ResteasyUriBuilder

use of org.jboss.resteasy.spi.ResteasyUriBuilder in project resteasy by resteasy.

the class ResteasyUriInfo method initialize.

protected void initialize(CharSequence absoluteUri, String queryString, String contextPath) {
    ResteasyUriBuilder absoluteBuilder = (ResteasyUriBuilder) ((ResteasyUriBuilder) RuntimeDelegate.getInstance().createUriBuilder()).uriFromCharSequence((CharSequence) absoluteUri);
    absolutePath = absoluteBuilder.build();
    requestURI = absoluteBuilder.replaceQuery(queryString).build();
    encodedPath = PathHelper.getEncodedPathInfo(absolutePath.getRawPath(), contextPath);
    baseURI = absolutePath;
    if (!encodedPath.trim().equals("")) {
        String tmpContextPath = contextPath;
        if (!tmpContextPath.endsWith("/"))
            tmpContextPath += "/";
        baseURI = absoluteBuilder.clone().replacePath(tmpContextPath).replaceQuery(null).build();
    }
    // make sure path starts with '/'
    if (encodedPath.length() == 0 || encodedPath.charAt(0) != '/') {
        encodedPath = "/" + encodedPath;
    }
    path = UriBuilder.fromPath(encodedPath).build().getPath();
    processPath();
}
Also used : ResteasyUriBuilder(org.jboss.resteasy.spi.ResteasyUriBuilder)

Example 4 with ResteasyUriBuilder

use of org.jboss.resteasy.spi.ResteasyUriBuilder in project resteasy by resteasy.

the class ResourceMethodRegistry method removeRegistration.

private void removeRegistration(String base, Class<?> clazz) {
    for (Method method : clazz.getMethods()) {
        Path path = method.getAnnotation(Path.class);
        Set<String> httpMethods = IsHttpMethod.getHttpMethods(method);
        if (path == null && httpMethods == null)
            continue;
        ResteasyUriBuilder builder = new ResteasyUriBuilderImpl();
        if (base != null)
            builder.path(base);
        if (clazz.isAnnotationPresent(Path.class))
            builder.path(clazz);
        String classExpression = builder.getPath();
        if (path != null)
            builder.path(method);
        String fullpath = builder.getPath();
        if (fullpath == null)
            fullpath = "";
        if (widerMatching)
            rootNode.removeBinding(fullpath, method);
        else
            root.removeBinding(classExpression, fullpath, method);
    }
}
Also used : Path(jakarta.ws.rs.Path) ResteasyUriBuilder(org.jboss.resteasy.spi.ResteasyUriBuilder) ResteasyUriBuilderImpl(org.jboss.resteasy.specimpl.ResteasyUriBuilderImpl) IsHttpMethod(org.jboss.resteasy.util.IsHttpMethod) ResourceMethod(org.jboss.resteasy.spi.metadata.ResourceMethod) Method(java.lang.reflect.Method)

Example 5 with ResteasyUriBuilder

use of org.jboss.resteasy.spi.ResteasyUriBuilder in project quarkus by quarkusio.

the class QuarkusRestClientBuilder 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 definition on type " + typeDef);
            }
        }
    }
    // invalid parameter
    Path classPathAnno = typeDef.getAnnotation(Path.class);
    ResteasyUriBuilder template = null;
    for (Method method : methods) {
        Path methodPathAnno = method.getAnnotation(Path.class);
        if (methodPathAnno != null) {
            template = classPathAnno == null ? (ResteasyUriBuilder) new ResteasyUriBuilderImpl().uri(methodPathAnno.value()) : (ResteasyUriBuilder) new ResteasyUriBuilderImpl().uri(classPathAnno.value() + "/" + methodPathAnno.value());
        } else if (classPathAnno != null) {
            template = (ResteasyUriBuilder) new ResteasyUriBuilderImpl().uri(classPathAnno.value());
        } else {
            template = null;
        }
        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");
            } else if (p.isAnnotationPresent(org.jboss.resteasy.annotations.jaxrs.PathParam.class)) {
                org.jboss.resteasy.annotations.jaxrs.PathParam rePathParam = p.getAnnotation(org.jboss.resteasy.annotations.jaxrs.PathParam.class);
                String name = rePathParam.value() == null || rePathParam.value().length() == 0 ? p.getName() : rePathParam.value();
                paramMap.put(name, "foobar");
            } else if (p.isAnnotationPresent(BeanParam.class)) {
                verifyBeanPathParam(p.getType(), paramMap);
            }
        }
        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(jakarta.ws.rs.Path) ResteasyUriBuilderImpl(org.jboss.resteasy.specimpl.ResteasyUriBuilderImpl) HashMap(java.util.HashMap) HttpMethod(jakarta.ws.rs.HttpMethod) Method(java.lang.reflect.Method) RestClientDefinitionException(org.eclipse.microprofile.rest.client.RestClientDefinitionException) Annotation(java.lang.annotation.Annotation) ResteasyUriBuilder(org.jboss.resteasy.spi.ResteasyUriBuilder) Parameter(java.lang.reflect.Parameter) PathParam(jakarta.ws.rs.PathParam) HashSet(java.util.HashSet)

Aggregations

ResteasyUriBuilder (org.jboss.resteasy.spi.ResteasyUriBuilder)13 ResteasyUriBuilderImpl (org.jboss.resteasy.specimpl.ResteasyUriBuilderImpl)5 UriBuilder (jakarta.ws.rs.core.UriBuilder)3 Method (java.lang.reflect.Method)3 URI (java.net.URI)3 HashMap (java.util.HashMap)3 POST (jakarta.ws.rs.POST)2 Path (jakarta.ws.rs.Path)2 Map (java.util.Map)2 ResourceMethod (org.jboss.resteasy.spi.metadata.ResourceMethod)2 HttpMethod (jakarta.ws.rs.HttpMethod)1 PathParam (jakarta.ws.rs.PathParam)1 MultivaluedMap (jakarta.ws.rs.core.MultivaluedMap)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Annotation (java.lang.annotation.Annotation)1 Parameter (java.lang.reflect.Parameter)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 RestClientDefinitionException (org.eclipse.microprofile.rest.client.RestClientDefinitionException)1