示例#1
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, "", "");
  }
示例#2
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();
    }
  }
示例#3
0
  /**
   * Simulates a configSystemRegistry operation.
   *
   * <p>Operation criteria: get - path put - path, resourcePath (existing resource), optional :
   * mediaType resourceExists - path delete - path importResource - path, param1 (URL: source URL),
   * optional : mediaType copy - path, param1 (target path) move - path, param1 (target path) rename
   * - path, param1 (target path) removeLink - path createLink - path, param1 (target path),
   * optional : param2 (target sub-path) invokeAspect - path, param1 (aspect name), param2 (action)
   * addAssociation - path, param1 (target path), param2 (association type) removeAssociation -
   * path, param1 (target path), param2 (association type) getAssociations - path, param1
   * (association type) getAllAssociations - path createVersion - path restoreVersion - path
   * getVersions - path applyTag - path, param1 (tag) removeTag - path, param1 (tag) getTags - path
   * getResourcePathsWithTag - param1 (tag) rateResource - path, param1 (Number: rating) getRating -
   * path, param1 (username) getAverageRating - path addComment - path, param1 (comment)
   * removeComment - path editComment - path, param1 (comment) getComments - path searchContent -
   * param1 (keywords) executeQuery - param1 (Map: parameters, ex:- key1:val1,key2:val2,...),
   * optional: path
   *
   * <p>Operations not-supported dump restore
   *
   * @param simulationRequest the simulation request.
   * @throws Exception if an exception occurs while executing any operation, or if an invalid
   *     parameter was entered.
   */
  public static void simulateRegistryOperation(
      Registry rootRegistry, SimulationRequest simulationRequest) throws Exception {
    String operation = simulationRequest.getOperation();
    if (operation == null) {
      return;
    }
    if (operation.toLowerCase().equals("get")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.get(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("resourceexists")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.resourceExists(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("put")) {
      String path = simulationRequest.getPath();
      String resourcePath = simulationRequest.getResourcePath();
      String type = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        type = params[0];
      }

      if (isInvalidateValue(path) || isInvalidateValue(type)) {
        return;
      }
      Resource resource;
      if (!isInvalidateValue(resourcePath) && rootRegistry.resourceExists(resourcePath)) {
        resource = rootRegistry.get(resourcePath);
      } else if (type.toLowerCase().equals("collection")) {
        resource = rootRegistry.newCollection();
      } else {
        resource = rootRegistry.newResource();
      }
      simulationService.setSimulation(true);
      resource.setMediaType(simulationRequest.getMediaType());
      rootRegistry.put(path, resource);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("delete")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.delete(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("importresource")) {
      String path = simulationRequest.getPath();
      String sourceURL = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        sourceURL = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(sourceURL)) {
        return;
      }
      simulationService.setSimulation(true);
      Resource resource = rootRegistry.newResource();
      resource.setMediaType(simulationRequest.getMediaType());
      rootRegistry.importResource(path, sourceURL, resource);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("rename")) {
      String path = simulationRequest.getPath();
      String target = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        target = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(target)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.rename(path, target);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("move")) {
      String path = simulationRequest.getPath();
      String target = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        target = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(target)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.move(path, target);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("copy")) {
      String path = simulationRequest.getPath();
      String target = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        target = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(target)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.copy(path, target);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("removelink")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.removeLink(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("createlink")) {
      String path = simulationRequest.getPath();
      String target = null;
      String targetSubPath = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length > 0) {
        target = params[0];
        if (params.length > 1) {
          targetSubPath = params[1];
        }
      }
      if (isInvalidateValue(path) || isInvalidateValue(target)) {
        return;
      }
      simulationService.setSimulation(true);
      if (isInvalidateValue(targetSubPath)) {
        rootRegistry.createLink(path, target);
      } else {
        rootRegistry.createLink(path, target, targetSubPath);
      }
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("invokeaspect")) {
      String path = simulationRequest.getPath();
      String aspectName = null;
      String action = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 2) {
        aspectName = params[0];
        action = params[1];
      }
      if (isInvalidateValue(path) || isInvalidateValue(aspectName) || isInvalidateValue(action)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.invokeAspect(path, aspectName, action);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("addassociation")) {
      String path = simulationRequest.getPath();
      String target = null;
      String associationType = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 2) {
        target = params[0];
        associationType = params[1];
      }
      if (isInvalidateValue(path)
          || isInvalidateValue(target)
          || isInvalidateValue(associationType)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.addAssociation(path, target, associationType);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("removeassociation")) {
      String path = simulationRequest.getPath();
      String target = null;
      String associationType = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 2) {
        target = params[0];
        associationType = params[1];
      }
      if (isInvalidateValue(path)
          || isInvalidateValue(target)
          || isInvalidateValue(associationType)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.removeAssociation(path, target, associationType);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("getassociations")) {
      String path = simulationRequest.getPath();
      String associationType = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        associationType = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(associationType)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.getAssociations(path, associationType);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("getallassociations")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.getAllAssociations(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("createversion")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.createVersion(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("restoreversion")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.restoreVersion(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("getversions")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.getVersions(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("applytag")) {
      String path = simulationRequest.getPath();
      String tag = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        tag = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(tag)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.applyTag(path, tag);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("removetag")) {
      String path = simulationRequest.getPath();
      String tag = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        tag = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(tag)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.removeTag(path, tag);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("gettags")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.getTags(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("getresourcepathswithtag")) {
      String tag = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        tag = params[0];
      }
      if (isInvalidateValue(tag)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.getResourcePathsWithTag(tag);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("rateresource")) {
      String path = simulationRequest.getPath();
      int rating = -1;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        try {
          rating = Integer.parseInt(params[0]);
        } catch (NumberFormatException ignored) {
          return;
        }
      }
      if (isInvalidateValue(path) || rating == -1) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.rateResource(path, rating);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("getrating")) {
      String path = simulationRequest.getPath();
      String username = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        username = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(username)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.getRating(path, username);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("getaveragerating")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.getAverageRating(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("addcomment")) {
      String path = simulationRequest.getPath();
      String comment = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        comment = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(comment)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.addComment(path, new Comment(comment));
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("editcomment")) {
      String path = simulationRequest.getPath();
      String comment = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        comment = params[0];
      }
      if (isInvalidateValue(path) || isInvalidateValue(comment)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.editComment(path, comment);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("removeComment")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.removeComment(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("getcomments")) {
      String path = simulationRequest.getPath();
      if (isInvalidateValue(path)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.getComments(path);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("searchcontent")) {
      String keywords = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        keywords = params[0];
      }
      if (isInvalidateValue(keywords)) {
        return;
      }
      simulationService.setSimulation(true);
      rootRegistry.searchContent(keywords);
      simulationService.setSimulation(false);
    } else if (operation.toLowerCase().equals("executequery")) {
      String path = simulationRequest.getPath();
      String queryParams = null;
      String[] params = simulationRequest.getParameters();
      if (params != null && params.length >= 1) {
        queryParams = params[0];
      }
      Map<String, String> paramMap = new LinkedHashMap<String, String>();
      if (isInvalidateValue(queryParams)) {
        return;
      }
      String[] entries = queryParams.split(",");
      if (entries != null) {
        for (String entry : entries) {
          String[] keyValPair = entry.split(":");
          if (keyValPair != null && keyValPair.length == 2) {
            paramMap.put(keyValPair[0], keyValPair[1]);
          }
        }
      }
      simulationService.setSimulation(true);
      rootRegistry.executeQuery(path, paramMap);
      simulationService.setSimulation(false);
    } else {
      throw new Exception("Unsupported Registry Operation: " + operation);
    }
  }