1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.model.message.control;
17
18 import java.util.logging.Logger;
19 import org.fourthline.cling.model.action.ActionInvocation;
20 import org.fourthline.cling.model.action.RemoteActionInvocation;
21 import org.fourthline.cling.model.meta.Action;
22 import org.fourthline.cling.model.meta.QueryStateVariableAction;
23 import org.fourthline.cling.model.message.StreamRequestMessage;
24 import org.fourthline.cling.model.message.UpnpRequest;
25 import org.fourthline.cling.model.message.header.ContentTypeHeader;
26 import org.fourthline.cling.model.message.header.SoapActionHeader;
27 import org.fourthline.cling.model.message.header.UpnpHeader;
28 import org.fourthline.cling.model.message.header.UserAgentHeader;
29 import org.fourthline.cling.model.types.SoapActionType;
30
31 import java.net.URL;
32
33
34
35
36 public class OutgoingActionRequestMessage extends StreamRequestMessage implements ActionRequestMessage {
37
38 private static Logger log = Logger.getLogger(OutgoingActionRequestMessage.class.getName());
39
40 final private String actionNamespace;
41
42 public OutgoingActionRequestMessage(ActionInvocation actionInvocation, URL controlURL) {
43 this(actionInvocation.getAction(), new UpnpRequest(UpnpRequest.Method.POST, controlURL));
44
45
46 if (actionInvocation instanceof RemoteActionInvocation) {
47 RemoteActionInvocation remoteActionInvocation = (RemoteActionInvocation) actionInvocation;
48 if (remoteActionInvocation.getRemoteClientInfo() != null
49 && remoteActionInvocation.getRemoteClientInfo().getRequestUserAgent() != null) {
50 getHeaders().add(
51 UpnpHeader.Type.USER_AGENT,
52 new UserAgentHeader(remoteActionInvocation.getRemoteClientInfo().getRequestUserAgent())
53 );
54 }
55 } else if (actionInvocation.getClientInfo() != null) {
56 getHeaders().putAll(actionInvocation.getClientInfo().getRequestHeaders());
57 }
58 }
59
60 public OutgoingActionRequestMessage(Action action, UpnpRequest operation) {
61 super(operation);
62
63 getHeaders().add(
64 UpnpHeader.Type.CONTENT_TYPE,
65 new ContentTypeHeader(ContentTypeHeader.DEFAULT_CONTENT_TYPE_UTF8)
66 );
67
68 SoapActionHeader soapActionHeader;
69 if (action instanceof QueryStateVariableAction) {
70 log.fine("Adding magic control SOAP action header for state variable query action");
71 soapActionHeader = new SoapActionHeader(
72 new SoapActionType(
73 SoapActionType.MAGIC_CONTROL_NS, SoapActionType.MAGIC_CONTROL_TYPE, null, action.getName()
74 )
75 );
76 } else {
77 soapActionHeader = new SoapActionHeader(
78 new SoapActionType(
79 action.getService().getServiceType(),
80 action.getName()
81 )
82 );
83 }
84
85
86 actionNamespace = soapActionHeader.getValue().getTypeString();
87
88 if (getOperation().getMethod().equals(UpnpRequest.Method.POST)) {
89
90 getHeaders().add(UpnpHeader.Type.SOAPACTION, soapActionHeader);
91 log.fine("Added SOAP action header: " + soapActionHeader);
92
93
94
95
96
97
98
99
100
101
102
103 } else {
104 throw new IllegalArgumentException("Can't send action with request method: " + getOperation().getMethod());
105 }
106 }
107
108 public String getActionNamespace() {
109 return actionNamespace;
110 }
111
112 }