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 org.fourthline.cling.model.action.ActionException;
19  import org.fourthline.cling.model.message.StreamRequestMessage;
20  import org.fourthline.cling.model.message.header.SoapActionHeader;
21  import org.fourthline.cling.model.message.header.UpnpHeader;
22  import org.fourthline.cling.model.meta.Action;
23  import org.fourthline.cling.model.meta.LocalService;
24  import org.fourthline.cling.model.meta.QueryStateVariableAction;
25  import org.fourthline.cling.model.types.ErrorCode;
26  import org.fourthline.cling.model.types.SoapActionType;
27  
28  /**
29   * @author Christian Bauer
30   */
31  public class IncomingActionRequestMessage extends StreamRequestMessage implements ActionRequestMessage {
32  
33      final private Action action;
34      final private String actionNamespace;
35  
36      public IncomingActionRequestMessage(StreamRequestMessage source,
37                                          LocalService service) throws ActionException {
38          super(source);
39  
40          SoapActionHeader soapActionHeader = getHeaders().getFirstHeader(UpnpHeader.Type.SOAPACTION, SoapActionHeader.class);
41          if (soapActionHeader == null) {
42              throw new ActionException(ErrorCode.INVALID_ACTION, "Missing SOAP action header");
43          }
44  
45          SoapActionType actionType = soapActionHeader.getValue();
46  
47          this.action = service.getAction(actionType.getActionName());
48          if (this.action == null) {
49              throw new ActionException(ErrorCode.INVALID_ACTION, "Service doesn't implement action: " + actionType.getActionName());
50          }
51  
52          if (!QueryStateVariableAction.ACTION_NAME.equals(actionType.getActionName())) {
53              if (!service.getServiceType().implementsVersion(actionType.getServiceType())) {
54                  throw new ActionException(ErrorCode.INVALID_ACTION, "Service doesn't support the requested service version");
55              }
56          }
57  
58          this.actionNamespace = actionType.getTypeString();
59      }
60  
61      public Action getAction() {
62          return action;
63      }
64  
65      public String getActionNamespace() {
66          return actionNamespace;
67      }
68  
69  }