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.CancelReason;
20  import org.fourthline.cling.model.gena.RemoteGENASubscription;
21  import org.fourthline.cling.model.message.StreamResponseMessage;
22  import org.fourthline.cling.model.message.gena.OutgoingUnsubscribeRequestMessage;
23  import org.fourthline.cling.protocol.SendingSync;
24  import org.fourthline.cling.transport.RouterException;
25  
26  import java.util.logging.Logger;
27  
28  /**
29   * Disconnecting a GENA event subscription with a remote host.
30   * <p>
31   * Calls the {@link org.fourthline.cling.model.gena.RemoteGENASubscription#end(org.fourthline.cling.model.gena.CancelReason, org.fourthline.cling.model.message.UpnpResponse)}
32   * method if the subscription request was responded to correctly. No {@link org.fourthline.cling.model.gena.CancelReason}
33   * will be provided if the unsubscribe procedure completed as expected, otherwise <code>UNSUBSCRIBE_FAILED</code>
34   * is used. The response might be <code>null</code> if no response was received from the remote host.
35   * </p>
36   *
37   * @author Christian Bauer
38   */
39  public class SendingUnsubscribe extends SendingSync<OutgoingUnsubscribeRequestMessage, StreamResponseMessage> {
40  
41      final private static Logger log = Logger.getLogger(SendingUnsubscribe.class.getName());
42  
43      final protected RemoteGENASubscription subscription;
44  
45      public SendingUnsubscribe(UpnpService upnpService, RemoteGENASubscription subscription) {
46          super(
47              upnpService,
48              new OutgoingUnsubscribeRequestMessage(
49                  subscription,
50                  upnpService.getConfiguration().getEventSubscriptionHeaders(subscription.getService())
51              )
52          );
53          this.subscription = subscription;
54      }
55  
56      protected StreamResponseMessage executeSync() throws RouterException {
57  
58          log.fine("Sending unsubscribe request: " + getInputMessage());
59  
60          StreamResponseMessage response = null;
61          try {
62              response = getUpnpService().getRouter().send(getInputMessage());
63              return response;
64          } finally {
65              onUnsubscribe(response);
66          }
67      }
68  
69      protected void onUnsubscribe(final StreamResponseMessage response) {
70          // Always remove from the registry and end the subscription properly - even if it's failed
71          getUpnpService().getRegistry().removeRemoteSubscription(subscription);
72  
73          getUpnpService().getConfiguration().getRegistryListenerExecutor().execute(
74              new Runnable() {
75                  public void run() {
76                      if (response == null) {
77                          log.fine("Unsubscribe failed, no response received");
78                          subscription.end(CancelReason.UNSUBSCRIBE_FAILED, null);
79                      } else if (response.getOperation().isFailed()) {
80                          log.fine("Unsubscribe failed, response was: " + response);
81                          subscription.end(CancelReason.UNSUBSCRIBE_FAILED, response.getOperation());
82                      } else {
83                          log.fine("Unsubscribe successful, response was: " + response);
84                          subscription.end(null, response.getOperation());
85                      }
86                  }
87              }
88          );
89      }
90  }