Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added SendGrid example

  • Loading branch information...
commit 7829a85b7994a3c51519cf3f97cddf817117f1bd 1 parent be2bf0a
Sidney Maestre SidneyAllen authored
1  java/src/main/java/com/stackmob/example/EntryPointExtender.java
@@ -29,6 +29,7 @@
29 29 List<CustomCodeMethod> list = new ArrayList<CustomCodeMethod>();
30 30 list.add(new HelloWorld());
31 31 list.add(new TwilioSMS());
  32 + list.add(new SendGrid());
32 33 return list;
33 34 }
34 35
206 java/src/main/java/com/stackmob/example/SendGrid.java
... ... @@ -0,0 +1,206 @@
  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 +import com.stackmob.sdkapi.*;
  24 +
  25 +import com.stackmob.sdkapi.http.HttpService;
  26 +import com.stackmob.sdkapi.http.request.HttpRequest;
  27 +import com.stackmob.sdkapi.http.request.GetRequest;
  28 +import com.stackmob.sdkapi.http.response.HttpResponse;
  29 +import com.stackmob.core.ServiceNotActivatedException;
  30 +import com.stackmob.sdkapi.http.exceptions.AccessDeniedException;
  31 +import com.stackmob.sdkapi.http.exceptions.TimeoutException;
  32 +import com.stackmob.core.InvalidSchemaException;
  33 +import com.stackmob.core.DatastoreException;
  34 +
  35 +import java.net.MalformedURLException;
  36 +import com.stackmob.sdkapi.http.request.PostRequest;
  37 +import com.stackmob.sdkapi.http.Header;
  38 +import com.stackmob.sdkapi.LoggerService;
  39 +
  40 +import java.net.HttpURLConnection;
  41 +import java.util.Arrays;
  42 +import java.util.HashMap;
  43 +import java.util.List;
  44 +import java.util.Map;
  45 +import java.util.HashSet;
  46 +import java.util.Set;
  47 +import java.net.URI;
  48 +import java.net.URISyntaxException;
  49 +import java.util.ArrayList;
  50 +
  51 +public class SendGrid implements CustomCodeMethod {
  52 +
  53 + //Create your SendGrid Acct at sendgrid.com
  54 + static String API_USER = "YOUR_SENDGRID_USERNAME";
  55 + static String API_KEY = "YOUR_SENDGRID_PASSWORD";
  56 +
  57 + @Override
  58 + public String getMethodName() {
  59 + return "sendgrid_email";
  60 + }
  61 +
  62 +
  63 + @Override
  64 + public List<String> getParams() {
  65 + return Arrays.asList("username","subject","text","from");
  66 + }
  67 +
  68 +
  69 + @Override
  70 + public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
  71 + int responseCode = 0;
  72 + String responseBody = "";
  73 + String body = "";
  74 + String url = "";
  75 + String to = "";
  76 + String toname = "";
  77 + String api_user = API_USER;
  78 + String api_key = API_KEY;
  79 +
  80 + LoggerService logger = serviceProvider.getLoggerService(SendGrid.class);
  81 +
  82 + // username should be a valid username in the StackMob user schema
  83 + String username = request.getParams().get("username");
  84 +
  85 + if (username == null || username.isEmpty()) {
  86 + HashMap<String, String> errParams = new HashMap<String, String>();
  87 + errParams.put("error", "the username passed was empty or null");
  88 + return new ResponseToProcess(HttpURLConnection.HTTP_BAD_REQUEST, errParams); // http 400 - bad request
  89 + }
  90 +
  91 + // get the StackMob datastore service and assemble the query
  92 + DataService dataService = serviceProvider.getDataService();
  93 +
  94 + // build a query
  95 + List<SMCondition> query = new ArrayList<SMCondition>();
  96 + query.add(new SMEquals("username", new SMString(username)));
  97 +
  98 + SMObject userObject;
  99 +
  100 + List<SMObject> result;
  101 + try {
  102 + // return results from user query
  103 + result = dataService.readObjects("user", query);
  104 + if (result != null && result.size() == 1) {
  105 + userObject = result.get(0);
  106 + to = userObject.getValue().get("email").toString();
  107 + toname = userObject.getValue().get("name").toString();
  108 + } else {
  109 + HashMap<String, String> errMap = new HashMap<String, String>();
  110 + errMap.put("error", "no user found");
  111 + errMap.put("detail", "no matches for the username passed");
  112 + return new ResponseToProcess(HttpURLConnection.HTTP_OK, errMap); // http 500 - internal server error
  113 + }
  114 +
  115 + } catch (InvalidSchemaException e) {
  116 + HashMap<String, String> errMap = new HashMap<String, String>();
  117 + errMap.put("error", "invalid_schema");
  118 + errMap.put("detail", e.toString());
  119 + return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errMap); // http 500 - internal server error
  120 + } catch (DatastoreException e) {
  121 + HashMap<String, String> errMap = new HashMap<String, String>();
  122 + errMap.put("error", "datastore_exception");
  123 + errMap.put("detail", e.toString());
  124 + return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errMap); // http 500 - internal server error
  125 + } catch(Exception e) {
  126 + HashMap<String, String> errMap = new HashMap<String, String>();
  127 + errMap.put("error", "unknown");
  128 + errMap.put("detail", e.toString());
  129 + return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errMap); // http 500 - internal server error
  130 + }
  131 +
  132 + // The following values could be static or dynamic
  133 + // and could be populated with values from the StackMob datastore
  134 + // SUBJECT - subject line for the email
  135 + String subject = request.getParams().get("subject");
  136 +
  137 + // TEXT - the text of the email
  138 + String text = request.getParams().get("text");
  139 +
  140 + // FROM - the text of the email
  141 + String from = request.getParams().get("from");
  142 +
  143 + if (subject == null || subject.equals("")) {
  144 + logger.error("Subject is missing");
  145 + }
  146 +
  147 + String queryParams = "api_user=" + api_user + "&api_key=" + api_key + "&to=" + to + "&toname=" + toname + "&subject=" + subject + "&text=" + text + "&from=" + from;
  148 +
  149 + try {
  150 + URI uri = new URI(
  151 + "https",
  152 + "sendgrid.com",
  153 + "/api/mail.send.json",
  154 + queryParams,
  155 + null);
  156 + url = uri.toASCIIString();
  157 + } catch (URISyntaxException e) {
  158 + logger.error(e.getMessage(), e);
  159 + responseCode = -1;
  160 + responseBody = e.getMessage();
  161 + }
  162 +
  163 + Header accept = new Header("Accept-Charset", "utf-8");
  164 + Header content = new Header("Content-Type", "application/x-www-form-urlencoded");
  165 +
  166 + Set<Header> set = new HashSet();
  167 + set.add(accept);
  168 + set.add(content);
  169 +
  170 + try {
  171 + HttpService http = serviceProvider.getHttpService();
  172 +
  173 + PostRequest req = new PostRequest(url,set,body);
  174 +
  175 + HttpResponse resp = http.post(req);
  176 + responseCode = resp.getCode();
  177 + responseBody = resp.getBody();
  178 +
  179 + } catch(TimeoutException e) {
  180 + logger.error(e.getMessage(), e);
  181 + responseCode = -1;
  182 + responseBody = e.getMessage();
  183 +
  184 + } catch(AccessDeniedException e) {
  185 + logger.error(e.getMessage(), e);
  186 + responseCode = -1;
  187 + responseBody = e.getMessage();
  188 +
  189 + } catch(MalformedURLException e) {
  190 + logger.error(e.getMessage(), e);
  191 + responseCode = -1;
  192 + responseBody = e.getMessage();
  193 +
  194 + } catch(ServiceNotActivatedException e) {
  195 + logger.error(e.getMessage(), e);
  196 + responseCode = -1;
  197 + responseBody = e.getMessage();
  198 + }
  199 +
  200 + Map<String, Object> map = new HashMap<String, Object>();
  201 + map.put("response_body", responseBody);
  202 + map.put("encodedURL", url);
  203 +
  204 + return new ResponseToProcess(responseCode, map);
  205 + }
  206 +}

0 comments on commit 7829a85

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