All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twilio.twiml.voice.Play Maven / Gradle / Ivy

There is a newer version: 10.6.3
Show newest version
/**
 * This code was generated by
 * \ / _    _  _|   _  _
 *  | (_)\/(_)(_|\/| |(/_  v1.0.0
 *       /       /
 */

package com.twilio.twiml.voice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.twilio.converter.Promoter;
import com.twilio.twiml.TwiML;
import com.twilio.twiml.TwiMLException;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * TwiML wrapper for {@code }
 */
@JsonDeserialize(builder = Play.Builder.class)
public class Play extends TwiML {
    private final Integer loop;
    private final String digits;
    private final URI url;

    /**
     * For XML Serialization/Deserialization
     */
    private Play() {
        this(new Builder());
    }

    /**
     * Create a new {@code } element
     */
    private Play(Builder b) {
        super("Play", b);
        this.loop = b.loop;
        this.digits = b.digits;
        this.url = b.url;
    }

    /**
     * The body of the TwiML element
     *
     * @return Element body as a string if present else null
     */
    protected String getElementBody() {
        return this.getUrl() == null ? null : this.getUrl().toString();
    }

    /**
     * Attributes to set on the generated XML element
     *
     * @return A Map of attribute keys to values
     */
    protected Map getElementAttributes() {
        // Preserve order of attributes
        Map attrs = new HashMap<>();

        if (this.getLoop() != null) {
            attrs.put("loop", this.getLoop().toString());
        }
        if (this.getDigits() != null) {
            attrs.put("digits", this.getDigits());
        }

        return attrs;
    }

    /**
     * Times to loop media
     *
     * @return Times to loop media
     */
    public Integer getLoop() {
        return loop;
    }

    /**
     * Play DTMF tones for digits
     *
     * @return Play DTMF tones for digits
     */
    public String getDigits() {
        return digits;
    }

    /**
     * Media URL
     *
     * @return Media URL
     */
    public URI getUrl() {
        return url;
    }

    /**
     * Create a new {@code } element
     */
    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder extends TwiML.Builder {
        /**
         * Create and return a {@code } from an XML string
         */
        public static Builder fromXml(final String xml) throws TwiMLException {
            try {
                return OBJECT_MAPPER.readValue(xml, Builder.class);
            } catch (final JsonProcessingException jpe) {
                throw new TwiMLException(
                    "Failed to deserialize a Play.Builder from the provided XML string: " + jpe.getMessage());
            } catch (final Exception e) {
                throw new TwiMLException("Unhandled exception: " + e.getMessage());
            }
        }

        private Integer loop;
        private String digits;
        private URI url;

        /**
         * Create a {@code } with url
         */
        public Builder(URI url) {
            this.url = url;
        }

        /**
         * Create a {@code } with url
         */
        public Builder(String url) {
            this.url = Promoter.uriFromString(url);
        }

        /**
         * Create a {@code } with child elements
         */
        public Builder() {
        }

        /**
         * Times to loop media
         */
        @JacksonXmlProperty(isAttribute = true, localName = "loop")
        public Builder loop(Integer loop) {
            this.loop = loop;
            return this;
        }

        /**
         * Play DTMF tones for digits
         */
        @JacksonXmlProperty(isAttribute = true, localName = "digits")
        public Builder digits(String digits) {
            this.digits = digits;
            return this;
        }

        /**
         * Media URL
         */
        @JacksonXmlProperty(isAttribute = true, localName = "url")
        public Builder url(URI url) {
            this.url = url;
            return this;
        }

        /**
         * Media URL
         */
        public Builder url(String url) {
            this.url = Promoter.uriFromString(url);
            return this;
        }

        /**
         * Create and return resulting {@code } element
         */
        public Play build() {
            return new Play(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy