summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/socket/WebSocketChatApplication.java
blob: 7d27b9f3689363ffb794ae361f10fa0a4c89a79e (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.simpleframework.http.socket;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.simpleframework.common.buffer.Allocator;
import org.simpleframework.common.buffer.ArrayAllocator;
import org.simpleframework.http.Cookie;
import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import org.simpleframework.http.Status;
import org.simpleframework.http.core.Container;
import org.simpleframework.http.core.ContainerTransportProcessor;
import org.simpleframework.http.socket.service.Router;
import org.simpleframework.http.socket.service.RouterContainer;
import org.simpleframework.http.socket.service.DirectRouter;
import org.simpleframework.transport.TransportProcessor;
import org.simpleframework.transport.TransportSocketProcessor;
import org.simpleframework.transport.SocketProcessor;
import org.simpleframework.transport.Transport;
import org.simpleframework.transport.connect.Connection;
import org.simpleframework.transport.connect.SocketConnection;
import org.simpleframework.transport.trace.TraceAnalyzer;

public class WebSocketChatApplication implements Container, TransportProcessor {   

   private final WebSocketCertificate certificate;
   private final Router negotiator;
   private final RouterContainer container;
   private final SocketAddress address;
   private final Connection connection;
   private final TransportProcessor processor;
   private final Allocator allocator;
   private final SocketProcessor server;
   
   public WebSocketChatApplication(WebSocketChatRoom service, WebSocketCertificate certificate, TraceAnalyzer agent, int port) throws Exception {
      this.negotiator = new DirectRouter(service);
      this.container = new RouterContainer(this, negotiator, 10);
      this.allocator = new ArrayAllocator();
      this.processor = new ContainerTransportProcessor(container, allocator, 1);
      this.server = new TransportSocketProcessor(this);
      this.connection = new SocketConnection(server, agent);
      this.address = new InetSocketAddress(port);
      this.certificate = certificate;
   }
   
   public void connect() throws Exception {
     // if(certificate != null) {
     //    SSLContext context = certificate.getContext();
     //    
     //    connection.connect(address, context);
     //    container.start();
     // } else {
         connection.connect(address);
     // }
   }

   public void handle(Request req, Response resp) {
      System.err.println(req);

      if(req.getTarget().equals("/")) {
         long time = System.currentTimeMillis();
         
         try {
            resp.setDate("Date", time);
            resp.setValue("Server", "WebSocketChatApplication/1.0");
            resp.setContentType("text/html");
            String page = loadPage("WebSocketChatLogin.html");
            
            resp.setDate("Date", time);
            resp.setValue("Server", "WebSocketChatApplication/1.0");
            resp.setContentType("text/html");
            
            PrintStream out = resp.getPrintStream();
            out.println(page);
            out.close();
         }catch(Exception e) {
            e.printStackTrace();
         }
      } else if(req.getTarget().equals("/login")) {
         String user = req.getParameter("user");
         long time = System.currentTimeMillis();
         
         try {
            resp.setStatus(Status.FOUND);
            resp.setValue("Location", "/chat");
            resp.setCookie("user", user);
            resp.setDate("Date", time);
            resp.setValue("Server", "WebSocketChatApplication/1.0");
            resp.setContentType("text/html");
            resp.close();
         }catch(Exception e) {
            e.printStackTrace();
         }      
      } else if(req.getTarget().equals("/chat")) {
         long time = System.currentTimeMillis();
         
         try {
            Cookie user = req.getCookie("user");
            String name = user.getValue();
            String page = loadPage("WebSocketChatRoom.html");
       
            resp.setDate("Date", time);
            resp.setValue("Server", "WebSocketChatApplication/1.0");
            resp.setContentType("text/html");
            
            PrintStream out = resp.getPrintStream();
            page = page.replaceAll("%1", name);
            out.println(page);
            out.close();
         } catch(Exception e) {
            e.printStackTrace();
         }
      } else if(req.getTarget().equals("/talk")){         
         long time = System.currentTimeMillis();
         try {
            container.handle(req, resp);
         } catch(Exception e) {
            e.printStackTrace();
         }
      } else {
         long time = System.currentTimeMillis();
         
         try {               
            resp.setCode(404);
            resp.setDescription("Not Found");
            resp.setDate("Date", time);
            resp.setValue("Server", "WebSocketChatApplication/1.0");
            resp.setContentType("text/plain");
            
            PrintStream out = resp.getPrintStream();
            
            out.println("Not Found");
            out.close();
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }
   
   public String loadPage(String name) throws IOException {
      InputStream loginPage = WebSocketChatApplication.class.getResourceAsStream(name);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      byte[] chunk = new byte[1024];
      int count = 0;
      
      while((count = loginPage.read(chunk)) != -1) {
         out.write(chunk, 0, count);
      }            
      out.close();
      return out.toString();
   }

   public void process(Transport transport) throws IOException {
      Map map = transport.getAttributes();
      map.put(Transport.class, transport);
      processor.process(transport);
   }

   public void stop() throws IOException {
      processor.stop();
   }      
}