<commit_before>/*

 * Copyright 2004-2005 the original author or authors.
 *
 * 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.
 */
package org.codehaus.groovy.grails.web.mime

import org.codehaus.groovy.grails.commons.ConfigurationHolder
import org.apache.commons.logging.*

/**
* @author <NAME>
* @since 1.0
*
* Created: Nov 23, 2007
*/
class DefaultAcceptHeaderParser implements AcceptHeaderParser{

    static final LOG = LogFactory.getLog(DefaultAcceptHeaderParser)
    public MimeType[] parse(String header) {
        def config = ConfigurationHolder.getConfig()
        def mimes = []
        def qualifiedMimes = []
        def mimeConfig = config?.grails?.mime?.types
        if(!mimeConfig) {
            LOG.warn "No mime types configured, defaulting to 'text/html'"
            return createDefaultMimeTypeConfig()
        }
        else if(!header) {
            for(entry in mimeConfig) {
                if(entry.value instanceof List) {
                    for(i in entry.value) {
                        mimes << new MimeType(i)
                        mimes[-1].extension = entry.key
                    }
                }
                else {
                    mimes << new MimeType(entry.value)
                    mimes[-1].extension = entry.key
                }
            }
        }
        else {
            def tokens = header.split(',')
            for(t in tokens) {
                if(t.indexOf(';') > -1) {
                    t = t.split(';')                         
                    def params = [:]
                    t[1..-1].each {
                        def i = it.indexOf('=')
                        params[it[0..i-1].trim()] = it[i+1..-1].trim()
                    }
                    def mimeList = params.q ? qualifiedMimes : mimes
                    createMimeTypeAndAddToList(t[0].trim(),mimeConfig, mimeList, params)
                }
                else {
                    createMimeTypeAndAddToList(t.trim(),mimeConfig, mimes)
                }
            }

            if(!mimes) {
               LOG.warn "No configured mime types found for Accept header: $header"
               return createDefaultMimeTypeConfig()
            }
        }
        return (qualifiedMimes.sort { it.parameters.q.toBigDecimal() }.reverse() + mimes) as MimeType[]
    }

    private createDefaultMimeTypeConfig() {<commit_msg>fix for GRAILS-1971, reduced log level<commit_after>/*

 * Copyright 2004-2005 the original author or authors.
 *
 * 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.
 */
package org.codehaus.groovy.grails.web.mime

import org.codehaus.groovy.grails.commons.ConfigurationHolder
import org.apache.commons.logging.*

/**
* @author <NAME>
* @since 1.0
*
* Created: Nov 23, 2007
*/
class DefaultAcceptHeaderParser implements AcceptHeaderParser{

    static final LOG = LogFactory.getLog(DefaultAcceptHeaderParser)
    public MimeType[] parse(String header) {
        def config = ConfigurationHolder.getConfig()
        def mimes = []
        def qualifiedMimes = []
        def mimeConfig = config?.grails?.mime?.types
        if(!mimeConfig) {
            LOG.debug "No mime types configured, defaulting to 'text/html'"
            return createDefaultMimeTypeConfig()
        }
        else if(!header) {
            for(entry in mimeConfig) {
                if(entry.value instanceof List) {
                    for(i in entry.value) {
                        mimes << new MimeType(i)
                        mimes[-1].extension = entry.key
                    }
                }
                else {
                    mimes << new MimeType(entry.value)
                    mimes[-1].extension = entry.key
                }
            }
        }
        else {
            def tokens = header.split(',')
            for(t in tokens) {
                if(t.indexOf(';') > -1) {
                    t = t.split(';')                         
                    def params = [:]
                    t[1..-1].each {
                        def i = it.indexOf('=')
                        params[it[0..i-1].trim()] = it[i+1..-1].trim()
                    }
                    def mimeList = params.q ? qualifiedMimes : mimes
                    createMimeTypeAndAddToList(t[0].trim(),mimeConfig, mimeList, params)
                }
                else {
                    createMimeTypeAndAddToList(t.trim(),mimeConfig, mimes)
                }
            }

            if(!mimes) {
               LOG.debug "No configured mime types found for Accept header: $header"
               return createDefaultMimeTypeConfig()
            }
        }
        return (qualifiedMimes.sort { it.parameters.q.toBigDecimal() }.reverse() + mimes) as MimeType[]
    }

    private createDefaultMimeTypeConfig() {
{'fraction_non_alphanumeric': 0.09905090595340811, 'fraction_numerical': 0.011044003451251078, 'mean_word_length': 1.657496561210454, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '379181', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1483, 'n_words': 419}