Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
View
@@ -24,48 +24,89 @@
<artifactId>java-example</artifactId>
<packaging>jar</packaging>
<version>0.1.0-SNAPSHOT</version>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <customcode.version>0.4.0</customcode.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>com.stackmob</groupId>
- <artifactId>customcode</artifactId>
- <version>${customcode.version}</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
-
- <build>
- <finalName>${project.artifactId}-${project.version}</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <version>2.4</version>
- <configuration>
- <archive>
- <manifest>
- <mainClass>com.stackmob.example.EntryPointExtender</mainClass>
- </manifest>
- </archive>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ <customcode.version>0.4.4</customcode.version>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.stackmob</groupId>
+ <artifactId>customcode</artifactId>
+ <version>${customcode.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.6</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <finalName>${project.artifactId}-${project.version}</finalName>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <configuration>
+ <archive>
+ <manifest>
+ <mainClass>com.stackmob.example.EntryPointExtender</mainClass>
+ </manifest>
+ </archive>
+ <descriptorRefs>
+ <descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs>
+ </configuration>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>single</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <groupId>org.dstovall</groupId>
+ <artifactId>onejar-maven-plugin</artifactId>
+ <version>1.4.4</version>
+ <executions>
+ <execution>
+ <configuration>
+ <!-- Optional -->
+ <onejarVersion>0.97</onejarVersion>
+
+ <!-- Optional, default is false -->
+ <attachToBuild>true</attachToBuild>
+ <!-- Optional, default is "onejar" -->
+ <classifier>onejar</classifier>
+ </configuration>
+ <goals>
+ <goal>one-jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ <pluginRepositories>
+ <pluginRepository>
+ <id>onejar-maven-plugin.googlecode.com</id>
+ <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
+ </pluginRepository>
+ </pluginRepositories>
</project>
1  java/src/main/java/com/stackmob/example/EntryPointExtender.java
View
@@ -28,6 +28,7 @@
public List<CustomCodeMethod> methods() {
List<CustomCodeMethod> list = new ArrayList<CustomCodeMethod>();
list.add(new HelloWorld());
+ list.add(new TwilioSMS());
return list;
}
137 java/src/main/java/com/stackmob/example/TwilioSMS.java
View
@@ -0,0 +1,137 @@
+/**
+ * Copyright 2012 StackMob
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.stackmob.example;
+
+import com.stackmob.core.customcode.CustomCodeMethod;
+import com.stackmob.core.rest.ProcessedAPIRequest;
+import com.stackmob.core.rest.ResponseToProcess;
+import com.stackmob.sdkapi.SDKServiceProvider;
+
+import com.stackmob.sdkapi.http.HttpService;
+import com.stackmob.sdkapi.http.request.HttpRequest;
+import com.stackmob.sdkapi.http.request.GetRequest;
+import com.stackmob.sdkapi.http.response.HttpResponse;
+import com.stackmob.core.ServiceNotActivatedException;
+import com.stackmob.sdkapi.http.exceptions.AccessDeniedException;
+import com.stackmob.sdkapi.http.exceptions.TimeoutException;
+import java.net.MalformedURLException;
+import com.stackmob.sdkapi.http.request.PostRequest;
+import com.stackmob.sdkapi.http.Header;
+import com.stackmob.sdkapi.LoggerService;
+
+import java.net.HttpURLConnection;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.HashSet;
+import java.util.Set;
+import org.apache.commons.codec.binary.Base64;
+
+public class TwilioSMS implements CustomCodeMethod {
+
+ //Create your Twilio Acct at twilio.com and enter
+ //Your accountsid and accesstoken below.
+ static String accountsid = "XXXXXXXXXXXXXXX";
+ static String accesstoken = "XXXXXXXXXXXXXXX";
+
+ @Override
+ public String getMethodName() {
+ return "twilio_sms";
+ }
+
+ @Override
+ public List<String> getParams() {
+ return new ArrayList<String>();
+ }
+
+ @Override
+ public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
+
+ LoggerService logger = serviceProvider.getLoggerService(TwilioSMS.class);
+
+ // You can pass to,from and body parameters from your client
+ //String to = request.getParams().get("to");
+ //String from = request.getParams().get("from");
+ //String body = request.getParams().get("body");
+
+ // The TO phonenumber should be YOUR cel phone
+ // The FROM phonenumber should be one create in
+ // the twilio dashboard at twilio.com
+ String body = "To=4155551212&From=4083331234&Body=hello";
+
+ int responseCode = 0;
+ String responseBody = "";
+
+ String url = "https://api.twilio.com/2010-04-01/Accounts/" + accountsid + "/SMS/Messages.json";
+
+ String pair = accountsid + ":" + accesstoken;
+
+ // Base 64 Encode the accountsid/accesstoken
+ byte[] b =Base64.encodeBase64(pair.getBytes());
+ String encodedString = new String(b);
+
+ Header accept = new Header("Accept-Charset", "utf-8");
+ Header auth = new Header("Authorization","Basic " + encodedString);
+ Header content = new Header("Content-Type", "application/x-www-form-urlencoded");
+
+ Set<Header> set = new HashSet();
+ set.add(accept);
+ set.add(content);
+ set.add(auth);
+
+ try {
+ HttpService http = serviceProvider.getHttpService();
+ try {
+ PostRequest req = new PostRequest(url,set,body);
+ try {
+ try {
+ HttpResponse resp = http.post(req);
+ responseCode = resp.getCode();
+ responseBody = resp.getBody();
+
+ } catch(TimeoutException e) {
+ logger.error(e.getMessage(), e);
+ responseCode = -1;
+ responseBody = e.getMessage();
+ }
+ } catch(AccessDeniedException e) {
+ logger.error(e.getMessage(), e);
+ responseCode = -1;
+ responseBody = e.getMessage();
+ }
+
+ } catch(MalformedURLException e) {
+ logger.error(e.getMessage(), e);
+ responseCode = -1;
+ responseBody = e.getMessage();
+ }
+
+ } catch(ServiceNotActivatedException e) {
+ logger.error(e.getMessage(), e);
+ responseCode = -1;
+ responseBody = e.getMessage();
+ }
+
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put("response_code", responseCode);
+ map.put("response_body", responseBody);
+
+ return new ResponseToProcess(HttpURLConnection.HTTP_OK, map);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.