.getInitialRequestContext());
// select the best variant for given policy in src
SelectedVariant selectedVariant = selectVariant(src);
if(selectedVariant == null) {
throw new XDIMEException("best selected variant not accessible");
}
AssetResolver assetResolver = pageContext.getAssetResolver();
// The external link rendered for device independent resource will have
// correct URL if used device support encoding from selected variant
// otherwise returned variant will be null because selected variant is
// filtered by PolicyVariantSelector#filter method
Variant variant = selectedVariant.getVariant();
VariantType variantType = null;
if(variant != null) {
variantType = variant.getVariantType();
}
// for MTXT resource
if (variantType == VariantType.TEXT) {
PolicyReferenceResolver resolver = pageContext
.getPolicyReferenceResolver();
TextAssetReference textReference = resolver
.resolveUnquotedTextExpression(src);
final String text = textReference.getText(TextEncoding.PLAIN);
if (text != null) {
// We have found the text, so let's try and write it out to
// attributes
attributes.setTextContainer(text);
} else {
if (logger.isDebugEnabled()) {
logger.debug("No text exists for text asset " + src);
}
}
// remove src pointed at text component
attributes.setSrc(null);
// remove srcType which contains list of device independent mime
// types
attributes.setSrcType(null);
}
// retrieve url only for IMAGE, AUDIO and VIDEO resource so far
String url = assetResolver.retrieveVariantURLAsString(selectedVariant);
// get dimensions for image and video and save it to params into
// attributes
if (variantType == VariantType.IMAGE
|| variantType == VariantType.VIDEO) {
PixelDimensionsMetaData dimMetaData =
(PixelDimensionsMetaData) selectedVariant
.getVariant().getMetaData();
// get the Map of parameters
Map params = attributes.getParamMap();
// save height and width in pixels unit to object's parameter map
if (dimMetaData.getHeight() != 0) {
params.put("height", StringConvertor.valueOf(dimMetaData
.getHeight()));
}
if (dimMetaData.getWidth() != 0) {
params.put("width", StringConvertor.valueOf(dimMetaData
.getWidth()));
}
}
// convert image if needed
if (variantType == VariantType.IMAGE) {
ImageMetaData imageMetaData = (ImageMetaData) selectedVariant
.getVariant().getMetaData();
// image must be convert
if (url != null
&& imageMetaData.getConversionMode() ==
ImageConversionMode.ALWAYS_CONVERT) {
url = assetResolver.rewriteURLWithPageURLRewriter(url,
PageURLType.IMAGE);
attributes.setConvertibleImageAsset(true);
}
}
if (url != null) {
// new mime types for device dependent resources
String mimeTypes = "";
Iterator encodingIterator = null;
// for each variant type get encoding and save its all mime types
// into attributes' srcType
if (variantType == VariantType.VIDEO) {
VideoMetaData metaData = (VideoMetaData) variant.getMetaData();
encodingIterator = metaData.getVideoEncoding().mimeTypes();
} else if (variantType == VariantType.AUDIO) {
AudioMetaData metaData = (AudioMetaData) variant.getMetaData();
encodingIterator = metaData.getAudioEncoding().mimeTypes();
} else if (variantType == VariantType.IMAGE) {
// This is incorrect if the asset is marked as
// <conversion-mode>always</conversion-mode>
// as we do not know what MAP will convert it to.
ImageMetaData metaData = (ImageMetaData) variant.getMetaData();
encodingIterator = metaData.getImageEncoding().mimeTypes();
}
int counter = 0;
while (encodingIterator != null && encodingIterator.hasNext()) {
if (counter > 0) {
mimeTypes += ",";
}
mimeTypes += (String) encodingIterator.next();
counter++;
}
attributes.setSrc(url);
attributes.setSrcType(mimeTypes);
// This method will reset the src and possibly the type if
// necessary. Otherwise it leaves them unchanged.
resetAsMapURL(context, variant, attributes);
} else {
// if url cannot be processed remove values src and srcType
// attribute
attributes.setSrc(null);
// remove srcType which contains list of device independent mime
// types
attributes.setSrcType(null);
}
VolantisProtocol protocol = ContextInternals.getMarinerPageContext(
context.getInitialRequestContext()).getProtocol();
try {
protocol.writeOpenObject(attributes);
// copy containedContent to current buffer
pageContext.getCurrentOutputBuffer().transferContentsFrom(bodyBuffer);
protocol.writeCloseObject(attributes);
} catch (ProtocolException e) {
throw new XDIMEException(e);
}
}