Source Code Cross Referenced for ProtocolSwitch.java in  » Net » Terracotta » com » tc » net » protocol » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Terracotta » com.tc.net.protocol 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice.  All rights reserved.
003:         */
004:        package com.tc.net.protocol;
005:
006:        import com.tc.async.api.Sink;
007:        import com.tc.bytes.TCByteBuffer;
008:        import com.tc.bytes.TCByteBufferFactory;
009:        import com.tc.net.core.TCConnection;
010:        import com.tc.util.Assert;
011:
012:        import java.io.IOException;
013:        import java.net.Socket;
014:        import java.util.Arrays;
015:        import java.util.HashSet;
016:        import java.util.Set;
017:
018:        /**
019:         * Inspects the first few bytes from a socket to decide if it should stay in the TC comms stack, or if it should be
020:         * handed off to Jetty. Evolving this switch behaviour to more protocols should probably involve refactoring the generic
021:         * comms code, not just this class
022:         */
023:        public class ProtocolSwitch implements  TCProtocolAdaptor {
024:
025:            private static final String[] HTTP_METHODS = new String[] { "GET",
026:                    "POST", "HEAD", "PUT", "OPTIONS", "DELETE", "TRACE",
027:                    "CONNECT" };
028:
029:            private static final Set METHODS = new HashSet(Arrays
030:                    .asList(HTTP_METHODS));
031:
032:            // The longest HTTP method is 7 chars, +1 for the space
033:            private static final int INSPECT = 8;
034:
035:            private static final int PROTOCOL_UNKNOWN = 0;
036:            private static final int PROTOCOL_NOT_HTTP = 1;
037:            private static final int PROTOCOL_HTTP = 2;
038:
039:            private volatile int protocol = PROTOCOL_UNKNOWN;
040:            private final TCByteBuffer[] buffer = new TCByteBuffer[] { TCByteBufferFactory
041:                    .wrap(new byte[INSPECT]) };
042:            private final TCProtocolAdaptor delegate;
043:            private final Sink httpSink;
044:
045:            public ProtocolSwitch(TCProtocolAdaptor delegate, Sink httpSink) {
046:                this .delegate = delegate;
047:                this .httpSink = httpSink;
048:            }
049:
050:            public void addReadData(TCConnection source, TCByteBuffer[] data,
051:                    int length) throws TCProtocolException {
052:                switch (protocol) {
053:                case PROTOCOL_NOT_HTTP: {
054:                    delegate.addReadData(source, data, length);
055:                    return;
056:                }
057:                case PROTOCOL_UNKNOWN: {
058:                    Assert.assertEquals(1, data.length);
059:                    TCByteBuffer buf = data[0];
060:                    if (buf.hasRemaining()) {
061:                        // didn't get enough bytes yet to make a decision
062:                        return;
063:                    }
064:
065:                    buf.flip();
066:                    boolean isHttp = isHttp(buf);
067:                    buf.rewind();
068:
069:                    if (isHttp) {
070:                        protocol = PROTOCOL_HTTP;
071:                        final Socket socket;
072:                        try {
073:                            socket = source.detach();
074:                        } catch (IOException e) {
075:                            throw new TCProtocolException(e);
076:                        }
077:                        httpSink.add(new HttpConnectionContext(socket, buf));
078:                        return;
079:                    } else {
080:                        protocol = PROTOCOL_NOT_HTTP;
081:                        feedDataToDelegate(source, buf);
082:                        return;
083:                    }
084:                }
085:                default:
086:                    throw new AssertionError("Protocol is " + protocol);
087:                }
088:
089:                // unreachable
090:            }
091:
092:            private void feedDataToDelegate(TCConnection source,
093:                    TCByteBuffer src) throws TCProtocolException {
094:                while (src.hasRemaining()) {
095:                    int count = 0;
096:
097:                    TCByteBuffer[] readBuffers = delegate.getReadBuffers();
098:                    for (int i = 0; i < readBuffers.length; i++) {
099:                        TCByteBuffer dest = readBuffers[i];
100:                        int len = Math.min(src.remaining(), dest.remaining());
101:                        count += len;
102:                        for (int j = 0; j < len; j++) {
103:                            dest.put(src.get());
104:                        }
105:                        if (!src.hasRemaining()) {
106:                            break;
107:                        }
108:                    }
109:
110:                    delegate.addReadData(source, readBuffers, count);
111:                }
112:            }
113:
114:            private static boolean isHttp(TCByteBuffer buf) {
115:                Assert.assertEquals(INSPECT, buf.limit());
116:                byte[] bytes = new byte[buf.limit()];
117:                buf.get(bytes);
118:
119:                final String s;
120:                try {
121:                    s = new String(bytes);
122:                } catch (Exception e) {
123:                    return false;
124:                }
125:
126:                int spaceIndex = s.indexOf(' ');
127:                if (spaceIndex < 0) {
128:                    return false;
129:                }
130:
131:                String token = s.substring(0, spaceIndex);
132:
133:                // best I can tell, HTTP methods are case sensitive, so I'm not uppercase'ing the token here
134:                return METHODS.contains(token);
135:            }
136:
137:            public TCByteBuffer[] getReadBuffers() {
138:                switch (protocol) {
139:                case PROTOCOL_NOT_HTTP: {
140:                    return delegate.getReadBuffers();
141:                }
142:                case PROTOCOL_UNKNOWN: {
143:                    return buffer;
144:                }
145:                default:
146:                    throw new AssertionError("Protocol is " + protocol);
147:                }
148:
149:                // unreachable
150:            }
151:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.