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.text.ParseException; 020import java.util.Date; 021import java.util.logging.Level; 022import java.util.logging.Logger; 023 024import org.jivesoftware.smack.packet.IQ; 025import org.jivesoftware.smack.provider.IQProvider; 026import org.jivesoftware.smack.util.XmppDateTime; 027import org.jivesoftware.smackx.si.packet.StreamInitiation; 028import org.jivesoftware.smackx.si.packet.StreamInitiation.File; 029import org.jivesoftware.smackx.xdata.packet.DataForm; 030import org.jivesoftware.smackx.xdata.provider.DataFormProvider; 031import org.xmlpull.v1.XmlPullParser; 032 033/** 034 * The StreamInitiationProvider parses StreamInitiation packets. 035 * 036 * @author Alexander Wenckus 037 * 038 */ 039public class StreamInitiationProvider implements IQProvider { 040 private static final Logger LOGGER = Logger.getLogger(StreamInitiationProvider.class.getName()); 041 042 public IQ parseIQ(final XmlPullParser parser) throws Exception { 043 boolean done = false; 044 045 // si 046 String id = parser.getAttributeValue("", "id"); 047 String mimeType = parser.getAttributeValue("", "mime-type"); 048 049 StreamInitiation initiation = new StreamInitiation(); 050 051 // file 052 String name = null; 053 String size = null; 054 String hash = null; 055 String date = null; 056 String desc = null; 057 boolean isRanged = false; 058 059 // feature 060 DataForm form = null; 061 DataFormProvider dataFormProvider = new DataFormProvider(); 062 063 int eventType; 064 String elementName; 065 String namespace; 066 while (!done) { 067 eventType = parser.next(); 068 elementName = parser.getName(); 069 namespace = parser.getNamespace(); 070 if (eventType == XmlPullParser.START_TAG) { 071 if (elementName.equals("file")) { 072 name = parser.getAttributeValue("", "name"); 073 size = parser.getAttributeValue("", "size"); 074 hash = parser.getAttributeValue("", "hash"); 075 date = parser.getAttributeValue("", "date"); 076 } else if (elementName.equals("desc")) { 077 desc = parser.nextText(); 078 } else if (elementName.equals("range")) { 079 isRanged = true; 080 } else if (elementName.equals("x") 081 && namespace.equals("jabber:x:data")) { 082 form = (DataForm) dataFormProvider.parseExtension(parser); 083 } 084 } else if (eventType == XmlPullParser.END_TAG) { 085 if (elementName.equals("si")) { 086 done = true; 087 } else if (elementName.equals("file")) { 088 long fileSize = 0; 089 if(size != null && size.trim().length() !=0){ 090 try { 091 fileSize = Long.parseLong(size); 092 } 093 catch (NumberFormatException e) { 094 LOGGER.log(Level.SEVERE, "Failed to parse file size from " + fileSize, e); 095 } 096 } 097 098 Date fileDate = new Date(); 099 if (date != null) { 100 try { 101 fileDate = XmppDateTime.parseDate(date); 102 } catch (ParseException e) { 103 // couldn't parse date, use current date-time 104 } 105 } 106 107 File file = new File(name, fileSize); 108 file.setHash(hash); 109 file.setDate(fileDate); 110 file.setDesc(desc); 111 file.setRanged(isRanged); 112 initiation.setFile(file); 113 } 114 } 115 } 116 117 initiation.setSessionID(id); 118 initiation.setMimeType(mimeType); 119 120 initiation.setFeatureNegotiationForm(form); 121 122 return initiation; 123 } 124 125}