001/** 002 * 003 * Copyright 2014 Vyacheslav Blinov 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.amp.provider; 018 019import java.util.logging.Logger; 020 021import org.jivesoftware.smack.packet.PacketExtension; 022import org.jivesoftware.smack.provider.PacketExtensionProvider; 023import org.jivesoftware.smackx.amp.AMPDeliverCondition; 024import org.jivesoftware.smackx.amp.AMPExpireAtCondition; 025import org.jivesoftware.smackx.amp.AMPMatchResourceCondition; 026import org.jivesoftware.smackx.amp.packet.AMPExtension; 027import org.xmlpull.v1.XmlPullParser; 028 029 030public class AMPExtensionProvider implements PacketExtensionProvider { 031 private static final Logger LOGGER = Logger.getLogger(AMPExtensionProvider.class.getName()); 032 033 /** 034 * Creates a new AMPExtensionProvider. 035 * ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor 036 */ 037 public AMPExtensionProvider() {} 038 039 /** 040 * Parses a AMPExtension packet (extension sub-packet). 041 * 042 * @param parser the XML parser, positioned at the starting element of the extension. 043 * @return a PacketExtension. 044 * @throws Exception if a parsing error occurs. 045 */ 046 @Override 047 public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 048 final String from = parser.getAttributeValue(null, "from"); 049 final String to = parser.getAttributeValue(null, "to"); 050 final String statusString = parser.getAttributeValue(null, "status"); 051 AMPExtension.Status status = null; 052 if (statusString != null) { 053 try { 054 status = AMPExtension.Status.valueOf(statusString); 055 } catch (IllegalArgumentException ex) { 056 LOGGER.severe("Found invalid amp status " + statusString); 057 } 058 } 059 060 AMPExtension ampExtension = new AMPExtension(from, to, status); 061 062 String perHopValue = parser.getAttributeValue(null, "per-hop"); 063 if (perHopValue != null) { 064 boolean perHop = Boolean.parseBoolean(perHopValue); 065 ampExtension.setPerHop(perHop); 066 } 067 068 boolean done = false; 069 while (!done) { 070 int eventType = parser.next(); 071 if (eventType == XmlPullParser.START_TAG) { 072 if (parser.getName().equals(AMPExtension.Rule.ELEMENT)) { 073 String actionString = parser.getAttributeValue(null, AMPExtension.Action.ATTRIBUTE_NAME); 074 String conditionName = parser.getAttributeValue(null, AMPExtension.Condition.ATTRIBUTE_NAME); 075 String conditionValue = parser.getAttributeValue(null, "value"); 076 077 AMPExtension.Condition condition = createCondition(conditionName, conditionValue); 078 AMPExtension.Action action = null; 079 if (actionString != null) { 080 try { 081 action = AMPExtension.Action.valueOf(actionString); 082 } catch (IllegalArgumentException ex) { 083 LOGGER.severe("Found invalid rule action value " + actionString); 084 } 085 } 086 087 if (action == null || condition == null) { 088 LOGGER.severe("Rule is skipped because either it's action or it's condition is invalid"); 089 } else { 090 AMPExtension.Rule rule = new AMPExtension.Rule(action, condition); 091 ampExtension.addRule(rule); 092 } 093 } 094 } else if (eventType == XmlPullParser.END_TAG) { 095 if (parser.getName().equals(AMPExtension.ELEMENT)) { 096 done = true; 097 } 098 } 099 } 100 101 return ampExtension; 102 } 103 104 private AMPExtension.Condition createCondition(String name, String value) { 105 if (name == null || value == null) { 106 LOGGER.severe("Can't create rule condition from null name and/or value"); 107 return null; 108 } 109 110 111 if (AMPDeliverCondition.NAME.equals(name)) { 112 try { 113 return new AMPDeliverCondition(AMPDeliverCondition.Value.valueOf(value)); 114 } catch (IllegalArgumentException ex) { 115 LOGGER.severe("Found invalid rule delivery condition value " + value); 116 return null; 117 } 118 } else if (AMPExpireAtCondition.NAME.equals(name)) { 119 return new AMPExpireAtCondition(value); 120 } else if (AMPMatchResourceCondition.NAME.equals(name)) { 121 try { 122 return new AMPMatchResourceCondition(AMPMatchResourceCondition.Value.valueOf(value)); 123 } catch (IllegalArgumentException ex) { 124 LOGGER.severe("Found invalid rule match-resource condition value " + value); 125 return null; 126 } 127 } else { 128 LOGGER.severe("Found unknown rule condition name " + name); 129 return null; 130 } 131 } 132}