Search in sources :

Example 1 with QueryParam

use of javax.ws.rs.QueryParam in project che by eclipse.

the class Service method generateLinkForMethod.

private Link generateLinkForMethod(UriInfo uriInfo, String linkRel, Method method, Object... pathParameters) {
    String httpMethod = null;
    final HttpMethod httpMethodAnnotation = getMetaAnnotation(method, HttpMethod.class);
    if (httpMethodAnnotation != null) {
        httpMethod = httpMethodAnnotation.value();
    }
    if (httpMethod == null) {
        throw new IllegalArgumentException(format("Method '%s' has not any HTTP method annotation and may not be used to produce link.", method.getName()));
    }
    final Consumes consumes = getAnnotation(method, Consumes.class);
    final Produces produces = getAnnotation(method, Produces.class);
    final UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
    final LinkedList<String> matchedURIs = new LinkedList<>(uriInfo.getMatchedURIs());
    // Get path to the root resource.
    if (uriInfo.getMatchedResources().size() < matchedURIs.size()) {
        matchedURIs.remove();
    }
    while (!matchedURIs.isEmpty()) {
        baseUriBuilder.path(matchedURIs.pollLast());
    }
    final Path path = method.getAnnotation(Path.class);
    if (path != null) {
        baseUriBuilder.path(path.value());
    }
    final Link link = DtoFactory.getInstance().createDto(Link.class).withRel(linkRel).withHref(baseUriBuilder.build(pathParameters).toString()).withMethod(httpMethod);
    if (consumes != null) {
        link.setConsumes(consumes.value()[0]);
    }
    if (produces != null) {
        link.setProduces(produces.value()[0]);
    }
    Class<?>[] parameterClasses = method.getParameterTypes();
    if (parameterClasses.length > 0) {
        Annotation[][] annotations = method.getParameterAnnotations();
        for (int i = 0; i < parameterClasses.length; i++) {
            if (annotations[i].length > 0) {
                boolean isBodyParameter = false;
                QueryParam queryParam = null;
                Description description = null;
                Required required = null;
                Valid valid = null;
                DefaultValue defaultValue = null;
                for (int j = 0; j < annotations[i].length; j++) {
                    Annotation annotation = annotations[i][j];
                    isBodyParameter |= !JAX_RS_ANNOTATIONS.contains(annotation.annotationType().getName());
                    Class<?> annotationType = annotation.annotationType();
                    if (annotationType == QueryParam.class) {
                        queryParam = (QueryParam) annotation;
                    } else if (annotationType == Description.class) {
                        description = (Description) annotation;
                    } else if (annotationType == Required.class) {
                        required = (Required) annotation;
                    } else if (annotationType == Valid.class) {
                        valid = (Valid) annotation;
                    } else if (annotationType == DefaultValue.class) {
                        defaultValue = (DefaultValue) annotation;
                    }
                }
                if (queryParam != null) {
                    LinkParameter parameter = DtoFactory.getInstance().createDto(LinkParameter.class).withName(queryParam.value()).withRequired(required != null).withType(getParameterType(parameterClasses[i]));
                    if (defaultValue != null) {
                        parameter.setDefaultValue(defaultValue.value());
                    }
                    if (description != null) {
                        parameter.setDescription(description.value());
                    }
                    if (valid != null) {
                        parameter.setValid(Arrays.asList(valid.value()));
                    }
                    link.getParameters().add(parameter);
                } else if (isBodyParameter) {
                    if (description != null) {
                        link.setRequestBody(DtoFactory.getInstance().createDto(RequestBodyDescriptor.class).withDescription(description.value()));
                    }
                }
            }
        }
    }
    return link;
}
Also used : Path(javax.ws.rs.Path) RequestBodyDescriptor(org.eclipse.che.api.core.rest.shared.dto.RequestBodyDescriptor) Description(org.eclipse.che.api.core.rest.annotations.Description) LinkParameter(org.eclipse.che.api.core.rest.shared.dto.LinkParameter) LinkedList(java.util.LinkedList) Annotation(java.lang.annotation.Annotation) DefaultValue(javax.ws.rs.DefaultValue) Required(org.eclipse.che.api.core.rest.annotations.Required) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) QueryParam(javax.ws.rs.QueryParam) Valid(org.eclipse.che.api.core.rest.annotations.Valid) UriBuilder(javax.ws.rs.core.UriBuilder) HttpMethod(javax.ws.rs.HttpMethod) Link(org.eclipse.che.api.core.rest.shared.dto.Link) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink)

Example 2 with QueryParam

use of javax.ws.rs.QueryParam in project che by eclipse.

the class MavenServerService method reconcilePom.

@GET
@Path("pom/reconcile")
@ApiOperation(value = "Reconcile pom.xml file")
@ApiResponses({ @ApiResponse(code = 200, message = "OK") })
@Produces("application/json")
public List<Problem> reconcilePom(@ApiParam(value = "The paths to pom.xml file which need to be reconciled") @QueryParam("pompath") String pomPath) {
    VirtualFileEntry entry = null;
    List<Problem> result = new ArrayList<>();
    try {
        entry = cheProjectManager.getProjectsRoot().getChild(pomPath);
        if (entry == null) {
            return result;
        }
        Model.readFrom(entry.getVirtualFile());
        org.eclipse.che.api.vfs.Path path = entry.getPath();
        String pomContent = entry.getVirtualFile().getContentAsString();
        MavenProject mavenProject = mavenProjectManager.findMavenProject(ResourcesPlugin.getWorkspace().getRoot().getProject(path.getParent().toString()));
        if (mavenProject == null) {
            return result;
        }
        List<MavenProjectProblem> problems = mavenProject.getProblems();
        int start = pomContent.indexOf("<project ") + 1;
        int end = start + "<project ".length();
        List<Problem> problemList = problems.stream().map(mavenProjectProblem -> DtoFactory.newDto(Problem.class).withError(true).withSourceStart(start).withSourceEnd(end).withMessage(mavenProjectProblem.getDescription())).collect(Collectors.toList());
        result.addAll(problemList);
    } catch (ServerException | ForbiddenException | IOException e) {
        LOG.error(e.getMessage(), e);
    } catch (XMLTreeException exception) {
        Throwable cause = exception.getCause();
        if (cause != null && cause instanceof SAXParseException) {
            result.add(createProblem(entry, (SAXParseException) cause));
        }
    }
    return result;
}
Also used : MavenWrapperManager(org.eclipse.che.plugin.maven.server.MavenWrapperManager) EclipseWorkspaceProvider(org.eclipse.che.plugin.maven.server.core.EclipseWorkspaceProvider) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ProjectRegistry(org.eclipse.che.api.project.server.ProjectRegistry) Inject(com.google.inject.Inject) VirtualFileEntry(org.eclipse.che.api.project.server.VirtualFileEntry) XMLTreeException(org.eclipse.che.commons.xml.XMLTreeException) MavenProjectProblem(org.eclipse.che.maven.data.MavenProjectProblem) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) MavenWorkspace(org.eclipse.che.plugin.maven.server.core.MavenWorkspace) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) ApiOperation(io.swagger.annotations.ApiOperation) Document(org.eclipse.jface.text.Document) MavenProgressNotifier(org.eclipse.che.plugin.maven.server.core.MavenProgressNotifier) QueryParam(javax.ws.rs.QueryParam) IProject(org.eclipse.core.resources.IProject) IWorkspace(org.eclipse.core.resources.IWorkspace) Model(org.eclipse.che.ide.maven.tools.Model) BadLocationException(org.eclipse.jface.text.BadLocationException) DtoFactory(org.eclipse.che.dto.server.DtoFactory) ClasspathManager(org.eclipse.che.plugin.maven.server.core.classpath.ClasspathManager) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) TEXT_XML(javax.ws.rs.core.MediaType.TEXT_XML) MavenServerWrapper(org.eclipse.che.plugin.maven.server.MavenServerWrapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) NotFoundException(org.eclipse.che.api.core.NotFoundException) SAXParseException(org.xml.sax.SAXParseException) List(java.util.List) ServerException(org.eclipse.che.api.core.ServerException) Response(javax.ws.rs.core.Response) MavenProjectManager(org.eclipse.che.plugin.maven.server.core.MavenProjectManager) ApiResponse(io.swagger.annotations.ApiResponse) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) RegisteredProject(org.eclipse.che.api.project.server.RegisteredProject) Problem(org.eclipse.che.ide.ext.java.shared.dto.Problem) MavenProject(org.eclipse.che.plugin.maven.server.core.project.MavenProject) ProjectManager(org.eclipse.che.api.project.server.ProjectManager) Collections(java.util.Collections) MavenTerminal(org.eclipse.che.maven.server.MavenTerminal) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ArrayList(java.util.ArrayList) VirtualFileEntry(org.eclipse.che.api.project.server.VirtualFileEntry) IOException(java.io.IOException) XMLTreeException(org.eclipse.che.commons.xml.XMLTreeException) MavenProject(org.eclipse.che.plugin.maven.server.core.project.MavenProject) MavenProjectProblem(org.eclipse.che.maven.data.MavenProjectProblem) SAXParseException(org.xml.sax.SAXParseException) MavenProjectProblem(org.eclipse.che.maven.data.MavenProjectProblem) Problem(org.eclipse.che.ide.ext.java.shared.dto.Problem) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with QueryParam

use of javax.ws.rs.QueryParam in project che by eclipse.

the class MavenServerService method reimportDependencies.

@POST
@Path("reimport")
@ApiOperation(value = "Re-import maven model")
@ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error") })
public Response reimportDependencies(@ApiParam(value = "The paths to projects which need to be reimported") @QueryParam("projectPath") List<String> paths) throws ServerException {
    IWorkspace workspace = eclipseWorkspaceProvider.get();
    List<IProject> projectsList = paths.stream().map(projectPath -> workspace.getRoot().getProject(projectPath)).collect(Collectors.toList());
    mavenWorkspace.update(projectsList);
    return Response.ok().build();
}
Also used : MavenWrapperManager(org.eclipse.che.plugin.maven.server.MavenWrapperManager) EclipseWorkspaceProvider(org.eclipse.che.plugin.maven.server.core.EclipseWorkspaceProvider) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ProjectRegistry(org.eclipse.che.api.project.server.ProjectRegistry) Inject(com.google.inject.Inject) VirtualFileEntry(org.eclipse.che.api.project.server.VirtualFileEntry) XMLTreeException(org.eclipse.che.commons.xml.XMLTreeException) MavenProjectProblem(org.eclipse.che.maven.data.MavenProjectProblem) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) MavenWorkspace(org.eclipse.che.plugin.maven.server.core.MavenWorkspace) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) ApiOperation(io.swagger.annotations.ApiOperation) Document(org.eclipse.jface.text.Document) MavenProgressNotifier(org.eclipse.che.plugin.maven.server.core.MavenProgressNotifier) QueryParam(javax.ws.rs.QueryParam) IProject(org.eclipse.core.resources.IProject) IWorkspace(org.eclipse.core.resources.IWorkspace) Model(org.eclipse.che.ide.maven.tools.Model) BadLocationException(org.eclipse.jface.text.BadLocationException) DtoFactory(org.eclipse.che.dto.server.DtoFactory) ClasspathManager(org.eclipse.che.plugin.maven.server.core.classpath.ClasspathManager) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) TEXT_XML(javax.ws.rs.core.MediaType.TEXT_XML) MavenServerWrapper(org.eclipse.che.plugin.maven.server.MavenServerWrapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) NotFoundException(org.eclipse.che.api.core.NotFoundException) SAXParseException(org.xml.sax.SAXParseException) List(java.util.List) ServerException(org.eclipse.che.api.core.ServerException) Response(javax.ws.rs.core.Response) MavenProjectManager(org.eclipse.che.plugin.maven.server.core.MavenProjectManager) ApiResponse(io.swagger.annotations.ApiResponse) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) RegisteredProject(org.eclipse.che.api.project.server.RegisteredProject) Problem(org.eclipse.che.ide.ext.java.shared.dto.Problem) MavenProject(org.eclipse.che.plugin.maven.server.core.project.MavenProject) ProjectManager(org.eclipse.che.api.project.server.ProjectManager) Collections(java.util.Collections) MavenTerminal(org.eclipse.che.maven.server.MavenTerminal) IWorkspace(org.eclipse.core.resources.IWorkspace) IProject(org.eclipse.core.resources.IProject) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with QueryParam

use of javax.ws.rs.QueryParam in project che by eclipse.

the class FactoryService method getFactoryByAttribute.

@GET
@Path("/find")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get factory by attribute, " + "the attribute must match one of the Factory model fields with type 'String', " + "e.g. (factory.name, factory.creator.name)", notes = "If specify more than one value for a single query parameter then will be taken the first one")
@ApiResponses({ @ApiResponse(code = 200, message = "Response contains list requested factories"), @ApiResponse(code = 400, message = "When query does not contain at least one attribute to search for"), @ApiResponse(code = 500, message = "Internal server error") })
public List<FactoryDto> getFactoryByAttribute(@DefaultValue("0") @QueryParam("skipCount") Integer skipCount, @DefaultValue("30") @QueryParam("maxItems") Integer maxItems, @Context UriInfo uriInfo) throws BadRequestException, ServerException {
    final Set<String> skip = ImmutableSet.of("token", "skipCount", "maxItems");
    final List<Pair<String, String>> query = URLEncodedUtils.parse(uriInfo.getRequestUri()).entrySet().stream().filter(param -> !skip.contains(param.getKey()) && !param.getValue().isEmpty()).map(entry -> Pair.of(entry.getKey(), entry.getValue().iterator().next())).collect(toList());
    checkArgument(!query.isEmpty(), "Query must contain at least one attribute");
    final List<FactoryDto> factories = new ArrayList<>();
    for (Factory factory : factoryManager.getByAttribute(maxItems, skipCount, query)) {
        factories.add(injectLinks(asDto(factory), null));
    }
    return factories;
}
Also used : URLEncodedUtils(org.eclipse.che.commons.lang.URLEncodedUtils) Produces(javax.ws.rs.Produces) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) FactoryBuilder(org.eclipse.che.api.factory.server.builder.FactoryBuilder) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) ApiOperation(io.swagger.annotations.ApiOperation) QueryParam(javax.ws.rs.QueryParam) Service(org.eclipse.che.api.core.rest.Service) PreferenceManager(org.eclipse.che.api.user.server.PreferenceManager) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) NameGenerator(org.eclipse.che.commons.lang.NameGenerator) DELETE(javax.ws.rs.DELETE) WorkspaceManager(org.eclipse.che.api.workspace.server.WorkspaceManager) ImmutableSet(com.google.common.collect.ImmutableSet) Context(javax.ws.rs.core.Context) Predicate(java.util.function.Predicate) Set(java.util.Set) Pair(org.eclipse.che.commons.lang.Pair) AuthorDto(org.eclipse.che.api.factory.shared.dto.AuthorDto) List(java.util.List) BadRequestException(org.eclipse.che.api.core.BadRequestException) Response(javax.ws.rs.core.Response) ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) UriInfo(javax.ws.rs.core.UriInfo) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) FactoryLinksHelper.createLinks(org.eclipse.che.api.factory.server.FactoryLinksHelper.createLinks) PathParam(javax.ws.rs.PathParam) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GET(javax.ws.rs.GET) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ApiException(org.eclipse.che.api.core.ApiException) User(org.eclipse.che.api.core.model.user.User) ConflictException(org.eclipse.che.api.core.ConflictException) MULTIPART_FORM_DATA(javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA) Api(io.swagger.annotations.Api) DtoFactory(org.eclipse.che.dto.server.DtoFactory) CONTENT_DISPOSITION(javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) Iterator(java.util.Iterator) JsonSyntaxException(com.google.gson.JsonSyntaxException) FileItem(org.apache.commons.fileupload.FileItem) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) Factory(org.eclipse.che.api.core.model.factory.Factory) Collectors.toList(java.util.stream.Collectors.toList) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) ServerException(org.eclipse.che.api.core.ServerException) ApiResponse(io.swagger.annotations.ApiResponse) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ProjectConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl) PUT(javax.ws.rs.PUT) UserManager(org.eclipse.che.api.user.server.UserManager) Collections(java.util.Collections) InputStream(java.io.InputStream) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) ArrayList(java.util.ArrayList) LoggerFactory(org.slf4j.LoggerFactory) DtoFactory(org.eclipse.che.dto.server.DtoFactory) Factory(org.eclipse.che.api.core.model.factory.Factory) Pair(org.eclipse.che.commons.lang.Pair) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with QueryParam

use of javax.ws.rs.QueryParam in project jersey by jersey.

the class WebResourceFactory method invoke.

@Override
@SuppressWarnings("unchecked")
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    if (args == null && method.getName().equals("toString")) {
        return toString();
    }
    if (args == null && method.getName().equals("hashCode")) {
        //unique instance in the JVM, and no need to override
        return hashCode();
    }
    if (args != null && args.length == 1 && method.getName().equals("equals")) {
        //unique instance in the JVM, and no need to override
        return equals(args[0]);
    }
    // get the interface describing the resource
    final Class<?> proxyIfc = proxy.getClass().getInterfaces()[0];
    // response type
    final Class<?> responseType = method.getReturnType();
    // determine method name
    String httpMethod = getHttpMethodName(method);
    if (httpMethod == null) {
        for (final Annotation ann : method.getAnnotations()) {
            httpMethod = getHttpMethodName(ann.annotationType());
            if (httpMethod != null) {
                break;
            }
        }
    }
    // create a new UriBuilder appending the @Path attached to the method
    WebTarget newTarget = addPathFromAnnotation(method, target);
    if (httpMethod == null) {
        if (newTarget == target) {
            // no path annotation on the method -> fail
            throw new UnsupportedOperationException("Not a resource method.");
        } else if (!responseType.isInterface()) {
            // not interface - can't help here
            throw new UnsupportedOperationException("Return type not an interface");
        }
    }
    // process method params (build maps of (Path|Form|Cookie|Matrix|Header..)Params
    // and extract entity type
    final MultivaluedHashMap<String, Object> headers = new MultivaluedHashMap<String, Object>(this.headers);
    final LinkedList<Cookie> cookies = new LinkedList<>(this.cookies);
    final Form form = new Form();
    form.asMap().putAll(this.form.asMap());
    final Annotation[][] paramAnns = method.getParameterAnnotations();
    Object entity = null;
    Type entityType = null;
    for (int i = 0; i < paramAnns.length; i++) {
        final Map<Class, Annotation> anns = new HashMap<>();
        for (final Annotation ann : paramAnns[i]) {
            anns.put(ann.annotationType(), ann);
        }
        Annotation ann;
        Object value = args[i];
        if (!hasAnyParamAnnotation(anns)) {
            entityType = method.getGenericParameterTypes()[i];
            entity = value;
        } else {
            if (value == null && (ann = anns.get(DefaultValue.class)) != null) {
                value = ((DefaultValue) ann).value();
            }
            if (value != null) {
                if ((ann = anns.get(PathParam.class)) != null) {
                    newTarget = newTarget.resolveTemplate(((PathParam) ann).value(), value);
                } else if ((ann = anns.get((QueryParam.class))) != null) {
                    if (value instanceof Collection) {
                        newTarget = newTarget.queryParam(((QueryParam) ann).value(), convert((Collection) value));
                    } else {
                        newTarget = newTarget.queryParam(((QueryParam) ann).value(), value);
                    }
                } else if ((ann = anns.get((HeaderParam.class))) != null) {
                    if (value instanceof Collection) {
                        headers.addAll(((HeaderParam) ann).value(), convert((Collection) value));
                    } else {
                        headers.addAll(((HeaderParam) ann).value(), value);
                    }
                } else if ((ann = anns.get((CookieParam.class))) != null) {
                    final String name = ((CookieParam) ann).value();
                    Cookie c;
                    if (value instanceof Collection) {
                        for (final Object v : ((Collection) value)) {
                            if (!(v instanceof Cookie)) {
                                c = new Cookie(name, v.toString());
                            } else {
                                c = (Cookie) v;
                                if (!name.equals(((Cookie) v).getName())) {
                                    // is this the right thing to do? or should I fail? or ignore the difference?
                                    c = new Cookie(name, c.getValue(), c.getPath(), c.getDomain(), c.getVersion());
                                }
                            }
                            cookies.add(c);
                        }
                    } else {
                        if (!(value instanceof Cookie)) {
                            cookies.add(new Cookie(name, value.toString()));
                        } else {
                            c = (Cookie) value;
                            if (!name.equals(((Cookie) value).getName())) {
                                // is this the right thing to do? or should I fail? or ignore the difference?
                                cookies.add(new Cookie(name, c.getValue(), c.getPath(), c.getDomain(), c.getVersion()));
                            }
                        }
                    }
                } else if ((ann = anns.get((MatrixParam.class))) != null) {
                    if (value instanceof Collection) {
                        newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), convert((Collection) value));
                    } else {
                        newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), value);
                    }
                } else if ((ann = anns.get((FormParam.class))) != null) {
                    if (value instanceof Collection) {
                        for (final Object v : ((Collection) value)) {
                            form.param(((FormParam) ann).value(), v.toString());
                        }
                    } else {
                        form.param(((FormParam) ann).value(), value.toString());
                    }
                }
            }
        }
    }
    if (httpMethod == null) {
        // the method is a subresource locator
        return WebResourceFactory.newResource(responseType, newTarget, true, headers, cookies, form);
    }
    // accepted media types
    Produces produces = method.getAnnotation(Produces.class);
    if (produces == null) {
        produces = proxyIfc.getAnnotation(Produces.class);
    }
    final String[] accepts = (produces == null) ? EMPTY : produces.value();
    // determine content type
    String contentType = null;
    if (entity != null) {
        final List<Object> contentTypeEntries = headers.get(HttpHeaders.CONTENT_TYPE);
        if ((contentTypeEntries != null) && (!contentTypeEntries.isEmpty())) {
            contentType = contentTypeEntries.get(0).toString();
        } else {
            Consumes consumes = method.getAnnotation(Consumes.class);
            if (consumes == null) {
                consumes = proxyIfc.getAnnotation(Consumes.class);
            }
            if (consumes != null && consumes.value().length > 0) {
                contentType = consumes.value()[0];
            }
        }
    }
    Invocation.Builder builder = newTarget.request().headers(// this resets all headers so do this first
    headers).accept(// if @Produces is defined, propagate values into Accept header; empty array is NO-OP
    accepts);
    for (final Cookie c : cookies) {
        builder = builder.cookie(c);
    }
    final Object result;
    if (entity == null && !form.asMap().isEmpty()) {
        entity = form;
        contentType = MediaType.APPLICATION_FORM_URLENCODED;
    } else {
        if (contentType == null) {
            contentType = MediaType.APPLICATION_OCTET_STREAM;
        }
        if (!form.asMap().isEmpty()) {
            if (entity instanceof Form) {
                ((Form) entity).asMap().putAll(form.asMap());
            } else {
            // TODO: should at least log some warning here
            }
        }
    }
    final GenericType responseGenericType = new GenericType(method.getGenericReturnType());
    if (entity != null) {
        if (entityType instanceof ParameterizedType) {
            entity = new GenericEntity(entity, entityType);
        }
        result = builder.method(httpMethod, Entity.entity(entity, contentType), responseGenericType);
    } else {
        result = builder.method(httpMethod, responseGenericType);
    }
    return result;
}
Also used : MatrixParam(javax.ws.rs.MatrixParam) Invocation(javax.ws.rs.client.Invocation) Form(javax.ws.rs.core.Form) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) Consumes(javax.ws.rs.Consumes) PathParam(javax.ws.rs.PathParam) Cookie(javax.ws.rs.core.Cookie) GenericType(javax.ws.rs.core.GenericType) Annotation(java.lang.annotation.Annotation) LinkedList(java.util.LinkedList) CookieParam(javax.ws.rs.CookieParam) MediaType(javax.ws.rs.core.MediaType) GenericType(javax.ws.rs.core.GenericType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) QueryParam(javax.ws.rs.QueryParam) Produces(javax.ws.rs.Produces) GenericEntity(javax.ws.rs.core.GenericEntity) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) FormParam(javax.ws.rs.FormParam)

Aggregations

QueryParam (javax.ws.rs.QueryParam)19 PathParam (javax.ws.rs.PathParam)15 Path (javax.ws.rs.Path)12 GET (javax.ws.rs.GET)11 Produces (javax.ws.rs.Produces)11 Response (javax.ws.rs.core.Response)9 Logger (org.slf4j.Logger)9 LoggerFactory (org.slf4j.LoggerFactory)9 ApiParam (io.swagger.annotations.ApiParam)8 Annotation (java.lang.annotation.Annotation)8 POST (javax.ws.rs.POST)8 ApiOperation (io.swagger.annotations.ApiOperation)7 ApiResponse (io.swagger.annotations.ApiResponse)7 ApiResponses (io.swagger.annotations.ApiResponses)7 List (java.util.List)7 Inject (javax.inject.Inject)7 Consumes (javax.ws.rs.Consumes)7 DefaultValue (javax.ws.rs.DefaultValue)7 ArrayList (java.util.ArrayList)6 DELETE (javax.ws.rs.DELETE)6