
org.restcomm.connect.http.TranscriptionsEndpoint 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 com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thoughtworks.xstream.XStream;
import java.util.List;
import static javax.ws.rs.core.MediaType.*;
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.Response;
import static javax.ws.rs.core.Response.*;
import static javax.ws.rs.core.Response.Status.*;
import org.apache.commons.configuration.Configuration;
import org.restcomm.connect.commons.annotations.concurrency.NotThreadSafe;
import org.restcomm.connect.http.converter.RestCommResponseConverter;
import org.restcomm.connect.http.converter.TranscriptionConverter;
import org.restcomm.connect.http.converter.TranscriptionListConverter;
import org.restcomm.connect.dao.DaoManager;
import org.restcomm.connect.dao.TranscriptionsDao;
import org.restcomm.connect.dao.entities.RestCommResponse;
import org.restcomm.connect.commons.dao.Sid;
import org.restcomm.connect.dao.entities.Transcription;
import org.restcomm.connect.dao.entities.TranscriptionList;
import org.restcomm.connect.dao.entities.Account;
/**
* @author [email protected] (Thomas Quintana)
*/
@NotThreadSafe
public abstract class TranscriptionsEndpoint extends SecuredEndpoint {
@Context
protected ServletContext context;
protected Configuration configuration;
protected TranscriptionsDao dao;
protected Gson gson;
protected XStream xstream;
public TranscriptionsEndpoint() {
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");
super.init(configuration);
dao = storage.getTranscriptionsDao();
final TranscriptionConverter converter = new TranscriptionConverter(configuration);
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Transcription.class, converter);
builder.setPrettyPrinting();
gson = builder.create();
xstream = new XStream();
xstream.alias("RestcommResponse", RestCommResponse.class);
xstream.registerConverter(converter);
xstream.registerConverter(new TranscriptionListConverter(configuration));
xstream.registerConverter(new RestCommResponseConverter(configuration));
}
protected Response getTranscription(final String accountSid, final String sid, final MediaType responseType) {
Account operatedAccount = accountsDao.getAccount(accountSid);
secure(operatedAccount, "RestComm:Read:Transcriptions");
final Transcription transcription = dao.getTranscription(new Sid(sid));
if (transcription == null) {
return status(NOT_FOUND).build();
} else {
secure(operatedAccount, transcription.getAccountSid(), SecuredType.SECURED_STANDARD);
if (APPLICATION_JSON_TYPE == responseType) {
return ok(gson.toJson(transcription), APPLICATION_JSON).build();
} else if (APPLICATION_XML_TYPE == responseType) {
final RestCommResponse response = new RestCommResponse(transcription);
return ok(xstream.toXML(response), APPLICATION_XML).build();
} else {
return null;
}
}
}
protected Response getTranscriptions(final String accountSid, final MediaType responseType) {
secure(accountsDao.getAccount(accountSid), "RestComm:Read:Transcriptions");
final List transcriptions = dao.getTranscriptions(new Sid(accountSid));
if (APPLICATION_JSON_TYPE == responseType) {
return ok(gson.toJson(transcriptions), APPLICATION_JSON).build();
} else if (APPLICATION_XML_TYPE == responseType) {
final RestCommResponse response = new RestCommResponse(new TranscriptionList(transcriptions));
return ok(xstream.toXML(response), APPLICATION_XML).build();
} else {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy