/** * 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; }
@Test public void testGetConfigFromImage() { AuthConfig reg1 = new AuthConfigBuilder() .withServeraddress("https://index.reg1.io/v1") .withAuth("reg1") .build(); AuthConfig reg2 = new AuthConfigBuilder().withServeraddress("https://reg2.io/v1").withAuth("reg2").build(); AuthConfig reg3 = new AuthConfigBuilder().withServeraddress("reg3.io").withAuth("reg3").build(); Config config = new ConfigBuilder() .addToAuthConfigs(reg1.getServeraddress(), reg1) .addToAuthConfigs(reg2.getServeraddress(), reg2) .addToAuthConfigs(reg3.getServeraddress(), reg3) .build(); AuthConfig auth1 = RegistryUtils.getConfigForImage("reg1.io/my/image", config); assertNotNull(auth1); assertEquals(reg1, auth1); }
/** * 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; }