<commit_before>package com.amee.base.resource;

import com.amee.base.domain.Version;
import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class RequestWrapper implements Serializable {

    private String target = "";
    private Version version;
    private Map<String, String> attributes = new HashMap<String, String>();
    private Map<String, String> matrixParameters = new HashMap<String, String>();
    private Map<String, String> queryParameters = new HashMap<String, String>();
    private Map<String, String> formParameters = new HashMap<String, String>();
    private byte[] body = null;
    private String mediaType = "";

    public RequestWrapper() {
        super();
    }

    public RequestWrapper(
            String target,
            Version version,
            Map<String, String> attributes) {
        this();
        setTarget(target);
        setVersion(version);
        setAttributes(attributes);
    }

    public RequestWrapper(
            String target,
            Version version,
            Map<String, String> attributes,
            Map<String, String> matrixParameters,
            Map<String, String> queryParameters) {
        this(target, version, attributes);
        setMatrixParameters(matrixParameters);
        setQueryParameters(queryParameters);
    }

    public RequestWrapper(
            String target,
            Version version,
            Map<String, String> attributes,
            Map<String, String> matrixParameters,
            Map<String, String> queryParameters,
            Map<String, String> formParameters) {
        this(target, version, attributes, matrixParameters, queryParameters);
        setFormParameters(formParameters);
        setMediaType("application/x-www-form-urlencoded");
    }

    public RequestWrapper(
            String target,
            Version version,
            Map<String, String> attributes,
            Map<String, String> matrixParameters,
            Map<String, String> queryParameters,
            InputStream body,
            String mediaType) {
        this(target, version, attributes, matrixParameters, queryParameters);
        setBody(body);
        setMediaType(mediaType);
    }

    public RequestWrapper(JSONObject obj) {
        super();
        try {
            setTarget(obj.getString("target"));
            setVersion(new Version(obj.getString("version")));
            addToMap(getAttributes(), obj, "attributes");
            addToMap(getMatrixParameters(), obj, "matrixParameters");
            addToMap(getQueryParameters(), obj, "queryParameters");
            addToMap(getFormParameters(), obj, "formParameters");
            setMediaType(obj.getString("mediaType"));
        } catch (JSONException e) {
            throw new RuntimeException("Caught JSONException: " + e.getMessage(), e);
        }
    }

    public JSONObject toJSONObject() {
        try {
            JSONObject obj = new JSONObject();
            obj.put("target", getTarget());
            obj.put("version", getVersion().toString());
            obj.put("attributes", new JSONObject(getAttributes()));
            obj.put("matrixParameters", new JSONObject(getMatrixParameters()));
            obj.put("queryParameters", new JSONObject(getQueryParameters()));
            obj.put("formParameters", new JSONObject(getFormParameters()));
            obj.put("mediaType", getMediaType());
            return obj;
        } catch (JSONException e) {
            throw new RuntimeException("Caught JSONException: " + e.getMessage(), e);
        }
    }

    protected void addToMap(Map<String, String> m, JSONObject obj, String name) throws JSONException {
        JSONObject node = obj.getJSONObject(name);
        for (Iterator i = node.keys(); i.hasNext();) {
            String key = (String) i.next();
            if (!node.isNull(key)) {
                m.put(key, node.getString(key));
            } else {
                m.put(key, "");
            }
        }
    }

    protected void setMapFromMap(Map<String, String> target, Map<String, String> source) {
        target.clear();
        if (source != null) {
            for (String key : source.keySet()) {
                target.put(key, source.get(key) != null ? source.get(key) : "");
            }
        }
    }

    public Version getVersion() {
        return version;
    }

    public void setVersion(Version version) {
        this.version = version;
    }

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, String> attributes) {
        setMapFromMap(this.attributes, attributes);
    }

    public Map<String, String> getMatrixParameters() {
        return matrixParameters;
    }

    public void setMatrixParameters(Map<String, String> matrixParameters) {
        setMapFromMap(this.matrixParameters, matrixParameters);
    }

    public Map<String, String> getQueryParameters() {
        return queryParameters;
    }

    public void setQueryParameters(Map<String, String> queryParameters) {<commit_msg>PL-1754 - Added acceptedMediaTypes attribute to RequestWrapper. Removed target from RequestWrapper constructor.<commit_after>package com.amee.base.resource;

import com.amee.base.domain.Version;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class RequestWrapper implements Serializable {

    private String target = "";
    private Version version;
    private Set<String> acceptedMediaTypes = new HashSet<String>();
    private Map<String, String> attributes = new HashMap<String, String>();
    private Map<String, String> matrixParameters = new HashMap<String, String>();
    private Map<String, String> queryParameters = new HashMap<String, String>();
    private Map<String, String> formParameters = new HashMap<String, String>();
    private byte[] body = null;
    private String mediaType = "";

    public RequestWrapper() {
        super();
    }

    public RequestWrapper(
            Version version,
            Set<String> acceptedMediaTypes,
            Map<String, String> attributes) {
        this();
        setVersion(version);
        setAcceptedMediaTypes(acceptedMediaTypes);
        setAttributes(attributes);
    }

    public RequestWrapper(
            Version version,
            Set<String> acceptedMediaTypes,
            Map<String, String> attributes,
            Map<String, String> matrixParameters,
            Map<String, String> queryParameters) {
        this(version, acceptedMediaTypes, attributes);
        setMatrixParameters(matrixParameters);
        setQueryParameters(queryParameters);
    }

    public RequestWrapper(
            Version version,
            Set<String> acceptedMediaTypes,
            Map<String, String> attributes,
            Map<String, String> matrixParameters,
            Map<String, String> queryParameters,
            Map<String, String> formParameters) {
        this(version, acceptedMediaTypes, attributes, matrixParameters, queryParameters);
        setFormParameters(formParameters);
        setMediaType("application/x-www-form-urlencoded");
    }

    public RequestWrapper(
            Version version,
            Set<String> acceptedMediaTypes,
            Map<String, String> attributes,
            Map<String, String> matrixParameters,
            Map<String, String> queryParameters,
            InputStream body,
            String mediaType) {
        this(version, acceptedMediaTypes, attributes, matrixParameters, queryParameters);
        setBody(body);
        setMediaType(mediaType);
    }

    public RequestWrapper(JSONObject obj) {
        super();
        try {
            setTarget(obj.getString("target"));
            setVersion(new Version(obj.getString("version")));
            addToSet(getAcceptedMediaTypes(), obj, "acceptedMediaTypes");
            addToMap(getAttributes(), obj, "attributes");
            addToMap(getMatrixParameters(), obj, "matrixParameters");
            addToMap(getQueryParameters(), obj, "queryParameters");
            addToMap(getFormParameters(), obj, "formParameters");
            setMediaType(obj.getString("mediaType"));
        } catch (JSONException e) {
            throw new RuntimeException("Caught JSONException: " + e.getMessage(), e);
        }
    }

    public JSONObject toJSONObject() {
        try {
            JSONObject obj = new JSONObject();
            obj.put("target", getTarget());
            obj.put("version", getVersion().toString());
            obj.put("acceptedMediaTypes", new JSONArray(getAcceptedMediaTypes()));
            obj.put("attributes", new JSONObject(getAttributes()));
            obj.put("matrixParameters", new JSONObject(getMatrixParameters()));
            obj.put("queryParameters", new JSONObject(getQueryParameters()));
            obj.put("formParameters", new JSONObject(getFormParameters()));
            obj.put("mediaType", getMediaType());
            return obj;
        } catch (JSONException e) {
            throw new RuntimeException("Caught JSONException: " + e.getMessage(), e);
        }
    }

    protected void addToMap(Map<String, String> m, JSONObject obj, String name) throws JSONException {
        JSONObject node = obj.getJSONObject(name);
        for (Iterator i = node.keys(); i.hasNext();) {
            String key = (String) i.next();
            if (!node.isNull(key)) {
                m.put(key, node.getString(key));
            } else {
                m.put(key, "");
            }
        }
    }

    protected void addToSet(Set<String> s, JSONObject obj, String name) throws JSONException {
        JSONArray arr = obj.getJSONArray(name);
        for (int i = 0; i < arr.length(); i++) {
            s.add((String) arr.get(i));
        }
    }

    protected void setMapFromMap(Map<String, String> target, Map<String, String> source) {
        target.clear();
        if (source != null) {
            for (String key : source.keySet()) {
                target.put(key, source.get(key) != null ? source.get(key) : "");
            }
        }
    }

    public Version getVersion() {
        return version;
    }

    public void setVersion(Version version) {
        this.version = version;
    }

    public Set<String> getAcceptedMediaTypes() {
        return acceptedMediaTypes;
    }

    public void setAcceptedMediaTypes(Set<String> acceptedMediaTypes) {
        this.acceptedMediaTypes.clear();
        if (acceptedMediaTypes != null) {
            this.acceptedMediaTypes.addAll(acceptedMediaTypes);
        }
    }

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, String> attributes) {
        setMapFromMap(this.attributes, attributes);
    }

    public Map<String, String> getMatrixParameters() {
        return matrixParameters;
    }

    public void setMatrixParameters(Map<String, String> matrixParameters) {
        setMapFromMap(this.matrixParameters, matrixParameters);
    }

    public Map<String, String> getQueryParameters() {
        return queryParameters;
    }

    public void setQueryParameters(Map<String, String> queryParameters) {
{'fraction_non_alphanumeric': 0.10905525238744884, 'fraction_numerical': 0.00042633015006821284, 'mean_word_length': 2.617828500925355, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 68, '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': '2492888', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2940, 'n_words': 705}