Search in sources :

Example 1 with MimeType

use of grails.web.mime.MimeType in project grails-core by grails.

the class DataBindingUtils method createDataBindingSource.

public static DataBindingSource createDataBindingSource(GrailsApplication grailsApplication, Class bindingTargetType, Object bindingSource) {
    final DataBindingSourceRegistry registry = getDataBindingSourceRegistry(grailsApplication);
    final MimeType mimeType = getMimeType(grailsApplication, bindingSource);
    return registry.createDataBindingSource(mimeType, bindingTargetType, bindingSource);
}
Also used : DataBindingSourceRegistry(org.grails.web.databinding.bindingsource.DataBindingSourceRegistry) DefaultDataBindingSourceRegistry(org.grails.web.databinding.bindingsource.DefaultDataBindingSourceRegistry) MimeType(grails.web.mime.MimeType)

Example 2 with MimeType

use of grails.web.mime.MimeType in project grails-core by grails.

the class MimeTypesConfiguration method mimeTypes.

@Bean("mimeTypes")
@Primary
protected MimeType[] mimeTypes() {
    final Config config = grailsApplication.getConfig();
    final Map<CharSequence, Object> mimeConfig = getMimeConfig(config);
    MimeType[] mimeTypes;
    if (mimeConfig == null || mimeConfig.isEmpty()) {
        mimeTypes = MimeType.createDefaults();
        return mimeTypes;
    } else {
        List<MimeType> mimes = new ArrayList<>();
        for (Map.Entry<CharSequence, Object> entry : mimeConfig.entrySet()) {
            final String key = entry.getKey().toString();
            final Object v = entry.getValue();
            if (v instanceof List) {
                List list = (List) v;
                for (Object i : list) {
                    mimes.add(new MimeType(i.toString(), key));
                }
            } else {
                mimes.add(new MimeType(v.toString(), key));
            }
        }
        final List<MimeTypeProvider> mimeTypeProviders = this.mimeTypeProviders;
        processProviders(mimes, mimeTypeProviders);
        final Map<String, MimeTypeProvider> childTypes = applicationContext.getBeansOfType(MimeTypeProvider.class);
        processProviders(mimes, childTypes.values());
        mimeTypes = mimes.toArray(new MimeType[0]);
        return mimeTypes;
    }
}
Also used : Config(grails.config.Config) ArrayList(java.util.ArrayList) MimeType(grails.web.mime.MimeType) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) MimeTypeProvider(grails.web.mime.MimeTypeProvider) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 3 with MimeType

use of grails.web.mime.MimeType in project grails-core by grails.

the class GrailsConventionGroovyPageLocator method lookupRequestFormat.

protected String lookupRequestFormat() {
    if (mimeTypeResolver != null) {
        MimeType mimeType = mimeTypeResolver.resolveResponseMimeType();
        if (mimeType != null) {
            return mimeType.getExtension();
        }
    } else {
        GrailsWebRequest webRequest = GrailsWebRequest.lookup();
        if (webRequest != null) {
            HttpServletRequest request = webRequest.getCurrentRequest();
            Object format = request.getAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT);
            return format == null ? null : format.toString();
        }
    }
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) MimeType(grails.web.mime.MimeType)

Example 4 with MimeType

use of grails.web.mime.MimeType in project grails-core by grails.

the class DataBindingUtils method createCollectionDataBindingSource.

public static CollectionDataBindingSource createCollectionDataBindingSource(GrailsApplication grailsApplication, Class bindingTargetType, Object bindingSource) {
    final DataBindingSourceRegistry registry = getDataBindingSourceRegistry(grailsApplication);
    final MimeType mimeType = getMimeType(grailsApplication, bindingSource);
    return registry.createCollectionDataBindingSource(mimeType, bindingTargetType, bindingSource);
}
Also used : DataBindingSourceRegistry(org.grails.web.databinding.bindingsource.DataBindingSourceRegistry) DefaultDataBindingSourceRegistry(org.grails.web.databinding.bindingsource.DefaultDataBindingSourceRegistry) MimeType(grails.web.mime.MimeType)

Example 5 with MimeType

use of grails.web.mime.MimeType in project grails-core by grails.

the class WebUtils method getFormatFromURI.

/**
 * Obtains the format from the URI. The format is the string following the . file extension in the last token of the URI.
 * If nothing comes after the ".", this method assumes that there is no format and returns <code>null</code>.
 *
 * @param uri The URI
 * @param mimeTypes The configured mime types
 * @return The format or null if none
 */
public static String getFormatFromURI(String uri, MimeType[] mimeTypes) {
    if (uri.endsWith("/")) {
        return null;
    }
    int idx = uri.lastIndexOf('/');
    if (idx > -1) {
        String lastToken = uri.substring(idx + 1, uri.length());
        idx = lastToken.lastIndexOf('.');
        if (idx > -1 && idx != lastToken.length() - 1) {
            String extension = lastToken.substring(idx + 1, lastToken.length());
            if (mimeTypes != null) {
                for (MimeType mimeType : mimeTypes) {
                    if (mimeType.getExtension().equals(extension))
                        return extension;
                }
            }
        }
    }
    return null;
}
Also used : MimeType(grails.web.mime.MimeType)

Aggregations

MimeType (grails.web.mime.MimeType)5 DataBindingSourceRegistry (org.grails.web.databinding.bindingsource.DataBindingSourceRegistry)2 DefaultDataBindingSourceRegistry (org.grails.web.databinding.bindingsource.DefaultDataBindingSourceRegistry)2 Config (grails.config.Config)1 MimeTypeProvider (grails.web.mime.MimeTypeProvider)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)1 Bean (org.springframework.context.annotation.Bean)1 Primary (org.springframework.context.annotation.Primary)1