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.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   * @author Christian Bauer
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          // For proxy remote invocations, pass through the user agent header
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          // We need to keep it for later, convenience for writing the SOAP body XML
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          /* TODO: Finish the M-POST crap (or not)
94          } else if (getOperation().getMethod().equals(UpnpRequest.Method.MPOST)) {
95  
96              getHeaders().add(UpnpHeader.Type.MAN, new MANHeader(Constants.SOAP_NS_ENVELOPE, "01"));
97  
98              getHeaders().add(UpnpHeader.Type.SOAPACTION, soapActionHeader);
99              getHeaders().setPrefix(UpnpHeader.Type.SOAPACTION, "01");
100             log.fine("Added SOAP action header with prefix '01': " + getHeaders().getFirstHeader(UpnpHeader.Type.SOAPACTION).getString());
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 }