Search in sources :

Example 1 with LinkParameter

use of org.eclipse.che.api.core.rest.shared.dto.LinkParameter 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 LinkParameter

use of org.eclipse.che.api.core.rest.shared.dto.LinkParameter in project che by eclipse.

the class ServiceDescriptorTest method testLinkParameters.

@Test
public void testLinkParameters() throws Exception {
    Link link = getLink("echo");
    List<LinkParameter> parameters = link.getParameters();
    Assert.assertEquals(parameters.size(), 1);
    LinkParameter linkParameter = parameters.get(0);
    Assert.assertEquals(linkParameter.getDefaultValue(), "a");
    Assert.assertEquals(linkParameter.getDescription(), "some text");
    Assert.assertEquals(linkParameter.getName(), "text");
    Assert.assertEquals(linkParameter.getType(), ParameterType.String);
    Assert.assertTrue(linkParameter.isRequired());
    List<String> valid = linkParameter.getValid();
    Assert.assertEquals(valid.size(), 2);
    Assert.assertTrue(valid.contains("a"));
    Assert.assertTrue(valid.contains("b"));
}
Also used : LinkParameter(org.eclipse.che.api.core.rest.shared.dto.LinkParameter) Link(org.eclipse.che.api.core.rest.shared.dto.Link) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with LinkParameter

use of org.eclipse.che.api.core.rest.shared.dto.LinkParameter in project che by eclipse.

the class WorkspaceEventsHandler method subscribeOnEnvironmentOutputChannel.

private void subscribeOnEnvironmentOutputChannel(WorkspaceDto workspace) {
    if (environmentOutputSubscriptionHandler != null) {
        return;
    }
    final Link link = workspace.getLink(LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL);
    final LinkParameter logsChannelLinkParameter = link.getParameter("channel");
    if (logsChannelLinkParameter == null) {
        return;
    }
    environmentOutputChannel = logsChannelLinkParameter.getDefaultValue();
    environmentOutputSubscriptionHandler = new EnvironmentOutputSubscriptionHandler();
    subscribeToChannel(environmentOutputChannel, environmentOutputSubscriptionHandler);
}
Also used : LinkParameter(org.eclipse.che.api.core.rest.shared.dto.LinkParameter) Link(org.eclipse.che.api.core.rest.shared.dto.Link)

Example 4 with LinkParameter

use of org.eclipse.che.api.core.rest.shared.dto.LinkParameter in project che by eclipse.

the class WorkspaceEventsHandler method subscribeOnEnvironmentStatusChannel.

private void subscribeOnEnvironmentStatusChannel(WorkspaceDto workspace) {
    if (environmentStatusSubscriptionHandler != null) {
        return;
    }
    final Link link = workspace.getLink(LINK_REL_ENVIRONMENT_STATUS_CHANNEL);
    final LinkParameter statusChannelLinkParameter = link.getParameter("channel");
    if (statusChannelLinkParameter == null) {
        return;
    }
    environmentStatusChannel = statusChannelLinkParameter.getDefaultValue();
    environmentStatusSubscriptionHandler = new EnvironmentStatusSubscriptionHandler();
    subscribeToChannel(environmentStatusChannel, environmentStatusSubscriptionHandler);
}
Also used : LinkParameter(org.eclipse.che.api.core.rest.shared.dto.LinkParameter) Link(org.eclipse.che.api.core.rest.shared.dto.Link)

Example 5 with LinkParameter

use of org.eclipse.che.api.core.rest.shared.dto.LinkParameter in project che by eclipse.

the class WorkspaceServiceLinksInjector method injectLinks.

public WorkspaceDto injectLinks(WorkspaceDto workspace, ServiceContext serviceContext) {
    final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder();
    final List<Link> links = new ArrayList<>();
    // add common workspace links
    links.add(createLink("GET", uriBuilder.clone().path(WorkspaceService.class, "getByKey").build(workspace.getId()).toString(), LINK_REL_SELF));
    links.add(createLink("POST", uriBuilder.clone().path(WorkspaceService.class, "startById").build(workspace.getId()).toString(), APPLICATION_JSON, LINK_REL_START_WORKSPACE));
    links.add(createLink("DELETE", uriBuilder.clone().path(WorkspaceService.class, "delete").build(workspace.getId()).toString(), APPLICATION_JSON, LINK_REL_REMOVE_WORKSPACE));
    links.add(createLink("GET", uriBuilder.clone().path(WorkspaceService.class, "getWorkspaces").build().toString(), APPLICATION_JSON, GET_ALL_USER_WORKSPACES));
    links.add(createLink("GET", uriBuilder.clone().path(WorkspaceService.class, "getSnapshot").build(workspace.getId()).toString(), APPLICATION_JSON, LINK_REL_GET_SNAPSHOT));
    //TODO here we add url to IDE with workspace name not good solution do it here but critical for this task  https://jira.codenvycorp.com/browse/IDEX-3619
    final URI ideUri = uriBuilder.clone().replacePath("").path(workspace.getNamespace()).path(workspace.getConfig().getName()).build();
    links.add(createLink("GET", ideUri.toString(), TEXT_HTML, LINK_REL_IDE_URL));
    // add workspace channel links
    final Link workspaceChannelLink = createLink("GET", serviceContext.getBaseUriBuilder().path("ws").scheme("https".equals(ideUri.getScheme()) ? "wss" : "ws").build().toString(), null);
    final LinkParameter channelParameter = newDto(LinkParameter.class).withName("channel").withRequired(true);
    links.add(cloneDto(workspaceChannelLink).withRel(LINK_REL_GET_WORKSPACE_EVENTS_CHANNEL).withParameters(singletonList(cloneDto(channelParameter).withDefaultValue("workspace:" + workspace.getId()))));
    links.add(cloneDto(workspaceChannelLink).withRel(LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL).withParameters(singletonList(cloneDto(channelParameter).withDefaultValue(format(ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE, workspace.getId())))));
    links.add(cloneDto(workspaceChannelLink).withRel(LINK_REL_ENVIRONMENT_STATUS_CHANNEL).withParameters(singletonList(cloneDto(channelParameter).withDefaultValue(format(ENVIRONMENT_STATUS_CHANNEL_TEMPLATE, workspace.getId())))));
    // add links for running workspace
    injectRuntimeLinks(workspace, ideUri, uriBuilder, serviceContext);
    return workspace.withLinks(links);
}
Also used : LinkParameter(org.eclipse.che.api.core.rest.shared.dto.LinkParameter) ArrayList(java.util.ArrayList) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) Link(org.eclipse.che.api.core.rest.shared.dto.Link) LinksHelper.createLink(org.eclipse.che.api.core.util.LinksHelper.createLink)

Aggregations

Link (org.eclipse.che.api.core.rest.shared.dto.Link)6 LinkParameter (org.eclipse.che.api.core.rest.shared.dto.LinkParameter)6 ArrayList (java.util.ArrayList)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 GenerateLink (org.eclipse.che.api.core.rest.annotations.GenerateLink)2 LinksHelper.createLink (org.eclipse.che.api.core.util.LinksHelper.createLink)2 Annotation (java.lang.annotation.Annotation)1 URI (java.net.URI)1 LinkedList (java.util.LinkedList)1 Consumes (javax.ws.rs.Consumes)1 DefaultValue (javax.ws.rs.DefaultValue)1 HttpMethod (javax.ws.rs.HttpMethod)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 QueryParam (javax.ws.rs.QueryParam)1 Description (org.eclipse.che.api.core.rest.annotations.Description)1 Required (org.eclipse.che.api.core.rest.annotations.Required)1 Valid (org.eclipse.che.api.core.rest.annotations.Valid)1 RequestBodyDescriptor (org.eclipse.che.api.core.rest.shared.dto.RequestBodyDescriptor)1 BeforeTest (org.testng.annotations.BeforeTest)1