From 0a9bba28616e594279cd659ec2eb57da2074b3cd Mon Sep 17 00:00:00 2001 From: steckbrief Date: Wed, 11 Jan 2017 14:55:53 +0100 Subject: Improved error handling for filetransfer:http:delete, Check for httpupload feature available extended to include filetransfer:http as well, method to check if http upload is available moved from data class 'Account' to 'AccountUtil' --- .../conversationsplus/entities/Account.java | 8 --- .../services/XmppConnectionService.java | 6 +- .../http/delete/DeleteRemoteFileService.java | 16 +++-- .../http/delete/DeleteTokenReceived.java | 10 +++ .../http/upload/HttpUploadFileTransferService.java | 26 +++++--- .../conversationsplus/ui/ConversationActivity.java | 5 +- .../conversationsplus/ui/ConversationFragment.java | 7 +-- .../conversationsplus/ui/ShareWithActivity.java | 5 +- .../ui/adapter/MessageAdapter.java | 17 +++--- .../conversationsplus/utils/AccountUtil.java | 34 +++++++++++ .../thedevstack/conversationsplus/utils/Xmlns.java | 3 - .../conversationsplus/xmpp/XmppConnection.java | 71 +++++++++++++++------- .../xmpp/utils/ErrorIqPacketExceptionHelper.java | 29 +++++---- 13 files changed, 158 insertions(+), 79 deletions(-) create mode 100644 src/main/java/de/thedevstack/conversationsplus/utils/AccountUtil.java (limited to 'src/main/java/de') diff --git a/src/main/java/de/thedevstack/conversationsplus/entities/Account.java b/src/main/java/de/thedevstack/conversationsplus/entities/Account.java index 2ee76504..1c7ad288 100644 --- a/src/main/java/de/thedevstack/conversationsplus/entities/Account.java +++ b/src/main/java/de/thedevstack/conversationsplus/entities/Account.java @@ -56,14 +56,6 @@ public class Account extends AbstractEntity { public static final int OPTION_USECOMPRESSION = 3; public final HashSet> inProgressDiscoFetches = new HashSet<>(); - public boolean httpUploadAvailable(long filesize) { - return xmppConnection != null && xmppConnection.getFeatures().httpUpload(filesize); - } - - public boolean httpUploadAvailable() { - return httpUploadAvailable(0); - } - public void setDisplayName(String displayName) { this.displayName = displayName; } diff --git a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java index 83cefc80..ee0ee14a 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java @@ -38,7 +38,6 @@ import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; import java.math.BigInteger; -import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; @@ -53,11 +52,11 @@ import java.util.Locale; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; -import de.duenndns.ssl.MemorizingTrustManager; import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; import de.thedevstack.conversationsplus.services.filetransfer.FileTransferManager; +import de.thedevstack.conversationsplus.utils.AccountUtil; import de.thedevstack.conversationsplus.utils.ImageUtil; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.UiUpdateHelper; @@ -85,7 +84,6 @@ import de.thedevstack.conversationsplus.entities.TransferablePlaceholder; import de.thedevstack.conversationsplus.generator.IqGenerator; import de.thedevstack.conversationsplus.generator.MessageGenerator; import de.thedevstack.conversationsplus.generator.PresenceGenerator; -import de.thedevstack.conversationsplus.http.HttpConnectionManager; import de.thedevstack.conversationsplus.parser.IqParser; import de.thedevstack.conversationsplus.parser.MessageParser; import de.thedevstack.conversationsplus.parser.PresenceParser; @@ -1080,7 +1078,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } else { for (Conversation conversation : getConversations()) { if (conversation.getMode() == Conversation.MODE_SINGLE - || conversation.getAccount().httpUploadAvailable()) { + || AccountUtil.isHttpUploadAvailable(conversation.getAccount())) { list.add(conversation); } } diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java index eb9f1b04..2085cf4b 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java @@ -31,12 +31,16 @@ public class DeleteRemoteFileService implements SimpleUserDecisionCallback { path = this.message.getFileParams().getUrl(); } - DeleteRemoteFile remoteFile = new DeleteRemoteFile(path, this.message); - Account account = this.message.getConversation().getAccount(); - Jid host = account.getXmppConnection().findDiscoItemByFeature(FileTransferHttp.NAMESPACE); - IqPacket request = FileTransferHttpDeleteSlotRequestPacketGenerator.generate(host, path); - MessageUtil.setAndSaveFileStatus(this.message, FileStatus.DELETING); - XmppSendUtil.sendIqPacket(account, request, new DeleteTokenReceived(remoteFile)); + if (null != path) { + DeleteRemoteFile remoteFile = new DeleteRemoteFile(path, this.message); + Account account = this.message.getConversation().getAccount(); + Jid host = account.getXmppConnection().findDiscoItemByFeature(FileTransferHttp.NAMESPACE); + if (null != host) { + IqPacket request = FileTransferHttpDeleteSlotRequestPacketGenerator.generate(host, path); + MessageUtil.setAndSaveFileStatus(this.message, FileStatus.DELETING); + XmppSendUtil.sendIqPacket(account, request, new DeleteTokenReceived(remoteFile)); + } + } } } diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java index 186454e4..83a250de 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java @@ -1,16 +1,21 @@ package de.thedevstack.conversationsplus.services.filetransfer.http.delete; +import android.widget.Toast; + import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import de.thedevstack.android.logcat.Logging; +import de.thedevstack.conversationsplus.R; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.enums.FileStatus; import de.thedevstack.conversationsplus.http.HttpClient; import de.thedevstack.conversationsplus.utils.MessageUtil; +import de.thedevstack.conversationsplus.utils.ui.ConversationsPlusToast; import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived; +import de.thedevstack.conversationsplus.xmpp.exceptions.ServiceUnavailableException; import de.thedevstack.conversationsplus.xmpp.exceptions.XmppException; import de.thedevstack.conversationsplus.xmpp.filetransfer.http.delete.DeleteSlotPacketParser; import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; @@ -81,6 +86,11 @@ public class DeleteTokenReceived implements OnIqPacketReceived { } catch (XmppException e) { Logging.e("filetransfer.http.delete", "Error while trying to get the delete token: " + e.getMessage()); + int messageResId = R.string.cplus_remote_file_delete_failed; + if (e instanceof ServiceUnavailableException) { + messageResId = R.string.cplus_remote_file_delete_service_unavailable; + } + ConversationsPlusToast.makeErrorToast(messageResId, Toast.LENGTH_LONG); MessageUtil.setAndSaveFileStatus(remoteFile.getMessage(), FileStatus.DELETE_FAILED); } } diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java index 6b19cd5f..f0bb438d 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java @@ -13,8 +13,10 @@ import de.thedevstack.conversationsplus.persistance.FileBackend; import de.thedevstack.conversationsplus.services.AbstractConnectionManager; import de.thedevstack.conversationsplus.services.FileTransferService; import de.thedevstack.conversationsplus.services.filetransfer.AbstractFileTransferService; +import de.thedevstack.conversationsplus.utils.AccountUtil; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.XmppSendUtil; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.FileTransferHttp; import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUpload; import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUploadRequestSlotPacketGenerator; import de.thedevstack.conversationsplus.xmpp.jid.Jid; @@ -62,19 +64,29 @@ public class HttpUploadFileTransferService extends AbstractFileTransferService i file.setExpectedSize(inputStreamAndExpectedSize.second); Logging.d("httpupload", "Requesting upload slot for file upload"); - Jid host = account.getXmppConnection().findDiscoItemByFeature(HttpUpload.NAMESPACE); - IqPacket request = HttpUploadRequestSlotPacketGenerator.generate(host, file.getName(), file.getSize(), file.getMimeType()); - XmppSendUtil.sendIqPacket(account, request, new HttpUploadSlotRequestReceived(entity)); - MessageUtil.markMessage(message, Message.STATUS_UNSEND); + Jid host = this.getHost(account); + if (null != host) { + IqPacket request = HttpUploadRequestSlotPacketGenerator.generate(host, file.getName(), file.getSize(), file.getMimeType()); + XmppSendUtil.sendIqPacket(account, request, new HttpUploadSlotRequestReceived(entity)); + MessageUtil.markMessage(message, Message.STATUS_UNSEND); - Logging.d("httpupload", "Upload slot for file upload requested"); - started = true; + Logging.d("httpupload", "Upload slot for file upload requested"); + started = true; + } } catch (FileNotFoundException e) { Logging.e("httpupload", "Could not find file, exception message: " + e.getMessage()); } return started; } + private Jid getHost(Account account) { + Jid host = account.getXmppConnection().findDiscoItemByFeature(FileTransferHttp.NAMESPACE); + if (null == host) { + host = account.getXmppConnection().findDiscoItemByFeature(HttpUpload.NAMESPACE); + } + return host; + } + /** * Checks whether a message can be sent using this service or not. * @@ -87,6 +99,6 @@ public class HttpUploadFileTransferService extends AbstractFileTransferService i && null != message.getConversation() && null != message.getConversation().getAccount() && null != message.getFileParams() - && message.getConversation().getAccount().httpUploadAvailable(message.getFileParams().getSize()); + && AccountUtil.isHttpUploadAvailable(message.getConversation().getAccount(), message.getFileParams().getSize()); } } diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationActivity.java index ac9c2766..bca0851a 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationActivity.java @@ -44,6 +44,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import de.thedevstack.android.logcat.Logging; +import de.thedevstack.conversationsplus.utils.AccountUtil; import de.timroes.android.listview.EnhancedListView; import de.thedevstack.conversationsplus.Config; @@ -422,7 +423,7 @@ public class ConversationActivity extends XmppActivity } if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) { menuContactDetails.setVisible(false); - menuAttach.setVisible(getSelectedConversation().getAccount().httpUploadAvailable() && getSelectedConversation().getMucOptions().participating()); + menuAttach.setVisible(AccountUtil.isHttpUploadAvailable(getSelectedConversation().getAccount()) && getSelectedConversation().getMucOptions().participating()); menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite()); menuSecure.setVisible((Config.supportOpenPgp() || Config.supportOmemo()) && Config.multipleEncryptionChoices()); //only if pgp is supported we have a choice } else { @@ -493,7 +494,7 @@ public class ConversationActivity extends XmppActivity } } }; - if ((account.httpUploadAvailable() || attachmentChoice == ATTACHMENT_CHOICE_LOCATION) && encryption != Message.ENCRYPTION_OTR) { + if ((AccountUtil.isHttpUploadAvailable(account) || attachmentChoice == ATTACHMENT_CHOICE_LOCATION) && encryption != Message.ENCRYPTION_OTR) { conversation.setNextCounterpart(null); callback.onPresenceSelected(); } else { diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java index aad15f51..157e6dc3 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java @@ -69,8 +69,7 @@ import de.thedevstack.conversationsplus.ui.adapter.MessageAdapter.OnContactPictu import de.thedevstack.conversationsplus.ui.adapter.MessageAdapter.OnContactPictureLongClicked; import de.thedevstack.conversationsplus.ui.listeners.ConversationSwipeRefreshListener; import de.thedevstack.conversationsplus.ui.listeners.DeleteFileCallback; -import de.thedevstack.conversationsplus.ui.listeners.SimpleUserDecisionCallback; -import de.thedevstack.conversationsplus.ui.listeners.UserDecisionListener; +import de.thedevstack.conversationsplus.utils.AccountUtil; import de.thedevstack.conversationsplus.utils.GeoHelper; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.UIHelper; @@ -578,7 +577,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa deleteFile.setVisible(true); deleteFile.setTitle(activity.getString(R.string.delete_x_file,UIHelper.getFileDescriptionString(activity, m))); } - if (m.isHttpUploaded() && MessageUtil.isMessageSent(m)) { + if (m.isHttpUploaded() && MessageUtil.isMessageSent(m) && AccountUtil.isFileTransferHttpAvailable(m.getConversation().getAccount())) { MenuItem deleteRemoteFile = menu.findItem(R.id.msg_ctx_menu_delete_remote_file); deleteRemoteFile.setVisible(true); } @@ -1038,7 +1037,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa final String text = this.mEditMessage == null ? "" : this.mEditMessage.getText().toString(); final boolean empty = text.length() == 0; final boolean conference = c.getMode() == Conversation.MODE_MULTI; - if (conference && !c.getAccount().httpUploadAvailable()) { + if (conference && !AccountUtil.isHttpUploadAvailable(c.getAccount())) { if (empty && c.getNextCounterpart() != null) { action = SendButtonAction.CANCEL; } else { diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/ShareWithActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/ShareWithActivity.java index 28e8beb8..8852212b 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/ShareWithActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/ShareWithActivity.java @@ -15,7 +15,6 @@ import android.widget.Toast; import java.net.URLConnection; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -28,9 +27,9 @@ import de.thedevstack.conversationsplus.R; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.Message; -import de.thedevstack.conversationsplus.persistance.FileBackend; import de.thedevstack.conversationsplus.services.XmppConnectionService; import de.thedevstack.conversationsplus.ui.adapter.ConversationAdapter; +import de.thedevstack.conversationsplus.utils.AccountUtil; import de.thedevstack.conversationsplus.utils.ConversationUtil; import de.thedevstack.conversationsplus.utils.FileUtils; import de.thedevstack.conversationsplus.xmpp.jid.InvalidJidException; @@ -307,7 +306,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer } }; } - if (account.httpUploadAvailable() + if (AccountUtil.isHttpUploadAvailable(account) && (conversation.getMode() == Conversation.MODE_MULTI || FileUtils.allFilesUnderSize(this, share.uris, max)) && conversation.getNextEncryption() != Message.ENCRYPTION_OTR) { diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java b/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java index ac8fba0a..a6bdec69 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java @@ -57,6 +57,8 @@ import de.thedevstack.conversationsplus.utils.CryptoHelper; import de.thedevstack.conversationsplus.utils.GeoHelper; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.UIHelper; +import de.thedevstack.conversationsplus.utils.ui.TextViewUtil; +import de.thedevstack.conversationsplus.utils.ui.ViewUtil; public class MessageAdapter extends ArrayAdapter { @@ -605,8 +607,7 @@ public class MessageAdapter extends ArrayAdapter { ViewHolder viewHolder = new ViewHolder(view); if (SENT == type || RECEIVED == type) { - viewHolder.message_box = (LinearLayout) view.findViewById(R.id.message_box); - viewHolder.message_box.setVisibility(View.VISIBLE); + viewHolder.message_box = ViewUtil.visible(view, R.id.message_box); viewHolder.indicator = (ImageView) view.findViewById(R.id.security_indicator); viewHolder.messageBody = (TextView) view.findViewById(R.id.message_body); viewHolder.time = (TextView) view.findViewById(R.id.message_time); @@ -618,17 +619,17 @@ public class MessageAdapter extends ArrayAdapter { viewHolder.image = (ImageView) view.findViewById(R.id.message_image); } if (RECEIVED == type) { // Extra block as preparation for new /me representation - viewHolder.contact_picture = (ImageView) view.findViewById(R.id.message_photo); - viewHolder.contact_picture.setVisibility(View.VISIBLE); + viewHolder.contact_picture = ViewUtil.visible(view, R.id.message_photo); } if (RECEIVED == type) { viewHolder.encryption = (TextView) view.findViewById(R.id.message_encryption); } if (STATUS == type) { - viewHolder.contact_picture = (ImageView) view.findViewById(R.id.message_photo); - viewHolder.contact_picture.setVisibility(View.VISIBLE); - viewHolder.status_message = (TextView) view.findViewById(R.id.status_message); - viewHolder.status_message.setVisibility(View.VISIBLE); + viewHolder.contact_picture = ViewUtil.visible(view, R.id.message_photo); + viewHolder.status_message = TextViewUtil.visible(view, R.id.status_message); + } + if (SENT == type) { + viewHolder.remoteFileStatus = TextViewUtil.visible(view, R.id.remote_file_status); } view.setTag(viewHolder); diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/AccountUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/AccountUtil.java new file mode 100644 index 00000000..0ea434f7 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/utils/AccountUtil.java @@ -0,0 +1,34 @@ +package de.thedevstack.conversationsplus.utils; + +import de.thedevstack.conversationsplus.entities.Account; + +/** + * Utility class to work with accounts. + */ +public final class AccountUtil { + + public static boolean isHttpUploadAvailable(Account account, long filesize) { + return null != account + && null != account.getXmppConnection() + && null != account.getXmppConnection().getFeatures() + && account.getXmppConnection().getFeatures().httpUpload(filesize); + } + + public static boolean isHttpUploadAvailable(Account account) { + return null != account + && null != account.getXmppConnection() + && null != account.getXmppConnection().getFeatures() + && account.getXmppConnection().getFeatures().httpUpload(0); + } + + public static boolean isFileTransferHttpAvailable(Account account) { + return null != account + && null != account.getXmppConnection() + && null != account.getXmppConnection().getFeatures() + && account.getXmppConnection().getFeatures().hasFeatureFileTransferHttp(0); + } + + private AccountUtil() { + // avoid instantiation of utility class + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/Xmlns.java b/src/main/java/de/thedevstack/conversationsplus/utils/Xmlns.java index 35adf6ec..80718dec 100644 --- a/src/main/java/de/thedevstack/conversationsplus/utils/Xmlns.java +++ b/src/main/java/de/thedevstack/conversationsplus/utils/Xmlns.java @@ -1,11 +1,8 @@ package de.thedevstack.conversationsplus.utils; -import de.thedevstack.conversationsplus.Config; - public final class Xmlns { public static final String BLOCKING = "urn:xmpp:blocking"; public static final String ROSTER = "jabber:iq:roster"; public static final String REGISTER = "jabber:iq:register"; public static final String BYTE_STREAMS = "http://jabber.org/protocol/bytestreams"; - public static final String HTTP_UPLOAD = "urn:xmpp:http:upload"; } diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/XmppConnection.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/XmppConnection.java index c5a8fe3b..1082e19f 100644 --- a/src/main/java/de/thedevstack/conversationsplus/xmpp/XmppConnection.java +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/XmppConnection.java @@ -71,6 +71,8 @@ import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xml.Tag; import de.thedevstack.conversationsplus.xml.TagWriter; import de.thedevstack.conversationsplus.xml.XmlReader; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.FileTransferHttp; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUpload; import de.thedevstack.conversationsplus.xmpp.forms.Data; import de.thedevstack.conversationsplus.xmpp.forms.Field; import de.thedevstack.conversationsplus.xmpp.jid.InvalidJidException; @@ -1534,35 +1536,62 @@ public class XmppConnection implements Runnable { this.blockListRequested = value; } + public boolean hasFeatureFileTransferHttp(long filesize) { + if (Config.DISABLE_HTTP_UPLOAD) { + return false; + } else { + List> items = findDiscoItemsByFeature(FileTransferHttp.NAMESPACE); + if (items.size() > 0) { + long maxsize = this.parseMaxHttpUploadSize(items.get(0), FileTransferHttp.NAMESPACE); + return filesize <= maxsize; + } else { + return false; + } + } + } + public boolean httpUpload(long filesize) { if (Config.DISABLE_HTTP_UPLOAD) { return false; } else { - List> items = findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD); - if (items.size() > 0) { - try { - long maxsize = Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(Xmlns.HTTP_UPLOAD, "max-file-size")); - return filesize <= maxsize; - } catch (Exception e) { - return true; - } - } else { - return false; - } + if (hasFeatureFileTransferHttp(filesize)) { + return true; + } else { + List> items = findDiscoItemsByFeature(HttpUpload.NAMESPACE); + if (items.size() > 0) { + long maxsize = this.parseMaxHttpUploadSize(items.get(0), HttpUpload.NAMESPACE); + return filesize <= maxsize; + } else { + return false; + } + } } } + private long parseMaxHttpUploadSize(Entry item, String namespace) { + long maxsize = Long.MAX_VALUE; + if (null != item && null != namespace) { + try { + maxsize = Long.parseLong(item.getValue().getExtendedDiscoInformation(namespace, "max-file-size")); + } catch (Exception e) { + // Suppress exception + } + } + return maxsize; + } + public long getMaxHttpUploadSize() { - List> items = findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD); - if (items.size() > 0) { - try { - return Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(Xmlns.HTTP_UPLOAD, "max-file-size")); - } catch (Exception e) { - return -1; - } - } else { - return -1; - } + List> items = findDiscoItemsByFeature(HttpUpload.NAMESPACE); + if (items.size() > 0) { + long maxsize = this.parseMaxHttpUploadSize(items.get(0), HttpUpload.NAMESPACE); + if (Long.MAX_VALUE == maxsize) { // For code compatibility - legacy behavior returns -1 in case of no max-file-size + return -1; + } else { + return maxsize; + } + } else { + return -1; + } } } diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java index f02d8d46..bcc5e9dd 100644 --- a/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java @@ -14,20 +14,23 @@ import de.thedevstack.conversationsplus.xmpp.exceptions.UndefinedConditionExcept public final class ErrorIqPacketExceptionHelper { private final static String ERROR_NAMESPACE = "urn:ietf:params:xml:ns:xmpp-stanzas"; - public static void throwIqErrorException(Element packet) throws IqPacketErrorException { - if (hasErrorElement(packet, "bad-request")) { - throw new BadRequestIqErrorException(packet, getErrorText(packet)); + public static void throwIqErrorException(Element errorIqPacket) throws IqPacketErrorException { + Element packet = IqPacketParser.findChild(errorIqPacket, "error", "jabber:client"); + if (null != packet) { + if (hasErrorElement(packet, "bad-request")) { + throw new BadRequestIqErrorException(errorIqPacket, getErrorText(packet)); + } + if (hasErrorElement(packet, "service-unavailable")) { + throw new ServiceUnavailableException(errorIqPacket, getErrorText(packet)); + } + if (hasErrorElement(packet, "internal-server-error")) { + throw new InternalServerErrorException(errorIqPacket, getErrorText(packet)); + } + if (hasErrorElement(packet, "undefined-condition")) { + throw new UndefinedConditionException(errorIqPacket, getErrorText(packet)); + } } - if (hasErrorElement(packet, "service-unavailable")) { - throw new ServiceUnavailableException(packet, getErrorText(packet)); - } - if (hasErrorElement(packet, "internal-server-error")) { - throw new InternalServerErrorException(packet, getErrorText(packet)); - } - if (hasErrorElement(packet, "undefined-condition")) { - throw new UndefinedConditionException(packet, getErrorText(packet)); - } - throw new IqPacketErrorException(packet, "Unknown error packet."); + throw new IqPacketErrorException(errorIqPacket, "Unknown error packet."); } private static boolean hasErrorElement(Element packet, String elementName) { -- cgit v1.2.3