It took me some time to find a solution to my problem. The way described in my question was a dead end. So I decided to try it in another way. I do not know if there are others whom would like to know how to build up an URL from a media file but for those who might be interested I have posted a solution here below.
First I have created a method which loops over the Object up towards the root parent where with each parent it passes the method collects the name of this object in a StringBuffer:
private String createUrl(final Media media, final Language language, final Project project) throws IOException {
StringBuilder sb = new StringBuilder();
if (media != null) {
sb.insert(0, media.getFilename());
sb.insert(0, "/");
IDProvider root = media;
boolean foundroot = false;
while (!foundroot) {
root = root.getParent();
if (root.getDisplayName(language).equals("root")) {
foundroot = true;
} else {
sb.insert(0, root.getUid());
sb.insert(0, "/");
}
}
getExtension(sb, media, project, language);
sb.insert(0, CONTENTSERVER);
}
return sb.toString();
}
Since each media object has an extension and there is a difference in getting an extension for a picture (because of the resolutions) and other media objects I have created two other methods:
private void getExtension(final StringBuilder fileName, final Media mediaContent, final Project project, final Language language) throws IOException {
if (mediaContent.getType() == Media.FILE) {
final File file = mediaContent.getFile(language);
fileName.append(".");
fileName.append(file.getExtension());
} else if (mediaContent.getType() == Media.PICTURE) {
final Picture picture = mediaContent.getPicture(language);
final String extension = this.getPictureExtension(project, picture);
if (extension != null) {
fileName.append(".");
fileName.append(extension);
}
}
}
private String getPictureExtension(final Project project, final Picture picture) throws IOException {
final List<Resolution> resolutions = project.getResolutions();
for (final Resolution resolution : resolutions) {
final PictureResolution pictureResolution = picture.getPictureResolution(resolution);
if (pictureResolution.getName().equalsIgnoreCase("ORIGINAL")) {
String extension = pictureResolution.getExtension();
if (extension != null) {
return extension;
} else {
return picture.getPictureMetaData(resolution).getExtension();
}
}
}
return null;
}
I haven't yet been able to test the functionality extensively but the first tests look promising. Also the code could still be written in cleaner style, this is still in progress.
I hope this might be usefull for someone.