Esempio n. 1
0
  /**
   * This method add array of tags to the specified resource
   *
   * @param resourcePath - Resource path
   * @return HTTP 204 No Content if success
   */
  @POST
  @Consumes("application/json")
  @Produces("application/json")
  public Response addTag(
      @QueryParam("path") String resourcePath,
      @QueryParam("name") String tagText,
      @HeaderParam("X-JWT-Assertion") String JWTToken) {

    RestAPIAuthContext authContext =
        RestAPISecurityUtils.getAuthContext(
            PrivilegedCarbonContext.getThreadLocalCarbonContext(), JWTToken);

    if (!authContext.isAuthorized()) {
      return Response.status(Response.Status.UNAUTHORIZED).build();
    }

    try {
      Registry registry = getUserRegistry(authContext.getUserName(), authContext.getTenantId());
      if (!registry.resourceExists(resourcePath)) {
        return Response.status(Response.Status.NOT_FOUND)
            .entity(RestAPIConstants.RESOURCE_NOT_FOUND + resourcePath)
            .build();
      }
      registry.applyTag(resourcePath, tagText);
      return Response.status(Response.Status.NO_CONTENT).build();

    } catch (RegistryException e) {
      log.error("user doesn't have permission to put the tags for the given resource", e);
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
  }
Esempio n. 2
0
  /**
   * This method retrieves the resource paths for a given tag name
   *
   * @param tagName - Name of the tag
   * @param start - Page start number
   * @param size - Number of records to be fetched
   * @return JSON object eg: {"path":[<array of resource paths tagged by the tagname>]}protected
   *     HTTP 200 OK.
   */
  @GET
  @Produces("application/json")
  public Response getTaggedResources(
      @QueryParam("name") String tagName,
      @QueryParam("start") int start,
      @QueryParam("size") int size,
      @HeaderParam("X-JWT-Assertion") String JWTToken) {
    RestAPIAuthContext authContext =
        RestAPISecurityUtils.getAuthContext(
            PrivilegedCarbonContext.getThreadLocalCarbonContext(), JWTToken);

    if (!authContext.isAuthorized()) {
      return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    try {

      Registry registry = getUserRegistry(authContext.getUserName(), authContext.getTenantId());
      TaggedResourcePath[] resourcePaths = registry.getResourcePathsWithTag(tagName);
      return getPaginatedResults(resourcePaths, start, size, "", "");

    } catch (RegistryException e) {
      log.error("Failed to get resource path having tag : " + tagName, e);
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
  }
Esempio n. 3
0
  /**
   * This method tags associated with the given resource
   *
   * @param resourcePath resource path
   * @param start starting page number
   * @param size number of tags to be fetched
   * @return JSON tag model eg: {"tags":[<array of tag names]}
   */
  @GET
  @Produces("application/json")
  @ApiOperation(
      value = "Get all tags on a resource",
      httpMethod = "GET",
      notes = "Fetch all tags on a resource",
      response = TagModel.class)
  @ApiResponses(
      value = {
        @ApiResponse(code = 200, message = "Found the tags and returned in body"),
        @ApiResponse(code = 401, message = "Invalid credentials provided"),
        @ApiResponse(code = 404, message = "Given specific resource not found"),
        @ApiResponse(code = 500, message = "Internal server error occurred")
      })
  public Response getTags(
      @QueryParam("path") String resourcePath,
      @QueryParam("start") int start,
      @QueryParam("size") int size,
      @HeaderParam("X-JWT-Assertion") String JWTToken) {

    RestAPIAuthContext authContext =
        RestAPISecurityUtils.getAuthContext(
            PrivilegedCarbonContext.getThreadLocalCarbonContext(), JWTToken);

    if (!authContext.isAuthorized()) {
      return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    if (resourcePath == null || "".equals(resourcePath)) {
      // Return tagsCloud, therefore no need pagination.
      return getAllTags();
    }
    org.wso2.carbon.registry.core.Tag[] tags = new org.wso2.carbon.registry.core.Tag[0];
    try {
      Registry registry = getUserRegistry(authContext.getUserName(), authContext.getTenantId());
      if (!registry.resourceExists(resourcePath)) {
        return Response.status(Response.Status.NOT_FOUND)
            .entity(RestAPIConstants.RESOURCE_NOT_FOUND)
            .build();
      }

      tags = registry.getTags(resourcePath);

    } catch (RegistryException e) {
      log.error("Failed to get tags on resource " + resourcePath, e);
      Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
    // Need paginate, because it return tags of a resource
    return getPaginatedResults(tags, start, size, "", "");
  }
Esempio n. 4
0
  /**
   * This method deletes the specified tag on the given resource
   *
   * @param resourcePath - Path of the resource.
   * @param tagName - Name of the tag
   * @return HTTP 204 No Content response, if success.
   */
  @DELETE
  @Produces("application/json")
  public Response deleteTag(
      @QueryParam("path") String resourcePath,
      @QueryParam("name") String tagName,
      @HeaderParam("X-JWT-Assertion") String JWTToken) {

    RestAPIAuthContext authContext =
        RestAPISecurityUtils.getAuthContext(
            PrivilegedCarbonContext.getThreadLocalCarbonContext(), JWTToken);

    if (!authContext.isAuthorized()) {
      return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    try {

      boolean tagFound = false;
      Registry registry = getUserRegistry(authContext.getUserName(), authContext.getTenantId());
      if (!registry.resourceExists(resourcePath)) {
        return Response.status(Response.Status.NOT_FOUND)
            .entity(RestAPIConstants.RESOURCE_NOT_FOUND + resourcePath)
            .build();
      }
      org.wso2.carbon.registry.core.Tag[] tags = registry.getTags(resourcePath);
      for (org.wso2.carbon.registry.core.Tag tag1 : tags) {
        // if tag has been found remove the tag,set the tag found
        // variable to true
        if (tagName.equals(tag1.getTagName())) {
          registry.removeTag(resourcePath, tagName);
          tagFound = true;
        }
      }
      if (tagFound) {
        // if tag deleted
        return Response.status(Response.Status.NO_CONTENT).build();
      } else {
        log.debug("tag not found");
        // if the specified tag is not found,returns http 404
        return Response.status(Response.Status.NOT_FOUND).build();
      }

    } catch (RegistryException e) {
      log.error("Failed to  delete a tag " + tagName + " " + "on resource " + resourcePath, e);
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
  }
Esempio n. 5
0
  /**
   * This method add array of tags to the specified resource
   *
   * @param resourcePath - Resource path
   * @param tags - eg:{"tags":[<array of tag names>]}
   * @return HTTP 204 No Content ,if success.
   */
  @POST
  @Consumes("application/json")
  @Produces("application/json")
  @ApiOperation(
      value = "Add an array of tags to a resource",
      httpMethod = "POST",
      notes = "Add an array of tags to a resource")
  @ApiResponses(
      value = {
        @ApiResponse(code = 204, message = "Resource tagged successfully"),
        @ApiResponse(code = 401, message = "Invalid credentials provided"),
        @ApiResponse(code = 404, message = "Specified resource not found"),
        @ApiResponse(code = 500, message = "Internal server error occurred")
      })
  public Response addTags(
      @QueryParam("path") String resourcePath,
      TagModel tags,
      @HeaderParam("X-JWT-Assertion") String JWTToken) {

    RestAPIAuthContext authContext =
        RestAPISecurityUtils.getAuthContext(
            PrivilegedCarbonContext.getThreadLocalCarbonContext(), JWTToken);

    if (!authContext.isAuthorized()) {
      return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    try {
      Registry registry = getUserRegistry(authContext.getUserName(), authContext.getTenantId());
      if (!registry.resourceExists(resourcePath)) {
        return Response.status(Response.Status.NOT_FOUND)
            .entity(RestAPIConstants.RESOURCE_NOT_FOUND)
            .build();
      }

      String[] tagsOnResource = tags.getTags();
      for (String aTagsOnResource : tagsOnResource) {
        registry.applyTag(resourcePath, aTagsOnResource);
      }
      return Response.status(Response.Status.NO_CONTENT).build();

    } catch (RegistryException e) {
      log.error("user doesn't have permission to put the tags for the given resource", e);
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
  }
Esempio n. 6
0
  /**
   * This method to move the registry resource.
   *
   * @param resourcePath - Source path of the resource.
   * @param destinationPath - Destination path of the resource.
   * @param JWTToken - Access token.
   * @return - HTTP 204 No Content
   */
  @POST
  @ApiOperation(
      value = "Move source resource to target path",
      httpMethod = "POST",
      notes = "Move source resource to target path")
  @ApiResponses(
      value = {
        @ApiResponse(code = 204, message = "Resource moved successfully"),
        @ApiResponse(code = 401, message = "Invalid credentials provided"),
        @ApiResponse(code = 404, message = "Specified resource not found"),
        @ApiResponse(code = 500, message = "Internal server error occurred")
      })
  public Response moveResource(
      @QueryParam("path") String resourcePath,
      @QueryParam("destination") String destinationPath,
      @HeaderParam("X-JWT-Assertion") String JWTToken) {
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    RestAPIAuthContext authContext = RestAPISecurityUtils.getAuthContext(carbonContext, JWTToken);

    if (!authContext.isAuthorized()) {
      return Response.status(Response.Status.UNAUTHORIZED).build();
    }

    try {

      Registry registry = getUserRegistry(authContext.getUserName(), authContext.getTenantId());
      if (!registry.resourceExists(resourcePath)) {
        return Response.status(Response.Status.NOT_FOUND)
            .entity(RestAPIConstants.RESOURCE_NOT_FOUND + resourcePath)
            .build();
      }
      registry.move(resourcePath, destinationPath);
      return Response.status(Response.Status.NO_CONTENT).build();

    } catch (RegistryException e) {
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
  }