Java tutorial
/* * 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 <http://www.gnu.org/licenses/> * */ package org.mobicents.servlet.restcomm.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.apache.shiro.authz.AuthorizationException; import org.mobicents.servlet.restcomm.annotations.concurrency.NotThreadSafe; import org.mobicents.servlet.restcomm.dao.AccountsDao; import org.mobicents.servlet.restcomm.dao.DaoManager; import org.mobicents.servlet.restcomm.dao.RecordingsDao; import org.mobicents.servlet.restcomm.entities.Recording; import org.mobicents.servlet.restcomm.entities.RecordingList; import org.mobicents.servlet.restcomm.entities.RestCommResponse; import org.mobicents.servlet.restcomm.entities.Sid; import org.mobicents.servlet.restcomm.http.converter.RecordingConverter; import org.mobicents.servlet.restcomm.http.converter.RecordingListConverter; import org.mobicents.servlet.restcomm.http.converter.RestCommResponseConverter; /** * @author quintana.thomas@gmail.com (Thomas Quintana) */ @NotThreadSafe public abstract class RecordingsEndpoint extends AbstractEndpoint { @Context protected ServletContext context; protected Configuration configuration; protected RecordingsDao dao; protected Gson gson; protected XStream xstream; protected AccountsDao accountsDao; public RecordingsEndpoint() { 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.getRecordingsDao(); accountsDao = storage.getAccountsDao(); final RecordingConverter converter = new RecordingConverter(configuration); final GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Recording.class, converter); builder.setPrettyPrinting(); gson = builder.create(); xstream = new XStream(); xstream.alias("RestcommResponse", RestCommResponse.class); xstream.registerConverter(converter); xstream.registerConverter(new RecordingListConverter(configuration)); xstream.registerConverter(new RestCommResponseConverter(configuration)); } protected Response getRecording(final String accountSid, final String sid, final MediaType responseType) { try { secure(accountsDao.getAccount(accountSid), "RestComm:Read:Recordings"); } catch (final AuthorizationException exception) { return status(UNAUTHORIZED).build(); } final Recording recording = dao.getRecording(new Sid(sid)); if (recording == null) { return status(NOT_FOUND).build(); } else { try { secureLevelControl(accountsDao, accountSid, String.valueOf(recording.getAccountSid())); } catch (final AuthorizationException exception) { return status(UNAUTHORIZED).build(); } if (APPLICATION_JSON_TYPE == responseType) { return ok(gson.toJson(recording), APPLICATION_JSON).build(); } else if (APPLICATION_XML_TYPE == responseType) { final RestCommResponse response = new RestCommResponse(recording); return ok(xstream.toXML(response), APPLICATION_XML).build(); } else { return null; } } } protected Response getRecordings(final String accountSid, final MediaType responseType) { try { secure(accountsDao.getAccount(accountSid), "RestComm:Read:Recordings"); secureLevelControl(accountsDao, accountSid, null); } catch (final AuthorizationException exception) { return status(UNAUTHORIZED).build(); } final List<Recording> recordings = dao.getRecordings(new Sid(accountSid)); if (APPLICATION_JSON_TYPE == responseType) { return ok(gson.toJson(recordings), APPLICATION_JSON).build(); } else if (APPLICATION_XML_TYPE == responseType) { final RestCommResponse response = new RestCommResponse(new RecordingList(recordings)); return ok(xstream.toXML(response), APPLICATION_XML).build(); } else { return null; } } }