{"_id":"56d6c28947f18a2d0088d620","project":"568fce2a04440a1700e4cb47","parentDoc":null,"category":{"_id":"56e92ef71996862200fd7f42","project":"568fce2a04440a1700e4cb47","__v":0,"version":"568fce2b04440a1700e4cb4a","pages":[],"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-03-16T10:01:27.194Z","from_sync":false,"order":3,"slug":"java-quickstarts","title":"Java Quickstarts"},"user":"55116f88e2990b0d00fb0552","__v":7,"version":{"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","__v":20,"createdAt":"2016-01-08T14:56:43.101Z","releaseDate":"2016-01-08T14:56:43.101Z","categories":["568fce2b04440a1700e4cb4b","568fd1b8b700ce0d002f4b1c","568fd23804440a1700e4cb5b","568fd2444719c119002ce5d8","568ff21204440a1700e4cbc1","5693732c8aa8040d009f2c28","5693738393445b0d00abdad0","5693740093445b0d00abdad1","56937445974aaa0d001ca699","5693b82173f48f0d0075c90d","5694c4cd1005590d0062cb25","569f854466a5640d00efa54c","56a264cdd15dd70d008d825b","56aa56bf318e6c1700a19ddb","56b0e6347ae4550d000627bd","56b200c0f48f270d00e0de6f","56b200c6f48f270d00e0de70","56b22a9665ddf50d0076ba40","56e92ef71996862200fd7f42","574d6577fb835c0e00ca316a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-03-02T10:38:01.238Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"\",\n  \"body\": \"Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/Java.zip\"\n}\n[/block]\n##Prerequisites\nYou'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing.\n\nThese quickstarts can be run using any server which can run Java Servlets. For these examples, we've used [Jetty](https://eclipse.org/jetty/) and jsp to provide a UI to view the receipts.\n\n1. Create a new Dynamic Website project\n2. Add the following libraries to the project\n * commons-logging-1.2.jar\n * httpasyncclient-4.1.1.jar\n * httpclient-4.5.1.jar\n * httpcore-4.4.4.jar\n * httpcore-nio-4.4.4.jar\n * servlet-api-3.0.jar\n * javax.servlet.jsp.jstl-1.2.0.v201105211821.jar\n\nCreate a file called Receipt.java to represent the incoming data\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package com.dynmark;\\n\\nimport java.util.Date;\\n\\npublic class Receipt {\\n\\tprivate Date date;\\n\\tprivate String recipient;\\n\\tprivate int statusId;\\n\\tprivate String statusDescription;\\n\\tprivate String clientRef;\\n\\n\\tpublic Date getDate() {\\n\\t\\treturn date;\\n\\t}\\n\\n\\tpublic void setDate(Date date) {\\n\\t\\tthis.date = date;\\n\\t}\\n\\n\\tpublic String getRecipient() {\\n\\t\\treturn recipient;\\n\\t}\\n\\n\\tpublic void setRecipient(String recipient) {\\n\\t\\tthis.recipient = recipient;\\n\\t}\\n\\n\\tpublic int getStatusId() {\\n\\t\\treturn statusId;\\n\\t}\\n\\n\\tpublic void setStatusId(int statusId) {\\n\\t\\tthis.statusId = statusId;\\n\\t}\\n\\n\\tpublic String getStatusDescription() {\\n\\t\\treturn statusDescription;\\n\\t}\\n\\n\\tpublic void setStatusDescription(String statusDescription) {\\n\\t\\tthis.statusDescription = statusDescription;\\n\\t}\\n\\n\\tpublic String getClientRef() {\\n\\t\\treturn clientRef;\\n\\t}\\n\\n\\tpublic void setClientRef(String clientRef) {\\n\\t\\tthis.clientRef = clientRef;\\n\\t}\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nCreate a new servlet called ReceiptServlet.java\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package com.dynmark;\\n\\nimport java.io.IOException;\\nimport java.lang.Integer;\\nimport java.text.ParseException;\\nimport java.text.SimpleDateFormat;\\nimport java.util.ArrayList;\\n\\nimport javax.servlet.ServletException;\\nimport javax.servlet.annotation.WebServlet;\\nimport javax.servlet.http.HttpServlet;\\nimport javax.servlet.http.HttpServletRequest;\\nimport javax.servlet.http.HttpServletResponse;\\n\\n:::at:::WebServlet(urlPatterns = { \\\"/receipt\\\" })\\npublic class ReceiptServlet extends HttpServlet {\\n\\tprivate static final long serialVersionUID = 1L;\\n\\tpublic static ArrayList<Receipt> receiptStore = new ArrayList<>();\\n\\n\\tprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\\n\\t\\t// Do some basic parameter validation before processing\\n\\t\\tif (request.getParameter(\\\"datetime\\\") != null)\\n\\t\\t{\\n\\t\\t\\tSimpleDateFormat dateFormat = new SimpleDateFormat(\\\"yyyy-MM-dd'T'HH:mm:ss.SSS\\\");\\n\\n\\t\\t\\tReceipt receipt = new Receipt();\\n\\t\\t\\ttry {\\n\\t\\t\\t\\treceipt.setDate(dateFormat.parse(request.getParameter(\\\"datetime\\\")));\\n\\t\\t\\t\\treceipt.setRecipient(request.getParameter(\\\"recipient\\\"));\\n\\t\\t\\t\\treceipt.setStatusId(Integer.parseInt(request.getParameter(\\\"statusid\\\")));\\n\\t\\t\\t\\treceipt.setStatusDescription(request.getParameter(\\\"statusdescription\\\"));\\n\\t\\t\\t\\treceipt.setClientRef(request.getParameter(\\\"clientref\\\"));\\n\\n\\t\\t\\t\\treceiptStore.add(receipt);\\n\\t\\t\\t} catch (ParseException e) {\\n\\t\\t\\t\\tresponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);\\n\\t\\t\\t}\\n\\t\\t}\\n\\n\\t\\tresponse.setStatus(HttpServletResponse.SC_OK);\\n\\t}\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nWe parse each piece of receipt data that arrives on out handler and push it into an ArrayList. In the real world we'd push receipts into a database or NoSQL storage via a decoupling buffer such as RabbitMQ and we'd also make sure we're threadsafe - we forward receipts on multiple threads!\n\nNow lets build some web UI to view the receipts. Create ReceiptViewer.jsp in /WebContent, which will read from our receipt \"storage\":\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<%@page import=\\\"java.util.ArrayList\\\"%>\\n<%@taglib uri=\\\"http://java.sun.com/jsp/jstl/core\\\" prefix=\\\"c\\\"%>\\n\\n<html>\\n<head>\\n<title>Receipt Viewer</title>\\n</head>\\n <%\\n \\tpageContext.setAttribute(\\\"receipts\\\", com.dynmark.ReceiptServlet.receiptStore);\\n %>\\n\\n<body>\\n<table>\\n    <tr>\\n        <th>ClientRef</th>\\n        <th>Received At</th>\\n        <th>Recipient</th>\\n        <th>StatusId</th>\\n        <th>Status Description</th>\\n    </tr>\\n\\n    <c:forEach var=\\\"receipt\\\" items=\\\"${pageScope.receipts}\\\">\\n        <tr>\\n            <td><c:out value=\\\"${receipt.clientRef}\\\"/></td>\\n            <td><c:out value=\\\"${receipt.date}\\\"/></td>\\n            <td><c:out value=\\\"${receipt.recipient}\\\"/></td>\\n            <td><c:out value=\\\"${receipt.statusId}\\\"/></td>\\n            <td><c:out value=\\\"${receipt.statusDescription}\\\"/></td>\\n        </tr>\\n    </c:forEach>\\n</table>\\n</body>\\n</html>\\n\",\n      \"language\": \"html\",\n      \"name\": \"jsp\"\n    }\n  ]\n}\n[/block]","excerpt":"Sending text messages via the REST API","slug":"qs-java-receive-sms-receipts","type":"basic","title":"Receive SMS Receipts"}

Receive SMS Receipts

Sending text messages via the REST API

[block:callout] { "type": "info", "title": "", "body": "Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/Java.zip" } [/block] ##Prerequisites You'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing. These quickstarts can be run using any server which can run Java Servlets. For these examples, we've used [Jetty](https://eclipse.org/jetty/) and jsp to provide a UI to view the receipts. 1. Create a new Dynamic Website project 2. Add the following libraries to the project * commons-logging-1.2.jar * httpasyncclient-4.1.1.jar * httpclient-4.5.1.jar * httpcore-4.4.4.jar * httpcore-nio-4.4.4.jar * servlet-api-3.0.jar * javax.servlet.jsp.jstl-1.2.0.v201105211821.jar Create a file called Receipt.java to represent the incoming data [block:code] { "codes": [ { "code": "package com.dynmark;\n\nimport java.util.Date;\n\npublic class Receipt {\n\tprivate Date date;\n\tprivate String recipient;\n\tprivate int statusId;\n\tprivate String statusDescription;\n\tprivate String clientRef;\n\n\tpublic Date getDate() {\n\t\treturn date;\n\t}\n\n\tpublic void setDate(Date date) {\n\t\tthis.date = date;\n\t}\n\n\tpublic String getRecipient() {\n\t\treturn recipient;\n\t}\n\n\tpublic void setRecipient(String recipient) {\n\t\tthis.recipient = recipient;\n\t}\n\n\tpublic int getStatusId() {\n\t\treturn statusId;\n\t}\n\n\tpublic void setStatusId(int statusId) {\n\t\tthis.statusId = statusId;\n\t}\n\n\tpublic String getStatusDescription() {\n\t\treturn statusDescription;\n\t}\n\n\tpublic void setStatusDescription(String statusDescription) {\n\t\tthis.statusDescription = statusDescription;\n\t}\n\n\tpublic String getClientRef() {\n\t\treturn clientRef;\n\t}\n\n\tpublic void setClientRef(String clientRef) {\n\t\tthis.clientRef = clientRef;\n\t}\n}", "language": "java" } ] } [/block] Create a new servlet called ReceiptServlet.java [block:code] { "codes": [ { "code": "package com.dynmark;\n\nimport java.io.IOException;\nimport java.lang.Integer;\nimport java.text.ParseException;\nimport java.text.SimpleDateFormat;\nimport java.util.ArrayList;\n\nimport javax.servlet.ServletException;\nimport javax.servlet.annotation.WebServlet;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\n\n@WebServlet(urlPatterns = { \"/receipt\" })\npublic class ReceiptServlet extends HttpServlet {\n\tprivate static final long serialVersionUID = 1L;\n\tpublic static ArrayList<Receipt> receiptStore = new ArrayList<>();\n\n\tprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n\t\t// Do some basic parameter validation before processing\n\t\tif (request.getParameter(\"datetime\") != null)\n\t\t{\n\t\t\tSimpleDateFormat dateFormat = new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss.SSS\");\n\n\t\t\tReceipt receipt = new Receipt();\n\t\t\ttry {\n\t\t\t\treceipt.setDate(dateFormat.parse(request.getParameter(\"datetime\")));\n\t\t\t\treceipt.setRecipient(request.getParameter(\"recipient\"));\n\t\t\t\treceipt.setStatusId(Integer.parseInt(request.getParameter(\"statusid\")));\n\t\t\t\treceipt.setStatusDescription(request.getParameter(\"statusdescription\"));\n\t\t\t\treceipt.setClientRef(request.getParameter(\"clientref\"));\n\n\t\t\t\treceiptStore.add(receipt);\n\t\t\t} catch (ParseException e) {\n\t\t\t\tresponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);\n\t\t\t}\n\t\t}\n\n\t\tresponse.setStatus(HttpServletResponse.SC_OK);\n\t}\n}", "language": "java" } ] } [/block] We parse each piece of receipt data that arrives on out handler and push it into an ArrayList. In the real world we'd push receipts into a database or NoSQL storage via a decoupling buffer such as RabbitMQ and we'd also make sure we're threadsafe - we forward receipts on multiple threads! Now lets build some web UI to view the receipts. Create ReceiptViewer.jsp in /WebContent, which will read from our receipt "storage": [block:code] { "codes": [ { "code": "<%@page import=\"java.util.ArrayList\"%>\n<%@taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\"%>\n\n<html>\n<head>\n<title>Receipt Viewer</title>\n</head>\n <%\n \tpageContext.setAttribute(\"receipts\", com.dynmark.ReceiptServlet.receiptStore);\n %>\n\n<body>\n<table>\n <tr>\n <th>ClientRef</th>\n <th>Received At</th>\n <th>Recipient</th>\n <th>StatusId</th>\n <th>Status Description</th>\n </tr>\n\n <c:forEach var=\"receipt\" items=\"${pageScope.receipts}\">\n <tr>\n <td><c:out value=\"${receipt.clientRef}\"/></td>\n <td><c:out value=\"${receipt.date}\"/></td>\n <td><c:out value=\"${receipt.recipient}\"/></td>\n <td><c:out value=\"${receipt.statusId}\"/></td>\n <td><c:out value=\"${receipt.statusDescription}\"/></td>\n </tr>\n </c:forEach>\n</table>\n</body>\n</html>\n", "language": "html", "name": "jsp" } ] } [/block]