Exemplo n.º 1
0
 @Test
 public void testLinkInfo() throws Exception {
   Link link = getLink("echo");
   Assert.assertEquals(link.getMethod(), HttpMethod.GET);
   Assert.assertEquals(link.getHref(), SERVICE_URI + "/my_method");
   Assert.assertEquals(link.getProduces(), MediaType.TEXT_PLAIN);
 }
Exemplo n.º 2
0
 /**
  * Find first link in the specified list by its relation.
  *
  * @param rel link's relation
  * @param links list of links
  * @return found link or {@code null}
  */
 public static Link getLink(String rel, List<Link> links) {
   for (Link link : links) {
     if (rel.equals(link.getRel())) {
       return link;
     }
   }
   return null;
 }
  /** {@inheritDoc} */
  @Override
  public void onLogsButtonClicked() {
    Link logUrl = selectedRunner.getLogUrl();
    if (logUrl == null) {
      return;
    }

    view.showLog(logUrl.getHref());
  }
Exemplo n.º 4
0
 /**
  * Find all links in the specified list by its relation.
  *
  * @param rel link's relation
  * @param links list of links
  * @return found link or {@code null}
  */
 public static List<Link> getLinks(String rel, List<Link> links) {
   final List<Link> result = new LinkedList<>();
   for (Link link : links) {
     if (rel.equals(link.getRel())) {
       result.add(link);
     }
   }
   return result;
 }
Exemplo n.º 5
0
 private Link getLink(String rel) throws Exception {
   List<Link> links = getDescriptor().getLinks();
   for (Link link : links) {
     if (link.getRel().equals(rel)) {
       return link;
     }
   }
   return null;
 }
Exemplo n.º 6
0
  @Test
  public void contentShouldBeReturned() throws Exception {
    Link link1 = mock(Link.class);
    Link link2 = mock(Link.class);

    when(data.getLinks()).thenReturn(Arrays.asList(link2, link1));
    when(link1.getRel()).thenReturn(DockerFile.GET_CONTENT);
    when(link1.getHref()).thenReturn(SOME_TEXT);
    when(link2.getRel()).thenReturn(SOME_TEXT);

    dockerFile.getContent(callback);

    verify(link1).getHref();
    verify(link2, never()).getHref();
  }
Exemplo n.º 7
0
 @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"));
 }
Exemplo n.º 8
0
  /**
   * Creates factory links.
   *
   * @param serviceContext the context to retrieve factory service base URI
   * @return list of factory links
   */
  public static List<Link> createLinks(
      FactoryDto factory, ServiceContext serviceContext, String userName) {
    final List<Link> links = new LinkedList<>();
    final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder();
    final String factoryId = factory.getId();
    if (factoryId != null) {
      // creation of link to retrieve factory
      links.add(
          createLink(
              HttpMethod.GET,
              uriBuilder
                  .clone()
                  .path(FactoryService.class, "getFactory")
                  .build(factoryId)
                  .toString(),
              null,
              APPLICATION_JSON,
              RETRIEVE_FACTORY_REL_ATT));

      // creation of snippet links
      links.addAll(
          SNIPPET_TYPES
              .stream()
              .map(
                  snippet ->
                      createLink(
                          HttpMethod.GET,
                          uriBuilder
                              .clone()
                              .path(FactoryService.class, "getFactorySnippet")
                              .queryParam("type", snippet)
                              .build(factoryId)
                              .toString(),
                          null,
                          TEXT_PLAIN,
                          SNIPPET_REL_ATT + '/' + snippet))
              .collect(toList()));

      // creation of accept factory link
      final Link createWorkspace =
          createLink(
              HttpMethod.GET,
              uriBuilder.clone().replacePath("f").queryParam("id", factoryId).build().toString(),
              null,
              TEXT_HTML,
              FACTORY_ACCEPTANCE_REL_ATT);
      links.add(createWorkspace);
      // creation of links for analytics
      links.add(
          createLink(
              HttpMethod.GET,
              uriBuilder
                  .clone()
                  .path("analytics")
                  .path("public-metric/factory_used")
                  .queryParam("factory", createWorkspace.getHref())
                  .toString(),
              null,
              TEXT_PLAIN,
              ACCEPTED_REL_ATT));
    }

    if (!Strings.isNullOrEmpty(factory.getName()) && !Strings.isNullOrEmpty(userName)) {
      // creation of accept factory link by name and creator
      final Link createWorkspaceFromNamedFactory =
          createLink(
              HttpMethod.GET,
              uriBuilder
                  .clone()
                  .replacePath("f")
                  .queryParam("name", factory.getName())
                  .queryParam("user", userName)
                  .build()
                  .toString(),
              null,
              TEXT_HTML,
              NAMED_FACTORY_ACCEPTANCE_REL_ATT);
      links.add(createWorkspaceFromNamedFactory);
    }
    return links;
  }
Exemplo n.º 9
0
 public static <DTO> List<DTO> requestArray(
     Class<DTO> dtoInterface, int timeout, Link link, Object body, Pair<String, ?>... parameters)
     throws IOException, ServerException, NotFoundException, ForbiddenException,
         UnauthorizedException, ConflictException {
   return requestArray(dtoInterface, timeout, link.getHref(), link.getMethod(), body, parameters);
 }
Exemplo n.º 10
0
 @Nullable
 private String getUrlByName(@NotNull String name) {
   Link link = RunnerUtils.getLink(descriptor, name);
   return link == null ? null : link.getHref();
 }
Exemplo n.º 11
0
  private ApplicationProcessDescriptor getDescriptor(
      RunnerProcess process, ServiceContext restfulRequestContext) throws RunnerException {
    final ApplicationStatus status =
        process.getError() == null
            ? (process.isCancelled()
                ? ApplicationStatus.CANCELLED
                : (process.isStopped()
                    ? ApplicationStatus.STOPPED
                    : (process.isStarted() ? ApplicationStatus.RUNNING : ApplicationStatus.NEW)))
            : ApplicationStatus.FAILED;
    final List<Link> links = new LinkedList<>();
    final UriBuilder servicePathBuilder = restfulRequestContext.getServiceUriBuilder();
    final DtoFactory dtoFactory = DtoFactory.getInstance();
    links.add(
        dtoFactory
            .createDto(Link.class)
            .withRel(Constants.LINK_REL_GET_STATUS)
            .withHref(
                servicePathBuilder
                    .clone()
                    .path(getClass(), "getStatus")
                    .build(process.getRunner(), process.getId())
                    .toString())
            .withMethod("GET")
            .withProduces(MediaType.APPLICATION_JSON));
    links.add(
        dtoFactory
            .createDto(Link.class)
            .withRel(Constants.LINK_REL_VIEW_LOG)
            .withHref(
                servicePathBuilder
                    .clone()
                    .path(getClass(), "getLogs")
                    .build(process.getRunner(), process.getId())
                    .toString())
            .withMethod("GET"));
    switch (status) {
      case NEW:
      case RUNNING:
        links.add(
            dtoFactory
                .createDto(Link.class)
                .withRel(Constants.LINK_REL_STOP)
                .withHref(
                    servicePathBuilder
                        .clone()
                        .path(getClass(), "stop")
                        .build(process.getRunner(), process.getId())
                        .toString())
                .withMethod("POST")
                .withProduces(MediaType.APPLICATION_JSON));
        break;
    }
    final RunnerConfiguration configuration = process.getConfiguration();
    final RunRequest request = configuration.getRequest();
    final java.io.File recipeFile = configuration.getRecipeFile();
    if (recipeFile != null) {
      links.add(
          dtoFactory
              .createDto(Link.class)
              .withRel(Constants.LINK_REL_RUNNER_RECIPE)
              .withHref(
                  servicePathBuilder
                      .clone()
                      .path(getClass(), "getRecipeFile")
                      .build(process.getRunner(), process.getId())
                      .toString())
              .withMethod("GET")
              .withProduces(MediaType.TEXT_PLAIN));
    }
    final List<Link> additionalLinks = new LinkedList<>();
    PortMapping portMapping = null;
    switch (status) {
      case NEW:
      case RUNNING:
        for (Link link : configuration.getLinks()) {
          additionalLinks.add(dtoFactory.clone(link));
        }
        final Map<String, String> ports = configuration.getPortMapping();
        if (!ports.isEmpty()) {
          portMapping =
              dtoFactory
                  .createDto(PortMapping.class)
                  .withHost(configuration.getHost())
                  .withPorts(new HashMap<>(ports));
        }
        break;
      default:
        for (Link link : configuration.getLinks()) {
          if ("web url".equals(link.getRel()) || "shell url".equals(link.getRel())) {
            // Hide web and shell links if application is not running.
            continue;
          }
          additionalLinks.add(dtoFactory.clone(link));
        }
        break;
    }

    links.addAll(additionalLinks);
    return dtoFactory
        .createDto(ApplicationProcessDescriptor.class)
        .withProcessId(process.getId())
        .withStatus(status)
        .withStartTime(process.getStartTime())
        .withStopTime(process.getStopTime())
        .withLinks(links)
        .withWorkspace(request.getWorkspace())
        .withProject(request.getProject())
        .withUserId(request.getUserId())
        .withDebugHost(configuration.getDebugHost())
        .withDebugPort(configuration.getDebugPort())
        .withPortMapping(portMapping);
  }