<commit_before>package org.jcouchdb.db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.Header;
import org.apache.log4j.Logger;
import org.jcouchdb.document.InstanceCachable;
import org.svenson.JSONParser;

/**
 * Encapsulates a couchdb server response with error code and received
 * body.
 *
 * @author shelmberger
 *
 */
public class Response
{
    protected static Logger log = Logger.getLogger(Response.class);

    private int code;
    private byte[] content;

    private JSONParser parser;

    private Map<Class,Object> responseCache = new HashMap<Class,Object>();

    private String stringContent;

    private Header[] headers;

    public Response(int code, byte[] content)
    {
        this(code,content,null);
    }

    public Response(int code, String content)
    {
        this(code,content,null);
    }

    public Response(int code, byte[] content, Header[] headers)
    {
        this.code = code;
        this.content = content;
        this.headers = headers;

        if (log.isDebugEnabled())
        {
            log.debug(this);
        }
    }

    public Response(int code, String content, Header[] headers)
    {
        this.code = code;
        this.content = null;
        this.stringContent = content;
        this.headers = headers;

        if (log.isDebugEnabled())
        {
            log.debug(this);
        }
    }

    public void setParser(JSONParser parser)
    {
        this.parser = parser;
    }

    private JSONParser getParser()
    {
        if (parser == null)
        {
            parser = new JSONParser();
        }
        return parser;
    }

    public int getCode()
    {
        return code;
    }

    public byte[] getContent()
    {
        if (content == null)
        {
            content = stringContent.getBytes();
        }

        return content;
    }

    public String getContentAsString()
    {
        if (stringContent == null)
        {
            stringContent = new String(content);
            content = null;
        }
        return stringContent;
    }

    /**
     * Returns the contents of the response as List
     * @return
     */
    public List getContentAsList()
    {
        return getParser().parse(List.class, getContentAsString());
    }

    /**
     * Returns the contents of the response as Map
     * @return
     */
    public Map getContentAsMap()
    {
        return getParser().parse(Map.class, getContentAsString());
    }

    /**
     * Returns the contents of the response as bean of the
     * given type.
     *
     * @return
     */
    public <T> T getContentAsBean(Class<T> cls)
    {
        T cached = null;

        boolean instanceCachable = cls.getAnnotation(InstanceCachable.class) != null;

        if (instanceCachable)
        {
            cached = (T)responseCache.get(cls);
        }
        if (cached == null)
        {
            cached = getParser().parse(cls, getContentAsString());

            if (instanceCachable)
            {
                responseCache.put(cls, cached);
            }
        }
        return cached;
    }

    public Header[] getResponseHeaders()
    {
        return headers;
    }

    /**
     * Returns <code>true</code> if the response code is
     * between 200 and 299
     * @return
     */
    public boolean isOk()
    {
        return code >= 200 && code <= 299;
    }

    @Override
    public String toString()
    {
        return super.toString()+": code = "+code+", content = "+( stringContent != null ? stringContent : new String(content));
    }
}

<commit_msg>set content output logging to trace<commit_after>package org.jcouchdb.db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.Header;
import org.apache.log4j.Logger;
import org.jcouchdb.document.InstanceCachable;
import org.svenson.JSONParser;

/**
 * Encapsulates a couchdb server response with error code and received
 * body.
 *
 * @author shelmberger
 *
 */
public class Response
{
    protected static Logger log = Logger.getLogger(Response.class);

    private int code;
    private byte[] content;

    private JSONParser parser;

    private Map<Class,Object> responseCache = new HashMap<Class,Object>();

    private String stringContent;

    private Header[] headers;

    public Response(int code, byte[] content)
    {
        this(code,content,null);
    }

    public Response(int code, String content)
    {
        this(code,content,null);
    }

    public Response(int code, byte[] content, Header[] headers)
    {
        this.code = code;
        this.content = content;
        this.headers = headers;

        if (log.isTraceEnabled())
        {
            log.trace(this);
        }
    }

    public Response(int code, String content, Header[] headers)
    {
        this.code = code;
        this.content = null;
        this.stringContent = content;
        this.headers = headers;

        if (log.isTraceEnabled())
        {
            log.trace(this);
        }
    }

    public void setParser(JSONParser parser)
    {
        this.parser = parser;
    }

    private JSONParser getParser()
    {
        if (parser == null)
        {
            parser = new JSONParser();
        }
        return parser;
    }

    public int getCode()
    {
        return code;
    }

    public byte[] getContent()
    {
        if (content == null)
        {
            content = stringContent.getBytes();
        }

        return content;
    }

    public String getContentAsString()
    {
        if (stringContent == null)
        {
            stringContent = new String(content);
            content = null;
        }
        return stringContent;
    }

    /**
     * Returns the contents of the response as List
     * @return
     */
    public List getContentAsList()
    {
        return getParser().parse(List.class, getContentAsString());
    }

    /**
     * Returns the contents of the response as Map
     * @return
     */
    public Map getContentAsMap()
    {
        return getParser().parse(Map.class, getContentAsString());
    }

    /**
     * Returns the contents of the response as bean of the
     * given type.
     *
     * @return
     */
    public <T> T getContentAsBean(Class<T> cls)
    {
        T cached = null;

        boolean instanceCachable = cls.getAnnotation(InstanceCachable.class) != null;

        if (instanceCachable)
        {
            cached = (T)responseCache.get(cls);
        }
        if (cached == null)
        {
            cached = getParser().parse(cls, getContentAsString());

            if (instanceCachable)
            {
                responseCache.put(cls, cached);
            }
        }
        return cached;
    }

    public Header[] getResponseHeaders()
    {
        return headers;
    }

    /**
     * Returns <code>true</code> if the response code is
     * between 200 and 299
     * @return
     */
    public boolean isOk()
    {
        return code >= 200 && code <= 299;
    }

    @Override
    public String toString()
    {
        return super.toString()+": code = "+code+", content = "+( stringContent != null ? stringContent : new String(content));
    }
}


{'fraction_non_alphanumeric': 0.10648275862068965, 'fraction_numerical': 0.003586206896551724, 'mean_word_length': 1.8865445859872612, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 17, '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': '2343646', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2012, 'n_words': 521}