1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-29 15:32:06 +01:00

[time] Use IqBuilder pattern and improve API

This commit is contained in:
Florian Schmaus 2021-05-02 17:14:15 +02:00
parent cd33b885ae
commit 469d4fb0dc
6 changed files with 265 additions and 84 deletions

View file

@ -21,6 +21,8 @@ import java.util.WeakHashMap;
import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
@ -75,12 +77,13 @@ public final class EntityTimeManager extends Manager {
Mode.async) { Mode.async) {
@Override @Override
public IQ handleIQRequest(IQ iqRequest) { public IQ handleIQRequest(IQ iqRequest) {
if (enabled) { if (!enabled) {
return Time.createResponse(iqRequest);
}
else {
return IQ.createErrorResponse(iqRequest, Condition.not_acceptable); return IQ.createErrorResponse(iqRequest, Condition.not_acceptable);
} }
Time timeRequest = (Time) iqRequest;
Time timeResponse = Time.builder(timeRequest).build();
return timeResponse;
} }
}); });
} }
@ -105,13 +108,16 @@ public final class EntityTimeManager extends Manager {
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, Time.NAMESPACE); return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, Time.NAMESPACE);
} }
public Time getTime(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { public Time getTime(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException,
if (!isTimeSupported(jid)) InterruptedException, FeatureNotSupportedException {
return null; if (!isTimeSupported(jid)) {
throw new SmackException.FeatureNotSupportedException(Time.NAMESPACE);
}
Time request = new Time(); XMPPConnection connection = connection();
// TODO Add Time(Jid) constructor and use this constructor instead Time request = Time.builder(connection)
request.setTo(jid); .to(jid)
return connection().createStanzaCollectorAndSend(request).nextResultOrThrow(); .build();
return connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
} }
} }

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2003-2007 Jive Software, 2014 Florian Schmaus * Copyright 2003-2007 Jive Software, 2014-2021 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,7 +21,10 @@ import java.util.Date;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IqData;
import org.jivesoftware.smack.util.StringUtils;
import org.jxmpp.util.XmppDateTime; import org.jxmpp.util.XmppDateTime;
@ -33,31 +36,41 @@ import org.jxmpp.util.XmppDateTime;
* @see <a href="http://www.xmpp.org/extensions/xep-0202.html">XEP-202</a> * @see <a href="http://www.xmpp.org/extensions/xep-0202.html">XEP-202</a>
* @author Florian Schmaus * @author Florian Schmaus
*/ */
public class Time extends IQ { public class Time extends IQ implements TimeView {
public static final String NAMESPACE = "urn:xmpp:time"; public static final String NAMESPACE = "urn:xmpp:time";
public static final String ELEMENT = "time"; public static final String ELEMENT = "time";
private static final Logger LOGGER = Logger.getLogger(Time.class.getName()); private static final Logger LOGGER = Logger.getLogger(Time.class.getName());
private String utc; private final String utc;
private String tzo; private final String tzo;
public Time() { public Time(TimeBuilder timeBuilder) {
super(ELEMENT, NAMESPACE); super(timeBuilder, ELEMENT, NAMESPACE);
setType(Type.get); utc = timeBuilder.getUtc();
} tzo = timeBuilder.getTzo();
/** Type type = getType();
* Creates a new Time instance using the specified calendar instance as switch (type) {
* the time value to send. case get:
* if (utc != null) {
* @param cal the time value. throw new IllegalArgumentException("Time requests must not have utc set");
*/ }
public Time(Calendar cal) { if (tzo != null) {
super(ELEMENT, NAMESPACE); throw new IllegalArgumentException("Time requests must not have tzo set");
tzo = XmppDateTime.asString(cal.getTimeZone()); }
// Convert local time to the UTC time. break;
utc = XmppDateTime.formatXEP0082Date(cal.getTime()); case result:
StringUtils.requireNotNullNorEmpty(utc, "Must have set a utc value");
StringUtils.requireNotNullNorEmpty(tzo, "Must have set a tzo value");
break;
case error:
// Nothing to check.
break;
case set:
throw new IllegalArgumentException("Invalid IQ type");
}
} }
/** /**
@ -79,67 +92,47 @@ public class Time extends IQ {
return date; return date;
} }
/** @Override
* Sets the time using the local time.
*
* @param time the current local time.
*/
public void setTime(Date time) {
}
/**
* Returns the time as a UTC formatted String using the format CCYY-MM-DDThh:mm:ssZ.
*
* @return the time as a UTC formatted String.
*/
public String getUtc() { public String getUtc() {
return utc; return utc;
} }
/** @Override
* Sets the time using UTC formatted String in the format CCYY-MM-DDThh:mm:ssZ.
*
* @param utc the time using a formatted String.
*/
public void setUtc(String utc) {
this.utc = utc;
}
/**
* Returns the time zone.
*
* @return the time zone.
*/
public String getTzo() { public String getTzo() {
return tzo; return tzo;
} }
/**
* Sets the time zone offset.
*
* @param tzo the time zone offset.
*/
public void setTzo(String tzo) {
this.tzo = tzo;
}
public static Time createResponse(IQ request) {
Time time = new Time(Calendar.getInstance());
time.setType(Type.result);
time.setTo(request.getFrom());
return time;
}
@Override @Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
if (utc != null) { if (utc != null) {
buf.rightAngleBracket(); buf.rightAngleBracket();
buf.append("<utc>").append(utc).append("</utc>"); buf.element("utc", utc);
buf.append("<tzo>").append(tzo).append("</tzo>"); buf.element("tzo", tzo);
} else { } else {
buf.setEmptyElement(); buf.setEmptyElement();
} }
return buf; return buf;
} }
public static TimeBuilder builder(XMPPConnection connection) {
return new TimeBuilder(connection);
}
public static TimeBuilder builder(IqData iqData) {
return new TimeBuilder(iqData);
}
public static TimeBuilder builder(String stanzaId) {
return new TimeBuilder(stanzaId);
}
public static TimeBuilder builder(Time timeRequest, Calendar calendar) {
IqData iqData = IqData.createResponseData(timeRequest);
return builder(iqData).setTime(calendar);
}
public static TimeBuilder builder(Time timeRequest) {
return builder(timeRequest, Calendar.getInstance());
}
} }

View file

@ -0,0 +1,94 @@
/**
*
* Copyright 2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.time.packet;
import java.text.ParseException;
import java.util.Calendar;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IqBuilder;
import org.jivesoftware.smack.packet.IqData;
import org.jivesoftware.smack.util.StringUtils;
import org.jxmpp.util.XmppDateTime;
// TODO: Use java.time.ZonedDataTime once Smack's minimum Android SDK API level is 26 or higher.
public class TimeBuilder extends IqBuilder<TimeBuilder, Time> implements TimeView {
private String utc;
private String tzo;
TimeBuilder(IqData iqCommon) {
super(iqCommon);
}
TimeBuilder(XMPPConnection connection) {
super(connection);
}
TimeBuilder(String stanzaId) {
super(stanzaId);
}
/**
* Sets the time using UTC formatted String, in the format CCYY-MM-DDThh:mm:ssZ, and the provided timezone
* definition in the format (+|-)hh:mm.
*
* @param utc the time using a formatted String.
* @param tzo the time zone definition.
* @return a reference to this builder.
* @throws ParseException if the provided string is not parsable (e.g. because it does not follow the expected
* format).
*/
public TimeBuilder setUtcAndTzo(String utc, String tzo) throws ParseException {
this.utc = StringUtils.requireNotNullNorEmpty(utc, "Must provide utc argument");
// Sanity check the provided string.
XmppDateTime.parseDate(utc);
this.tzo = StringUtils.requireNotNullNorEmpty(tzo, "Must provide tzo argument");
return getThis();
}
public TimeBuilder setTime(Calendar calendar) {
// Convert local time to the UTC time.
utc = XmppDateTime.formatXEP0082Date(calendar.getTime());
tzo = XmppDateTime.asString(calendar.getTimeZone());
return getThis();
}
@Override
public String getUtc() {
return utc;
}
@Override
public String getTzo() {
return tzo;
}
@Override
public Time build() {
return new Time(this);
}
@Override
public TimeBuilder getThis() {
return this;
}
}

View file

@ -0,0 +1,37 @@
/**
*
* Copyright 2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.time.packet;
import org.jivesoftware.smack.packet.IqView;
public interface TimeView extends IqView {
/**
* Returns the time as a UTC formatted String using the format CCYY-MM-DDThh:mm:ssZ.
*
* @return the time as a UTC formatted String.
*/
String getUtc();
/**
* Returns the time zone.
*
* @return the time zone.
*/
String getTzo();
}

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2014 Florian Schmaus * Copyright © 2014-2021 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,14 +16,55 @@
*/ */
package org.jivesoftware.smackx.time.provider; package org.jivesoftware.smackx.time.provider;
import org.jivesoftware.smack.provider.IntrospectionProvider.IQIntrospectionProvider; import java.io.IOException;
import java.text.ParseException;
import org.jivesoftware.smack.packet.IqData;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.provider.IqProvider;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.time.packet.Time; import org.jivesoftware.smackx.time.packet.Time;
import org.jivesoftware.smackx.time.packet.TimeBuilder;
public class TimeProvider extends IQIntrospectionProvider<Time> { public class TimeProvider extends IqProvider<Time> {
public TimeProvider() { @Override
super(Time.class); public Time parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, ParseException {
String utc = null, tzo = null;
TimeBuilder timeBuilder = Time.builder(iqData);
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch (eventType) {
case START_ELEMENT:
String name = parser.getName();
switch (name) {
case "utc":
utc = parser.nextText();
break;
case "tzo":
tzo = parser.nextText();
break;
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
break;
}
}
if (utc != null) {
timeBuilder.setUtcAndTzo(utc, tzo);
}
return timeBuilder.build();
} }
} }

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Florian Schmaus * Copyright 2014-2021 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -31,7 +32,10 @@ public class TimeTest extends SmackTestSuite {
@Test @Test
public void parseCurrentTimeTest() { public void parseCurrentTimeTest() {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Time time = new Time(calendar); Time time = Time.builder("dummy")
.ofType(IQ.Type.result)
.setTime(calendar)
.build();
Date date = time.getTime(); Date date = time.getTime();
Date calendarDate = calendar.getTime(); Date calendarDate = calendar.getTime();
@ -43,7 +47,10 @@ public class TimeTest extends SmackTestSuite {
public void negativeTimezoneTest() { public void negativeTimezoneTest() {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT-830")); calendar.setTimeZone(TimeZone.getTimeZone("GMT-830"));
Time time = new Time(calendar); Time time = Time.builder("dummy")
.ofType(IQ.Type.result)
.setTime(calendar)
.build();
assertEquals("-8:30", time.getTzo()); assertEquals("-8:30", time.getTzo());
} }
@ -52,7 +59,10 @@ public class TimeTest extends SmackTestSuite {
public void positiveTimezoneTest() { public void positiveTimezoneTest() {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+830")); calendar.setTimeZone(TimeZone.getTimeZone("GMT+830"));
Time time = new Time(calendar); Time time = Time.builder("dummy")
.ofType(IQ.Type.result)
.setTime(calendar)
.build();
assertEquals("+8:30", time.getTzo()); assertEquals("+8:30", time.getTzo());
} }