diff --git a/war/WEB-INF/lib/json-simple-1.1.1.jar b/war/WEB-INF/lib/json-simple-1.1.1.jar new file mode 100644 index 0000000..ab6a87e Binary files /dev/null and b/war/WEB-INF/lib/json-simple-1.1.1.jar differ diff --git a/war/api-error.jsp b/war/api-error.jsp new file mode 100755 index 0000000..5dec5c2 --- /dev/null +++ b/war/api-error.jsp @@ -0,0 +1,4 @@ +<%@ page language="java" contentType="application/json; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@ page isErrorPage="true" %> +<%= exception %> diff --git a/war/api.jsp b/war/api.jsp new file mode 100644 index 0000000..84aa79e --- /dev/null +++ b/war/api.jsp @@ -0,0 +1,46 @@ +<%@ page language="java" contentType="application/json; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@ page trimDirectiveWhitespaces="true" %> +<%@ page import="uk.org.mafoo.wordsearch.*" %> +<%@ page import="java.util.*" %> +<%@ page import="org.apache.commons.lang.StringUtils" %> +<%@ page import="org.apache.commons.lang.StringEscapeUtils" %> +<%@ page import="org.json.simple.JSONObject" %> +<%@ page errorPage="api-error.jsp" %> +<% + + int height = Integer.parseInt(request.getParameter("height")); + int width = Integer.parseInt(request.getParameter("width")); + boolean simple = request.getParameter("simple") != null; + String name = StringEscapeUtils.escapeHtml(request.getParameter("name")); + + if (request.getParameter("words").length() > 2048) { throw new Exception("Input too large"); } + if (height > 100 || width > 100) { throw new Exception("Dimentions too large"); } + + List words = new ArrayList(); + for ( String line : request.getParameter("words").split(",")) { + words.add(line.trim()); + } + Collections.sort(words); + + char[][] grid = GridFactory.makeGrid(words, height, width, simple); + + List> grid_ar = new ArrayList>(); + for ( char[] row : grid ) { + ArrayList row_ar = new ArrayList(); + for ( char cell : row ) { + row_ar.add("" + cell); + } + grid_ar.add(row_ar); + } + + JSONObject resp = new JSONObject(); + + resp.put("grid", grid_ar); + resp.put("words", words); + resp.put("height", height); + resp.put("width", width); + resp.put("isSimple", simple); + +%> +<%= resp %>