<commit_before>/**
 * Copyright 2013 isandlaTech
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cohorte.remote.jsonrpc;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

import org.jabsorb.ng.client.ClientError;
import org.jabsorb.ng.client.HTTPSession;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Overrides the Jabsorb implementation of its HTTP Session
 * 
 * @author <NAME>
 */
public class JabsorbHttpSession extends HTTPSession {

    /** The session URL */
    private final URL pUrl;

    /**
     * Sets up the HTTP session
     * 
     * @param aUri
     *            The URI to use for communication
     * @throws MalformedURLException
     *             The given URI is malformed
     */
    public JabsorbHttpSession(final URI aUri) throws MalformedURLException {

        super(aUri);
        pUrl = aUri.toURL();
    }

    /**
     * Sends a POST request to the session URL with the given content
     * 
     * @param aRequestContent
     *            Request content
     * @return The result page
     * @throws ClientError
     *             Something wrong happened
     */
    protected String getUrlPostResult(final byte[] aRequestContent) {

        // Open a connection
        HttpURLConnection httpConnection = null;
        try {
            // Open the connection and cast it
            final URLConnection connection = pUrl.openConnection();
            if (!(connection instanceof HttpURLConnection)) {
                throw new ClientError("Unknown URL connection for : " + pUrl);
            }

            httpConnection = (HttpURLConnection) connection;

            // Make the connection writable (POST)
            httpConnection.setRequestMethod("POST");
            httpConnection.setDoOutput(true);

            // Set up the headers
            httpConnection
                    .addRequestProperty("Content-Type", JSON_CONTENT_TYPE);
            httpConnection.addRequestProperty("Content-Length",
                    Integer.toString(aRequestContent.length));

            // Set POST data
            httpConnection.getOutputStream().write(aRequestContent);

            // Wait for an answer
            final int responseCode = httpConnection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                throw new ClientError("Got HTTP Status " + responseCode
                        + " for URL " + pUrl);
            }

            /*
             * Read the response content See here for more information :
             * http://weblogs
             * .java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
             */
            return new Scanner(connection.getInputStream()).useDelimiter("\\A")
                    .next();

        } catch (final IOException e) {
            throw new ClientError(e);

        } finally {
            // In any case, free the connection
            if (httpConnection != null) {
                httpConnection.disconnect();
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.jabsorb.ng.client.HTTPSession#sendAndReceive(org.json.JSONObject)
     */
    @Override
    public JSONObject sendAndReceive(final JSONObject aMessage) {

        try {
            // Get the result and parse it
            final String result = getUrlPostResult(aMessage.toString()
                    .getBytes());
            return new JSONObject(result);

        } catch (final JSONException e) {<commit_msg>Ensure the Scanner/InputStream is closed after getUrlPostResult()<commit_after>/**
 * Copyright 2013 isandlaTech
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cohorte.remote.jsonrpc;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

import org.jabsorb.ng.client.ClientError;
import org.jabsorb.ng.client.HTTPSession;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Overrides the Jabsorb implementation of its HTTP Session
 * 
 * @author <NAME>
 */
public class JabsorbHttpSession extends HTTPSession {

    /** The session URL */
    private final URL pUrl;

    /**
     * Sets up the HTTP session
     * 
     * @param aUri
     *            The URI to use for communication
     * @throws MalformedURLException
     *             The given URI is malformed
     */
    public JabsorbHttpSession(final URI aUri) throws MalformedURLException {

        super(aUri);
        pUrl = aUri.toURL();
    }

    /**
     * Sends a POST request to the session URL with the given content
     * 
     * @param aRequestContent
     *            Request content
     * @return The result page
     * @throws ClientError
     *             Something wrong happened
     */
    protected String getUrlPostResult(final byte[] aRequestContent) {

        // Open a connection
        HttpURLConnection httpConnection = null;
        Scanner scanner = null;
        try {
            // Open the connection and cast it
            final URLConnection connection = pUrl.openConnection();
            if (!(connection instanceof HttpURLConnection)) {
                throw new ClientError("Unknown URL connection for : " + pUrl);
            }

            httpConnection = (HttpURLConnection) connection;

            // Make the connection writable (POST)
            httpConnection.setRequestMethod("POST");
            httpConnection.setDoOutput(true);

            // Set up the headers
            httpConnection
                    .addRequestProperty("Content-Type", JSON_CONTENT_TYPE);
            httpConnection.addRequestProperty("Content-Length",
                    Integer.toString(aRequestContent.length));

            // Set POST data
            httpConnection.getOutputStream().write(aRequestContent);

            // Wait for an answer
            final int responseCode = httpConnection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                throw new ClientError("Got HTTP Status " + responseCode
                        + " for URL " + pUrl);
            }

            // Use a scanner to read the response content See here for more
            // information:
            // http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
            scanner = new Scanner(connection.getInputStream());
            scanner.useDelimiter("\\A");
            return scanner.next();

        } catch (final IOException e) {
            // Convert error class
            throw new ClientError(e);

        } finally {
            // In any case, free the connection
            if (httpConnection != null) {
                httpConnection.disconnect();
            }

            // ... and close the scanner
            if (scanner != null) {
                scanner.close();
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.jabsorb.ng.client.HTTPSession#sendAndReceive(org.json.JSONObject)
     */
    @Override
    public JSONObject sendAndReceive(final JSONObject aMessage) {

        try {
            // Get the result and parse it
            final String result = getUrlPostResult(aMessage.toString()
                    .getBytes());
            return new JSONObject(result);

        } catch (final JSONException e) {
{'fraction_non_alphanumeric': 0.08502212904728627, 'fraction_numerical': 0.0034940600978336828, 'mean_word_length': 2.061319073083779, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2281914', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2118, 'n_words': 663}