Implemented UPnP presentation page for the BinaryLight sample (Felix-71)
Now you can access to its web page from the Network Resources directory in Win platforms.

git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@454714 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/Activator.java b/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/Activator.java
index 4da4ac8..ebd8146 100644
--- a/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/Activator.java
+++ b/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/Activator.java
@@ -21,9 +21,13 @@
 
 import java.util.Dictionary;
 
+import javax.servlet.Servlet;
+
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.http.HttpService;
 import org.osgi.service.upnp.UPnPDevice;
 
 
@@ -36,6 +40,7 @@
 	static BundleContext context;
 	private ServiceRegistration serviceRegistration;
 	private LightDevice light;
+	private HttpService httpServ;
 	
 	/**
 	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
@@ -43,8 +48,10 @@
 	public void start(BundleContext context) throws Exception {
 		Activator.context = context;
 		doServiceRegistration();
+		doServletRegistration();
 	}
 
+
 	private void doServiceRegistration() {
 		light = new LightDevice(context);
 		Dictionary dict = light.getDescriptions(null);
@@ -55,6 +62,35 @@
 				dict
 			);
 	}
+	
+	private void doServletRegistration() {
+        ServiceReference sr = context.getServiceReference(HttpService.class.getName());
+        if (sr != null){
+        	httpServ = (HttpService) context.getService(sr);
+        	
+            try
+            {
+             Servlet presentationServlet = new PresentationServlet(light.getModel());
+             httpServ.registerServlet("/upnp/binaryLight", presentationServlet,null, null);
+            }
+            catch (Exception e)
+            {
+                System.err.println("Exception registering presentationServlet:" + e);
+            }
+            
+            
+            try
+            {
+                httpServ.registerResources("/upnp/binaryLight/images",
+                        "/org/apache/felix/upnp/sample/binaryLight/images", null);
+            }
+            catch (Exception e)
+            {
+                System.err.println("Exception registering /resource:" + e);
+            }
+
+        }		
+	}
 
 	/**
 	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
diff --git a/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/LightDevice.java b/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/LightDevice.java
index f7c3e04..85bca3f 100644
--- a/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/LightDevice.java
+++ b/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/LightDevice.java
@@ -19,6 +19,8 @@
 

 package org.apache.felix.upnp.sample.binaryLight;

 

+import java.net.InetAddress;

+import java.net.UnknownHostException;

 import java.util.Dictionary;

 import java.util.Properties;

 

@@ -52,6 +54,9 @@
 		buildEventNotifyer();

 	}

 

+	public LightModel getModel(){

+		return model;

+	}

 	/**

 	 * 

 	 */

@@ -75,7 +80,16 @@
 		dictionary.put(UPnPDevice.MODEL_NAME,"Lucciola");

 		dictionary.put(UPnPDevice.MODEL_NUMBER,"1.0");

 		dictionary.put(UPnPDevice.MODEL_URL,"http://incubator.apache.org/felix/lucciola");

-		dictionary.put(UPnPDevice.PRESENTATION_URL,"http://incubator.apache.org/felix/lucciola/presentation");

+		String port = context.getProperty("org.osgi.service.http.port");

+        InetAddress inet;

+		try {

+			inet = InetAddress.getLocalHost();

+	        String hostname = inet.getHostName();

+            //String hostname = inet.getHostAddress();

+		dictionary.put(UPnPDevice.PRESENTATION_URL,"http://"+hostname + ":"+port+"/upnp/binaryLight/");

+		} catch (UnknownHostException e) {

+			System.out.println("Warning: enable to cacth localhost name");

+		}

 		dictionary.put(UPnPDevice.SERIAL_NUMBER,"123456789");

 		dictionary.put(UPnPDevice.TYPE,"urn:schemas-upnp-org:device:BinaryLight:1");

 		dictionary.put(UPnPDevice.UDN,DEVICE_ID);

diff --git a/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/PresentationServlet.java b/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/PresentationServlet.java
new file mode 100644
index 0000000..288253f
--- /dev/null
+++ b/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/PresentationServlet.java
@@ -0,0 +1,87 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.upnp.sample.binaryLight;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+class PresentationServlet extends HttpServlet
+{
+	LightModel model;
+
+    public PresentationServlet(LightModel model) {
+		this.model = model;
+	}
+
+	public void init()
+    {
+		//ServletConfig config = getServletConfig();
+        System.out.println("BinaryLight Servlet init called:" );
+    }
+
+    public void destroy() {}
+
+
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+        throws IOException, ServletException
+    {
+        String path = request.getPathInfo();
+        if (path != null){
+	        if (path.equals("/On"))
+	        	model.switchOn();
+	        else if (path.equals("/Off"))
+	        	model.switchOff();
+        }
+        response.setContentType("text/html");
+        PrintWriter out = response.getWriter();
+        out.println("<HTML>");
+        out.println("<head><title>Apache Felix UPnP BinaryLight</title></head>");
+        out.println("<body>");
+        out.println("  <center>");
+        out.println("  <h1><font face='Arial' color='#808080'>Apache Felix UPnP BinaryLight</font></h1>");
+       
+        if (model.getStatus()== false){
+        	out.println("  <p><a href=/upnp/binaryLight/On><img border='0' src='images/LightOff.gif' width='64' height='64'></a></p>");
+        }
+        else{       	
+        	out.println("  <p><a href=/upnp/binaryLight/Off><img border='0' src='images/LightOn.gif' width='64' height='64'></a></p>");
+        }
+        
+        out.println("  <p><a href=/upnp/binaryLight/>Refresh status</a></p>");
+        out.println("  </center>");
+        out.println("  </BODY>");
+        out.println("</HTML>");
+        out.flush();
+    }
+
+
+    public void doPost(HttpServletRequest request, HttpServletResponse response)
+        throws IOException, ServletException
+    {
+        doGet(request, response);
+    }
+
+}