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

"Smackify" HOXT code: Use Smack programming idioms

This commit is contained in:
Florian Schmaus 2015-07-01 13:39:04 +02:00
parent 4d57848a0f
commit 9ea5c0a2ce
9 changed files with 102 additions and 104 deletions

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Florian Schmaus * Copyright 2014-2015 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.
@ -328,6 +328,13 @@ public class XmlStringBuilder implements Appendable, CharSequence {
return this; return this;
} }
public XmlStringBuilder optBooleanAttributeDefaultTrue(String name, boolean bool) {
if (!bool) {
sb.append(' ').append(name).append("='false'");
}
return this;
}
public XmlStringBuilder xmlnsAttribute(String value) { public XmlStringBuilder xmlnsAttribute(String value) {
optAttribute("xmlns", value); optAttribute("xmlns", value);
return this; return this;

View file

@ -18,6 +18,7 @@ package org.jivesoftware.smackx.hoxt.packet;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.shim.packet.HeadersExtension; import org.jivesoftware.smackx.shim.packet.HeadersExtension;
@ -31,25 +32,22 @@ public abstract class AbstractHttpOverXmpp extends IQ {
public static final String NAMESPACE = "urn:xmpp:http"; public static final String NAMESPACE = "urn:xmpp:http";
private HeadersExtension headers; private final HeadersExtension headers;
private Data data; private final Data data;
protected String version; private final String version;
protected AbstractHttpOverXmpp(String element, Builder<?, ?> builder) { protected AbstractHttpOverXmpp(String element, Builder<?, ?> builder) {
super(element, NAMESPACE); super(element, NAMESPACE);
this.headers = builder.headers; this.headers = builder.headers;
this.data = builder.data; this.data = builder.data;
this.version = builder.version; this.version = Objects.requireNonNull(builder.version, "version must not be null");
} }
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
IQChildElementXmlStringBuilder builder = getIQHoxtChildElementBuilder(xml); IQChildElementXmlStringBuilder builder = getIQHoxtChildElementBuilder(xml);
builder.optAppend(headers); builder.optAppend(headers);
/* data cannot be fed to optAppend */ builder.optAppend(data);
if (data != null) {
builder.optAppend(data.toXML());
}
return builder; return builder;
} }
@ -149,7 +147,9 @@ public abstract class AbstractHttpOverXmpp extends IQ {
* <p> * <p>
* This class is immutable. * This class is immutable.
*/ */
public static class Data { public static class Data implements NamedElement {
public static final String ELEMENT = "data";
private final NamedElement child; private final NamedElement child;
@ -167,12 +167,13 @@ public abstract class AbstractHttpOverXmpp extends IQ {
* *
* @return xml representation of this object * @return xml representation of this object
*/ */
public String toXML() { @Override
StringBuilder builder = new StringBuilder(); public XmlStringBuilder toXML() {
builder.append("<data>"); XmlStringBuilder xml = new XmlStringBuilder(this);
builder.append(child.toXML()); xml.rightAngleBracket();
builder.append("</data>"); xml.element(child);
return builder.toString(); xml.closeElement(this);
return xml;
} }
/** /**
@ -183,6 +184,11 @@ public abstract class AbstractHttpOverXmpp extends IQ {
public NamedElement getChild() { public NamedElement getChild() {
return child; return child;
} }
@Override
public String getElementName() {
return ELEMENT;
}
} }
/** /**

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Andriy Tsykholyas * Copyright 2014 Andriy Tsykholyas, 2015 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.
@ -17,6 +17,8 @@
package org.jivesoftware.smackx.hoxt.packet; package org.jivesoftware.smackx.hoxt.packet;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.hoxt.HOXTManager; import org.jivesoftware.smackx.hoxt.HOXTManager;
/** /**
@ -47,8 +49,11 @@ public class Base64BinaryChunk implements ExtensionElement {
* @param last value of last attribute * @param last value of last attribute
*/ */
public Base64BinaryChunk(String text, String streamId, int nr, boolean last) { public Base64BinaryChunk(String text, String streamId, int nr, boolean last) {
this.text = text; this.text = Objects.requireNonNull(text, "text must not be null");
this.streamId = streamId; this.streamId = Objects.requireNonNull(streamId, "streamId must not be null");
if (nr < 0) {
throw new IllegalArgumentException("nr must be a non negative integer");
}
this.nr = nr; this.nr = nr;
this.last = last; this.last = last;
} }
@ -111,17 +116,14 @@ public class Base64BinaryChunk implements ExtensionElement {
} }
@Override @Override
public String toXML() { public XmlStringBuilder toXML() {
StringBuilder builder = new StringBuilder(); XmlStringBuilder xml = new XmlStringBuilder(this);
builder.append("<chunk xmlns='urn:xmpp:http' streamId='"); xml.attribute("streamId", streamId);
builder.append(streamId); xml.attribute("nr", nr);
builder.append("' nr='"); xml.optBooleanAttribute("last", last);
builder.append(nr); xml.rightAngleBracket();
builder.append("' last='"); xml.append(text);
builder.append(Boolean.toString(last)); xml.closeElement(this);
builder.append("'>"); return xml;
builder.append(text);
builder.append("</chunk>");
return builder.toString();
} }
} }

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Andriy Tsykholyas * Copyright 2014 Andriy Tsykholyas, 2015 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,8 +16,6 @@
*/ */
package org.jivesoftware.smackx.hoxt.packet; package org.jivesoftware.smackx.hoxt.packet;
import org.jivesoftware.smack.util.StringUtils;
/** /**
* Represents Req IQ packet. * Represents Req IQ packet.
* *
@ -39,36 +37,26 @@ public final class HttpOverXmppReq extends AbstractHttpOverXmpp {
setType(Type.set); setType(Type.set);
} }
private HttpMethod method; private final HttpMethod method;
private String resource; private final String resource;
// TODO: validate: xs:minInclusive value='256' xs:maxInclusive value='65536' private final int maxChunkSize;
private int maxChunkSize; // 0 means not set
private boolean sipub; private final boolean sipub;
private boolean ibb; private final boolean ibb;
private boolean jingle; private final boolean jingle;
@Override @Override
protected IQChildElementXmlStringBuilder getIQHoxtChildElementBuilder(IQChildElementXmlStringBuilder builder) { protected IQChildElementXmlStringBuilder getIQHoxtChildElementBuilder(IQChildElementXmlStringBuilder builder) {
builder.append(" "); builder.attribute("method", method);
builder.append("method='").append(method.toString()).append("'"); builder.attribute("resource", resource);
builder.append(" "); builder.attribute("version", getVersion());
builder.append("resource='").append(StringUtils.escapeForXML(resource)).append("'"); builder.optIntAttribute("maxChunkSize", maxChunkSize);
builder.append(" "); builder.optBooleanAttributeDefaultTrue("sipub", sipub);
builder.append("version='").append(StringUtils.escapeForXML(version)).append("'"); builder.optBooleanAttributeDefaultTrue("ibb", ibb);
if (maxChunkSize != 0) { builder.optBooleanAttributeDefaultTrue("jingle", jingle);
builder.append(" "); builder.rightAngleBracket();
builder.append("maxChunkSize='").append(Integer.toString(maxChunkSize)).append("'");
}
builder.append(" ");
builder.append("sipub='").append(Boolean.toString(sipub)).append("'");
builder.append(" ");
builder.append("ibb='").append(Boolean.toString(ibb)).append("'");
builder.append(" ");
builder.append("jingle='").append(Boolean.toString(jingle)).append("'");
builder.append(">");
return builder; return builder;
} }
@ -139,9 +127,7 @@ public final class HttpOverXmppReq extends AbstractHttpOverXmpp {
private HttpMethod method; private HttpMethod method;
private String resource; private String resource;
// TODO: validate: xs:minInclusive value='256' xs:maxInclusive private int maxChunkSize = -1;
// value='65536'
private int maxChunkSize = 0; // 0 means not set
private boolean sipub = true; private boolean sipub = true;
@ -149,7 +135,6 @@ public final class HttpOverXmppReq extends AbstractHttpOverXmpp {
private boolean jingle = true; private boolean jingle = true;
private Builder() { private Builder() {
} }
/** /**
@ -220,6 +205,9 @@ public final class HttpOverXmppReq extends AbstractHttpOverXmpp {
* @return the builder * @return the builder
*/ */
public Builder setMaxChunkSize(int maxChunkSize) { public Builder setMaxChunkSize(int maxChunkSize) {
if (maxChunkSize < 256 || maxChunkSize > 65536) {
throw new IllegalArgumentException("maxChunkSize must be within [256, 65536]");
}
this.maxChunkSize = maxChunkSize; this.maxChunkSize = maxChunkSize;
return this; return this;
} }

View file

@ -16,7 +16,7 @@
*/ */
package org.jivesoftware.smackx.hoxt.packet; package org.jivesoftware.smackx.hoxt.packet;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.Objects;
/** /**
* Represents Resp IQ packet. * Represents Resp IQ packet.
@ -30,24 +30,19 @@ public final class HttpOverXmppResp extends AbstractHttpOverXmpp {
private HttpOverXmppResp(Builder builder) { private HttpOverXmppResp(Builder builder) {
super(ELEMENT, builder); super(ELEMENT, builder);
this.statusCode = builder.statusCode; this.statusCode = Objects.requireNonNull(builder.statusCode, "statusCode must not be null");
this.statusMessage = builder.statusMessage; this.statusMessage = builder.statusMessage;
} }
private int statusCode; private final int statusCode;
private String statusMessage; private final String statusMessage;
@Override @Override
protected IQChildElementXmlStringBuilder getIQHoxtChildElementBuilder(IQChildElementXmlStringBuilder builder) { protected IQChildElementXmlStringBuilder getIQHoxtChildElementBuilder(IQChildElementXmlStringBuilder builder) {
builder.append(" "); builder.attribute("version", getVersion());
builder.append("version='").append(StringUtils.escapeForXML(version)).append("'"); builder.attribute("statusCode", statusCode);
builder.append(" "); builder.optAttribute("statusMessage", statusMessage);
builder.append("statusCode='").append(Integer.toString(statusCode)).append("'"); builder.rightAngleBracket();
if (statusMessage != null) {
builder.append(" ");
builder.append("statusMessage='").append(StringUtils.escapeForXML(statusMessage)).append("'");
}
builder.append(">");
return builder; return builder;
} }
@ -82,6 +77,9 @@ public final class HttpOverXmppResp extends AbstractHttpOverXmpp {
private int statusCode = 200; private int statusCode = 200;
private String statusMessage = null; private String statusMessage = null;
private Builder() {
}
/** /**
* Sets statusCode attribute. * Sets statusCode attribute.
* *

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Andriy Tsykholyas * Copyright 2014 Andriy Tsykholyas, 2015 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.
@ -124,7 +124,7 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
return data; return data;
} }
private AbstractHttpOverXmpp.Text parseText(XmlPullParser parser) throws XmlPullParserException, IOException { private static AbstractHttpOverXmpp.Text parseText(XmlPullParser parser) throws XmlPullParserException, IOException {
String text = null; String text = null;
boolean done = false; boolean done = false;
@ -147,7 +147,8 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
return new AbstractHttpOverXmpp.Text(text); return new AbstractHttpOverXmpp.Text(text);
} }
private AbstractHttpOverXmpp.Xml parseXml(XmlPullParser parser) throws XmlPullParserException, IOException { private static AbstractHttpOverXmpp.Xml parseXml(XmlPullParser parser)
throws XmlPullParserException, IOException {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
boolean done = false; boolean done = false;
boolean startClosed = true; boolean startClosed = true;
@ -195,7 +196,7 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
return new AbstractHttpOverXmpp.Xml(builder.toString()); return new AbstractHttpOverXmpp.Xml(builder.toString());
} }
private void appendXmlAttributes(XmlPullParser parser, StringBuilder builder) { private static void appendXmlAttributes(XmlPullParser parser, StringBuilder builder) {
// NOTE: for now we ignore namespaces // NOTE: for now we ignore namespaces
int count = parser.getAttributeCount(); int count = parser.getAttributeCount();
@ -211,7 +212,8 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
} }
} }
private AbstractHttpOverXmpp.Base64 parseBase64(XmlPullParser parser) throws XmlPullParserException, IOException { private static AbstractHttpOverXmpp.Base64 parseBase64(XmlPullParser parser) throws XmlPullParserException,
IOException {
String text = null; String text = null;
boolean done = false; boolean done = false;
@ -235,7 +237,8 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
return new AbstractHttpOverXmpp.Base64(text); return new AbstractHttpOverXmpp.Base64(text);
} }
private AbstractHttpOverXmpp.ChunkedBase64 parseChunkedBase64(XmlPullParser parser) throws XmlPullParserException, IOException { private static AbstractHttpOverXmpp.ChunkedBase64 parseChunkedBase64(XmlPullParser parser)
throws XmlPullParserException, IOException {
String streamId = parser.getAttributeValue("", ATTRIBUTE_STREAM_ID); String streamId = parser.getAttributeValue("", ATTRIBUTE_STREAM_ID);
AbstractHttpOverXmpp.ChunkedBase64 child = new AbstractHttpOverXmpp.ChunkedBase64(streamId); AbstractHttpOverXmpp.ChunkedBase64 child = new AbstractHttpOverXmpp.ChunkedBase64(streamId);
boolean done = false; boolean done = false;
@ -256,7 +259,7 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
return child; return child;
} }
private AbstractHttpOverXmpp.Ibb parseIbb(XmlPullParser parser) throws XmlPullParserException, IOException { private static AbstractHttpOverXmpp.Ibb parseIbb(XmlPullParser parser) throws XmlPullParserException, IOException {
String sid = parser.getAttributeValue("", ATTRIBUTE_SID); String sid = parser.getAttributeValue("", ATTRIBUTE_SID);
AbstractHttpOverXmpp.Ibb child = new AbstractHttpOverXmpp.Ibb(sid); AbstractHttpOverXmpp.Ibb child = new AbstractHttpOverXmpp.Ibb(sid);
boolean done = false; boolean done = false;

View file

@ -16,10 +16,8 @@
*/ */
package org.jivesoftware.smackx.hoxt.provider; package org.jivesoftware.smackx.hoxt.provider;
import org.jivesoftware.smackx.hoxt.packet.AbstractHttpOverXmpp;
import org.jivesoftware.smackx.hoxt.packet.HttpMethod; import org.jivesoftware.smackx.hoxt.packet.HttpMethod;
import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq; import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq;
import org.jivesoftware.smackx.shim.packet.HeadersExtension;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
/** /**
@ -36,39 +34,35 @@ public class HttpOverXmppReqProvider extends AbstractHttpOverXmppProvider<HttpOv
@Override @Override
public HttpOverXmppReq parse(XmlPullParser parser, int initialDepth) throws Exception { public HttpOverXmppReq parse(XmlPullParser parser, int initialDepth) throws Exception {
HttpOverXmppReq.Builder builder = HttpOverXmppReq.builder();
builder.setResource(parser.getAttributeValue("", ATTRIBUTE_RESOURCE));
builder.setVersion(parser.getAttributeValue("", ATTRIBUTE_VERSION));
String method = parser.getAttributeValue("", ATTRIBUTE_METHOD); String method = parser.getAttributeValue("", ATTRIBUTE_METHOD);
String resource = parser.getAttributeValue("", ATTRIBUTE_RESOURCE); builder.setMethod(HttpMethod.valueOf(method));
String version = parser.getAttributeValue("", ATTRIBUTE_VERSION);
String maxChunkSize = parser.getAttributeValue("", ATTRIBUTE_MAX_CHUNK_SIZE);
HttpMethod reqMethod = HttpMethod.valueOf(method);
Boolean sipub = true;
Boolean jingle = true;
Boolean ibb = true;
String sipubStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_SIPUB); String sipubStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_SIPUB);
String ibbStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_IBB); String ibbStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_IBB);
String jingleStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_JINGLE); String jingleStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_JINGLE);
if (sipubStr != null) { if (sipubStr != null) {
sipub = Boolean.valueOf(sipubStr); builder.setSipub(Boolean.valueOf(sipubStr));
} }
if (ibbStr != null) { if (ibbStr != null) {
ibb = Boolean.valueOf(ibbStr); builder.setIbb(Boolean.valueOf(ibbStr));
} }
if (jingleStr != null) { if (jingleStr != null) {
jingle = Boolean.valueOf(jingleStr); builder.setJingle(Boolean.valueOf(jingleStr));
} }
int maxChunkSizeValue = 0; String maxChunkSize = parser.getAttributeValue("", ATTRIBUTE_MAX_CHUNK_SIZE);
if (maxChunkSize != null) { if (maxChunkSize != null) {
maxChunkSizeValue = Integer.parseInt(maxChunkSize); builder.setMaxChunkSize(Integer.parseInt(maxChunkSize));
} }
HeadersExtension headers = parseHeaders(parser); builder.setHeaders(parseHeaders(parser));
AbstractHttpOverXmpp.Data data = parseData(parser); builder.setData(parseData(parser));
return HttpOverXmppReq.builder().setMethod(reqMethod).setResource(resource).setIbb(ibb).setSipub(sipub).setJingle(jingle).setMaxChunkSize(maxChunkSizeValue).setData(data).setHeaders(headers).setVersion(version).build(); return builder.build();
} }
} }

View file

@ -181,7 +181,7 @@ public class AbstractHttpOverXmppProviderTest {
} }
// TODO The method name makes no sense after the HOXT re-design, change to parseHttpOverXmppResp() // TODO The method name makes no sense after the HOXT re-design, change to parseHttpOverXmppResp()
private HttpOverXmppResp parseAbstractBody(String string, String tag) throws Exception { private static HttpOverXmppResp parseAbstractBody(String string, String tag) throws Exception {
HttpOverXmppRespProvider provider = new HttpOverXmppRespProvider(); HttpOverXmppRespProvider provider = new HttpOverXmppRespProvider();
XmlPullParser parser = PacketParserUtils.getParserFor(string, tag); XmlPullParser parser = PacketParserUtils.getParserFor(string, tag);
@ -190,7 +190,7 @@ public class AbstractHttpOverXmppProviderTest {
return (HttpOverXmppResp) iq; return (HttpOverXmppResp) iq;
} }
private void checkHeaders(HeadersExtension headers, Map<String, String> expectedHeaders) { private static void checkHeaders(HeadersExtension headers, Map<String, String> expectedHeaders) {
Collection<Header> collection = headers.getHeaders(); Collection<Header> collection = headers.getHeaders();
assertEquals(collection.size(), expectedHeaders.size()); assertEquals(collection.size(), expectedHeaders.size());

View file

@ -65,7 +65,7 @@ public class HttpOverXmppReqProviderTest {
assertEquals(req.isJingle(), true); assertEquals(req.isJingle(), true);
} }
private HttpOverXmppReq parseReq(String string) throws Exception { private static HttpOverXmppReq parseReq(String string) throws Exception {
HttpOverXmppReqProvider provider = new HttpOverXmppReqProvider(); HttpOverXmppReqProvider provider = new HttpOverXmppReqProvider();
XmlPullParser parser = PacketParserUtils.getParserFor(string); XmlPullParser parser = PacketParserUtils.getParserFor(string);
IQ iq = provider.parse(parser); IQ iq = provider.parse(parser);