ForwardedProvider.java

  1. /**
  2.  *
  3.  * Copyright 2013-2014 Georg Lukas, 2020-2021 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.forward.provider;

  18. import java.io.IOException;
  19. import java.text.ParseException;
  20. import java.util.logging.Logger;

  21. import org.jivesoftware.smack.packet.Message;
  22. import org.jivesoftware.smack.packet.Stanza;
  23. import org.jivesoftware.smack.packet.XmlEnvironment;
  24. import org.jivesoftware.smack.parsing.SmackParsingException;
  25. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  26. import org.jivesoftware.smack.util.PacketParserUtils;
  27. import org.jivesoftware.smack.xml.XmlPullParser;
  28. import org.jivesoftware.smack.xml.XmlPullParserException;

  29. import org.jivesoftware.smackx.delay.packet.DelayInformation;
  30. import org.jivesoftware.smackx.delay.provider.DelayInformationProvider;
  31. import org.jivesoftware.smackx.forward.packet.Forwarded;

  32. /**
  33.  * This class implements the {@link ExtensionElementProvider} to parse
  34.  * forwarded messages from a packet.  It will return a {@link Forwarded} stanza extension.
  35.  *
  36.  * @author Georg Lukas
  37.  */
  38. public class ForwardedProvider extends ExtensionElementProvider<Forwarded<?>> {

  39.     public static final ForwardedProvider INSTANCE = new ForwardedProvider();

  40.     private static final Logger LOGGER = Logger.getLogger(ForwardedProvider.class.getName());

  41.     @Override
  42.     public Forwarded<?> parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
  43.                     throws XmlPullParserException, IOException, SmackParsingException, ParseException {
  44.         DelayInformation di = null;
  45.         Stanza packet = null;

  46.         outerloop: while (true) {
  47.             XmlPullParser.Event eventType = parser.next();
  48.             switch (eventType) {
  49.             case START_ELEMENT:
  50.                 String name = parser.getName();
  51.                 String namespace = parser.getNamespace();
  52.                 switch (name) {
  53.                 case DelayInformation.ELEMENT:
  54.                     if (DelayInformation.NAMESPACE.equals(namespace)) {
  55.                         di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth(), null);
  56.                     } else {
  57.                         LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '"
  58.                                         + DelayInformation.NAMESPACE + "'");
  59.                     }
  60.                     break;
  61.                 case Message.ELEMENT:
  62.                     packet = PacketParserUtils.parseMessage(parser);
  63.                     break;
  64.                 default:
  65.                     LOGGER.warning("Unsupported forwarded packet type: " + name);
  66.                 }
  67.                 break;
  68.             case END_ELEMENT:
  69.                 if (parser.getDepth() == initialDepth) {
  70.                     break outerloop;
  71.                 }
  72.                 break;
  73.             default:
  74.                 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  75.                 break;
  76.             }
  77.         }

  78.         if (packet == null) {
  79.             // TODO: Should be SmackParseException.
  80.             throw new IOException("forwarded extension must contain a packet");
  81.         }
  82.         return new Forwarded<>(packet, di);
  83.     }

  84.     public static Forwarded<Message> parseForwardedMessage(XmlPullParser parser, XmlEnvironment xmlEnvironment)
  85.                     throws XmlPullParserException, IOException, SmackParsingException, ParseException {
  86.         return parseForwardedMessage(parser, parser.getDepth(), xmlEnvironment);
  87.     }

  88.     @SuppressWarnings("unchecked")
  89.     public static Forwarded<Message> parseForwardedMessage(XmlPullParser parser, int initialDepth,
  90.                     XmlEnvironment xmlEnvironment)
  91.                     throws XmlPullParserException, IOException, SmackParsingException, ParseException {
  92.         Forwarded<?> forwarded = INSTANCE.parse(parser, initialDepth, xmlEnvironment);
  93.         if (!forwarded.isForwarded(Message.class)) {
  94.             throw new SmackParsingException("Expecting a forwarded message, but got " + forwarded);
  95.         }
  96.         return (Forwarded<Message>) forwarded;
  97.     }
  98. }