All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.wcm.handler.mediasource.inline.InlineAsset Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2014 wcm.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.handler.mediasource.inline;

import io.wcm.handler.media.Asset;
import io.wcm.handler.media.Media;
import io.wcm.handler.media.MediaArgs;
import io.wcm.handler.media.Rendition;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.adapter.SlingAdaptable;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

/**
 * {@link Asset} implementation for inline media objects stored in a node in a content page.
 */
class InlineAsset extends SlingAdaptable implements Asset {

  private final Adaptable adaptable;
  private final Resource resource;
  private final Media media;
  private final MediaArgs defaultMediaArgs;
  private final String fileName;

  /**
   * @param resource Binary resource
   * @param media Media metadata
   * @param fileName File name
   */
  InlineAsset(Resource resource, Media media, String fileName, Adaptable adaptable) {
    this.resource = resource;
    this.media = media;
    this.defaultMediaArgs = media.getMediaRequest().getMediaArgs();
    this.fileName = fileName;
    this.adaptable = adaptable;
  }

  @Override
  public String getTitle() {
    return this.fileName;
  }

  @Override
  public String getAltText() {
    return this.defaultMediaArgs.getAltText();
  }

  @Override
  public String getDescription() {
    // not supported
    return null;
  }

  @Override
  public String getPath() {
    return this.resource.getPath();
  }

  @Override
  public ValueMap getProperties() {
    return this.resource.getValueMap();
  }

  @Override
  public Rendition getDefaultRendition() {
    return getRendition(this.defaultMediaArgs);
  }

  @Override
  public Rendition getRendition(MediaArgs mediaArgs) {
    Rendition rendition = getInlineRendition(mediaArgs);

    // check if rendition is valid - otherwise return null
    if (StringUtils.isEmpty(rendition.getUrl())) {
      rendition = null;
    }

    return rendition;
  }

  @Override
  public Rendition getImageRendition(MediaArgs mediaArgs) {
    Rendition rendition = getRendition(mediaArgs);
    if (rendition != null && rendition.isImage()) {
      return rendition;
    }
    else {
      return null;
    }
  }

  @Override
  public Rendition getFlashRendition(MediaArgs mediaArgs) {
    Rendition rendition = getRendition(mediaArgs);
    if (rendition != null && rendition.isFlash()) {
      return rendition;
    }
    else {
      return null;
    }
  }

  @Override
  public Rendition getDownloadRendition(MediaArgs mediaArgs) {
    Rendition rendition = getRendition(mediaArgs);
    if (rendition != null && rendition.isDownload()) {
      return rendition;
    }
    else {
      return null;
    }
  }

  /**
   * Get inline rendition instance.
   * @param mediaArgs Media args
   * @return Inline rendition instance (may be invalid rendition)
   */
  private Rendition getInlineRendition(MediaArgs mediaArgs) {
    return new InlineRendition(this.resource, this.media, mediaArgs, this.fileName, this.adaptable);
  }

  @Override
  @SuppressWarnings("unchecked")
  public  AdapterType adaptTo(Class type) {
    if (type == Resource.class) {
      return (AdapterType)this.resource;
    }
    return super.adaptTo(type);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy