<commit_before>package chids;

import static com.google.common.base.Preconditions.checkArgument;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.OK;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;

import javax.ws.rs.WebApplicationException;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

public class SnsSubscriptionRequestFilter implements ContainerRequestFilter {

    @Override
    public ContainerRequest filter(final ContainerRequest request) {
        if(isPlainTextPost(request)) {
            interceptSubscriptionConfirmation(request);
        }
        return request;
    }

    private static boolean isPlainTextPost(final ContainerRequest request) {
        return "POST".equals(request.getMethod()) && TEXT_PLAIN_TYPE.isCompatible(request.getMediaType());
    }

    private static void interceptSubscriptionConfirmation(final ContainerRequest request) {
        try {
            final JsonNode payload = SnsOrchestrateProxy.JSON.readTree(request.getEntityInputStream());
            if(isSubscriptionConfirmation(payload)) {
                final URI confirmation = URI.create(payload.get("SubscribeURL").asText());
                final HttpURLConnection connection = (HttpURLConnection)confirmation.toURL().openConnection();
                connection.setRequestMethod("GET");
                checkArgument(2 == (connection.getResponseCode() / 100));
                throw new WebApplicationException(OK);
            }
        }
        catch(final IOException e) {
            throw new WebApplicationException(BAD_REQUEST);
        }
    }

    private static boolean isSubscriptionConfirmation(final JsonNode payload) {<commit_msg>Trying to have my cake while eating it<commit_after>package chids;

import static com.google.common.base.Preconditions.checkArgument;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.OK;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;

import javax.ws.rs.WebApplicationException;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.io.ByteStreams;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

public class SnsSubscriptionRequestFilter implements ContainerRequestFilter {

    @Override
    public ContainerRequest filter(final ContainerRequest request) {
        if(isPlainTextPost(request)) {
            interceptSubscriptionConfirmation(request);
        }
        return request;
    }

    private static boolean isPlainTextPost(final ContainerRequest request) {
        return "POST".equals(request.getMethod()) && TEXT_PLAIN_TYPE.isCompatible(request.getMediaType());
    }

    private static void interceptSubscriptionConfirmation(final ContainerRequest request) {
        try {
            final byte[] entity = readEntity(request);
            final JsonNode payload = SnsOrchestrateProxy.JSON.readTree(entity);
            if(isSubscriptionConfirmation(payload)) {
                final URI confirmation = URI.create(payload.get("SubscribeURL").asText());
                final HttpURLConnection connection = (HttpURLConnection)confirmation.toURL().openConnection();
                connection.setRequestMethod("GET");
                checkArgument(2 == (connection.getResponseCode() / 100));
                throw new WebApplicationException(OK);
            }
        }
        catch(final IOException e) {
            throw new WebApplicationException(BAD_REQUEST);
        }
    }

    private static byte[] readEntity(final ContainerRequest request) throws IOException {
        final byte[] entity = ByteStreams.toByteArray(request.getEntityInputStream());
        request.setEntityInputStream(new ByteArrayInputStream(entity));
        return entity;
    }

    private static boolean isSubscriptionConfirmation(final JsonNode payload) {
{'fraction_non_alphanumeric': 0.08820079756040347, 'fraction_numerical': 0.0018766127140511376, 'mean_word_length': 3.619718309859155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7553923', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1063, 'n_words': 213}