예제 #1
0
 public static boolean generateHandler(Registry configSystemRegistry, String resourceFullPath)
     throws RegistryException, XMLStreamException {
   RegistryContext registryContext = configSystemRegistry.getRegistryContext();
   if (registryContext == null) {
     return false;
   }
   Resource resource = configSystemRegistry.get(resourceFullPath);
   if (resource != null) {
     String content = null;
     if (resource.getContent() != null) {
       content = RegistryUtils.decodeBytes((byte[]) resource.getContent());
     }
     if (content != null) {
       OMElement handler = AXIOMUtil.stringToOM(content);
       if (handler != null) {
         OMElement dummy = OMAbstractFactory.getOMFactory().createOMElement("dummy", null);
         dummy.addChild(handler);
         try {
           configSystemRegistry.beginTransaction();
           boolean status =
               RegistryConfigurationProcessor.updateHandler(
                   dummy,
                   configSystemRegistry.getRegistryContext(),
                   HandlerLifecycleManager.USER_DEFINED_HANDLER_PHASE);
           configSystemRegistry.commitTransaction();
           return status;
         } catch (Exception e) {
           configSystemRegistry.rollbackTransaction();
           throw new RegistryException("Unable to add handler", e);
         }
       }
     }
   }
   return false;
 }
예제 #2
0
  public void testDefaultQuery() throws Exception {
    Resource r1 = registry.newResource();
    String r1Content = "this is r1 content";
    r1.setContent(r1Content.getBytes());
    r1.setDescription("production ready.");
    String r1Path = "/c3/r1";
    registry.put(r1Path, r1);

    Resource r2 = registry.newResource();
    String r2Content = "content for r2 :)";
    r2.setContent(r2Content);
    r2.setDescription("ready for production use.");
    String r2Path = "/c3/r2";
    registry.put(r2Path, r2);

    Resource r3 = registry.newResource();
    String r3Content = "content for r3 :)";
    r3.setContent(r3Content);
    r3.setDescription("only for government use.");
    String r3Path = "/c3/r3";
    registry.put(r3Path, r3);

    registry.applyTag("/c3/r1", "java");
    registry.applyTag("/c3/r2", "jsp");
    registry.applyTag("/c3/r3", "ajax");

    String sql1 =
        "SELECT RT.REG_TAG_ID FROM REG_RESOURCE_TAG RT, REG_RESOURCE R "
            + "WHERE (R.REG_VERSION=RT.REG_VERSION OR "
            + "(R.REG_PATH_ID=RT.REG_PATH_ID AND R.REG_NAME=RT.REG_RESOURCE_NAME)) "
            + "AND R.REG_DESCRIPTION LIKE ? ORDER BY RT.REG_TAG_ID";

    Resource q1 = systemRegistry.newResource();
    q1.setContent(sql1);
    q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
    q1.addProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME, RegistryConstants.TAGS_RESULT_TYPE);
    systemRegistry.put("/qs/q3", q1);

    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("1", "%production%");
    Collection result = registry.executeQuery("/qs/q3", parameters);

    String[] tagPaths = result.getChildren();
    assertEquals("There should be two matching tags.", tagPaths.length, 2);

    Resource tag2 = registry.get(tagPaths[0]);
    assertEquals("First matching tag should be 'java'", (String) tag2.getContent(), "java");

    Resource tag1 = registry.get(tagPaths[1]);
    assertEquals("Second matching tag should be 'jsp'", (String) tag1.getContent(), "jsp");
  }
예제 #3
0
 public static String getHandlerConfiguration(Registry configSystemRegistry, String name)
     throws RegistryException, XMLStreamException {
   String path = getContextRoot() + name;
   Resource resource;
   if (handlerExists(configSystemRegistry, name)) {
     resource = configSystemRegistry.get(path);
     return RegistryUtils.decodeBytes((byte[]) resource.getContent());
   }
   return null;
 }
예제 #4
0
 private Scriptable parseJsonResource(Resource resource) throws IOException {
   JsonParser parser = new JsonParser(Context.getCurrentContext(), globalScope);
   try {
     Object result = parser.parseValue(resource.getContent());
     if (!(result instanceof Scriptable)) {
       throw new RuntimeException("Expected Object from package.json, got " + result);
     }
     return (Scriptable) result;
   } catch (JsonParser.ParseException px) {
     throw new RuntimeException(px);
   }
 }
예제 #5
0
  public void testWithoutTableParamsQuery() throws Exception {
    Resource r1 = registry.newResource();
    String r1Content = "this is r1 content";
    r1.setContent(r1Content.getBytes());
    r1.setDescription("production ready.");
    String r1Path = "/c1/r1";
    registry.put(r1Path, r1);

    Resource r2 = registry.newResource();
    String r2Content = "content for r2 :)";
    r2.setContent(r2Content);
    r2.setDescription("ready for production use.");
    String r2Path = "/c2/r2";
    registry.put(r2Path, r2);

    Resource r3 = registry.newResource();
    String r3Content = "content for r3 :)";
    r3.setContent(r3Content);
    r3.setDescription("only for government use.");
    String r3Path = "/c2/r3";
    registry.put(r3Path, r3);

    String sql1 =
        "SELECT REG_PATH_ID, REG_NAME FROM REG_RESOURCE, " + "REG_TAG WHERE REG_DESCRIPTION LIKE ?";
    Resource q1 = systemRegistry.newResource();
    q1.setContent(sql1);
    q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
    q1.addProperty(
        RegistryConstants.RESULT_TYPE_PROPERTY_NAME, RegistryConstants.RESOURCES_RESULT_TYPE);
    systemRegistry.put("/qs/q1", q1);

    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("1", "%production%");
    Resource result = registry.executeQuery("/qs/q1", parameters);

    assertTrue(
        "Search with result type Resource should return a directory.",
        result instanceof org.wso2.carbon.registry.core.Collection);

    List<String> matchingPaths = new ArrayList<String>();
    String[] paths = (String[]) result.getContent();
    matchingPaths.addAll(Arrays.asList(paths));

    assertTrue("Path /c1/r1 should be in the results.", matchingPaths.contains("/c1/r1"));
    assertTrue("Path /c2/r2 should be in the results.", matchingPaths.contains("/c2/r2"));
  }
예제 #6
0
  public void testWithoutWhereQuery() throws Exception {
    Resource r1 = registry.newResource();
    String r1Content = "this is r1 content";
    r1.setContent(r1Content.getBytes());
    r1.setDescription("production ready.");
    String r1Path = "/c1/r1";
    registry.put(r1Path, r1);

    Resource r2 = registry.newResource();
    String r2Content = "content for r2 :)";
    r2.setContent(r2Content);
    r2.setDescription("ready for production use.");
    String r2Path = "/c2/r2";
    registry.put(r2Path, r2);

    Resource r3 = registry.newResource();
    String r3Content = "content for r3 :)";
    r3.setContent(r3Content);
    r3.setDescription("only for government use.");
    String r3Path = "/c2/r3";
    registry.put(r3Path, r3);

    String sql1 = "SELECT REG_PATH_ID, REG_NAME FROM REG_RESOURCE, REG_TAG";
    Resource q1 = systemRegistry.newResource();
    q1.setContent(sql1);
    q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
    q1.addProperty(
        RegistryConstants.RESULT_TYPE_PROPERTY_NAME, RegistryConstants.RESOURCES_RESULT_TYPE);
    systemRegistry.put("/qs/q1", q1);

    Map parameters = new HashMap();
    Resource result = registry.executeQuery("/qs/q1", parameters);

    assertTrue(
        "Search with result type Resource should return a directory.",
        result instanceof org.wso2.carbon.registry.core.Collection);

    String[] paths = (String[]) result.getContent();
    assertTrue("Should return all the resources", paths.length >= 3);
  }
예제 #7
0
  public String addWadlToRegistry(
      RequestContext requestContext, Resource resource, String resourcePath, boolean skipValidation)
      throws RegistryException {
    String wadlName = RegistryUtils.getResourceName(resourcePath);
    String version =
        requestContext.getResource().getProperty(RegistryConstants.VERSION_PARAMETER_NAME);

    if (version == null) {
      version = CommonConstants.WADL_VERSION_DEFAULT_VALUE;
      requestContext.getResource().setProperty(RegistryConstants.VERSION_PARAMETER_NAME, version);
    }

    OMElement wadlElement;
    String wadlContent;
    Object resourceContent = resource.getContent();
    if (resourceContent instanceof String) {
      wadlContent = (String) resourceContent;
    } else {
      wadlContent = new String((byte[]) resourceContent);
    }

    try {
      XMLStreamReader reader =
          XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(wadlContent));
      StAXOMBuilder builder = new StAXOMBuilder(reader);
      wadlElement = builder.getDocumentElement();
    } catch (XMLStreamException e) {
      // This exception is unexpected because the WADL already validated
      String msg = "Unexpected error occured " + "while reading the WADL at " + resourcePath + ".";
      log.error(msg);
      throw new RegistryException(msg, e);
    }

    String wadlNamespace = wadlElement.getNamespace().getNamespaceURI();
    String namespaceSegment =
        CommonUtil.derivePathFragmentFromNamespace(wadlNamespace).replace("//", "/");
    String actualPath =
        getChrootedWadlLocation(requestContext.getRegistryContext())
            + namespaceSegment
            + version
            + "/"
            + wadlName;

    OMElement grammarsElement =
        wadlElement.getFirstChildWithName(new QName(wadlNamespace, "grammars"));

    if (StringUtils.isNotBlank(requestContext.getSourceURL())) {
      String uri = requestContext.getSourceURL();
      if (!skipValidation) {
        validateWADL(uri);
      }

      if (resource.getUUID() == null) {
        resource.setUUID(UUID.randomUUID().toString());
      }

      String wadlBaseUri = uri.substring(0, uri.lastIndexOf("/") + 1);
      if (grammarsElement != null) {
        // This is to avoid evaluating the grammars import when building AST
        grammarsElement.detach();
        wadlElement.addChild(resolveImports(grammarsElement, wadlBaseUri, version));
      }
    } else {
      if (!skipValidation) {
        File tempFile = null;
        BufferedWriter bufferedWriter = null;
        try {
          tempFile = File.createTempFile(wadlName, null);
          bufferedWriter = new BufferedWriter(new FileWriter(tempFile));
          bufferedWriter.write(wadlElement.toString());
          bufferedWriter.flush();
        } catch (IOException e) {
          String msg = "Error occurred while reading the WADL File";
          log.error(msg, e);
          throw new RegistryException(msg, e);
        } finally {
          if (bufferedWriter != null) {
            try {
              bufferedWriter.close();
            } catch (IOException e) {
              String msg = "Error occurred while closing File writer";
              log.warn(msg, e);
            }
          }
        }
        validateWADL(tempFile.toURI().toString());
        try {
          delete(tempFile);
        } catch (IOException e) {
          String msg =
              "An error occurred while deleting the temporary files from local file system.";
          log.warn(msg, e);
          throw new RegistryException(msg, e);
        }
      }

      if (grammarsElement != null) {
        grammarsElement = resolveImports(grammarsElement, null, version);
        wadlElement.addChild(grammarsElement);
      }
    }

    requestContext.setResourcePath(new ResourcePath(actualPath));
    if (resource.getProperty(CommonConstants.SOURCE_PROPERTY) == null) {
      resource.setProperty(CommonConstants.SOURCE_PROPERTY, CommonConstants.SOURCE_AUTO);
    }
    registry.put(actualPath, resource);
    addImportAssociations(actualPath);
    if (getCreateService()) {
      OMElement serviceElement =
          RESTServiceUtils.createRestServiceArtifact(
              wadlElement,
              wadlName,
              version,
              RegistryUtils.getRelativePath(requestContext.getRegistryContext(), actualPath));
      String servicePath = RESTServiceUtils.addServiceToRegistry(requestContext, serviceElement);
      registry.addAssociation(servicePath, actualPath, CommonConstants.DEPENDS);
      registry.addAssociation(actualPath, servicePath, CommonConstants.USED_BY);
      String endpointPath = createEndpointElement(requestContext, wadlElement, version);
      if (endpointPath != null) {
        registry.addAssociation(servicePath, endpointPath, CommonConstants.DEPENDS);
        registry.addAssociation(endpointPath, servicePath, CommonConstants.USED_BY);
      }
    }

    return resource.getPath();
  }