View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.server.mux;
20  
21  import java.io.IOException;
22  
23  import org.eclipse.jetty.http.HttpField;
24  import org.eclipse.jetty.http.HttpHeader;
25  import org.eclipse.jetty.http.HttpMethod;
26  import org.eclipse.jetty.http.HttpVersion;
27  import org.eclipse.jetty.io.EndPoint;
28  import org.eclipse.jetty.server.Connector;
29  import org.eclipse.jetty.server.HttpConfiguration;
30  import org.eclipse.jetty.websocket.api.UpgradeRequest;
31  import org.eclipse.jetty.websocket.api.UpgradeResponse;
32  import org.eclipse.jetty.websocket.common.extensions.mux.MuxChannel;
33  import org.eclipse.jetty.websocket.common.extensions.mux.MuxException;
34  import org.eclipse.jetty.websocket.common.extensions.mux.Muxer;
35  import org.eclipse.jetty.websocket.common.extensions.mux.add.MuxAddServer;
36  
37  /**
38   * Handler for incoming MuxAddChannel requests.
39   */
40  public class MuxAddHandler implements MuxAddServer
41  {
42      /** Represents physical connector */
43      private Connector connector;
44  
45      /** Used for local address */
46      private EndPoint endPoint;
47  
48      /** The original request handshake */
49      private UpgradeRequest baseHandshakeRequest;
50  
51      /** The original request handshake */
52      private UpgradeResponse baseHandshakeResponse;
53  
54      private int maximumHeaderSize = 32 * 1024;
55  
56      @Override
57      public UpgradeRequest getPhysicalHandshakeRequest()
58      {
59          // TODO Auto-generated method stub
60          return null;
61      }
62  
63      @Override
64      public UpgradeResponse getPhysicalHandshakeResponse()
65      {
66          // TODO Auto-generated method stub
67          return null;
68      }
69  
70      /**
71       * An incoming MuxAddChannel request.
72       * 
73       * @param the
74       *            channel this request should be bound to
75       * @param request
76       *            the incoming request headers (complete and merged if delta encoded)
77       * @return the outgoing response headers
78       */
79      @Override
80      public void handshake(Muxer muxer, MuxChannel channel, UpgradeRequest request) throws MuxException, IOException
81      {
82          // Need to call into HttpChannel to get the websocket properly setup.
83          HttpTransportOverMux transport = new HttpTransportOverMux(muxer,channel);
84          EmptyHttpInput input = new EmptyHttpInput();
85          HttpConfiguration configuration = new HttpConfiguration();
86  
87          HttpChannelOverMux httpChannel = new HttpChannelOverMux(//
88                  connector,configuration,endPoint,transport,input);
89  
90          HttpMethod method = HttpMethod.fromString(request.getMethod());
91          HttpVersion version = HttpVersion.fromString(request.getHttpVersion());
92          httpChannel.startRequest(method,request.getMethod(),request.getRequestURI().toASCIIString(),version);
93  
94          for (String headerName : request.getHeaders().keySet())
95          {
96              HttpHeader header = HttpHeader.CACHE.getBest(headerName.getBytes(),0,headerName.length());
97              for (String value : request.getHeaders().get(headerName))
98              {
99                  httpChannel.parsedHeader(new HttpField(header,value));
100             }
101         }
102 
103         httpChannel.headerComplete();
104         httpChannel.messageComplete();
105         httpChannel.run(); // calls into server for appropriate resource
106 
107         // TODO: what's in request handshake is not enough to process the request.
108         // like a partial http request. (consider this a AddChannelRequest failure)
109         throw new MuxException("Not a valid request");
110     }
111 }