001/* 002 * 003 * Copyright 2003-2006 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.si.provider; 018 019import java.io.IOException; 020import java.text.ParseException; 021import java.util.Date; 022import java.util.logging.Level; 023import java.util.logging.Logger; 024 025import org.jivesoftware.smack.packet.IqData; 026import org.jivesoftware.smack.packet.XmlEnvironment; 027import org.jivesoftware.smack.parsing.SmackParsingException; 028import org.jivesoftware.smack.provider.IqProvider; 029import org.jivesoftware.smack.xml.XmlPullParser; 030import org.jivesoftware.smack.xml.XmlPullParserException; 031 032import org.jivesoftware.smackx.si.packet.StreamInitiation; 033import org.jivesoftware.smackx.si.packet.StreamInitiation.File; 034import org.jivesoftware.smackx.xdata.packet.DataForm; 035import org.jivesoftware.smackx.xdata.provider.DataFormProvider; 036 037import org.jxmpp.JxmppContext; 038import org.jxmpp.util.XmppDateTime; 039 040/** 041 * The StreamInitiationProvider parses StreamInitiation packets. 042 * 043 * @author Alexander Wenckus 044 * 045 */ 046public class StreamInitiationProvider extends IqProvider<StreamInitiation> { 047 private static final Logger LOGGER = Logger.getLogger(StreamInitiationProvider.class.getName()); 048 049 @SuppressWarnings("JavaUtilDate") 050 @Override 051 public StreamInitiation parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException, SmackParsingException { 052 // si 053 String id = parser.getAttributeValue("", "id"); 054 String mimeType = parser.getAttributeValue("", "mime-type"); 055 056 StreamInitiation initiation = new StreamInitiation(); 057 058 // file 059 String name = null; 060 String size = null; 061 String hash = null; 062 String date = null; 063 String desc = null; 064 boolean isRanged = false; 065 066 // feature 067 DataForm form = null; 068 DataFormProvider dataFormProvider = new DataFormProvider(); 069 070 outerloop: while (true) { 071 XmlPullParser.Event eventType = parser.next(); 072 if (eventType == XmlPullParser.Event.START_ELEMENT) { 073 String elementName = parser.getName(); 074 String namespace = parser.getNamespace(); 075 if (elementName.equals("file")) { 076 name = parser.getAttributeValue("", "name"); 077 size = parser.getAttributeValue("", "size"); 078 hash = parser.getAttributeValue("", "hash"); 079 date = parser.getAttributeValue("", "date"); 080 } else if (elementName.equals("desc")) { 081 desc = parser.nextText(); 082 } else if (elementName.equals("range")) { 083 isRanged = true; 084 } else if (elementName.equals("x") 085 && namespace.equals("jabber:x:data")) { 086 form = dataFormProvider.parse(parser); 087 } 088 } else if (eventType == XmlPullParser.Event.END_ELEMENT) { 089 if (parser.getDepth() == initialDepth) { 090 break outerloop; 091 } 092 if (parser.getName().equals("file")) { 093 long fileSize = 0; 094 if (size != null && size.trim().length() != 0) { 095 try { 096 fileSize = Long.parseLong(size); 097 } 098 catch (NumberFormatException e) { 099 LOGGER.log(Level.SEVERE, "Failed to parse file size from " + fileSize, e); 100 } 101 } 102 103 Date fileDate = new Date(); 104 if (date != null) { 105 try { 106 fileDate = XmppDateTime.parseDate(date); 107 } catch (ParseException e) { 108 // couldn't parse date, use current date-time 109 } 110 } 111 112 File file = new File(name, fileSize); 113 file.setHash(hash); 114 file.setDate(fileDate); 115 file.setDesc(desc); 116 file.setRanged(isRanged); 117 initiation.setFile(file); 118 } 119 } 120 } 121 122 initiation.setSessionID(id); 123 initiation.setMimeType(mimeType); 124 125 initiation.setFeatureNegotiationForm(form); 126 127 return initiation; 128 } 129 130}