001/** 002 * 003 * Copyright © 2017 Grigory Fedorov 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.httpfileupload.provider; 018 019import org.jivesoftware.smack.SmackException; 020import org.jivesoftware.smack.provider.IQProvider; 021import org.jivesoftware.smack.util.ParserUtils; 022import org.jivesoftware.smackx.httpfileupload.element.Slot; 023import org.jivesoftware.smackx.httpfileupload.element.Slot_V0_2; 024import org.xmlpull.v1.XmlPullParser; 025import org.xmlpull.v1.XmlPullParserException; 026 027import java.io.IOException; 028import java.net.URL; 029import java.util.HashMap; 030import java.util.Map; 031 032/** 033 * Provider for Slot. 034 * 035 * @author Grigory Fedorov 036 * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a> 037 */ 038public class SlotProvider extends IQProvider<Slot> { 039 040 @Override 041 public Slot parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { 042 final String namespace = parser.getNamespace(); 043 URL putUrl = null; 044 URL getUrl = null; 045 Map<String, String> headers = null; 046 047 outerloop: while (true) { 048 int event = parser.next(); 049 050 switch (event) { 051 case XmlPullParser.START_TAG: 052 String name = parser.getName(); 053 switch(name) { 054 case "put": 055 putUrl = new URL(parser.nextText()); 056 break; 057 case "get": 058 getUrl = new URL(parser.nextText()); 059 break; 060 case "header": 061 String headerName = ParserUtils.getRequiredAttribute(parser, "name"); 062 String headerValue = ParserUtils.getRequiredNextText(parser); 063 if (headers == null) { 064 headers = new HashMap<>(); 065 } 066 headers.put(headerName, headerValue); 067 break; 068 } 069 break; 070 case XmlPullParser.END_TAG: 071 if (parser.getDepth() == initialDepth) { 072 break outerloop; 073 } 074 break; 075 } 076 } 077 078 switch (namespace) { 079 case Slot.NAMESPACE: 080 return new Slot(putUrl, getUrl, headers); 081 case Slot_V0_2.NAMESPACE: 082 return new Slot_V0_2(putUrl, getUrl); 083 default: 084 throw new AssertionError(); 085 } 086 } 087}