View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.transport.impl;
17  
18  import java.util.logging.Level;
19  import java.util.logging.Logger;
20  
21  import org.fourthline.cling.model.message.gena.IncomingEventRequestMessage;
22  import org.fourthline.cling.model.meta.StateVariable;
23  import org.fourthline.cling.model.state.StateVariableValue;
24  import org.fourthline.cling.transport.spi.GENAEventProcessor;
25  import org.fourthline.cling.model.UnsupportedDataException;
26  import org.seamless.xml.XmlPullParserUtils;
27  import org.xmlpull.v1.XmlPullParser;
28  
29  import javax.enterprise.inject.Alternative;
30  
31  /**
32   * Implementation based on the <em>Xml Pull Parser</em> XML processing API.
33   * <p>
34   * This processor is more lenient with parsing, looking only for the required XML tags.
35   * </p>
36   * <p>
37   * To use this parser you need to install an implementation of the
38   * <a href="http://www.xmlpull.org/impls.shtml">XMLPull API</a>.
39   * </p>
40   *
41   * @author Michael Pujos
42   */
43  @Alternative
44  public class PullGENAEventProcessorImpl extends GENAEventProcessorImpl {
45  
46  	private static Logger log = Logger.getLogger(GENAEventProcessor.class.getName());
47  
48  	public void readBody(IncomingEventRequestMessage requestMessage) throws UnsupportedDataException {
49          log.fine("Reading body of: " + requestMessage);
50          if (log.isLoggable(Level.FINER)) {
51              log.finer("===================================== GENA BODY BEGIN ============================================");
52              log.finer(requestMessage.getBody() != null ? requestMessage.getBody().toString() : null);
53              log.finer("-===================================== GENA BODY END ============================================");
54          }
55  
56          String body = getMessageBody(requestMessage);
57  		try {
58  			XmlPullParser xpp = XmlPullParserUtils.createParser(body);
59  			readProperties(xpp, requestMessage);
60  		} catch (Exception ex) {
61  			throw new UnsupportedDataException("Can't transform message payload: " + ex.getMessage(), ex, body);	
62  		}
63  	}
64  
65  	protected void readProperties(XmlPullParser xpp, IncomingEventRequestMessage message) throws Exception {
66  		// We're inside the propertyset tag
67  		StateVariable[] stateVariables = message.getService().getStateVariables();
68  		int event;
69  		while((event = xpp.next()) != XmlPullParser.END_DOCUMENT) {
70  			if(event != XmlPullParser.START_TAG) continue;
71  			if(xpp.getName().equals("property")) {
72  				readProperty(xpp, message, stateVariables);
73  			} 
74  		}
75  	}
76  
77  	protected void readProperty(XmlPullParser xpp, IncomingEventRequestMessage message, StateVariable[] stateVariables) throws Exception  {
78  		// We're inside the property tag
79  		int event ;
80  		do {
81  			event = xpp.next();
82  			if(event == XmlPullParser.START_TAG) {
83  
84  				String stateVariableName = xpp.getName();
85  				for (StateVariable stateVariable : stateVariables) {
86  					if (stateVariable.getName().equals(stateVariableName)) {
87  						log.fine("Reading state variable value: " + stateVariableName);
88  						String value = xpp.nextText();
89  						message.getStateVariableValues().add(new StateVariableValue(stateVariable, value));
90  						break;
91  					}
92  				} 
93  			}
94  		} while(event != XmlPullParser.END_DOCUMENT && (event != XmlPullParser.END_TAG || !xpp.getName().equals("property")));
95  	}
96  }