<commit_before>/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is Web Questionnaires 2
 *
 * The Initial Owner of the Original Code is European Environment
 * Agency. Portions created by TripleDev are Copyright
 * (C) European Environment Agency.  All Rights Reserved.
 *
 * Contributor(s):
 *        <NAME>
 */
package eionet.webq.converter;

import eionet.webq.dto.CdrRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.util.Base64;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;

/**
 * Converts {@link javax.servlet.http.HttpServletRequest} to {@link eionet.webq.dto.CdrRequest}.
 *
 * @see Converter
 */
@Component
public class CdrRequestConverter implements Converter<HttpServletRequest, CdrRequest> {
    /**
     * Basic authorization prefix.
     */
    private static final String BASIC_AUTHORIZATION_PREFIX = "Basic ";

    @Override
    public CdrRequest convert(HttpServletRequest httpRequest) {
        QueriedParametersTracker request = new QueriedParametersTracker(httpRequest);
        CdrRequest parameters = new CdrRequest();
        parameters.setEnvelopeUrl(request.getParameter("envelope"));
        parameters.setSchema(request.getParameter("schema"));
        parameters.setLanguage(request.getParameter("language"));
        parameters.setJavascriptEnabled(Boolean.valueOf(request.getParameter("JavaScript")));
        parameters.setNewFormCreationAllowed(Boolean.valueOf(request.getParameter("add")));
        parameters.setNewFileName(request.getParameter("file_id"));
        parameters.setInstanceUrl(request.getParameter("instance"));
        parameters.setInstanceTitle(request.getParameter("instance_title"));

        String authorizationHeader = httpRequest.getHeader("Authorization");
        if (hasBasicAuthorization(authorizationHeader)) {
            setAuthorizationDetails(parameters, extractCredentialsFromBasicAuthorization(authorizationHeader));
        }
        parameters.setAdditionalParametersAsQueryString(createQueryString(request.getNotReadParametersWithValues()));
        return parameters;
    }

    /**
     * Set authorization details.
     *
     * @param parameters parameters.
     * @param credentials credentials.
     */
    private void setAuthorizationDetails(CdrRequest parameters, String[] credentials) {
        if (credentials.length != 2) {
            return;
        }
        parameters.setAuthorizationSet(true);
        parameters.setUserName(credentials[0]);
        parameters.setPassword(<PASSWORD>[1]);
    }

    /**
     * Decodes and parses basic authentication header to username and password.
     *
     * @param authHeader authentication header
     * @return username and password or empty array in case of error.
     */
    private String[] extractCredentialsFromBasicAuthorization(String authHeader) {
        String encodedCredentials = authHeader.substring(BASIC_AUTHORIZATION_PREFIX.length());
        String credentials = new String(Base64.decodeBase64(encodedCredentials));
        return credentials.split(":");
    }

    /**
     * Check whether authorization header contains basic authorization data.
     *
     * @param authorizationHeader authorization header value
     * @return is basic authorization is present
     */
    private boolean hasBasicAuthorization(String authorizationHeader) {
        return StringUtils.isNotEmpty(authorizationHeader) && authorizationHeader.startsWith(BASIC_AUTHORIZATION_PREFIX);
    }

    /**
     * Produces http request compatible query string part.
     * E.g. &foo=bar&number=42
     *
     * @param notReadParametersWithValues parameters with values to be transformed to such string
     * @return query string part
     */
    private String createQueryString(Map<String, String> notReadParametersWithValues) {
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, String> entry : notReadParametersWithValues.entrySet()) {
            builder.append('&').append(entry.getKey()).append('=').append(entry.getValue());
        }
        return builder.toString();
    }

    /**
     * Class wraps {@link javax.servlet.http.HttpServletRequest} to keep track of parameters queried.
     */
    private final class QueriedParametersTracker {
        /**
         * Http request.
         */
        private HttpServletRequest request;
        /**
         * Parameters that were queried.
         */
        private Collection<String> parametersRead = new ArrayList<String>();

        /**
         * Constructs new instance.
         *
         * @param request http request to be queried for parameters.
         */
        private QueriedParametersTracker(HttpServletRequest request) {
            this.request = request;
        }

        /**
         * Returns parameter value from wrapped {@link javax.servlet.http.HttpServletRequest}.
         * Remembers parameter name that was queried.
         *
         * @param parameterName parameter name
         * @return parameter value
         */
        public String getParameter(String parameterName) {
            parametersRead.add(parameterName);
            return request.getParameter(parameterName);
        }

        /**
         * Returns map of parameters and values that was not queried before.
         *
         * @return map of parameters with values, not queried previously.
         */
        public Map<String, String> getNotReadParametersWithValues() {
            Map<String, String> notReadParametersWithValues = new TreeMap<String, String>();
            for (String parameterName : request.getParameterMap().keySet()) {
                if (!parametersRead.contains(parameterName)) {
                    notReadParametersWithValues.put(parameterName, request.getParameter(parameterName));
                }
            }
            return notReadParametersWithValues;
        }
    }
}<commit_msg>no need to create not read parameters map. Refs #16741<commit_after>/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is Web Questionnaires 2
 *
 * The Initial Owner of the Original Code is European Environment
 * Agency. Portions created by TripleDev are Copyright
 * (C) European Environment Agency.  All Rights Reserved.
 *
 * Contributor(s):
 *        <NAME>
 */
package eionet.webq.converter;

import eionet.webq.dto.CdrRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.util.Base64;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Converts {@link javax.servlet.http.HttpServletRequest} to {@link eionet.webq.dto.CdrRequest}.
 *
 * @see Converter
 */
@Component
public class CdrRequestConverter implements Converter<HttpServletRequest, CdrRequest> {
    /**
     * Basic authorization prefix.
     */
    private static final String BASIC_AUTHORIZATION_PREFIX = "Basic ";

    @Override
    public CdrRequest convert(HttpServletRequest httpRequest) {
        QueriedParametersTracker parametersTracker = new QueriedParametersTracker(httpRequest);

        CdrRequest parameters = new CdrRequest();
        parameters.setEnvelopeUrl(parametersTracker.getParameter("envelope"));
        parameters.setSchema(parametersTracker.getParameter("schema"));
        parameters.setLanguage(parametersTracker.getParameter("language"));
        parameters.setJavascriptEnabled(Boolean.valueOf(parametersTracker.getParameter("JavaScript")));
        parameters.setNewFormCreationAllowed(Boolean.valueOf(parametersTracker.getParameter("add")));
        parameters.setNewFileName(parametersTracker.getParameter("file_id"));
        parameters.setInstanceUrl(parametersTracker.getParameter("instance"));
        parameters.setInstanceTitle(parametersTracker.getParameter("instance_title"));

        String authorizationHeader = httpRequest.getHeader("Authorization");
        if (hasBasicAuthorization(authorizationHeader)) {
            setAuthorizationDetails(parameters, extractCredentialsFromBasicAuthorization(authorizationHeader));
        }
        parameters.setAdditionalParametersAsQueryString(createQueryStringFromParametersNotRead(parametersTracker));
        return parameters;
    }

    /**
     * Set authorization details.
     *
     * @param parameters parameters.
     * @param credentials credentials.
     */
    private void setAuthorizationDetails(CdrRequest parameters, String[] credentials) {
        if (credentials.length != 2) {
            return;
        }
        parameters.setAuthorizationSet(true);
        parameters.setUserName(credentials[0]);
        parameters.setPassword(<PASSWORD>[1]);
    }

    /**
     * Decodes and parses basic authentication header to username and password.
     *
     * @param authHeader authentication header
     * @return username and password or empty array in case of error.
     */
    private String[] extractCredentialsFromBasicAuthorization(String authHeader) {
        String encodedCredentials = authHeader.substring(BASIC_AUTHORIZATION_PREFIX.length());
        String credentials = new String(Base64.decodeBase64(encodedCredentials));
        return credentials.split(":");
    }

    /**
     * Check whether authorization header contains basic authorization data.
     *
     * @param authorizationHeader authorization header value
     * @return is basic authorization is present
     */
    private boolean hasBasicAuthorization(String authorizationHeader) {
        return StringUtils.isNotEmpty(authorizationHeader) && authorizationHeader.startsWith(BASIC_AUTHORIZATION_PREFIX);
    }

    /**
     * Produces http request compatible query string part.
     * E.g. &foo=bar&number=42
     *
     * @param parameters holder of all parameters with queried parameters tracking
     * @return query string part
     */
    private String createQueryStringFromParametersNotRead(QueriedParametersTracker parameters) {
        StringBuilder builder = new StringBuilder();
        for (String entry : parameters.getAllParameterNames()) {
            if (parameters.isNotReadParameter(entry)) {
                builder.append('&').append(entry).append('=').append(parameters.getParameter(entry));
            }
        }
        return builder.toString();
    }

    /**
     * Class wraps {@link javax.servlet.http.HttpServletRequest} to keep track of parameters queried.
     */
    private final class QueriedParametersTracker {
        /**
         * Http request.
         */
        private HttpServletRequest request;
        /**
         * Parameters that were queried.
         */
        private Collection<String> parametersRead = new ArrayList<String>();

        /**
         * Constructs new instance.
         *
         * @param request http request to be queried for parameters.
         */
        private QueriedParametersTracker(HttpServletRequest request) {
            this.request = request;
        }

        /**
         * Returns parameter value from wrapped {@link javax.servlet.http.HttpServletRequest}.
         * Remembers parameter name that was queried.
         *
         * @param parameterName parameter name
         * @return parameter value
         */
        public String getParameter(String parameterName) {
            parametersRead.add(parameterName);
            return request.getParameter(parameterName);
        }

        /**
         * Check whether parameter with such name were read.
         *
         * @param parameterName parameter name
         * @return is not parameter read
         */
        public boolean isNotReadParameter(String parameterName) {
            return !parametersRead.contains(parameterName);
        }

        public Collection<String> getAllParameterNames() {
            return request.getParameterMap().keySet();
        }
    }
}
{'fraction_non_alphanumeric': 0.08768154922001076, 'fraction_numerical': 0.0026896180742334587, 'mean_word_length': 3.0937401698647373, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 19, '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': '6718739', 'n_tokens_mistral': 3137, 'n_tokens_neox': 3094, 'n_words': 838}