{"_id":"56d87228a957fb1500765a63","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"},"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"},"project":"568fce2a04440a1700e4cb47","user":"55116f88e2990b0d00fb0552","__v":8,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-03-03T17:19:36.664Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"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\nThis quickstart can be run using any server which can run Java servlets. For this example, 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 InboundMessage.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 InboundMessage {\\n\\tprivate long messageId;\\n\\tprivate Date date;\\n\\tprivate String sender;\\n\\tprivate String recipient;\\n\\tprivate String body;\\n\\n\\tpublic long getMessageId() {\\n\\t\\treturn messageId;\\n\\t}\\n\\n\\tpublic void setMessageId(long messageId) {\\n\\t\\tthis.messageId = messageId;\\n\\t}\\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 getSender() {\\n\\t\\treturn sender;\\n\\t}\\n\\n\\tpublic void setSender(String sender) {\\n\\t\\tthis.sender = sender;\\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 String getBody() {\\n\\t\\treturn body;\\n\\t}\\n\\n\\tpublic void setBody(String body) {\\n\\t\\tthis.body = body;\\n\\t}\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nCreate a new servlet called InboundServlet.java\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package com.dynmark;\\n\\nimport java.io.IOException;\\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 = { \\\"/inbound\\\" })\\npublic class InboundServlet extends HttpServlet {\\n\\tprivate static final long serialVersionUID = 1L;\\n\\tpublic static ArrayList<InboundMessage> inboundStore = new ArrayList<>();\\n\\n\\tprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\\n\\t    // Do some basic parameter validation before processing\\n\\t    if (request.getParameter(\\\"msgid\\\") != null)\\n\\t    {\\n\\t    \\tSimpleDateFormat dateFormat = new SimpleDateFormat(\\\"dd MMM yyyy HH:mm:ss\\\");\\n\\t    \\tInboundMessage message = new InboundMessage();\\n\\n\\t    \\ttry {\\n\\t\\t    \\tmessage.setMessageId(Long.parseLong(request.getParameter(\\\"msgid\\\")));\\n\\t\\t    \\tmessage.setDate(dateFormat.parse(request.getParameter(\\\"datetime\\\")));\\n\\t\\t\\t\\tmessage.setSender(request.getParameter(\\\"sender\\\"));\\n\\t\\t\\t\\tmessage.setRecipient(request.getParameter(\\\"recipient\\\"));\\n\\t            message.setBody(request.getParameter(\\\"content\\\"));\\n\\t\\t\\t} catch (ParseException e) {\\n\\t\\t\\t    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);\\n\\t\\t\\t}\\n\\t    \\t\\n\\t        inboundStore.add(message);\\n\\t    }\\n\\t\\t\\n\\t    response.setStatus(HttpServletResponse.SC_OK);\\n\\t}\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nWe parse each piece of message data that arrives on our 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 messages on multiple threads!\n\nNow lets build some web UI to view the receipts. Create InboundViewer.jsp in /WebContent, which will read from our message \"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>Inbound Message Viewer</title>\\n</head>\\n <%\\n \\tpageContext.setAttribute(\\\"inbounds\\\", com.dynmark.InboundServlet.inboundStore);\\n %>\\n\\n<body>\\n<table>\\n    <tr>\\n        <th>MessageId</th>\\n        <th>Date</th>\\n        <th>Sender</th>\\n        <th>Recipient</th>\\n        <th>Body</th>\\n    </tr>\\n\\n    <c:forEach var=\\\"inbound\\\" items=\\\"${pageScope.inbounds}\\\">\\n        <tr>\\n            <td><c:out value=\\\"${inbound.messageId}\\\"/></td>\\n            <td><c:out value=\\\"${inbound.date}\\\"/></td>\\n            <td><c:out value=\\\"${inbound.sender}\\\"/></td>\\n            <td><c:out value=\\\"${inbound.recipient}\\\"/></td>\\n            <td><c:out value=\\\"${inbound.body}\\\"/></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-messages","type":"basic","title":"Receive SMS Messages"}

Receive SMS Messages

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. This quickstart can be run using any server which can run Java servlets. For this example, 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 InboundMessage.java to represent the incoming data [block:code] { "codes": [ { "code": "package com.dynmark;\n\nimport java.util.Date;\n\npublic class InboundMessage {\n\tprivate long messageId;\n\tprivate Date date;\n\tprivate String sender;\n\tprivate String recipient;\n\tprivate String body;\n\n\tpublic long getMessageId() {\n\t\treturn messageId;\n\t}\n\n\tpublic void setMessageId(long messageId) {\n\t\tthis.messageId = messageId;\n\t}\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 getSender() {\n\t\treturn sender;\n\t}\n\n\tpublic void setSender(String sender) {\n\t\tthis.sender = sender;\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 String getBody() {\n\t\treturn body;\n\t}\n\n\tpublic void setBody(String body) {\n\t\tthis.body = body;\n\t}\n}", "language": "java" } ] } [/block] Create a new servlet called InboundServlet.java [block:code] { "codes": [ { "code": "package com.dynmark;\n\nimport java.io.IOException;\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 = { \"/inbound\" })\npublic class InboundServlet extends HttpServlet {\n\tprivate static final long serialVersionUID = 1L;\n\tpublic static ArrayList<InboundMessage> inboundStore = new ArrayList<>();\n\n\tprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n\t // Do some basic parameter validation before processing\n\t if (request.getParameter(\"msgid\") != null)\n\t {\n\t \tSimpleDateFormat dateFormat = new SimpleDateFormat(\"dd MMM yyyy HH:mm:ss\");\n\t \tInboundMessage message = new InboundMessage();\n\n\t \ttry {\n\t\t \tmessage.setMessageId(Long.parseLong(request.getParameter(\"msgid\")));\n\t\t \tmessage.setDate(dateFormat.parse(request.getParameter(\"datetime\")));\n\t\t\t\tmessage.setSender(request.getParameter(\"sender\"));\n\t\t\t\tmessage.setRecipient(request.getParameter(\"recipient\"));\n\t message.setBody(request.getParameter(\"content\"));\n\t\t\t} catch (ParseException e) {\n\t\t\t response.setStatus(HttpServletResponse.SC_BAD_REQUEST);\n\t\t\t}\n\t \t\n\t inboundStore.add(message);\n\t }\n\t\t\n\t response.setStatus(HttpServletResponse.SC_OK);\n\t}\n}", "language": "java" } ] } [/block] We parse each piece of message data that arrives on our 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 messages on multiple threads! Now lets build some web UI to view the receipts. Create InboundViewer.jsp in /WebContent, which will read from our message "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>Inbound Message Viewer</title>\n</head>\n <%\n \tpageContext.setAttribute(\"inbounds\", com.dynmark.InboundServlet.inboundStore);\n %>\n\n<body>\n<table>\n <tr>\n <th>MessageId</th>\n <th>Date</th>\n <th>Sender</th>\n <th>Recipient</th>\n <th>Body</th>\n </tr>\n\n <c:forEach var=\"inbound\" items=\"${pageScope.inbounds}\">\n <tr>\n <td><c:out value=\"${inbound.messageId}\"/></td>\n <td><c:out value=\"${inbound.date}\"/></td>\n <td><c:out value=\"${inbound.sender}\"/></td>\n <td><c:out value=\"${inbound.recipient}\"/></td>\n <td><c:out value=\"${inbound.body}\"/></td>\n </tr>\n </c:forEach>\n</table>\n</body>\n</html>\n", "language": "html", "name": "jsp" } ] } [/block]