1
2
3
4
5
6
7
8
9
10
11
12
13
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
33
34
35
36
37
38
39
40
41
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
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
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 }