summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Clayton <alex.clayton@isode.com>2016-01-29 17:01:39 (GMT)
committerNick Hudson <nick.hudson@isode.com>2016-02-01 16:45:38 (GMT)
commitf21f0d6f9c3662c4f224383ec84c69317c465514 (patch)
treeabb30a766cb0d0cf3e6ede0eee290fe011527735
parentceb4798aa0739ebd7177632c3f508babacb2f8d7 (diff)
downloadstroke-f21f0d6f9c3662c4f224383ec84c69317c465514.zip
stroke-f21f0d6f9c3662c4f224383ec84c69317c465514.tar.bz2
Add S5BProxyRequest Serializers and Parser
Adds a S5BProxyRequestParser and a S5BProxyRequestSerializer to stroke based on the Swiften classes of the same name. These has been missed out previous stroke patches for some reason. Test-information: Code inspection against the Swifen classes. Sanity test ran the unit tests for stroke they still all pass (note I could not find any unit tests for this class to transfer from Swiften). Change-Id: Ic5463169ada33c72127e87a416d42a650aad6399
-rw-r--r--src/com/isode/stroke/parser/payloadparsers/S5BProxyRequestParser.java91
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/S5BProxyRequestSerializer.java47
2 files changed, 138 insertions, 0 deletions
diff --git a/src/com/isode/stroke/parser/payloadparsers/S5BProxyRequestParser.java b/src/com/isode/stroke/parser/payloadparsers/S5BProxyRequestParser.java
new file mode 100644
index 0000000..ff56fb5
--- /dev/null
+++ b/src/com/isode/stroke/parser/payloadparsers/S5BProxyRequestParser.java
@@ -0,0 +1,91 @@
+/* Copyright (c) 2016, Isode Limited, London, England.
+ * All rights reserved.
+ *
+ * Acquisition and use of this software and related materials for any
+ * purpose requires a written license agreement from Isode Limited,
+ * or a written license from an organisation licensed by Isode Limited
+ * to grant such a license.
+ *
+ */
+package com.isode.stroke.parser.payloadparsers;
+
+import com.isode.stroke.elements.S5BProxyRequest;
+import com.isode.stroke.elements.S5BProxyRequest.StreamHost;
+import com.isode.stroke.jid.JID;
+import com.isode.stroke.parser.AttributeMap;
+import com.isode.stroke.parser.GenericPayloadParser;
+
+/**
+ * S5BProxyRequestParaser
+ */
+public class S5BProxyRequestParser extends
+ GenericPayloadParser<S5BProxyRequest> {
+
+ private boolean parseActivate = false;
+
+ private final StringBuilder activeJIDBuilder = new StringBuilder();
+
+ /**
+ * Constructor
+ */
+ public S5BProxyRequestParser() {
+ super(new S5BProxyRequest());
+ }
+
+ @Override
+ public void handleStartElement(String element, String ns,
+ AttributeMap attributes) {
+ if ("streamhost".equals(element)) {
+ String hostValue = attributes.getAttributeValue("host");
+ String jidValue = attributes.getAttributeValue("jid");
+ String portValue = attributes.getAttributeValue("port");
+ if (hostValue != null && jidValue != null && portValue != null) {
+ int port = -1;
+ try {
+ port = Integer.parseInt(portValue);
+ }
+ catch (NumberFormatException nfe) {
+ port = -1;
+ }
+ JID jid = new JID(jidValue);
+ if (!hostValue.isEmpty() && port != -1 && jid.isValid()) {
+ StreamHost streamHost = new StreamHost();
+ streamHost.host = hostValue;
+ streamHost.port = port;
+ streamHost.jid = jid;
+ getPayloadInternal().setStreamHost(streamHost);
+ }
+ }
+ }
+ else if ("active".equals(element)) {
+ parseActivate = true;
+ }
+ else if ("query".equals(element)) {
+ String sidValue = attributes.getAttributeValue("sid");
+ if (sidValue != null) {
+ getPayloadInternal().setSID(sidValue);
+ };
+ }
+
+ }
+
+ @Override
+ public void handleEndElement(String element, String ns) {
+ if ("activate".equals(element)) {
+ JID active = new JID(activeJIDBuilder.toString());
+ if (active.isValid()) {
+ getPayloadInternal().setActivate(active);
+ }
+ parseActivate = false;
+ }
+
+ }
+
+ @Override
+ public void handleCharacterData(String data) {
+ if (parseActivate) {
+ activeJIDBuilder.append(data);
+ }
+ }
+
+}
diff --git a/src/com/isode/stroke/serializer/payloadserializers/S5BProxyRequestSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/S5BProxyRequestSerializer.java
new file mode 100644
index 0000000..d2e8dae
--- /dev/null
+++ b/src/com/isode/stroke/serializer/payloadserializers/S5BProxyRequestSerializer.java
@@ -0,0 +1,47 @@
+/* Copyright (c) 2016, Isode Limited, London, England.
+ * All rights reserved.
+ *
+ * Acquisition and use of this software and related materials for any
+ * purpose requires a written license agreement from Isode Limited,
+ * or a written license from an organisation licensed by Isode Limited
+ * to grant such a license.
+ *
+ */
+package com.isode.stroke.serializer.payloadserializers;
+
+import com.isode.stroke.elements.S5BProxyRequest;
+import com.isode.stroke.serializer.GenericPayloadSerializer;
+import com.isode.stroke.serializer.xml.XMLElement;
+
+/**
+ *
+ */
+public class S5BProxyRequestSerializer extends
+ GenericPayloadSerializer<S5BProxyRequest> {
+
+ /**
+ * Constructor
+ */
+ public S5BProxyRequestSerializer() {
+ super(S5BProxyRequest.class);
+ }
+
+ @Override
+ protected String serializePayload(S5BProxyRequest s5bProxyRequest) {
+ XMLElement queryElement = new XMLElement("query", "http://jabber.org/protocol/bytestreams");
+ if (s5bProxyRequest != null && s5bProxyRequest.getStreamHost() != null) {
+ XMLElement streamHost = new XMLElement("streamhost");
+ streamHost.setAttribute("host", s5bProxyRequest.getStreamHost().host);
+ streamHost.setAttribute("port", String.valueOf(s5bProxyRequest.getStreamHost().port));
+ streamHost.setAttribute("jid", s5bProxyRequest.getStreamHost().jid.toString());
+ queryElement.addNode(streamHost);
+ }
+ else if (s5bProxyRequest != null && s5bProxyRequest.getActivate() != null) {
+ queryElement.setAttribute("sid", s5bProxyRequest.getSID());
+ XMLElement active = new XMLElement("active","",s5bProxyRequest.getActivate().toString());
+ queryElement.addNode(active);
+ }
+ return queryElement.serialize();
+ }
+
+}