1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: HtmlResponse.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   package com.mjh.switchrmi.protocol.html;
27   
28   import com.mjh.switchrmi.*;
29   import com.mjh.switchrmi.jndi.*;
30   
31   import java.io.*;
32   
33   import java.lang.reflect.*;
34   
35   import java.util.*;
36   
37   import javax.naming.*;
38   
39   public class HtmlResponse implements RmiResponse
40   {
41       protected transient RmiContext context;
42       protected String htmlString;
43   
44       public HtmlResponse()
45       {
46       }
47   
48       public HtmlResponse(InputStream in)
49       {
50           ByteArrayOutputStream out = new ByteArrayOutputStream();
51   
52           try
53           {
54               byte[] buf = new byte[1024];
55               boolean reading = true;
56   
57               while (reading)
58               {
59                   int numRead = in.read(buf);
60   
61                   reading = (numRead > 0);
62   
63                   if (reading)
64                   {
65                       out.write(buf, 0, numRead);
66                   }
67               }
68   
69               htmlString = out.toString();
70               out.close();
71           }
72           catch (IOException ex)
73           {
74           }
75           finally
76           {
77               out = null;
78           }
79       }
80   
81       protected void setContext(RmiContext ctxt)
82       {
83           context = ctxt;
84       }
85   
86       protected String toHtmlString()
87                              throws Exception
88       {
89           Context jndiCtx = context.getJndiContext();
90           Context ctx = (Context) jndiCtx.lookup("/switchrmi/service/object");
91           Hashtable env = ctx.getEnvironment();
92           ObjectReferenceable ref = 
93                   (ObjectReferenceable) env.get(context.getObjectName());
94   
95           StringBuffer sb = new StringBuffer();
96   
97           sb.append("<html>\n");
98           sb.append("<head>\n");
99           sb.append("  <title>SwitchRMI Inspector</title>\n");
100          appendStyles(sb);
101          sb.append("</head>\n");
102          sb.append("<body bgcolor=\"lightsteelblue\">\n");
103          sb.append("<p class=\"heading\">\n");
104          sb.append("SwitchRMI Object Name: " + ref.getName() + "\n");
105          sb.append("<p>\n");
106          sb.append(ref.getDescription());
107          sb.append("<p>\n");
108          sb.append("<table border=\1\" cellpadding=\"2\" cellspacing=\"0\">\n");
109  
110          sb.append("  <tr>\n");
111          sb.append("    <td valign=\"top\" align=\"right\">Supported Interfaces</td>\n");
112          sb.append("    <td  valign=\"top\" >");
113          sb.append("<table  border=\"0\" width=\"100%\">");
114  
115          String[] interfaces = ref.getInterfaces();
116  
117          for (int i = 0; i < interfaces.length; i++)
118          {
119              sb.append("<tr><td>");
120              sb.append(interfaces[i]);
121              sb.append("</td></tr>");
122          }
123  
124          sb.append("</table>");
125          sb.append("</td>\n");
126          sb.append("  </tr>\n");
127  
128          sb.append("  <tr>\n");
129          sb.append("    <td valign=\"top\" align=\"right\">Scope</td>\n");
130          sb.append("    <td valign=\"top\" >" + ref.getScope() + "</td>\n");
131          sb.append("  </tr>\n");
132  
133          sb.append("  <tr>\n");
134          sb.append("    <td valign=\"top\" align=\"right\">Implementation Class</td>\n");
135          sb.append("    <td>" + ref.getClassName() + "</td>\n");
136          sb.append("  </tr>\n");
137  
138          sb.append("</table>\n");
139  
140          sb.append("</body>\n");
141          sb.append("</html>\n");
142  
143          htmlString = sb.toString();
144  
145          return htmlString;
146      }
147  
148      public Object getObject()
149      {
150          return htmlString;
151      }
152  
153      protected void appendStyles(StringBuffer sb)
154      {
155          sb.append("<style>\n");
156          sb.append("<!--\n");
157  
158          sb.append("TABLE     { font-family: Helvetica, sans-serif; font-size: 10pt; }\n");
159          sb.append("P.heading { font-family: Helvetica, sans-serif; font-size: 18pt; }\n");
160          sb.append("P         { font-family: Helvetica, sans-serif; font-size: 10pt; }\n");
161          sb.append("-->n");
162          sb.append("</style>\n");
163      }
164  }