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.transport.spi; 17 18 import org.fourthline.cling.model.message.StreamRequestMessage; 19 import org.fourthline.cling.model.message.StreamResponseMessage; 20 import org.fourthline.cling.model.message.UpnpResponse; 21 import org.fourthline.cling.protocol.ProtocolCreationException; 22 import org.fourthline.cling.protocol.ProtocolFactory; 23 import org.fourthline.cling.protocol.ReceivingSync; 24 import org.seamless.util.Exceptions; 25 26 import java.util.logging.Logger; 27 28 /** 29 * A runnable representation of a single HTTP request/response procedure. 30 * <p> 31 * Instantiated by the {@link StreamServer}, executed by the 32 * {@link org.fourthline.cling.transport.Router}. See the pseudo-code example 33 * in the documentation of {@link StreamServer}. An implementation's 34 * <code>run()</code> method has to call the {@link #process(org.fourthline.cling.model.message.StreamRequestMessage)}, 35 * {@link #responseSent(org.fourthline.cling.model.message.StreamResponseMessage)} and 36 * {@link #responseException(Throwable)} methods. 37 * </p> 38 * <p> 39 * An implementation does not have to be thread-safe. 40 * </p> 41 * @author Christian Bauer 42 */ 43 public abstract class UpnpStream implements Runnable { 44 45 private static Logger log = Logger.getLogger(UpnpStream.class.getName()); 46 47 protected final ProtocolFactory protocolFactory; 48 protected ReceivingSync syncProtocol; 49 50 protected UpnpStream(ProtocolFactory protocolFactory) { 51 this.protocolFactory = protocolFactory; 52 } 53 54 public ProtocolFactory getProtocolFactory() { 55 return protocolFactory; 56 } 57 58 /** 59 * Selects a UPnP protocol, runs it within the calling thread, returns the response. 60 * <p> 61 * This method will return <code>null</code> if the UPnP protocol returned <code>null</code>. 62 * The HTTP response in this case is always <em>404 NOT FOUND</em>. Any other (HTTP) error 63 * condition will be encapsulated in the returned response message and has to be 64 * passed to the HTTP client as it is. 65 * </p> 66 * @param requestMsg The TCP (HTTP) stream request message. 67 * @return The TCP (HTTP) stream response message, or <code>null</code> if a 404 should be send to the client. 68 */ 69 public StreamResponseMessage process(StreamRequestMessage requestMsg) { 70 log.fine("Processing stream request message: " + requestMsg); 71 72 try { 73 // Try to get a protocol implementation that matches the request message 74 syncProtocol = getProtocolFactory().createReceivingSync(requestMsg); 75 } catch (ProtocolCreationException ex) { 76 log.warning("Processing stream request failed - " + Exceptions.unwrap(ex).toString()); 77 return new StreamResponseMessage(UpnpResponse.Status.NOT_IMPLEMENTED); 78 } 79 80 // Run it 81 log.fine("Running protocol for synchronous message processing: " + syncProtocol); 82 syncProtocol.run(); 83 84 // ... then grab the response 85 StreamResponseMessage responseMsg = syncProtocol.getOutputMessage(); 86 87 if (responseMsg == null) { 88 // That's ok, the caller is supposed to handle this properly (e.g. convert it to HTTP 404) 89 log.finer("Protocol did not return any response message"); 90 return null; 91 } 92 log.finer("Protocol returned response: " + responseMsg); 93 return responseMsg; 94 } 95 96 /** 97 * Must be called by a subclass after the response has been successfully sent to the client. 98 * 99 * @param responseMessage The response message successfully sent to the client. 100 */ 101 protected void responseSent(StreamResponseMessage responseMessage) { 102 if (syncProtocol != null) 103 syncProtocol.responseSent(responseMessage); 104 } 105 106 /** 107 * Must be called by a subclass if the response was not delivered to the client. 108 * 109 * @param t The reason why the response wasn't delivered. 110 */ 111 protected void responseException(Throwable t) { 112 if (syncProtocol != null) 113 syncProtocol.responseException(t); 114 } 115 116 @Override 117 public String toString() { 118 return "(" + getClass().getSimpleName() + ")"; 119 } 120 }