/**
  * Method to obtain the collection media types.
  *
  * @param registryContext the registry context.
  * @return a String of collection media types, in the format name:type,name:type,...
  * @throws RegistryException if the operation failed.
  */
 public static String getCollectionMediaTypeMappings(RegistryContext registryContext)
     throws RegistryException {
   if (registryContext == null) {
     return RegistryContext.getBaseInstance().getCollectionMediaTypes();
   } else {
     return registryContext.getCollectionMediaTypes();
   }
 }
  /**
   * Method to obtain the custom UI media types.
   *
   * @param configSystemRegistry a configuration system registry instance.
   * @return a String of custom UI media types, in the format name:type,name:type,...
   * @throws RegistryException if the operation failed.
   */
  public static String getCustomUIMediaTypeMappings(Registry configSystemRegistry)
      throws RegistryException {

    RegistryContext registryContext = configSystemRegistry.getRegistryContext();

    if (getCustomUIMediaTypeMappings(registryContext) != null) {
      return getCustomUIMediaTypeMappings(registryContext);
    }

    Resource resource;
    String mediaTypeString = null;

    String resourcePath =
        MIME_TYPE_COLLECTION + RegistryConstants.PATH_SEPARATOR + RESOURCE_MIME_TYPE_INDEX;

    // TODO: Adding the media types should ideally be done by the handler associated with the
    // media type
    if (!configSystemRegistry.resourceExists(resourcePath)) {
      getResourceMediaTypeMappings(configSystemRegistry);
    }
    if (!configSystemRegistry.resourceExists(
        resourcePath + RegistryConstants.PATH_SEPARATOR + CUSTOM_UI_MIME_TYPE_INDEX)) {
      resource = configSystemRegistry.newResource();
      resource.setProperty("profiles", "application/vnd.wso2-profiles+xml");
      // resource.setProperty("service", "application/vnd.wso2-service+xml");
      resource.setDescription(
          "This resource contains the media Types associated with "
              + "custom user interfaces on the Registry. Add, Edit or Delete properties to "
              + "Manage Media Types.");
      configSystemRegistry.put(
          resourcePath + RegistryConstants.PATH_SEPARATOR + CUSTOM_UI_MIME_TYPE_INDEX, resource);
    } else {
      resource =
          configSystemRegistry.get(
              resourcePath + RegistryConstants.PATH_SEPARATOR + CUSTOM_UI_MIME_TYPE_INDEX);
    }
    Properties properties = resource.getProperties();
    if (properties.size() > 0) {
      Set<Object> keySet = properties.keySet();
      for (Object key : keySet) {
        if (key instanceof String) {
          String ext = (String) key;
          if (RegistryUtils.isHiddenProperty(ext)) {
            continue;
          }
          String value = resource.getProperty(ext);
          String mediaTypeMapping = ext + ":" + value;
          if (mediaTypeString == null) {
            mediaTypeString = mediaTypeMapping;
          } else {
            mediaTypeString = mediaTypeString + "," + mediaTypeMapping;
          }
        }
      }
    }
    registryContext.setCustomUIMediaTypes(mediaTypeString);
    return mediaTypeString;
  }
 /**
  * Method to obtain the media type of a given resource.
  *
  * @param resourceName the name of the resource.
  * @return the media type.
  * @throws RegistryException if the operation failed.
  */
 public static String getMediaType(String resourceName) throws RegistryException {
   if (resourceName == null
       || resourceName.indexOf('.') == -1
       || resourceName.indexOf('.') + 1 >= resourceName.length()) {
     return null;
   }
   String extension = resourceName.substring(resourceName.lastIndexOf('.') + 1).toLowerCase();
   String mediaTypes = getResourceMediaTypeMappings(RegistryContext.getBaseInstance());
   if (mediaTypes == null) {
     // We don't treat this as an error, since some collections and resources would be
     // created even before the media types have been stored into the registry, and have
     // been initialized.
     return null;
   }
   extension += ":";
   if (mediaTypes.contains(extension)) {
     String temp = mediaTypes.substring(mediaTypes.indexOf(extension) + extension.length());
     if (temp.indexOf(',') == -1) {
       return temp;
     } else {
       return temp.substring(0, temp.indexOf(','));
     }
   }
   return null;
 }
  public void testSimpleLifecycle() throws Exception {
    final String RESOURCE = "/r1";
    final String LIFECYCLE = "simpleLifecycle";

    RegistryContext context = registry.getRegistryContext();
    context.selectDBConfig("h2-db");
    context.addAspect(LIFECYCLE, new SimpleLifecycle(), MultitenantConstants.SUPER_TENANT_ID);

    String[] aspects = registry.getAvailableAspects();
    assertTrue(aspects.length > 0);
    boolean found = false;
    for (String aspect : aspects) {
      if (aspect.equals(LIFECYCLE)) {
        found = true;
      }
    }
    assertTrue("Lifecycle not found in available aspects", found);

    Resource resource = registry.newResource();
    resource.setDescription("My thing");
    registry.put(RESOURCE, resource);

    registry.associateAspect(RESOURCE, LIFECYCLE);

    resource = registry.get(RESOURCE);
    assertNotNull(resource);
    String propValue = resource.getProperty(SimpleLifecycle.STATE_PROP);
    assertNotNull(propValue);
    assertEquals("Wrong initial state!", SimpleLifecycle.INIT, propValue);

    String[] actions = registry.getAspectActions(RESOURCE, LIFECYCLE);
    assertNotNull("No available actions", actions);
    assertEquals("Wrong # of available actions", 1, actions.length);
    assertEquals("Wrong available action", SimpleLifecycle.ACTION, actions[0]);

    registry.invokeAspect(RESOURCE, LIFECYCLE, SimpleLifecycle.ACTION);

    resource = registry.get(RESOURCE);

    // OK, now we should be in the next state
    propValue = resource.getProperty(SimpleLifecycle.STATE_PROP);
    assertNotNull(propValue);
    assertEquals("Wrong state!", SimpleLifecycle.FINAL, propValue);
  }
示例#5
0
  public void setUp() throws RegistryException {
    if (System.getProperty("carbon.home") == null) {
      File file = new File("src/test/resources/carbon-home");
      if (file.exists()) {
        System.setProperty("carbon.home", file.getAbsolutePath());
      }
    }

    // The line below is responsible for initializing the cache.
    CarbonContext.getCurrentContext();

    String carbonHome = System.getProperty("carbon.home");
    System.out.println("carbon home " + carbonHome);
    String carbonXMLPath =
        carbonHome
            + File.separator
            + "repository"
            + File.separator
            + "conf"
            + File.separator
            + "carbon.xml";
    RegistryConfiguration regConfig = new RegistryConfiguration(carbonXMLPath);
    RegistryCoreServiceComponent.setRegistryConfig(regConfig);

    RealmService realmService = new InMemoryRealmService();
    InputStream is;

    try {
      is = this.getClass().getClassLoader().getResourceAsStream("registry.xml");
    } catch (Exception e) {
      is = null;
    }
    ctx = RegistryContext.getBaseInstance(is, realmService);
    // RegistryConfigurationProcessor.populateRegistryConfig(is, ctx);
    ctx.setSetup(true);
    ctx.selectDBConfig("h2-db");
  }
  /**
   * Method to obtain the resource media types.
   *
   * @param configSystemRegistry a configuration system registry instance.
   * @return a String of resource media types, in the format extension:type,extension:type,...
   * @throws RegistryException if the operation failed.
   */
  public static String getResourceMediaTypeMappings(Registry configSystemRegistry)
      throws RegistryException {

    RegistryContext registryContext = configSystemRegistry.getRegistryContext();

    if (getResourceMediaTypeMappings(registryContext) != null) {
      return getResourceMediaTypeMappings(registryContext);
    }

    Resource resource;
    String mediaTypeString = null;

    String resourcePath =
        MIME_TYPE_COLLECTION + RegistryConstants.PATH_SEPARATOR + RESOURCE_MIME_TYPE_INDEX;

    if (!configSystemRegistry.resourceExists(resourcePath)) {
      resource = configSystemRegistry.newCollection();
    } else {
      resource = configSystemRegistry.get(resourcePath);
      Properties properties = resource.getProperties();
      if (properties.size() > 0) {
        Set<Object> keySet = properties.keySet();
        for (Object key : keySet) {
          if (key instanceof String) {
            String ext = (String) key;
            if (RegistryUtils.isHiddenProperty(ext)) {
              continue;
            }
            String value = resource.getProperty(ext);
            String mediaTypeMapping = ext + ":" + value;
            if (mediaTypeString == null) {
              mediaTypeString = mediaTypeMapping;
            } else {
              mediaTypeString = mediaTypeString + "," + mediaTypeMapping;
            }
          }
        }
      }
      registryContext.setResourceMediaTypes(mediaTypeString);
      return mediaTypeString;
    }

    BufferedReader reader;
    try {
      File mimeFile = getMediaTypesFile();
      reader = new BufferedReader(new InputStreamReader(new FileInputStream(mimeFile)));
    } catch (Exception e) {
      String msg =
          "Failed to read the the media type definitions file. Only a limited "
              + "set of media type definitions will be populated. ";
      log.error(msg, e);
      mediaTypeString = "txt:text/plain,jpg:image/jpeg,gif:image/gif";
      registryContext.setResourceMediaTypes(mediaTypeString);
      return mediaTypeString;
    }

    try {
      while (reader.ready()) {
        String mediaTypeData = reader.readLine().trim();
        if (mediaTypeData.startsWith("#")) {
          // ignore the comments
          continue;
        }

        if (mediaTypeData.length() == 0) {
          // ignore the blank lines
          continue;
        }

        // mime.type file delimits media types:extensions by tabs. if there is no
        // extension associated with a media type, there are no tabs in the line. so we
        // don't need such lines.
        if (mediaTypeData.indexOf('\t') > 0) {

          String[] parts = mediaTypeData.split("\t+");
          if (parts.length == 2 && parts[0].length() > 0 && parts[1].length() > 0) {

            // there can multiple extensions associated with a single media type. in
            // that case, extensions are delimited by a space.
            String[] extensions = parts[1].trim().split(" ");
            for (String extension : extensions) {
              if (extension.length() > 0) {
                String mediaTypeMapping = extension + ":" + parts[0];
                resource.setProperty(extension, parts[0]);
                if (mediaTypeString == null) {
                  mediaTypeString = mediaTypeMapping;
                } else {
                  mediaTypeString = mediaTypeString + "," + mediaTypeMapping;
                }
              }
            }
          }
        }
      }
      resource.setDescription(
          "This collection contains the media Types available for "
              + "resources on the Registry. Add, Edit or Delete properties to Manage Media "
              + "Types.");
      Resource collection = configSystemRegistry.newCollection();
      collection.setDescription(
          "This collection lists the media types available on the "
              + "Registry Server. Before changing an existing media type, please make sure "
              + "to alter existing resources/collections and related configuration details.");
      configSystemRegistry.put(MIME_TYPE_COLLECTION, collection);
      configSystemRegistry.put(resourcePath, resource);

    } catch (IOException e) {
      String msg = "Could not read the media type mappings file from the location: ";
      throw new RegistryException(msg, e);

    } finally {
      try {
        reader.close();
      } catch (IOException ignore) {
      }
    }
    registryContext.setResourceMediaTypes(mediaTypeString);
    return mediaTypeString;
  }
 /**
  * Create an return a registry context.
  *
  * @return new registry context
  */
 public static RegistryContext getCloneContext() {
   RegistryContext context = new RegistryContext();
   context.setClone(true);
   return context;
 }
 /**
  * Method to set custom UI media types.
  *
  * @param customUIMediaTypes the custom UI media types.
  */
 public void setCustomUIMediaTypes(String customUIMediaTypes) {
   RegistryContext.getBaseInstance().customUIMediaTypes = customUIMediaTypes;
 }
 /**
  * Method to obtain custom UI media types.
  *
  * @return the custom UI media types.
  */
 public String getCustomUIMediaTypes() {
   return RegistryContext.getBaseInstance().customUIMediaTypes;
 }
 /**
  * Method to set collection media types.
  *
  * @param collectionMediaTypes the collection media types.
  */
 public void setCollectionMediaTypes(String collectionMediaTypes) {
   RegistryContext.getBaseInstance().collectionMediaTypes = collectionMediaTypes;
 }
 /**
  * Method to obtain collection media types.
  *
  * @return the collection media types.
  */
 public String getCollectionMediaTypes() {
   return RegistryContext.getBaseInstance().collectionMediaTypes;
 }
 /**
  * Method to set resource media types.
  *
  * @param resourceMediaTypes the resource media types.
  */
 public void setResourceMediaTypes(String resourceMediaTypes) {
   RegistryContext.getBaseInstance().resourceMediaTypes = resourceMediaTypes;
 }
 /**
  * Method to obtain resource media types.
  *
  * @return the resource media types.
  */
 public String getResourceMediaTypes() {
   return RegistryContext.getBaseInstance().resourceMediaTypes;
 }