<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 */
package org.searchisko.api.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.Singleton;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.ws.rs.core.MultivaluedMap;

import org.elasticsearch.common.joda.time.format.ISODateTimeFormat;
import org.searchisko.api.model.QuerySettings;
import org.searchisko.api.model.SortByValue;
import org.searchisko.api.service.SearchService;

/**
 * Search Query parameters parser component.
 * 
 * @author <NAME>
 * @author <NAME>
 * @author <NAME> (velias at redhat dot com)
 */
@Named
@ApplicationScoped
@Singleton
public class QuerySettingsParser {

	private final static Logger log = Logger.getLogger(QuerySettingsParser.class.getName());

	/**
	 * Parse REST parameters, validate them, sanity them, and store into {@link QuerySettings} bean.
	 * 
	 * @param params REST request params to parse
	 * @return query settings instance filled with valid search settings
	 * @throws IllegalArgumentException if some param has invalid value. Message from exception contains parameter name
	 *           and is used for error handling later!
	 */
	public QuerySettings parseUriParams(MultivaluedMap<String, String> params) throws IllegalArgumentException {

		QuerySettings settings = new QuerySettings();
		if (params == null) {
			settings.getFiltersInit();
			return settings;
		}

		// Make copy of all param keys. Remove key from this copy each time a particular param is processed.
		// The idea is to process the defined parameters first (and remove relevant keys) and then process
		// the rest, where the rest can match configured filters.
		Set<String> paramKeys = params.keySet();

		// process query
		for (String key = QuerySettings.QUERY_KEY; paramKeys.contains(key); paramKeys.remove(key)) {
			settings.setQuery(normalizeQueryString(params.getFirst(key)));
		}

		// process highlighting
		for (String key = QuerySettings.QUERY_HIGHLIGHT_KEY; paramKeys.contains(key); paramKeys.remove(key)) {
			settings.setQueryHighlight(readBooleanParam(params, key));
		}

		// process fields
		for (String key = QuerySettings.FIELDS_KEY; paramKeys.contains(key); paramKeys.remove(key)) {
			settings.setFields(normalizeListParam(params.get(key)));<commit_msg>Fix, we have to make copy of param set.<commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 */
package org.searchisko.api.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.Singleton;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.ws.rs.core.MultivaluedMap;

import org.elasticsearch.common.joda.time.format.ISODateTimeFormat;
import org.searchisko.api.model.QuerySettings;
import org.searchisko.api.model.SortByValue;
import org.searchisko.api.service.SearchService;

/**
 * Search Query parameters parser component.
 * 
 * @author <NAME>
 * @author <NAME>
 * @author <NAME> (velias at redhat dot com)
 */
@Named
@ApplicationScoped
@Singleton
public class QuerySettingsParser {

	private final static Logger log = Logger.getLogger(QuerySettingsParser.class.getName());

	/**
	 * Parse REST parameters, validate them, sanity them, and store into {@link QuerySettings} bean.
	 * 
	 * @param params REST request params to parse
	 * @return query settings instance filled with valid search settings
	 * @throws IllegalArgumentException if some param has invalid value. Message from exception contains parameter name
	 *           and is used for error handling later!
	 */
	public QuerySettings parseUriParams(final MultivaluedMap<String, String> params) throws IllegalArgumentException {

		QuerySettings settings = new QuerySettings();
		if (params == null) {
			settings.getFiltersInit();
			return settings;
		}

		// Make copy of all param keys. Remove key from this copy each time a particular param is processed.
		// The idea is to process the defined parameters first (and remove relevant keys) and then process
		// the rest, where the rest can match configured filters.
		Set<String> paramKeys = new TreeSet<>(params.keySet());

		// process query
		for (String key = QuerySettings.QUERY_KEY; paramKeys.contains(key); paramKeys.remove(key)) {
			settings.setQuery(normalizeQueryString(params.getFirst(key)));
		}

		// process highlighting
		for (String key = QuerySettings.QUERY_HIGHLIGHT_KEY; paramKeys.contains(key); paramKeys.remove(key)) {
			settings.setQueryHighlight(readBooleanParam(params, key));
		}

		// process fields
		for (String key = QuerySettings.FIELDS_KEY; paramKeys.contains(key); paramKeys.remove(key)) {
			settings.setFields(normalizeListParam(params.get(key)));
{'fraction_non_alphanumeric': 0.09622458857696031, 'fraction_numerical': 0.001548886737657309, 'mean_word_length': 4.639737991266376, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6000164', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1410, 'n_words': 459}