
org.restcomm.connect.http.SmsMessagesEndpoint Maven / Gradle / Ivy
/*
* TeleStax, Open Source Cloud Communications
* Copyright 2011-2014, Telestax Inc and individual contributors
* by the @authors tag.
*
* This program is free software: you can redistribute it and/or modify
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see
*
*/
package org.restcomm.connect.http;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.actor.UntypedActorContext;
import akka.actor.UntypedActorFactory;
import akka.util.Timeout;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
import com.thoughtworks.xstream.XStream;
import org.apache.commons.configuration.Configuration;
import org.joda.time.DateTime;
import org.restcomm.connect.commons.annotations.concurrency.NotThreadSafe;
import org.restcomm.connect.http.converter.RestCommResponseConverter;
import org.restcomm.connect.http.converter.SmsMessageConverter;
import org.restcomm.connect.http.converter.SmsMessageListConverter;
import org.restcomm.connect.dao.DaoManager;
import org.restcomm.connect.dao.SmsMessagesDao;
import org.restcomm.connect.dao.entities.RestCommResponse;
import org.restcomm.connect.commons.dao.Sid;
import org.restcomm.connect.dao.entities.SmsMessage;
import org.restcomm.connect.dao.entities.SmsMessage.Status;
import org.restcomm.connect.dao.entities.SmsMessageList;
import org.restcomm.connect.dao.entities.Account;
import org.restcomm.connect.commons.patterns.Observe;
import org.restcomm.connect.sms.api.CreateSmsSession;
import org.restcomm.connect.sms.api.SmsServiceResponse;
import org.restcomm.connect.sms.api.SmsSessionAttribute;
import org.restcomm.connect.sms.api.SmsSessionInfo;
import org.restcomm.connect.sms.api.SmsSessionRequest;
import org.restcomm.connect.sms.api.SmsSessionResponse;
import org.restcomm.connect.commons.util.StringUtils;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import java.math.BigDecimal;
import java.net.URI;
import java.util.Currency;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import static akka.pattern.Patterns.ask;
import static javax.ws.rs.core.MediaType.*;
import static javax.ws.rs.core.Response.Status.*;
import static javax.ws.rs.core.Response.ok;
import static javax.ws.rs.core.Response.status;
/**
* @author [email protected] (Thomas Quintana)
*/
@NotThreadSafe
public abstract class SmsMessagesEndpoint extends SecuredEndpoint {
@Context
protected ServletContext context;
protected ActorSystem system;
protected Configuration configuration;
protected ActorRef aggregator;
protected SmsMessagesDao dao;
protected Gson gson;
protected XStream xstream;
private boolean normalizePhoneNumbers;
public SmsMessagesEndpoint() {
super();
}
@PostConstruct
public void init() {
final DaoManager storage = (DaoManager) context.getAttribute(DaoManager.class.getName());
configuration = (Configuration) context.getAttribute(Configuration.class.getName());
configuration = configuration.subset("runtime-settings");
dao = storage.getSmsMessagesDao();
aggregator = (ActorRef) context.getAttribute("org.restcomm.connect.sms.SmsService");
system = (ActorSystem) context.getAttribute(ActorSystem.class.getName());
super.init(configuration);
final SmsMessageConverter converter = new SmsMessageConverter(configuration);
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(SmsMessage.class, converter);
builder.setPrettyPrinting();
gson = builder.create();
xstream = new XStream();
xstream.alias("RestcommResponse", RestCommResponse.class);
xstream.registerConverter(converter);
xstream.registerConverter(new SmsMessageListConverter(configuration));
xstream.registerConverter(new RestCommResponseConverter(configuration));
normalizePhoneNumbers = configuration.getBoolean("normalize-numbers-for-outbound-calls");
}
protected Response getSmsMessage(final String accountSid, final String sid, final MediaType responseType) {
Account operatedAccount = accountsDao.getAccount(accountSid);
secure(operatedAccount, "RestComm:Read:SmsMessages");
final SmsMessage smsMessage = dao.getSmsMessage(new Sid(sid));
if (smsMessage == null) {
return status(NOT_FOUND).build();
} else {
secure(operatedAccount, smsMessage.getAccountSid(), SecuredType.SECURED_STANDARD);
if (APPLICATION_JSON_TYPE == responseType) {
return ok(gson.toJson(smsMessage), APPLICATION_JSON).build();
} else if (APPLICATION_XML_TYPE == responseType) {
final RestCommResponse response = new RestCommResponse(smsMessage);
return ok(xstream.toXML(response), APPLICATION_XML).build();
} else {
return null;
}
}
}
protected Response getSmsMessages(final String accountSid, final MediaType responseType) {
secure(accountsDao.getAccount(accountSid), "RestComm:Read:SmsMessages");
final List smsMessages = dao.getSmsMessages(new Sid(accountSid));
if (APPLICATION_JSON_TYPE == responseType) {
return ok(gson.toJson(smsMessages), APPLICATION_JSON).build();
} else if (APPLICATION_XML_TYPE == responseType) {
final RestCommResponse response = new RestCommResponse(new SmsMessageList(smsMessages));
return ok(xstream.toXML(response), APPLICATION_XML).build();
} else {
return null;
}
}
private void normalize(final MultivaluedMap data) throws IllegalArgumentException {
final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
final String from = data.getFirst("From");
data.remove("From");
try {
data.putSingle("From", phoneNumberUtil.format(phoneNumberUtil.parse(from, "US"), PhoneNumberFormat.E164));
} catch (final NumberParseException exception) {
throw new IllegalArgumentException(exception);
}
final String to = data.getFirst("To");
data.remove("To");
try {
data.putSingle("To", phoneNumberUtil.format(phoneNumberUtil.parse(to, "US"), PhoneNumberFormat.E164));
} catch (final NumberParseException exception) {
throw new IllegalArgumentException(exception);
}
final String body = data.getFirst("Body");
if (body.getBytes().length > 160) {
data.remove("Body");
data.putSingle("Body", body.substring(0, 159));
}
}
@SuppressWarnings("unchecked")
protected Response putSmsMessage(final String accountSid, final MultivaluedMap data,
final MediaType responseType) {
secure(accountsDao.getAccount(accountSid), "RestComm:Create:SmsMessages");
try {
validate(data);
if(normalizePhoneNumbers)
normalize(data);
} catch (final RuntimeException exception) {
return status(BAD_REQUEST).entity(exception.getMessage()).build();
}
final String sender = data.getFirst("From");
final String recipient = data.getFirst("To");
final String body = data.getFirst("Body");
final SmsSessionRequest.Encoding encoding;
if (!data.containsKey("Encoding")) {
encoding = SmsSessionRequest.Encoding.GSM;
} else {
encoding = SmsSessionRequest.Encoding.valueOf(data.getFirst("Encoding").replace('-', '_'));
}
ConcurrentHashMap customRestOutgoingHeaderMap = new ConcurrentHashMap();
Iterator iter = data.keySet().iterator();
while (iter.hasNext()) {
String name = iter.next();
if (name.startsWith("X-")){
customRestOutgoingHeaderMap.put(name, data.getFirst(name));
}
}
final Timeout expires = new Timeout(Duration.create(60, TimeUnit.SECONDS));
try {
Future
© 2015 - 2025 Weber Informatics LLC | Privacy Policy