Search in sources :

Example 1 with MimeTypeService

use of org.apache.sling.commons.mime.MimeTypeService in project sling by apache.

the class SlingHttpContext method getMimeType.

// ---------- HttpContext interface ----------------------------------------
/**
     * Returns the MIME type as resolved by the <code>MimeTypeService</code> or
     * <code>null</code> if the service is not available.
     */
@Override
public String getMimeType(String name) {
    MimeTypeService mtservice = mimeTypeService;
    if (mtservice != null) {
        return mtservice.getMimeType(name);
    }
    log.debug("getMimeType: MimeTypeService not available, cannot resolve mime type for {}", name);
    return null;
}
Also used : MimeTypeService(org.apache.sling.commons.mime.MimeTypeService)

Example 2 with MimeTypeService

use of org.apache.sling.commons.mime.MimeTypeService in project sling by apache.

the class ContentLoader method detectMimeTypeFromName.

/**
     * Detected mime type from name (file extension) using Mime Type service.
     * Fallback to application/octet-stream.
     * @param name Node name
     * @return Mime type (never null)
     */
private String detectMimeTypeFromName(String name) {
    String mimeType = null;
    String fileExtension = StringUtils.substringAfterLast(name, ".");
    if (bundleContext != null && StringUtils.isNotEmpty(fileExtension)) {
        ServiceReference<MimeTypeService> ref = bundleContext.getServiceReference(MimeTypeService.class);
        if (ref != null) {
            MimeTypeService mimeTypeService = (MimeTypeService) bundleContext.getService(ref);
            mimeType = mimeTypeService.getMimeType(fileExtension);
        }
    }
    return StringUtils.defaultString(mimeType, CONTENTTYPE_OCTET_STREAM);
}
Also used : MimeTypeService(org.apache.sling.commons.mime.MimeTypeService)

Aggregations

MimeTypeService (org.apache.sling.commons.mime.MimeTypeService)2