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