KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > net > HTTPTransportImpl


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Uri Schneider.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.core.net;
48
49 import java.io.IOException JavaDoc;
50 import java.net.InetSocketAddress JavaDoc;
51 import java.net.SocketAddress JavaDoc;
52 import java.nio.ByteBuffer JavaDoc;
53 import java.nio.channels.SocketChannel JavaDoc;
54
55 import org.apache.commons.logging.LogFactory;
56 import org.mr.core.util.SystemTime;
57
58 /**
59  * HTTPTransportImpl.java
60  *
61  *
62  * Created: Mon May 02 13:35:33 2005
63  *
64  * @author Uri Schneider
65  * @version 1.0
66  */

67 public class HTTPTransportImpl extends TCPTransportImpl {
68     private boolean isClient;
69     private boolean readLF;
70     private boolean readHTTPHeader;
71     private static final int HTTP_HEADER_SIZE = 1000;
72
73     public HTTPTransportImpl(SocketChannel JavaDoc channel) {
74         super(channel);
75         this.isClient = false;
76         this.readLF = false;
77         this.readHTTPHeader = false;
78         this.log = LogFactory.getLog("HTTPTransportImpl");
79         writeHTTPHeader();
80     }
81
82     public HTTPTransportImpl(SocketAddress JavaDoc local, SocketAddress JavaDoc remote)
83         throws IOException JavaDoc
84     {
85         super (local, remote);
86         this.isClient = true;
87         this.readLF = false;
88         this.readHTTPHeader = false;
89         this.log = LogFactory.getLog("HTTPTransportImpl");
90     }
91
92     public void onConnect() {
93         writeHTTPHeader();
94     }
95
96     public void read() {
97         if (this.readHTTPHeader) {
98             super.read();
99         } else {
100             ByteBuffer JavaDoc buf = ByteBuffer.allocate(1);
101             int n;
102             try {
103                 while (true) {
104                     n = this.channel.read(buf);
105                     if (n == 0) {
106                         return;
107                     } else if (n == -1) {
108                         if(log.isWarnEnabled()) {
109                             log.warn("Channel " + toString() +
110                                      " EOF. Shutting down.");
111                         }
112                         shutdown();
113                     } else {
114                         buf.flip();
115                         byte b = buf.get();
116                         if (b == '\n') {
117                             if (this.readLF) {
118                                 this.readHTTPHeader = true;
119                                 return;
120                             } else {
121                                 this.readLF = true;
122                             }
123                         } else {
124                             this.readLF = false;
125                         }
126                     }
127                 }
128             } catch (IOException JavaDoc e) {
129                 if(log.isWarnEnabled())
130                     log.warn("Error reading from channel (remote = " +
131                              channel.socket().getRemoteSocketAddress()
132                              .toString() +"):" + e.getMessage());
133                 shutdown();
134             }
135         }
136     }
137
138     private void writeHTTPHeader() {
139         ByteBuffer JavaDoc buf = ByteBuffer.allocate(HTTP_HEADER_SIZE);
140         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
141         if (this.isClient) {
142             // request
143
sbuf.append("POST /index.html?crap=");
144             sbuf.append(SystemTime.currentTimeMillis() / 1000);
145             sbuf.append(" HTTP/1.1\n");
146
147             // host
148
sbuf.append("Host: ");
149             InetSocketAddress JavaDoc remote = (InetSocketAddress JavaDoc)
150                 this.channel.socket().getRemoteSocketAddress();
151             sbuf.append(remote.getHostName()).append(":")
152                 .append(remote.getPort()).append("\n");
153
154             // keep-alive
155
sbuf.append("Connection: Keep-Alive\n");
156             sbuf.append("Content-Type: application/octet-stream\n");
157             sbuf.append("\n");
158         } else {
159             sbuf.append("HTTP/1.1 200 OK\n");
160             sbuf.append("Connection: Keep-Alive\n");
161             sbuf.append("Cache-Control: no-cache, no-store, must-revalidate\n");
162             sbuf.append("Expires: 0\n");
163             sbuf.append("Content-Type: application/octet-stream\n");
164             sbuf.append("\n");
165         }
166         buf.put(sbuf.toString().getBytes());
167         buf.flip();
168         int written = 0;
169         try {
170             while (buf.remaining() > 0) {
171                 this.channel.write(buf);
172             }
173         } catch (IOException JavaDoc e) {
174             if(log.isErrorEnabled())
175                 log.error("Error writing to " + toString() + ": " +
176                           e.toString() + ".");
177             shutdown();
178         }
179     }
180
181     public String JavaDoc toString() {
182         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
183         try {
184             buf.append(this.channel.socket().getLocalSocketAddress().
185                        toString());
186             buf.append(this.channel.socket().getRemoteSocketAddress().
187                        toString());
188         } catch (Throwable JavaDoc t) {
189             buf.append("/unknown/unknown");
190         }
191         buf.append("@HTTP");
192
193         return buf.toString();
194     }
195
196     public TransportType getType() {
197         return TransportType.HTTP;
198     }//TransportType
199
}
Popular Tags