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.protocol.sync;
17  
18  import org.fourthline.cling.UpnpService;
19  import org.fourthline.cling.model.gena.LocalGENASubscription;
20  import org.fourthline.cling.model.message.StreamRequestMessage;
21  import org.fourthline.cling.model.message.StreamResponseMessage;
22  import org.fourthline.cling.model.message.UpnpResponse;
23  import org.fourthline.cling.model.message.gena.IncomingUnsubscribeRequestMessage;
24  import org.fourthline.cling.model.resource.ServiceEventSubscriptionResource;
25  import org.fourthline.cling.protocol.ReceivingSync;
26  import org.fourthline.cling.transport.RouterException;
27  
28  import java.util.logging.Logger;
29  
30  /**
31   * Handles reception of GENA event unsubscribe messages.
32   *
33   * @author Christian Bauer
34   */
35  public class ReceivingUnsubscribe extends ReceivingSync<StreamRequestMessage, StreamResponseMessage> {
36  
37      final private static Logger log = Logger.getLogger(ReceivingUnsubscribe.class.getName());
38  
39      public ReceivingUnsubscribe(UpnpService upnpService, StreamRequestMessage inputMessage) {
40          super(upnpService, inputMessage);
41      }
42  
43      protected StreamResponseMessage executeSync() throws RouterException {
44  
45          ServiceEventSubscriptionResource resource =
46                  getUpnpService().getRegistry().getResource(
47                          ServiceEventSubscriptionResource.class,
48                          getInputMessage().getUri()
49          );
50  
51          if (resource == null) {
52              log.fine("No local resource found: " + getInputMessage());
53              return null;
54          }
55  
56          log.fine("Found local event subscription matching relative request URI: " + getInputMessage().getUri());
57  
58          IncomingUnsubscribeRequestMessage requestMessage =
59                  new IncomingUnsubscribeRequestMessage(getInputMessage(), resource.getModel());
60  
61          // Error conditions UDA 1.0 section 4.1.3
62          if (requestMessage.getSubscriptionId() != null &&
63                  (requestMessage.hasNotificationHeader() || requestMessage.hasCallbackHeader())) {
64              log.fine("Subscription ID and NT or Callback in unsubcribe request: " + getInputMessage());
65              return new StreamResponseMessage(UpnpResponse.Status.BAD_REQUEST);
66          }
67  
68          LocalGENASubscription subscription =
69                  getUpnpService().getRegistry().getLocalSubscription(requestMessage.getSubscriptionId());
70  
71          if (subscription == null) {
72              log.fine("Invalid subscription ID for unsubscribe request: " + getInputMessage());
73              return new StreamResponseMessage(UpnpResponse.Status.PRECONDITION_FAILED);
74          }
75  
76          log.fine("Unregistering subscription: " + subscription);
77          if (getUpnpService().getRegistry().removeLocalSubscription(subscription)) {
78              subscription.end(null); // No reason, just an unsubscribe
79          } else {
80              log.fine("Subscription was already removed from registry");
81          }
82  
83          return new StreamResponseMessage(UpnpResponse.Status.OK);
84      }
85  }