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: HtmlProtocol.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   
30   import java.lang.reflect.Method;
31   
32   public class HtmlProtocol implements RmiProtocol
33   {
34       private static final String INDEX_NAME = "index";
35   
36       public HtmlProtocol()
37       {
38       }
39   
40       public String getName()
41       {
42           return RmiProtocol.HTML;
43       }
44   
45       public String getMimeType()
46       {
47           return RmiProtocol.HTML_CONTENT_TYPE;
48       }
49   
50       public RmiRequest createRequest(Method method, Object[] args, 
51                                       RmiContext context)
52       {
53           return new HtmlRequest();
54       }
55   
56       public RmiResponse createResponse(RmiRequest request, Object result, 
57                                         RmiContext context)
58       {
59           return (context.getObjectName().equals(INDEX_NAME))
60                  ? new HtmlIndexResponse() : new HtmlResponse();
61       }
62   
63       public void writeRequest(RmiRequest request, RmiContext context)
64                         throws Exception
65       {
66       }
67   
68       public RmiResponse readResponse(RmiContext context)
69                                throws Exception
70       {
71           return new HtmlResponse(context.getTransport().getInputStream(context));
72       }
73   
74       public RmiRequest readRequest(RmiContext context)
75                              throws Exception
76       {
77           return new HtmlRequest();
78       }
79   
80       public void writeResponse(RmiResponse response, RmiContext context)
81                          throws Exception
82       {
83           HtmlResponse r = (HtmlResponse) response;
84   
85           r.setContext(context);
86   
87           String htmlString = r.toHtmlString();
88   
89           context.getTransport().getOutputStream(context)
90                  .write(htmlString.getBytes());
91       }
92   }