001/* 002 * 003 * Copyright 2020 Paul Schaub 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.urldata.provider; 018 019import static org.jivesoftware.smackx.urldata.element.UrlDataElement.ATTR_SID; 020import static org.jivesoftware.smackx.urldata.element.UrlDataElement.ATTR_TARGET; 021 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.List; 025 026import org.jivesoftware.smack.packet.XmlEnvironment; 027import org.jivesoftware.smack.parsing.SmackParsingException; 028import org.jivesoftware.smack.provider.ExtensionElementProvider; 029import org.jivesoftware.smack.util.ParserUtils; 030import org.jivesoftware.smack.xml.XmlPullParser; 031import org.jivesoftware.smack.xml.XmlPullParserException; 032 033import org.jivesoftware.smackx.urldata.element.UrlDataElement; 034import org.jivesoftware.smackx.urldata.http.element.AuthParamElement; 035import org.jivesoftware.smackx.urldata.http.element.CookieElement; 036import org.jivesoftware.smackx.urldata.http.element.HeaderElement; 037import org.jivesoftware.smackx.urldata.http.element.HttpAuthElement; 038 039import org.jxmpp.JxmppContext; 040 041public class UrlDataElementProvider extends ExtensionElementProvider<UrlDataElement> { 042 043 @Override 044 public UrlDataElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException, SmackParsingException { 045 String target = parser.getAttributeValue(ATTR_TARGET); 046 String sid = parser.getAttributeValue(ATTR_SID); 047 List<HttpAuthElement> authElements = new ArrayList<>(); 048 List<CookieElement> cookieElements = new ArrayList<>(); 049 List<HeaderElement> headerElements = new ArrayList<>(); 050 do { 051 XmlPullParser.TagEvent event = parser.nextTag(); 052 String name = parser.getName(); 053 054 if (event == XmlPullParser.TagEvent.START_ELEMENT) { 055 switch (name) { 056 case UrlDataElement.ELEMENT: 057 continue; 058 059 case HttpAuthElement.ELEMENT: 060 String scheme = parser.getAttributeValue(HttpAuthElement.ATTR_SCHEME); 061 List<AuthParamElement> authParamElements = new ArrayList<>(); 062 int innerDepth = parser.getDepth(); 063 do { 064 XmlPullParser.TagEvent innerTag = parser.nextTag(); 065 String innerName = parser.getName(); 066 if (innerTag.equals(XmlPullParser.TagEvent.START_ELEMENT)) { 067 if (innerName.equals(AuthParamElement.ELEMENT)) { 068 String attrName = ParserUtils.getRequiredAttribute(parser, AuthParamElement.ATTR_NAME); 069 String attrVal = ParserUtils.getRequiredAttribute(parser, AuthParamElement.ATTR_VALUE); 070 authParamElements.add(new AuthParamElement(attrName, attrVal)); 071 } 072 } 073 } while (parser.getDepth() != innerDepth); 074 075 authElements.add(new HttpAuthElement(scheme, authParamElements)); 076 break; 077 078 case CookieElement.ELEMENT: 079 String cookieName = ParserUtils.getRequiredAttribute(parser, CookieElement.ATTR_NAME); 080 String cookieValue = ParserUtils.getRequiredAttribute(parser, CookieElement.ATTR_VALUE); 081 String cookieDomain = parser.getAttributeValue(CookieElement.ATTR_DOMAIN); 082 Integer cookieMaxAge = ParserUtils.getIntegerAttribute(parser, CookieElement.ATTR_MAX_AGE); 083 String cookiePath = parser.getAttributeValue(CookieElement.ATTR_PATH); 084 String cookieComment = parser.getAttributeValue(CookieElement.ATTR_COMMENT); 085 Boolean cookieSecure = ParserUtils.getBooleanAttribute(parser, CookieElement.ATTR_SECURE); 086 String cookieVersion = parser.getAttributeValue(CookieElement.ATTR_VERSION); 087 088 cookieElements.add(new CookieElement(cookieName, cookieValue, cookieDomain, cookieMaxAge, cookiePath, cookieComment, cookieVersion, cookieSecure)); 089 break; 090 091 case HeaderElement.ELEMENT: 092 String headerName = ParserUtils.getRequiredAttribute(parser, HeaderElement.ATTR_NAME); 093 String headerValue = ParserUtils.getRequiredAttribute(parser, HeaderElement.ATTR_VALUE); 094 095 headerElements.add(new HeaderElement(headerName, headerValue)); 096 break; 097 } 098 } 099 } while (parser.getDepth() != initialDepth); 100 101 return new UrlDataElement(target, sid, authElements, cookieElements, headerElements); 102 } 103}