<commit_before>package org.jboss.aerogear.unifiedpush.test.sender.gcm;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.CharsetUtil;
import org.jboss.aerogear.unifiedpush.test.Tokens;
import org.jboss.aerogear.unifiedpush.test.sender.SenderStatisticsEndpoint;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

/**
 * Created by asaleh on 12/11/14.
 */
public class HttpMockingServerHandler extends SimpleChannelInboundHandler<Object> {

    private HttpRequest request;

    /**
     * Buffer that stores the response content
     */
    private final StringBuilder buf = new StringBuilder();

    private final StringBuilder requestContentBuffer = new StringBuilder();

    // TODO does this need to start at 1337?
    private static int multicastIdCounter = 1337;
    private static int messageIdCounter = 1337;

    private String uri = null;

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    private HashMap<String, Object> createResponse(ArrayList<String> regIds) {

        HashMap<String, Object> jsonResponse = new HashMap<String, Object>();

        ArrayList<HashMap<String, String>> out = new ArrayList<HashMap<String, String>>();

        int success = 0;
        int failures = 0;

        for (String s : regIds) {
            HashMap<String, String> hm = new HashMap<String, String>();

            if (s.toLowerCase().startsWith(Tokens.TOKEN_INVALIDATION_PREFIX)) {
                failures++;
                hm.put("error", "InvalidRegistration");
            } else {
                success++;
                hm.put("message_id", "1:" + messageIdCounter++);
            }

            out.add(hm);
        }

        jsonResponse.put("success", success);
        jsonResponse.put("multicast_id", multicastIdCounter++);
        jsonResponse.put("failure", failures);
        jsonResponse.put("results", out);

        jsonResponse.put("canonical_ids", 0);

        return jsonResponse;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext context, Object msg) {
        if (msg instanceof HttpRequest) {
            HttpRequest request = this.request = (HttpRequest) msg;
            uri = request.getUri();
            if (HttpHeaders.is100ContinueExpected(request)) {
                send100Continue(context);
            }
        }

        if (msg instanceof HttpContent) {
            HttpContent httpContent = (HttpContent) msg;
            ByteBuf content = httpContent.content();

            requestContentBuffer.append(content.toString(CharsetUtil.UTF_8));

            if (msg instanceof LastHttpContent) {
                buf.setLength(0);
                if (uri.contains("gcm")) {
                    ObjectMapper mapper = new ObjectMapper();
                    try {
                        GCMMessage message = mapper.readValue(requestContentBuffer.toString(), GCMMessage.class);
                        SenderStatisticsEndpoint.addGCMMessage(message);

                        requestContentBuffer.delete(0, requestContentBuffer.length());
                        try {
                            buf.append(mapper.writeValueAsString(this.createResponse(message.registrationIds)));
                        } catch (JsonProcessingException e) {
                            e.printStackTrace();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }


                writeResponse((HttpObject) msg, context);
            }
        }

    }


    private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
        // Decide whether to close the connection or not.
        boolean keepAlive = HttpHeaders.isKeepAlive(request);

        // Build the response object.
        FullHttpResponse response = new DefaultFullHttpResponse(<commit_msg>Add correct naming strategy for GCMMessage reading in proxy.<commit_after>package org.jboss.aerogear.unifiedpush.test.sender.gcm;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.CharsetUtil;
import org.jboss.aerogear.unifiedpush.test.Tokens;
import org.jboss.aerogear.unifiedpush.test.sender.SenderStatisticsEndpoint;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

/**
 * Created by asaleh on 12/11/14.
 */
public class HttpMockingServerHandler extends SimpleChannelInboundHandler<Object> {

    private HttpRequest request;

    /**
     * Buffer that stores the response content
     */
    private final StringBuilder buf = new StringBuilder();

    private final StringBuilder requestContentBuffer = new StringBuilder();

    // TODO does this need to start at 1337?
    private static int multicastIdCounter = 1337;
    private static int messageIdCounter = 1337;

    private String uri = null;

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    private HashMap<String, Object> createResponse(ArrayList<String> regIds) {

        HashMap<String, Object> jsonResponse = new HashMap<String, Object>();

        ArrayList<HashMap<String, String>> out = new ArrayList<HashMap<String, String>>();

        int success = 0;
        int failures = 0;

        for (String s : regIds) {
            HashMap<String, String> hm = new HashMap<String, String>();

            if (s.toLowerCase().startsWith(Tokens.TOKEN_INVALIDATION_PREFIX)) {
                failures++;
                hm.put("error", "InvalidRegistration");
            } else {
                success++;
                hm.put("message_id", "1:" + messageIdCounter++);
            }

            out.add(hm);
        }

        jsonResponse.put("success", success);
        jsonResponse.put("multicast_id", multicastIdCounter++);
        jsonResponse.put("failure", failures);
        jsonResponse.put("results", out);

        jsonResponse.put("canonical_ids", 0);

        return jsonResponse;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext context, Object msg) {
        if (msg instanceof HttpRequest) {
            HttpRequest request = this.request = (HttpRequest) msg;
            uri = request.getUri();
            if (HttpHeaders.is100ContinueExpected(request)) {
                send100Continue(context);
            }
        }

        if (msg instanceof HttpContent) {
            HttpContent httpContent = (HttpContent) msg;
            ByteBuf content = httpContent.content();

            requestContentBuffer.append(content.toString(CharsetUtil.UTF_8));

            if (msg instanceof LastHttpContent) {
                buf.setLength(0);
                if (uri.contains("gcm")) {
                    ObjectMapper mapper = new ObjectMapper();
                    mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
                    try {
                        GCMMessage message = mapper.readValue(requestContentBuffer.toString(), GCMMessage.class);
                        SenderStatisticsEndpoint.addGCMMessage(message);

                        requestContentBuffer.delete(0, requestContentBuffer.length());
                        try {
                            buf.append(mapper.writeValueAsString(this.createResponse(message.registrationIds)));
                        } catch (JsonProcessingException e) {
                            e.printStackTrace();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }


                writeResponse((HttpObject) msg, context);
            }
        }

    }


    private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
        // Decide whether to close the connection or not.
        boolean keepAlive = HttpHeaders.isKeepAlive(request);

        // Build the response object.
        FullHttpResponse response = new DefaultFullHttpResponse(
{'fraction_non_alphanumeric': 0.09543729934818562, 'fraction_numerical': 0.006615429516489931, 'mean_word_length': 2.6261022927689592, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, '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': '2377528', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2640, 'n_words': 542}