Commit 5dab4a3a authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Optimized usage of StringBuilder. JM-480

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@3156 b35dd754-fafc-0310-a699-88a17e54d16e
parent fc325453
...@@ -151,9 +151,7 @@ public class ClientSession extends Session { ...@@ -151,9 +151,7 @@ public class ClientSession extends Session {
connection.getInetAddress().getHostAddress()); connection.getInetAddress().getHostAddress());
// Include the not-authorized error in the response // Include the not-authorized error in the response
StreamError error = new StreamError(StreamError.Condition.not_authorized); StreamError error = new StreamError(StreamError.Condition.not_authorized);
StringBuilder sb = new StringBuilder(); connection.deliverRawText(error.toXML());
sb.append(error.toXML());
connection.deliverRawText(sb.toString());
// Close the underlying connection // Close the underlying connection
connection.close(); connection.close();
return null; return null;
...@@ -211,7 +209,7 @@ public class ClientSession extends Session { ...@@ -211,7 +209,7 @@ public class ClientSession extends Session {
Writer writer = connection.getWriter(); Writer writer = connection.getWriter();
// Build the start packet response // Build the start packet response
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(200);
sb.append("<?xml version='1.0' encoding='"); sb.append("<?xml version='1.0' encoding='");
sb.append(CHARSET); sb.append(CHARSET);
sb.append("'?>"); sb.append("'?>");
...@@ -248,7 +246,7 @@ public class ClientSession extends Session { ...@@ -248,7 +246,7 @@ public class ClientSession extends Session {
} }
// Otherwise, this is at least XMPP 1.0 so we need to announce stream features. // Otherwise, this is at least XMPP 1.0 so we need to announce stream features.
sb = new StringBuilder(); sb = new StringBuilder(300);
sb.append("<stream:features>"); sb.append("<stream:features>");
if (tlsPolicy != SocketConnection.TLSPolicy.disabled) { if (tlsPolicy != SocketConnection.TLSPolicy.disabled) {
sb.append("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\">"); sb.append("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\">");
......
...@@ -93,7 +93,7 @@ public class ClientSocketReader extends SocketReader { ...@@ -93,7 +93,7 @@ public class ClientSocketReader extends SocketReader {
} }
protected String getAvailableStreamFeatures() { protected String getAvailableStreamFeatures() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(110);
// TODO Create and use #hasSASLAuthentication // TODO Create and use #hasSASLAuthentication
if (((ClientSession)session).getAuthToken() == null) { if (((ClientSession)session).getAuthToken() == null) {
// Advertise that the server supports Non-SASL Authentication // Advertise that the server supports Non-SASL Authentication
......
...@@ -104,7 +104,7 @@ public class SASLAuthentication { ...@@ -104,7 +104,7 @@ public class SASLAuthentication {
* @return a string with the valid SASL mechanisms available for the specified session. * @return a string with the valid SASL mechanisms available for the specified session.
*/ */
public static String getSASLMechanisms(Session session) { public static String getSASLMechanisms(Session session) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(195);
sb.append("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">"); sb.append("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
// Check if the user provider in use supports passwords retrieval. Accessing to the users // Check if the user provider in use supports passwords retrieval. Accessing to the users
// passwords will be required by the CallbackHandler // passwords will be required by the CallbackHandler
...@@ -287,7 +287,7 @@ public class SASLAuthentication { ...@@ -287,7 +287,7 @@ public class SASLAuthentication {
} }
private void sendChallenge(byte[] challenge) { private void sendChallenge(byte[] challenge) {
StringBuilder reply = new StringBuilder(); StringBuilder reply = new StringBuilder(250);
reply.append( reply.append(
"<challenge xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">"); "<challenge xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
reply.append(StringUtils.encodeBase64(challenge).trim()); reply.append(StringUtils.encodeBase64(challenge).trim());
...@@ -296,9 +296,7 @@ public class SASLAuthentication { ...@@ -296,9 +296,7 @@ public class SASLAuthentication {
} }
private void authenticationSuccessful(String username) { private void authenticationSuccessful(String username) {
StringBuilder reply = new StringBuilder(); connection.deliverRawText("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
reply.append("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
connection.deliverRawText(reply.toString());
// We only support SASL for c2s // We only support SASL for c2s
if (session instanceof ClientSession) { if (session instanceof ClientSession) {
((ClientSession) session).setAuthToken(new AuthToken(username)); ((ClientSession) session).setAuthToken(new AuthToken(username));
...@@ -314,7 +312,7 @@ public class SASLAuthentication { ...@@ -314,7 +312,7 @@ public class SASLAuthentication {
} }
private void authenticationFailed() { private void authenticationFailed() {
StringBuilder reply = new StringBuilder(); StringBuilder reply = new StringBuilder(80);
reply.append("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">"); reply.append("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
reply.append("<not-authorized/></failure>"); reply.append("<not-authorized/></failure>");
connection.deliverRawText(reply.toString()); connection.deliverRawText(reply.toString());
......
...@@ -452,12 +452,10 @@ public abstract class SocketReader implements Runnable { ...@@ -452,12 +452,10 @@ public abstract class SocketReader implements Runnable {
* closing the connection a stream error will be sent to the entity. * closing the connection a stream error will be sent to the entity.
*/ */
private void closeNeverSecuredConnection() { private void closeNeverSecuredConnection() {
StringBuilder sb = new StringBuilder();
// Set the not_authorized error // Set the not_authorized error
StreamError error = new StreamError(StreamError.Condition.not_authorized); StreamError error = new StreamError(StreamError.Condition.not_authorized);
sb.append(error.toXML());
// Deliver stanza // Deliver stanza
connection.deliverRawText(sb.toString()); connection.deliverRawText(error.toXML());
// Close the underlying connection // Close the underlying connection
connection.close(); connection.close();
// Log a warning so that admins can track this case from the server side // Log a warning so that admins can track this case from the server side
...@@ -495,7 +493,7 @@ public abstract class SocketReader implements Runnable { ...@@ -495,7 +493,7 @@ public abstract class SocketReader implements Runnable {
// error and close the underlying connection. // error and close the underlying connection.
String host = reader.getXPPParser().getAttributeValue("", "to"); String host = reader.getXPPParser().getAttributeValue("", "to");
if (validateHost() && isHostUnknown(host)) { if (validateHost() && isHostUnknown(host)) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(250);
sb.append("<?xml version='1.0' encoding='"); sb.append("<?xml version='1.0' encoding='");
sb.append(CHARSET); sb.append(CHARSET);
sb.append("'?>"); sb.append("'?>");
...@@ -524,7 +522,7 @@ public abstract class SocketReader implements Runnable { ...@@ -524,7 +522,7 @@ public abstract class SocketReader implements Runnable {
else if (!createSession(xpp.getNamespace(null))) { else if (!createSession(xpp.getNamespace(null))) {
// No session was created because of an invalid namespace prefix so answer a stream // No session was created because of an invalid namespace prefix so answer a stream
// error and close the underlying connection // error and close the underlying connection
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(250);
sb.append("<?xml version='1.0' encoding='"); sb.append("<?xml version='1.0' encoding='");
sb.append(CHARSET); sb.append(CHARSET);
sb.append("'?>"); sb.append("'?>");
...@@ -578,12 +576,10 @@ public abstract class SocketReader implements Runnable { ...@@ -578,12 +576,10 @@ public abstract class SocketReader implements Runnable {
*/ */
private boolean negotiateTLS() throws IOException, XmlPullParserException { private boolean negotiateTLS() throws IOException, XmlPullParserException {
if (connection.getTlsPolicy() == SocketConnection.TLSPolicy.disabled) { if (connection.getTlsPolicy() == SocketConnection.TLSPolicy.disabled) {
StringBuilder sb = new StringBuilder();
// Set the not_authorized error // Set the not_authorized error
StreamError error = new StreamError(StreamError.Condition.not_authorized); StreamError error = new StreamError(StreamError.Condition.not_authorized);
sb.append(error.toXML());
// Deliver stanza // Deliver stanza
connection.deliverRawText(sb.toString()); connection.deliverRawText(error.toXML());
// Close the underlying connection // Close the underlying connection
connection.close(); connection.close();
// Log a warning so that admins can track this case from the server side // Log a warning so that admins can track this case from the server side
...@@ -620,7 +616,7 @@ public abstract class SocketReader implements Runnable { ...@@ -620,7 +616,7 @@ public abstract class SocketReader implements Runnable {
*/ */
private void tlsNegotiated() { private void tlsNegotiated() {
// Offer stream features including SASL Mechanisms // Offer stream features including SASL Mechanisms
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(340);
sb.append(geStreamHeader()); sb.append(geStreamHeader());
sb.append("<stream:features>"); sb.append("<stream:features>");
// Include available SASL Mechanisms // Include available SASL Mechanisms
...@@ -641,7 +637,7 @@ public abstract class SocketReader implements Runnable { ...@@ -641,7 +637,7 @@ public abstract class SocketReader implements Runnable {
* to servers or external components) * to servers or external components)
*/ */
private void saslSuccessful() throws XmlPullParserException, IOException { private void saslSuccessful() throws XmlPullParserException, IOException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(145);
sb.append(geStreamHeader()); sb.append(geStreamHeader());
sb.append("<stream:features>"); sb.append("<stream:features>");
...@@ -697,7 +693,7 @@ public abstract class SocketReader implements Runnable { ...@@ -697,7 +693,7 @@ public abstract class SocketReader implements Runnable {
abstract boolean validateHost(); abstract boolean validateHost();
private String geStreamHeader() { private String geStreamHeader() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(200);
sb.append("<?xml version='1.0' encoding='"); sb.append("<?xml version='1.0' encoding='");
sb.append(CHARSET); sb.append(CHARSET);
sb.append("'?>"); sb.append("'?>");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment