1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-22 20:47:57 +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");
* you may not use this file except in compliance with the License.
@ -328,6 +328,13 @@ public class XmlStringBuilder implements Appendable, CharSequence {
return this;
}
public XmlStringBuilder optBooleanAttributeDefaultTrue(String name, boolean bool) {
if (!bool) {
sb.append(' ').append(name).append("='false'");
}
return this;
}
public XmlStringBuilder xmlnsAttribute(String value) {
optAttribute("xmlns", value);
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.NamedElement;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder;
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";
private HeadersExtension headers;
private Data data;
private final HeadersExtension headers;
private final Data data;
protected String version;
private final String version;
protected AbstractHttpOverXmpp(String element, Builder<?, ?> builder) {
super(element, NAMESPACE);
this.headers = builder.headers;
this.data = builder.data;
this.version = builder.version;
this.version = Objects.requireNonNull(builder.version, "version must not be null");
}
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
IQChildElementXmlStringBuilder builder = getIQHoxtChildElementBuilder(xml);
builder.optAppend(headers);
/* data cannot be fed to optAppend */
if (data != null) {
builder.optAppend(data.toXML());
}
builder.optAppend(data);
return builder;
}
@ -149,7 +147,9 @@ public abstract class AbstractHttpOverXmpp extends IQ {
* <p>
* This class is immutable.
*/
public static class Data {
public static class Data implements NamedElement {
public static final String ELEMENT = "data";
private final NamedElement child;
@ -167,12 +167,13 @@ public abstract class AbstractHttpOverXmpp extends IQ {
*
* @return xml representation of this object
*/
public String toXML() {
StringBuilder builder = new StringBuilder();
builder.append("<data>");
builder.append(child.toXML());
builder.append("</data>");
return builder.toString();
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.rightAngleBracket();
xml.element(child);
xml.closeElement(this);
return xml;
}
/**
@ -183,6 +184,11 @@ public abstract class AbstractHttpOverXmpp extends IQ {
public NamedElement getChild() {
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");
* you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
package org.jivesoftware.smackx.hoxt.packet;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.hoxt.HOXTManager;
/**
@ -47,8 +49,11 @@ public class Base64BinaryChunk implements ExtensionElement {
* @param last value of last attribute
*/
public Base64BinaryChunk(String text, String streamId, int nr, boolean last) {
this.text = text;
this.streamId = streamId;
this.text = Objects.requireNonNull(text, "text must not be null");
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.last = last;
}
@ -111,17 +116,14 @@ public class Base64BinaryChunk implements ExtensionElement {
}
@Override
public String toXML() {
StringBuilder builder = new StringBuilder();
builder.append("<chunk xmlns='urn:xmpp:http' streamId='");
builder.append(streamId);
builder.append("' nr='");
builder.append(nr);
builder.append("' last='");
builder.append(Boolean.toString(last));
builder.append("'>");
builder.append(text);
builder.append("</chunk>");
return builder.toString();
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.attribute("streamId", streamId);
xml.attribute("nr", nr);
xml.optBooleanAttribute("last", last);
xml.rightAngleBracket();
xml.append(text);
xml.closeElement(this);
return xml;
}
}

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

View file

@ -16,7 +16,7 @@
*/
package org.jivesoftware.smackx.hoxt.packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.Objects;
/**
* Represents Resp IQ packet.
@ -30,24 +30,19 @@ public final class HttpOverXmppResp extends AbstractHttpOverXmpp {
private HttpOverXmppResp(Builder builder) {
super(ELEMENT, builder);
this.statusCode = builder.statusCode;
this.statusCode = Objects.requireNonNull(builder.statusCode, "statusCode must not be null");
this.statusMessage = builder.statusMessage;
}
private int statusCode;
private String statusMessage;
private final int statusCode;
private final String statusMessage;
@Override
protected IQChildElementXmlStringBuilder getIQHoxtChildElementBuilder(IQChildElementXmlStringBuilder builder) {
builder.append(" ");
builder.append("version='").append(StringUtils.escapeForXML(version)).append("'");
builder.append(" ");
builder.append("statusCode='").append(Integer.toString(statusCode)).append("'");
if (statusMessage != null) {
builder.append(" ");
builder.append("statusMessage='").append(StringUtils.escapeForXML(statusMessage)).append("'");
}
builder.append(">");
builder.attribute("version", getVersion());
builder.attribute("statusCode", statusCode);
builder.optAttribute("statusMessage", statusMessage);
builder.rightAngleBracket();
return builder;
}
@ -82,6 +77,9 @@ public final class HttpOverXmppResp extends AbstractHttpOverXmpp {
private int statusCode = 200;
private String statusMessage = null;
private Builder() {
}
/**
* 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");
* 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;
}
private AbstractHttpOverXmpp.Text parseText(XmlPullParser parser) throws XmlPullParserException, IOException {
private static AbstractHttpOverXmpp.Text parseText(XmlPullParser parser) throws XmlPullParserException, IOException {
String text = null;
boolean done = false;
@ -147,7 +147,8 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
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();
boolean done = false;
boolean startClosed = true;
@ -195,7 +196,7 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
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
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;
boolean done = false;
@ -235,7 +237,8 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
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);
AbstractHttpOverXmpp.ChunkedBase64 child = new AbstractHttpOverXmpp.ChunkedBase64(streamId);
boolean done = false;
@ -256,7 +259,7 @@ public abstract class AbstractHttpOverXmppProvider<H extends AbstractHttpOverXmp
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);
AbstractHttpOverXmpp.Ibb child = new AbstractHttpOverXmpp.Ibb(sid);
boolean done = false;

View file

@ -16,10 +16,8 @@
*/
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.HttpOverXmppReq;
import org.jivesoftware.smackx.shim.packet.HeadersExtension;
import org.xmlpull.v1.XmlPullParser;
/**
@ -36,39 +34,35 @@ public class HttpOverXmppReqProvider extends AbstractHttpOverXmppProvider<HttpOv
@Override
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 resource = parser.getAttributeValue("", ATTRIBUTE_RESOURCE);
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;
builder.setMethod(HttpMethod.valueOf(method));
String sipubStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_SIPUB);
String ibbStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_IBB);
String jingleStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_JINGLE);
if (sipubStr != null) {
sipub = Boolean.valueOf(sipubStr);
builder.setSipub(Boolean.valueOf(sipubStr));
}
if (ibbStr != null) {
ibb = Boolean.valueOf(ibbStr);
builder.setIbb(Boolean.valueOf(ibbStr));
}
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) {
maxChunkSizeValue = Integer.parseInt(maxChunkSize);
builder.setMaxChunkSize(Integer.parseInt(maxChunkSize));
}
HeadersExtension headers = parseHeaders(parser);
AbstractHttpOverXmpp.Data data = parseData(parser);
builder.setHeaders(parseHeaders(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()
private HttpOverXmppResp parseAbstractBody(String string, String tag) throws Exception {
private static HttpOverXmppResp parseAbstractBody(String string, String tag) throws Exception {
HttpOverXmppRespProvider provider = new HttpOverXmppRespProvider();
XmlPullParser parser = PacketParserUtils.getParserFor(string, tag);
@ -190,7 +190,7 @@ public class AbstractHttpOverXmppProviderTest {
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();
assertEquals(collection.size(), expectedHeaders.size());

View file

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