
org.restcomm.connect.http.UssdPushEndpoint 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 static akka.pattern.Patterns.ask;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static javax.ws.rs.core.MediaType.APPLICATION_XML_TYPE;
import static javax.ws.rs.core.Response.ok;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import java.net.URI;
import java.util.concurrent.TimeUnit;
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 org.apache.commons.configuration.Configuration;
import org.restcomm.connect.dao.DaoManager;
import org.restcomm.connect.dao.entities.CallDetailRecord;
import org.restcomm.connect.dao.entities.CallDetailRecordList;
import org.restcomm.connect.dao.entities.RestCommResponse;
import org.restcomm.connect.commons.dao.Sid;
import org.restcomm.connect.http.converter.CallDetailRecordConverter;
import org.restcomm.connect.http.converter.CallDetailRecordListConverter;
import org.restcomm.connect.http.converter.RestCommResponseConverter;
import org.restcomm.connect.telephony.api.CallInfo;
import org.restcomm.connect.telephony.api.CallManagerResponse;
import org.restcomm.connect.telephony.api.CallResponse;
import org.restcomm.connect.telephony.api.CreateCall;
import org.restcomm.connect.telephony.api.ExecuteCallScript;
import org.restcomm.connect.telephony.api.GetCallInfo;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import akka.actor.ActorRef;
import akka.util.Timeout;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thoughtworks.xstream.XStream;
/**
* @author gvagenas
*
*/
public class UssdPushEndpoint extends SecuredEndpoint {
@Context
protected ServletContext context;
protected Configuration configuration;
private ActorRef ussdCallManager;
private DaoManager daos;
private Gson gson;
private GsonBuilder builder;
private XStream xstream;
private CallDetailRecordListConverter listConverter;
public UssdPushEndpoint() {
super();
}
@PostConstruct
public void init() {
configuration = (Configuration) context.getAttribute(Configuration.class.getName());
configuration = configuration.subset("runtime-settings");
ussdCallManager = (ActorRef) context.getAttribute("org.restcomm.connect.ussd.telephony.UssdCallManager");
daos = (DaoManager) context.getAttribute(DaoManager.class.getName());
super.init(configuration);
CallDetailRecordConverter converter = new CallDetailRecordConverter(configuration);
listConverter = new CallDetailRecordListConverter(configuration);
builder = new GsonBuilder();
builder.registerTypeAdapter(CallDetailRecord.class, converter);
builder.registerTypeAdapter(CallDetailRecordList.class, listConverter);
builder.setPrettyPrinting();
gson = builder.create();
xstream = new XStream();
xstream.alias("RestcommResponse", RestCommResponse.class);
xstream.registerConverter(converter);
xstream.registerConverter(new RestCommResponseConverter(configuration));
xstream.registerConverter(listConverter);
}
@SuppressWarnings("unchecked")
protected Response putCall(final String accountSid, final MultivaluedMap data, final MediaType responseType) {
final Sid accountId = new Sid(accountSid);
secure(daos.getAccountsDao().getAccount(accountSid), "RestComm:Create:Calls");
try {
validate(data);
} catch (final RuntimeException exception) {
return status(BAD_REQUEST).entity(exception.getMessage()).build();
}
final String from = data.getFirst("From");
final String to = data.getFirst("To");
final String username = data.getFirst("Username");
final String password = data.getFirst("Password");
final Integer timeout = getTimeout(data);
final Timeout expires = new Timeout(Duration.create(60, TimeUnit.SECONDS));
CreateCall create = null;
try {
create = new CreateCall(from, to, username, password, true, timeout != null ? timeout : 30, CreateCall.Type.USSD,
accountId, null);
create.setCreateCDR(false);
Future
© 2015 - 2025 Weber Informatics LLC | Privacy Policy