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.util.XmppDateTime; 038 039/** 040 * The StreamInitiationProvider parses StreamInitiation packets. 041 * 042 * @author Alexander Wenckus 043 * 044 */ 045public class StreamInitiationProvider extends IqProvider<StreamInitiation> { 046 private static final Logger LOGGER = Logger.getLogger(StreamInitiationProvider.class.getName()); 047 048 @Override 049 public StreamInitiation parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { 050 // si 051 String id = parser.getAttributeValue("", "id"); 052 String mimeType = parser.getAttributeValue("", "mime-type"); 053 054 StreamInitiation initiation = new StreamInitiation(); 055 056 // file 057 String name = null; 058 String size = null; 059 String hash = null; 060 String date = null; 061 String desc = null; 062 boolean isRanged = false; 063 064 // feature 065 DataForm form = null; 066 DataFormProvider dataFormProvider = new DataFormProvider(); 067 068 outerloop: while (true) { 069 XmlPullParser.Event eventType = parser.next(); 070 if (eventType == XmlPullParser.Event.START_ELEMENT) { 071 String elementName = parser.getName(); 072 String namespace = parser.getNamespace(); 073 if (elementName.equals("file")) { 074 name = parser.getAttributeValue("", "name"); 075 size = parser.getAttributeValue("", "size"); 076 hash = parser.getAttributeValue("", "hash"); 077 date = parser.getAttributeValue("", "date"); 078 } else if (elementName.equals("desc")) { 079 desc = parser.nextText(); 080 } else if (elementName.equals("range")) { 081 isRanged = true; 082 } else if (elementName.equals("x") 083 && namespace.equals("jabber:x:data")) { 084 form = dataFormProvider.parse(parser); 085 } 086 } else if (eventType == XmlPullParser.Event.END_ELEMENT) { 087 if (parser.getDepth() == initialDepth) { 088 break outerloop; 089 } 090 if (parser.getName().equals("file")) { 091 long fileSize = 0; 092 if (size != null && size.trim().length() != 0) { 093 try { 094 fileSize = Long.parseLong(size); 095 } 096 catch (NumberFormatException e) { 097 LOGGER.log(Level.SEVERE, "Failed to parse file size from " + fileSize, e); 098 } 099 } 100 101 Date fileDate = new Date(); 102 if (date != null) { 103 try { 104 fileDate = XmppDateTime.parseDate(date); 105 } catch (ParseException e) { 106 // couldn't parse date, use current date-time 107 } 108 } 109 110 File file = new File(name, fileSize); 111 file.setHash(hash); 112 file.setDate(fileDate); 113 file.setDesc(desc); 114 file.setRanged(isRanged); 115 initiation.setFile(file); 116 } 117 } 118 } 119 120 initiation.setSessionID(id); 121 initiation.setMimeType(mimeType); 122 123 initiation.setFeatureNegotiationForm(form); 124 125 return initiation; 126 } 127 128}