001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java $
003: * $Revision: 613298 $
004: * $Date: 2008-01-18 23:09:22 +0100 (Fri, 18 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.nio;
033:
034: import org.apache.http.HttpResponseFactory;
035: import org.apache.http.impl.DefaultHttpResponseFactory;
036: import org.apache.http.nio.NHttpClientIOTarget;
037: import org.apache.http.nio.NHttpClientHandler;
038: import org.apache.http.nio.reactor.IOEventDispatch;
039: import org.apache.http.nio.reactor.IOSession;
040: import org.apache.http.nio.util.ByteBufferAllocator;
041: import org.apache.http.nio.util.HeapByteBufferAllocator;
042: import org.apache.http.params.HttpParams;
043:
044: public class DefaultClientIOEventDispatch implements IOEventDispatch {
045:
046: private static final String NHTTP_CONN = "NHTTP_CONN";
047:
048: protected final ByteBufferAllocator allocator;
049: protected final NHttpClientHandler handler;
050: protected final HttpParams params;
051:
052: public DefaultClientIOEventDispatch(
053: final NHttpClientHandler handler, final HttpParams params) {
054: super ();
055: if (handler == null) {
056: throw new IllegalArgumentException(
057: "HTTP client handler may not be null");
058: }
059: if (params == null) {
060: throw new IllegalArgumentException(
061: "HTTP parameters may not be null");
062: }
063: this .allocator = createByteBufferAllocator();
064: this .handler = handler;
065: this .params = params;
066: }
067:
068: protected ByteBufferAllocator createByteBufferAllocator() {
069: return new HeapByteBufferAllocator();
070: }
071:
072: protected HttpResponseFactory createHttpResponseFactory() {
073: return new DefaultHttpResponseFactory();
074: }
075:
076: protected NHttpClientIOTarget createConnection(
077: final IOSession session) {
078: return new DefaultNHttpClientConnection(session,
079: createHttpResponseFactory(), this .allocator,
080: this .params);
081: }
082:
083: public void connected(final IOSession session) {
084: NHttpClientIOTarget conn = createConnection(session);
085: Object attachment = session
086: .getAttribute(IOSession.ATTACHMENT_KEY);
087: session.setAttribute(NHTTP_CONN, conn);
088: this .handler.connected(conn, attachment);
089: }
090:
091: public void disconnected(final IOSession session) {
092: NHttpClientIOTarget conn = (NHttpClientIOTarget) session
093: .getAttribute(NHTTP_CONN);
094: this .handler.closed(conn);
095: }
096:
097: public void inputReady(final IOSession session) {
098: NHttpClientIOTarget conn = (NHttpClientIOTarget) session
099: .getAttribute(NHTTP_CONN);
100: conn.consumeInput(this .handler);
101: }
102:
103: public void outputReady(final IOSession session) {
104: NHttpClientIOTarget conn = (NHttpClientIOTarget) session
105: .getAttribute(NHTTP_CONN);
106: conn.produceOutput(this .handler);
107: }
108:
109: public void timeout(final IOSession session) {
110: NHttpClientIOTarget conn = (NHttpClientIOTarget) session
111: .getAttribute(NHTTP_CONN);
112: this.handler.timeout(conn);
113: }
114:
115: }
|