001/**
002 *
003 * Copyright 2014 Vyacheslav Blinov
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.amp;
018
019import java.util.Date;
020
021import org.jivesoftware.smack.SmackException.NoResponseException;
022import org.jivesoftware.smack.SmackException.NotConnectedException;
023import org.jivesoftware.smack.XMPPConnection;
024import org.jivesoftware.smack.XMPPException.XMPPErrorException;
025
026import org.jivesoftware.smackx.amp.packet.AMPExtension;
027
028import org.jxmpp.util.XmppDateTime;
029
030
031public class AMPExpireAtCondition implements AMPExtension.Condition {
032
033    public static final String NAME = "expire-at";
034
035    /**
036     * Check if server supports expire-at condition.
037     * @param connection Smack connection instance
038     * @return true if expire-at condition is supported.
039     * @throws XMPPErrorException if there was an XMPP error returned.
040     * @throws NoResponseException if there was no response from the remote entity.
041     * @throws NotConnectedException if the XMPP connection is not connected.
042     * @throws InterruptedException if the calling thread was interrupted.
043     */
044    public static boolean isSupported(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
045        return AMPManager.isConditionSupported(connection, NAME);
046    }
047
048    private final String value;
049
050    /**
051     * Create new expire-at amp condition with value set as XEP-0082 formatted date.
052     * @param utcDateTime Date instance of time
053     *                    that will be used as value parameter after formatting to XEP-0082 format. Can't be null.
054     */
055    public AMPExpireAtCondition(Date utcDateTime) {
056        if (utcDateTime == null)
057            throw new NullPointerException("Can't create AMPExpireAtCondition with null value");
058        this.value = XmppDateTime.formatXEP0082Date(utcDateTime);
059    }
060
061    /**
062     * Create new expire-at amp condition with defined value.
063     * @param utcDateTime UTC time string that will be used as value parameter.
064     *                    Should be formatted as XEP-0082 Date format. Can't be null.
065     */
066    public AMPExpireAtCondition(String utcDateTime) {
067        if (utcDateTime == null)
068            throw new NullPointerException("Can't create AMPExpireAtCondition with null value");
069        this.value = utcDateTime;
070    }
071
072    @Override
073    public String getName() {
074        return NAME;
075    }
076
077    @Override
078    public String getValue() {
079        return value;
080    }
081
082}