StreamInitiationProvider.java

  1. /**
  2.  *
  3.  * Copyright 2003-2006 Jive Software.
  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.si.provider;

  18. import java.io.IOException;
  19. import java.text.ParseException;
  20. import java.util.Date;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;

  23. import org.jivesoftware.smack.packet.XmlEnvironment;
  24. import org.jivesoftware.smack.parsing.SmackParsingException;
  25. import org.jivesoftware.smack.provider.IQProvider;
  26. import org.jivesoftware.smack.xml.XmlPullParser;
  27. import org.jivesoftware.smack.xml.XmlPullParserException;

  28. import org.jivesoftware.smackx.si.packet.StreamInitiation;
  29. import org.jivesoftware.smackx.si.packet.StreamInitiation.File;
  30. import org.jivesoftware.smackx.xdata.packet.DataForm;
  31. import org.jivesoftware.smackx.xdata.provider.DataFormProvider;

  32. import org.jxmpp.util.XmppDateTime;

  33. /**
  34.  * The StreamInitiationProvider parses StreamInitiation packets.
  35.  *
  36.  * @author Alexander Wenckus
  37.  *
  38.  */
  39. public class StreamInitiationProvider extends IQProvider<StreamInitiation> {
  40.     private static final Logger LOGGER = Logger.getLogger(StreamInitiationProvider.class.getName());

  41.     @Override
  42.     public StreamInitiation parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
  43.         // si
  44.         String id = parser.getAttributeValue("", "id");
  45.         String mimeType = parser.getAttributeValue("", "mime-type");

  46.         StreamInitiation initiation = new StreamInitiation();

  47.         // file
  48.         String name = null;
  49.         String size = null;
  50.         String hash = null;
  51.         String date = null;
  52.         String desc = null;
  53.         boolean isRanged = false;

  54.         // feature
  55.         DataForm form = null;
  56.         DataFormProvider dataFormProvider = new DataFormProvider();

  57.         outerloop: while (true) {
  58.             XmlPullParser.Event eventType = parser.next();
  59.             if (eventType == XmlPullParser.Event.START_ELEMENT) {
  60.                 String elementName = parser.getName();
  61.                 String namespace = parser.getNamespace();
  62.                 if (elementName.equals("file")) {
  63.                     name = parser.getAttributeValue("", "name");
  64.                     size = parser.getAttributeValue("", "size");
  65.                     hash = parser.getAttributeValue("", "hash");
  66.                     date = parser.getAttributeValue("", "date");
  67.                 } else if (elementName.equals("desc")) {
  68.                     desc = parser.nextText();
  69.                 } else if (elementName.equals("range")) {
  70.                     isRanged = true;
  71.                 } else if (elementName.equals("x")
  72.                         && namespace.equals("jabber:x:data")) {
  73.                     form = dataFormProvider.parse(parser);
  74.                 }
  75.             } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  76.                 if (parser.getDepth() == initialDepth) {
  77.                     break outerloop;
  78.                 }
  79.                 if (parser.getName().equals("file")) {
  80.                     long fileSize = 0;
  81.                     if (size != null && size.trim().length() != 0) {
  82.                         try {
  83.                             fileSize = Long.parseLong(size);
  84.                         }
  85.                         catch (NumberFormatException e) {
  86.                             LOGGER.log(Level.SEVERE, "Failed to parse file size from " + fileSize, e);
  87.                         }
  88.                     }

  89.                     Date fileDate = new Date();
  90.                     if (date != null) {
  91.                         try {
  92.                             fileDate = XmppDateTime.parseDate(date);
  93.                         } catch (ParseException e) {
  94.                             // couldn't parse date, use current date-time
  95.                         }
  96.                     }

  97.                     File file = new File(name, fileSize);
  98.                     file.setHash(hash);
  99.                     file.setDate(fileDate);
  100.                     file.setDesc(desc);
  101.                     file.setRanged(isRanged);
  102.                     initiation.setFile(file);
  103.                 }
  104.             }
  105.         }

  106.         initiation.setSessionID(id);
  107.         initiation.setMimeType(mimeType);

  108.         initiation.setFeatureNegotiationForm(form);

  109.         return initiation;
  110.     }

  111. }