* @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);
} finally {
try {
reader.close();
} catch (IOException ignore) {
}
}
registryContext.setResourceMediaTypes(mediaTypeString);
return mediaTypeString;
}