ForwardedProvider.java

  1. /**
  2.  *
  3.  * Copyright 2013-2014 Georg Lukas
  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.util.logging.Logger;

  19. import org.jivesoftware.smack.SmackException;
  20. import org.jivesoftware.smack.packet.Message;
  21. import org.jivesoftware.smack.packet.Stanza;
  22. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  23. import org.jivesoftware.smack.util.PacketParserUtils;
  24. import org.jivesoftware.smackx.delay.packet.DelayInformation;
  25. import org.jivesoftware.smackx.delay.provider.DelayInformationProvider;
  26. import org.jivesoftware.smackx.forward.packet.Forwarded;
  27. import org.xmlpull.v1.XmlPullParser;

  28. /**
  29.  * This class implements the {@link ExtensionElementProvider} to parse
  30.  * forwarded messages from a packet.  It will return a {@link Forwarded} packet extension.
  31.  *
  32.  * @author Georg Lukas
  33.  */
  34. public class ForwardedProvider extends ExtensionElementProvider<Forwarded> {

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

  36.     @Override
  37.     public Forwarded parse(XmlPullParser parser, int initialDepth) throws Exception {
  38.         DelayInformation di = null;
  39.         Stanza packet = null;

  40.         outerloop: while (true) {
  41.             int eventType = parser.next();
  42.             switch (eventType) {
  43.             case XmlPullParser.START_TAG:
  44.                 String name = parser.getName();
  45.                 String namespace = parser.getNamespace();
  46.                 switch (name) {
  47.                 case DelayInformation.ELEMENT:
  48.                     if (DelayInformation.NAMESPACE.equals(namespace)) {
  49.                         di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth());
  50.                     } else {
  51.                         LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '"
  52.                                         + DelayInformation.NAMESPACE + "'");
  53.                     }
  54.                     break;
  55.                 case Message.ELEMENT:
  56.                     packet = PacketParserUtils.parseMessage(parser);
  57.                     break;
  58.                 default:
  59.                     LOGGER.warning("Unsupported forwarded packet type: " + name);
  60.                 }
  61.             case XmlPullParser.END_TAG:
  62.                 if (parser.getDepth() == initialDepth) {
  63.                     break outerloop;
  64.                 }
  65.                 break;
  66.             }
  67.         }

  68.         if (packet == null)
  69.             throw new SmackException("forwarded extension must contain a packet");
  70.         return new Forwarded(di, packet);
  71.     }
  72. }