Search the FirstSpirit Knowledge Base
Hello All,
For a customer I am rewriting a Beanshell script to a Java module.
The script was generating an XML which could be used for an other system as an index of the available media components in the system and storing their internal URL's in its own database.
In order to generate this XML the code had to loop through all the media items and get its URL's with the following piece of code:
<portalUrl>$CMS_SET(portalUrl)$$CMS_REF(media:"jquery_142min",abs:1)$$CMS_END_SET$$CMS_VALUE(portalUrl.toString.convert2("Unicode to FOP entities"))$</portalUrl>
We would like the same functionality in the Java code.
I created an Java class which implements the Executable interface and therefore have a context object available.
1.)UrlCreatorProvider urlCreatorProvider = (UrlCreatorProvider) context.getProperty(GenerateTaskExecutor.CONTEXT_KEY_MEDIAFILESYSTEM);
2.)UrlCreatorProvider urlCreatorProvider = (UrlCreatorProvider) context.getProperty(GenerateTaskExecutor.CONTEXT_KEY_FILESYSTEM);
3.)UrlCreatorProvider urlCreatorProvider = (UrlCreatorProvider) context.getProperty(GenerateTaskExecutor.CONTEXT_KEY_URLCREATOR);
UrlCreator uc = urlCreatorProvider.getUrlCreator();
String url = uc.getUrl(media, masterLanguage, context.getProject().getResolutionByName("ORIGINAL"), UrlCreator.ABSOLUTE_URL);
I have tried all three UrlCreatorProvider properties, none of them however provides me with an UrlCreatorProvider. I checked and get a null pointer exception.
So I have looked into the context object and I see this object has only two properties available, none of them of any use to me:
property: path
property: #securityManager
Can someone tell me how I can get from this situation described above an URL generated from a media file?
Thanks in advance.
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.
See here: Media.getStoredUrl(..)
Hello Peter,
Thank you for your reply. I forgot to mention we are using FirstSpirit 4.2.499. So the given solution would probably work in the near future after the upgrade but not for our current system since this method is not available in 4.2.499 for the Media class.
Kind regards,
Nando
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.