<commit_before>package com.tinkerpop.rexster;

import com.tinkerpop.rexster.server.RexsterApplication;
import com.tinkerpop.rexster.extension.HttpMethod;
import com.tinkerpop.rexster.util.StatisticsHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONTokener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Base resource from which most other resources extend.  Exposes the request/response object and other
 * request and context information.
 *
 * @author <NAME> (http://stephen.genoprime.com)
 */
public abstract class BaseResource {

    private static Logger logger = Logger.getLogger(BaseResource.class);

    protected final List<Variant> producesVariantList = Variant.VariantListBuilder.newInstance().mediaTypes(
            MediaType.APPLICATION_JSON_TYPE,
            RexsterMediaType.APPLICATION_REXSTER_JSON_TYPE,
            RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON_TYPE).add().build();

    protected final StatisticsHelper sh = new StatisticsHelper();

    /**
     * This request object goes through the mapping of a URI to JSON.
     */
    private JSONObject requestObject = null;

    /**
     * This request object is just a single layered map of keys/values.
     */
    private JSONObject requestObjectFlat = null;

    protected JSONObject resultObject = new JSONObject();

    @Context
    private RexsterApplication rexsterApplication;

    @Context
    protected HttpServletRequest httpServletRequest;

    @Context
    protected UriInfo uriInfo;

    @Context
    protected ServletContext servletContext;

    @Context
    protected SecurityContext securityContext;

    public BaseResource(final RexsterApplication rexsterApplication) {
        if (rexsterApplication != null) {
            this.rexsterApplication = rexsterApplication;
        }

        sh.stopWatch();

        try {
            this.resultObject.put(Tokens.VERSION, Tokens.REXSTER_VERSION);
        } catch (JSONException ex) {
            JSONObject error = generateErrorObject(ex.getMessage());
            throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(error).build());
        }
    }

    public JSONObject generateErrorObject(String message) {
        return generateErrorObject(message, null);
    }

    public JSONObject generateErrorObjectJsonFail(Throwable source) {
        return generateErrorObject("An error occurred while generating the response object", source);
    }

    public JSONObject generateErrorObject(String message, Throwable source) {
        Map<String, String> m = new HashMap<String, String>();
        m.put(Tokens.MESSAGE, message);

        if (source != null) {
            m.put("error", source.getMessage());
        }

        // use a hashmap with the constructor so that a JSONException
        // will not be thrown
        return new JSONObject(m);
    }

    protected RexsterApplication getRexsterApplication() {
        return this.rexsterApplication;
    }

    protected String getUriPath() {
        String baseUri = "";
        if (this.uriInfo != null) {
            baseUri = this.uriInfo.getAbsolutePath().toString();

            if (!baseUri.endsWith("/")) {
                baseUri = baseUri + "/";
            }
        }

        return baseUri;
    }

    /**
     * Sets the request object.
     * <p/>
     * If this is set then the any previous call to getRequestObject which instantiated
     * the request object from the URI parameters will be overriden.
     *
     * @param jsonObject The JSON Object.
     */
    protected void setRequestObject(JSONObject jsonObject) {
        this.requestObject = jsonObject;
        this.requestObjectFlat = jsonObject;
    }

    public JSONObject getRequestObject() {
        return this.getRequestObject(true);
    }

    public JSONObject getRequestObjectFlat() {
        return this.getRequestObject(false);
    }

    /**
     * Gets the request object.
     * <p/>
     * If it does not exist then an attempt is made to parse the parameter list on
     * the URI into a JSON object.
     *
     * @return The request object.
     */
    public JSONObject getRequestObject(boolean parseToJson) {
        if (this.requestObject == null) {
            try {
                this.requestObject = new JSONObject();
                this.requestObjectFlat = new JSONObject();

                if (this.httpServletRequest != null) {
                    // unclear if this block of code is still necessary ???
                    Map<String, String[]> queryParameters = this.httpServletRequest.getParameterMap();
                    this.buildRequestObject(queryParameters);
                }

            } catch (JSONException ex) {

                logger.error(ex);

                JSONObject error = generateErrorObjectJsonFail(ex);
                throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(error).build());<commit_msg>Suppress grizzly warning messages #228<commit_after>package com.tinkerpop.rexster;

import com.tinkerpop.rexster.server.RexsterApplication;
import com.tinkerpop.rexster.extension.HttpMethod;
import com.tinkerpop.rexster.util.StatisticsHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONTokener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Base resource from which most other resources extend.  Exposes the request/response object and other
 * request and context information.
 *
 * @author <NAME> (http://stephen.genoprime.com)
 */
public abstract class BaseResource {

    private static Logger logger = Logger.getLogger(BaseResource.class);

    protected final List<Variant> producesVariantList = Variant.VariantListBuilder.newInstance().mediaTypes(
            MediaType.APPLICATION_JSON_TYPE,
            RexsterMediaType.APPLICATION_REXSTER_JSON_TYPE,
            RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON_TYPE).add().build();

    protected final StatisticsHelper sh = new StatisticsHelper();

    /**
     * This request object goes through the mapping of a URI to JSON.
     */
    private JSONObject requestObject = null;

    /**
     * This request object is just a single layered map of keys/values.
     */
    private JSONObject requestObjectFlat = null;

    protected JSONObject resultObject = new JSONObject();

    @Context
    private RexsterApplication rexsterApplication;

    @Context
    protected HttpServletRequest httpServletRequest;

    @Context
    protected UriInfo uriInfo;

    @Context
    protected ServletContext servletContext;

    @Context
    protected SecurityContext securityContext;

    public BaseResource(final RexsterApplication rexsterApplication) {
        if (rexsterApplication != null) {
            this.rexsterApplication = rexsterApplication;
        }

        sh.stopWatch();

        try {
            this.resultObject.put(Tokens.VERSION, Tokens.REXSTER_VERSION);
        } catch (JSONException ex) {
            JSONObject error = generateErrorObject(ex.getMessage());
            throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(error).build());
        }
    }

    public JSONObject generateErrorObject(String message) {
        return generateErrorObject(message, null);
    }

    public JSONObject generateErrorObjectJsonFail(Throwable source) {
        return generateErrorObject("An error occurred while generating the response object", source);
    }

    public JSONObject generateErrorObject(String message, Throwable source) {
        Map<String, String> m = new HashMap<String, String>();
        m.put(Tokens.MESSAGE, message);

        if (source != null) {
            m.put("error", source.getMessage());
        }

        // use a hashmap with the constructor so that a JSONException
        // will not be thrown
        return new JSONObject(m);
    }

    protected RexsterApplication getRexsterApplication() {
        return this.rexsterApplication;
    }

    protected String getUriPath() {
        String baseUri = "";
        if (this.uriInfo != null) {
            baseUri = this.uriInfo.getAbsolutePath().toString();

            if (!baseUri.endsWith("/")) {
                baseUri = baseUri + "/";
            }
        }

        return baseUri;
    }

    /**
     * Sets the request object.
     * <p/>
     * If this is set then the any previous call to getRequestObject which instantiated
     * the request object from the URI parameters will be overriden.
     *
     * @param jsonObject The JSON Object.
     */
    protected void setRequestObject(JSONObject jsonObject) {
        this.requestObject = jsonObject;
        this.requestObjectFlat = jsonObject;
    }

    public JSONObject getRequestObject() {
        return this.getRequestObject(true);
    }

    public JSONObject getRequestObjectFlat() {
        return this.getRequestObject(false);
    }

    /**
     * Gets the request object.
     * <p/>
     * If it does not exist then an attempt is made to parse the parameter list on
     * the URI into a JSON object.
     *
     * @return The request object.
     */
    public JSONObject getRequestObject(boolean parseToJson) {
        if (this.requestObject == null) {
            try {
                this.requestObject = new JSONObject();
                this.requestObjectFlat = new JSONObject();

                if (this.httpServletRequest != null && this.httpServletRequest.getParameterNames().hasMoreElements()) {
                    // unclear if this block of code is still necessary ???
                    Map<String, String[]> queryParameters = this.httpServletRequest.getParameterMap();
                    this.buildRequestObject(queryParameters);
                }

            } catch (JSONException ex) {

                logger.error(ex);

                JSONObject error = generateErrorObjectJsonFail(ex);
                throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(error).build());
{'fraction_non_alphanumeric': 0.08886925795053004, 'fraction_numerical': 0.00044169611307420494, 'mean_word_length': 2.8402306648575304, 'pattern_counts': {'":': 0, '<': 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': '1373668', 'n_tokens_mistral': 3052, 'n_tokens_neox': 2938, 'n_words': 754}