Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add Twilio SMS Example

Modify POM.XML to include apache commons dependency for baseEncoding
authentication headers.  Add One Jar to keep dependencies in JAR format.

Add Twilio SMS Example code and create a new EntryPointExtender for
TwilioSMS
  • Loading branch information...
commit 54a4836c1a620fcb08cdf5c9512e45725f295ada 1 parent 9fa11fe
Sidney Maestre authored
127 java/pom.xml
@@ -24,48 +24,89 @@
24 24 <artifactId>java-example</artifactId>
25 25 <packaging>jar</packaging>
26 26 <version>0.1.0-SNAPSHOT</version>
27   -
28   - <properties>
29   - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30   - <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
31   - <customcode.version>0.4.0</customcode.version>
32   - </properties>
33   -
34   - <dependencies>
35   - <dependency>
36   - <groupId>com.stackmob</groupId>
37   - <artifactId>customcode</artifactId>
38   - <version>${customcode.version}</version>
39   - <scope>provided</scope>
40   - </dependency>
41   - </dependencies>
42   -
43   - <build>
44   - <finalName>${project.artifactId}-${project.version}</finalName>
45   - <plugins>
46   - <plugin>
47   - <groupId>org.apache.maven.plugins</groupId>
48   - <artifactId>maven-compiler-plugin</artifactId>
49   - <version>2.3.2</version>
50   - <configuration>
51   - <source>1.6</source>
52   - <target>1.6</target>
53   - </configuration>
54   - </plugin>
55   - <plugin>
56   - <groupId>org.apache.maven.plugins</groupId>
57   - <artifactId>maven-jar-plugin</artifactId>
58   - <version>2.4</version>
59   - <configuration>
60   - <archive>
61   - <manifest>
62   - <mainClass>com.stackmob.example.EntryPointExtender</mainClass>
63   - </manifest>
64   - </archive>
65   - </configuration>
66   - </plugin>
67   - </plugins>
68   - </build>
69   -
  27 +
  28 + <properties>
  29 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  30 + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  31 + <customcode.version>0.4.4</customcode.version>
  32 + </properties>
  33 +
  34 + <dependencies>
  35 + <dependency>
  36 + <groupId>com.stackmob</groupId>
  37 + <artifactId>customcode</artifactId>
  38 + <version>${customcode.version}</version>
  39 + <scope>provided</scope>
  40 + </dependency>
  41 + <dependency>
  42 + <groupId>commons-codec</groupId>
  43 + <artifactId>commons-codec</artifactId>
  44 + <version>1.6</version>
  45 + </dependency>
  46 + </dependencies>
  47 +
  48 + <build>
  49 + <finalName>${project.artifactId}-${project.version}</finalName>
  50 + <plugins>
  51 + <plugin>
  52 + <groupId>org.apache.maven.plugins</groupId>
  53 + <artifactId>maven-compiler-plugin</artifactId>
  54 + <version>2.3.2</version>
  55 + <configuration>
  56 + <source>1.6</source>
  57 + <target>1.6</target>
  58 + </configuration>
  59 + </plugin>
  60 + <plugin>
  61 + <artifactId>maven-assembly-plugin</artifactId>
  62 + <configuration>
  63 + <archive>
  64 + <manifest>
  65 + <mainClass>com.stackmob.example.EntryPointExtender</mainClass>
  66 + </manifest>
  67 + </archive>
  68 + <descriptorRefs>
  69 + <descriptorRef>jar-with-dependencies</descriptorRef>
  70 + </descriptorRefs>
  71 + </configuration>
  72 + <executions>
  73 + <execution>
  74 + <phase>package</phase>
  75 + <goals>
  76 + <goal>single</goal>
  77 + </goals>
  78 + </execution>
  79 + </executions>
  80 + </plugin>
  81 +
  82 + <plugin>
  83 + <groupId>org.dstovall</groupId>
  84 + <artifactId>onejar-maven-plugin</artifactId>
  85 + <version>1.4.4</version>
  86 + <executions>
  87 + <execution>
  88 + <configuration>
  89 + <!-- Optional -->
  90 + <onejarVersion>0.97</onejarVersion>
  91 +
  92 + <!-- Optional, default is false -->
  93 + <attachToBuild>true</attachToBuild>
  94 + <!-- Optional, default is "onejar" -->
  95 + <classifier>onejar</classifier>
  96 + </configuration>
  97 + <goals>
  98 + <goal>one-jar</goal>
  99 + </goals>
  100 + </execution>
  101 + </executions>
  102 + </plugin>
  103 + </plugins>
  104 + </build>
  105 + <pluginRepositories>
  106 + <pluginRepository>
  107 + <id>onejar-maven-plugin.googlecode.com</id>
  108 + <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
  109 + </pluginRepository>
  110 + </pluginRepositories>
70 111 </project>
71 112
1  java/src/main/java/com/stackmob/example/EntryPointExtender.java
@@ -28,6 +28,7 @@
28 28 public List<CustomCodeMethod> methods() {
29 29 List<CustomCodeMethod> list = new ArrayList<CustomCodeMethod>();
30 30 list.add(new HelloWorld());
  31 + list.add(new TwilioSMS());
31 32 return list;
32 33 }
33 34
137 java/src/main/java/com/stackmob/example/TwilioSMS.java
... ... @@ -0,0 +1,137 @@
  1 +/**
  2 + * Copyright 2012 StackMob
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package com.stackmob.example;
  18 +
  19 +import com.stackmob.core.customcode.CustomCodeMethod;
  20 +import com.stackmob.core.rest.ProcessedAPIRequest;
  21 +import com.stackmob.core.rest.ResponseToProcess;
  22 +import com.stackmob.sdkapi.SDKServiceProvider;
  23 +
  24 +import com.stackmob.sdkapi.http.HttpService;
  25 +import com.stackmob.sdkapi.http.request.HttpRequest;
  26 +import com.stackmob.sdkapi.http.request.GetRequest;
  27 +import com.stackmob.sdkapi.http.response.HttpResponse;
  28 +import com.stackmob.core.ServiceNotActivatedException;
  29 +import com.stackmob.sdkapi.http.exceptions.AccessDeniedException;
  30 +import com.stackmob.sdkapi.http.exceptions.TimeoutException;
  31 +import java.net.MalformedURLException;
  32 +import com.stackmob.sdkapi.http.request.PostRequest;
  33 +import com.stackmob.sdkapi.http.Header;
  34 +import com.stackmob.sdkapi.LoggerService;
  35 +
  36 +import java.net.HttpURLConnection;
  37 +import java.util.ArrayList;
  38 +import java.util.HashMap;
  39 +import java.util.List;
  40 +import java.util.Map;
  41 +import java.util.HashSet;
  42 +import java.util.Set;
  43 +import org.apache.commons.codec.binary.Base64;
  44 +
  45 +public class TwilioSMS implements CustomCodeMethod {
  46 +
  47 + //Create your Twilio Acct at twilio.com and enter
  48 + //Your accountsid and accesstoken below.
  49 + static String accountsid = "XXXXXXXXXXXXXXX";
  50 + static String accesstoken = "XXXXXXXXXXXXXXX";
  51 +
  52 + @Override
  53 + public String getMethodName() {
  54 + return "twilio_sms";
  55 + }
  56 +
  57 + @Override
  58 + public List<String> getParams() {
  59 + return new ArrayList<String>();
  60 + }
  61 +
  62 + @Override
  63 + public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
  64 +
  65 + LoggerService logger = serviceProvider.getLoggerService(TwilioSMS.class);
  66 +
  67 + // You can pass to,from and body parameters from your client
  68 + //String to = request.getParams().get("to");
  69 + //String from = request.getParams().get("from");
  70 + //String body = request.getParams().get("body");
  71 +
  72 + // The TO phonenumber should be YOUR cel phone
  73 + // The FROM phonenumber should be one create in
  74 + // the twilio dashboard at twilio.com
  75 + String body = "To=4155551212&From=4083331234&Body=hello";
  76 +
  77 + int responseCode = 0;
  78 + String responseBody = "";
  79 +
  80 + String url = "https://api.twilio.com/2010-04-01/Accounts/" + accountsid + "/SMS/Messages.json";
  81 +
  82 + String pair = accountsid + ":" + accesstoken;
  83 +
  84 + // Base 64 Encode the accountsid/accesstoken
  85 + byte[] b =Base64.encodeBase64(pair.getBytes());
  86 + String encodedString = new String(b);
  87 +
  88 + Header accept = new Header("Accept-Charset", "utf-8");
  89 + Header auth = new Header("Authorization","Basic " + encodedString);
  90 + Header content = new Header("Content-Type", "application/x-www-form-urlencoded");
  91 +
  92 + Set<Header> set = new HashSet();
  93 + set.add(accept);
  94 + set.add(content);
  95 + set.add(auth);
  96 +
  97 + try {
  98 + HttpService http = serviceProvider.getHttpService();
  99 + try {
  100 + PostRequest req = new PostRequest(url,set,body);
  101 + try {
  102 + try {
  103 + HttpResponse resp = http.post(req);
  104 + responseCode = resp.getCode();
  105 + responseBody = resp.getBody();
  106 +
  107 + } catch(TimeoutException e) {
  108 + logger.error(e.getMessage(), e);
  109 + responseCode = -1;
  110 + responseBody = e.getMessage();
  111 + }
  112 + } catch(AccessDeniedException e) {
  113 + logger.error(e.getMessage(), e);
  114 + responseCode = -1;
  115 + responseBody = e.getMessage();
  116 + }
  117 +
  118 + } catch(MalformedURLException e) {
  119 + logger.error(e.getMessage(), e);
  120 + responseCode = -1;
  121 + responseBody = e.getMessage();
  122 + }
  123 +
  124 + } catch(ServiceNotActivatedException e) {
  125 + logger.error(e.getMessage(), e);
  126 + responseCode = -1;
  127 + responseBody = e.getMessage();
  128 + }
  129 +
  130 + Map<String, Object> map = new HashMap<String, Object>();
  131 + map.put("response_code", responseCode);
  132 + map.put("response_body", responseBody);
  133 +
  134 + return new ResponseToProcess(HttpURLConnection.HTTP_OK, map);
  135 + }
  136 +
  137 +}

0 comments on commit 54a4836

Please sign in to comment.
Something went wrong with that request. Please try again.