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.text.ParseException;
  19. import java.util.Date;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;

  22. import org.jivesoftware.smack.provider.IQProvider;
  23. import org.jxmpp.util.XmppDateTime;
  24. import org.jivesoftware.smackx.si.packet.StreamInitiation;
  25. import org.jivesoftware.smackx.si.packet.StreamInitiation.File;
  26. import org.jivesoftware.smackx.xdata.packet.DataForm;
  27. import org.jivesoftware.smackx.xdata.provider.DataFormProvider;
  28. import org.xmlpull.v1.XmlPullParser;

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

  37.     @Override
  38.     public StreamInitiation parse(XmlPullParser parser, int initialDepth)
  39.                     throws Exception {
  40.         boolean done = false;

  41.         // si
  42.         String id = parser.getAttributeValue("", "id");
  43.         String mimeType = parser.getAttributeValue("", "mime-type");

  44.         StreamInitiation initiation = new StreamInitiation();

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

  52.         // feature
  53.         DataForm form = null;
  54.         DataFormProvider dataFormProvider = new DataFormProvider();

  55.         int eventType;
  56.         String elementName;
  57.         String namespace;
  58.         while (!done) {
  59.             eventType = parser.next();
  60.             elementName = parser.getName();
  61.             namespace = parser.getNamespace();
  62.             if (eventType == XmlPullParser.START_TAG) {
  63.                 if (elementName.equals("file")) {
  64.                     name = parser.getAttributeValue("", "name");
  65.                     size = parser.getAttributeValue("", "size");
  66.                     hash = parser.getAttributeValue("", "hash");
  67.                     date = parser.getAttributeValue("", "date");
  68.                 } else if (elementName.equals("desc")) {
  69.                     desc = parser.nextText();
  70.                 } else if (elementName.equals("range")) {
  71.                     isRanged = true;
  72.                 } else if (elementName.equals("x")
  73.                         && namespace.equals("jabber:x:data")) {
  74.                     form = dataFormProvider.parse(parser);
  75.                 }
  76.             } else if (eventType == XmlPullParser.END_TAG) {
  77.                 if (elementName.equals("si")) {
  78.                     done = true;
  79.                 } else if (elementName.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.                    
  90.                     Date fileDate = new Date();
  91.                     if (date != null) {
  92.                         try {
  93.                             fileDate = XmppDateTime.parseDate(date);
  94.                         } catch (ParseException e) {
  95.                             // couldn't parse date, use current date-time
  96.                         }
  97.                     }
  98.                    
  99.                     File file = new File(name, fileSize);
  100.                     file.setHash(hash);
  101.                     file.setDate(fileDate);
  102.                     file.setDesc(desc);
  103.                     file.setRanged(isRanged);
  104.                     initiation.setFile(file);
  105.                 }
  106.             }
  107.         }

  108.         initiation.setSessionID(id);
  109.         initiation.setMimeType(mimeType);

  110.         initiation.setFeatureNegotiationForm(form);

  111.         return initiation;
  112.     }

  113. }