1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 }