<commit_before>package controllers;

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

import models.Book;
import models.BookValidator;

import org.bson.types.ObjectId;
import org.codehaus.jackson.JsonNode;

import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.With;

import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

public class Rest extends Controller {

	@With(JsonAction.class)
	public static Result books(List<String> sort) {
		@SuppressWarnings("unchecked")
		List<Book> books = Collections.EMPTY_LIST;
		if (null == sort || sort.size() == 0) {
			books = MorphiaObject.dao.findAll();
		} else {
			Map<String, String> queries = new HashMap<String, String>();
			for (String field : sort) {
				String[] split = field.split(":");
				if (split.length == 2) {
					queries.put(split[0], split[1]);
				} else {
					queries.put(split[0], "asc");
				}
			}
			books = MorphiaObject.dao.findByQuery(queries);
		}
		JsonNode result = Json.toJson(books);
		return ok(result);
	}

	@With(JsonAction.class)
	@BodyParser.Of(BodyParser.Json.class)
	public static Result newBook() {
		JsonNode json = request().body().asJson();
		Book newBook = Json.fromJson(json, Book.class);
		BookValidator validator = new BookValidator();
		if (validator.validate(newBook)) {
			MorphiaObject.dao.save(newBook, new WriteConcern(true));
			JsonNode result = Json.toJson(newBook);
			return created(result);
		}
		return badRequest();
	}

	@With(JsonAction.class)
	public static Result getBook(String id) {
		Book book = MorphiaObject.dao.get(new ObjectId(id));
		if (null != book) {
			JsonNode result = Json.toJson(book);
			return ok(result);
		}
		return notFound();
	}

	@With(JsonAction.class)
	@BodyParser.Of(BodyParser.Json.class)
	public static Result updateBook(String id) {
		JsonNode json = request().body().asJson();
		Book updatedBook = Json.fromJson(json, Book.class);
		BookValidator validator = new BookValidator();
		if (validator.validate(updatedBook) && id.equals(updatedBook.getId().toString())) {
			try {
				Book saved = MorphiaObject.dao.safeUpdate(updatedBook);
				JsonNode result = Json.toJson(saved);
				return ok(result);
			} catch (ConcurrentModificationException e) {
				return status(CONFLICT);
			}
		}
		return notFound();
	}
<commit_msg>Reject messages with missing JSON body<commit_after>package controllers;

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

import models.Book;
import models.BookValidator;

import org.bson.types.ObjectId;
import org.codehaus.jackson.JsonNode;

import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.With;

import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

public class Rest extends Controller {

	@With(JsonAction.class)
	public static Result books(List<String> sort) {
		@SuppressWarnings("unchecked")
		List<Book> books = Collections.EMPTY_LIST;
		if (null == sort || sort.size() == 0) {
			books = MorphiaObject.dao.findAll();
		} else {
			Map<String, String> queries = new HashMap<String, String>();
			for (String field : sort) {
				String[] split = field.split(":");
				if (split.length == 2) {
					queries.put(split[0], split[1]);
				} else {
					queries.put(split[0], "asc");
				}
			}
			books = MorphiaObject.dao.findByQuery(queries);
		}
		JsonNode result = Json.toJson(books);
		return ok(result);
	}

	@With(JsonAction.class)
	@BodyParser.Of(BodyParser.Json.class)
	public static Result newBook() {
		JsonNode json = request().body().asJson();
		if (null == json) {
			return badRequest();
		}

		Book newBook = Json.fromJson(json, Book.class);
		BookValidator validator = new BookValidator();
		if (validator.validate(newBook)) {
			MorphiaObject.dao.save(newBook, new WriteConcern(true));
			JsonNode result = Json.toJson(newBook);
			return created(result);
		}
		return badRequest();
	}

	@With(JsonAction.class)
	public static Result getBook(String id) {
		Book book = MorphiaObject.dao.get(new ObjectId(id));
		if (null != book) {
			JsonNode result = Json.toJson(book);
			return ok(result);
		}
		return notFound();
	}

	@With(JsonAction.class)
	@BodyParser.Of(BodyParser.Json.class)
	public static Result updateBook(String id) {
		JsonNode json = request().body().asJson();
		if (null == json) {
			return badRequest();
		}

		Book updatedBook = Json.fromJson(json, Book.class);
		BookValidator validator = new BookValidator();
		if (validator.validate(updatedBook) && id.equals(updatedBook.getId().toString())) {
			try {
				Book saved = MorphiaObject.dao.safeUpdate(updatedBook);
				JsonNode result = Json.toJson(saved);
				return ok(result);
			} catch (ConcurrentModificationException e) {
				return status(CONFLICT);
			}
		}
		return notFound();
	}

{'fraction_non_alphanumeric': 0.13639082751744766, 'fraction_numerical': 0.0019940179461615153, 'mean_word_length': 4.347547974413646, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, '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': '1041807', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1644, 'n_words': 357}