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 039public class UrlDataElementProvider extends ExtensionElementProvider<UrlDataElement> { 040 041 @Override 042 public UrlDataElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { 043 String target = parser.getAttributeValue(ATTR_TARGET); 044 String sid = parser.getAttributeValue(ATTR_SID); 045 List<HttpAuthElement> authElements = new ArrayList<>(); 046 List<CookieElement> cookieElements = new ArrayList<>(); 047 List<HeaderElement> headerElements = new ArrayList<>(); 048 do { 049 XmlPullParser.TagEvent event = parser.nextTag(); 050 String name = parser.getName(); 051 052 if (event == XmlPullParser.TagEvent.START_ELEMENT) { 053 switch (name) { 054 case UrlDataElement.ELEMENT: 055 continue; 056 057 case HttpAuthElement.ELEMENT: 058 String scheme = parser.getAttributeValue(HttpAuthElement.ATTR_SCHEME); 059 List<AuthParamElement> authParamElements = new ArrayList<>(); 060 int innerDepth = parser.getDepth(); 061 do { 062 XmlPullParser.TagEvent innerTag = parser.nextTag(); 063 String innerName = parser.getName(); 064 if (innerTag.equals(XmlPullParser.TagEvent.START_ELEMENT)) { 065 if (innerName.equals(AuthParamElement.ELEMENT)) { 066 String attrName = ParserUtils.getRequiredAttribute(parser, AuthParamElement.ATTR_NAME); 067 String attrVal = ParserUtils.getRequiredAttribute(parser, AuthParamElement.ATTR_VALUE); 068 authParamElements.add(new AuthParamElement(attrName, attrVal)); 069 } 070 } 071 } while (parser.getDepth() != innerDepth); 072 073 authElements.add(new HttpAuthElement(scheme, authParamElements)); 074 break; 075 076 case CookieElement.ELEMENT: 077 String cookieName = ParserUtils.getRequiredAttribute(parser, CookieElement.ATTR_NAME); 078 String cookieValue = ParserUtils.getRequiredAttribute(parser, CookieElement.ATTR_VALUE); 079 String cookieDomain = parser.getAttributeValue(CookieElement.ATTR_DOMAIN); 080 Integer cookieMaxAge = ParserUtils.getIntegerAttribute(parser, CookieElement.ATTR_MAX_AGE); 081 String cookiePath = parser.getAttributeValue(CookieElement.ATTR_PATH); 082 String cookieComment = parser.getAttributeValue(CookieElement.ATTR_COMMENT); 083 Boolean cookieSecure = ParserUtils.getBooleanAttribute(parser, CookieElement.ATTR_SECURE); 084 String cookieVersion = parser.getAttributeValue(CookieElement.ATTR_VERSION); 085 086 cookieElements.add(new CookieElement(cookieName, cookieValue, cookieDomain, cookieMaxAge, cookiePath, cookieComment, cookieVersion, cookieSecure)); 087 break; 088 089 case HeaderElement.ELEMENT: 090 String headerName = ParserUtils.getRequiredAttribute(parser, HeaderElement.ATTR_NAME); 091 String headerValue = ParserUtils.getRequiredAttribute(parser, HeaderElement.ATTR_VALUE); 092 093 headerElements.add(new HeaderElement(headerName, headerValue)); 094 break; 095 } 096 } 097 } while (parser.getDepth() != initialDepth); 098 099 return new UrlDataElement(target, sid, authElements, cookieElements, headerElements); 100 } 101}