Example #1
0
  /**
   * Creates factory links and links for retrieving factory images.
   *
   * @param images a set of factory images
   * @param serviceContext the context to retrieve factory service base URI
   * @return list of factory and factory images links
   */
  public static List<Link> createLinks(
      FactoryDto factory,
      Set<FactoryImage> images,
      ServiceContext serviceContext,
      String userName) {
    final List<Link> links = new LinkedList<>(createLinks(factory, serviceContext, userName));
    final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder();
    final String factoryId = factory.getId();

    // creation of links to retrieve images
    links.addAll(
        images
            .stream()
            .map(
                image ->
                    createLink(
                        HttpMethod.GET,
                        uriBuilder
                            .clone()
                            .path(FactoryService.class, "getImage")
                            .queryParam("imgId", image.getName())
                            .build(factoryId)
                            .toString(),
                        null,
                        image.getMediaType(),
                        IMAGE_REL_ATT))
            .collect(toList()));
    return links;
  }
Example #2
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;
  }
  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);
  }