Author: mwebb
Date: Mon Aug 27 17:02:26 2007
New Revision: 570274
URL: http://svn.apache.org/viewvc?rev=570274&view=rev
Log:
initial check-in. much is left to do in the commenting section. This will at
least get the ball rolling and allow others to make updates.
Added:
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClient.java
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClientCallback.java
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/HttpIoHandler.java
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/ssl/
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/ssl/TrustManagerFactoryImpl.java
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AbstractTest.java
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AsyncHttpClientTest.java
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/ChunkedTest.java
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeIoSession.java
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeProtocolDecoderOutput.java
Added:
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClient.java
URL:
http://svn.apache.org/viewvc/mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClient.java?rev=570274&view=auto
==============================================================================
---
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClient.java
(added)
+++
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClient.java
Mon Aug 27 17:02:26 2007
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.http.client;
+
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.URL;
+import java.security.GeneralSecurityException;
+
+import javax.net.ssl.SSLContext;
+
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.filter.ssl.SSLFilter;
+import org.apache.mina.http.client.ssl.TrustManagerFactoryImpl;
+import org.apache.mina.http.codec.HttpProtocolCodecFactory;
+import org.apache.mina.http.codec.HttpRequestMessage;
+import org.apache.mina.transport.socket.nio.SocketConnector;
+
+
+public class AsyncHttpClient
+{
+
+ public static int DEFAULT_CONNECTION_TIMEOUT = 30;
+ public static String DEFAULT_SSL_PROTOCOL = "TLS";
+
+ private URL url;
+ private boolean followRedirects = true;
+ private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
+ private String sslProtocol = DEFAULT_SSL_PROTOCOL;
+ private IoSession session;
+ private AsyncHttpClientCallback callback;
+
+
+ public AsyncHttpClient( URL url, AsyncHttpClientCallback callback )
+ {
+ this.url = url;
+ this.callback = callback;
+ }
+
+
+ public void connect() throws Exception
+ {
+ SocketConnector connector = new SocketConnector();
+
+ connector.setConnectTimeout( connectionTimeout );
+
+ String scheme = url.getProtocol();
+ int port = url.getPort();
+ if ( scheme.toLowerCase().equals( "https" ) )
+ {
+ SSLFilter filter = new SSLFilter( createClientSSLContext() );
+ filter.setUseClientMode( true );
+ connector.getFilterChain().addLast( "SSL", filter );
+ if ( port == -1 )
+ {
+ port = 443;
+ }
+ }
+ if ( scheme.toLowerCase().equals( "http" ) && ( port == -1 ) )
+ {
+ port = 80;
+ }
+
+ connector.getFilterChain().addLast( "protocolFilter", new
ProtocolCodecFilter( new HttpProtocolCodecFactory( url ) ) );
+ connector.setHandler( new HttpIoHandler(callback) );
+ ConnectFuture future = connector.connect( new InetSocketAddress(
url.getHost(), port ) );
+ future.awaitUninterruptibly();
+ if ( !future.isConnected() )
+ {
+ throw new IOException( "Cannot connect to " + url.toString() );
+ }
+ session = future.getSession();
+ }
+
+
+ public void disconnect()
+ {
+ if ( session != null && session.isConnected() )
+ {
+ session.close();
+ }
+ session = null;
+ }
+
+
+ public void sendRequest( HttpRequestMessage message )
+ {
+ session.write( message );
+ }
+
+
+ public int getConnectionTimeout()
+ {
+ return connectionTimeout;
+ }
+
+
+ public void setConnectionTimeout( int connectionTimeout )
+ {
+ this.connectionTimeout = connectionTimeout;
+ }
+
+
+ public boolean isFollowRedirects()
+ {
+ return followRedirects;
+ }
+
+
+ public void setFollowRedirects( boolean followRedirects )
+ {
+ this.followRedirects = followRedirects;
+ }
+
+
+ public String getSslProtocol()
+ {
+ return sslProtocol;
+ }
+
+
+ public void setSslProtocol( String sslProtocol )
+ {
+ this.sslProtocol = sslProtocol;
+ }
+
+
+ private SSLContext createClientSSLContext() throws GeneralSecurityException
+ {
+ SSLContext context = SSLContext.getInstance( sslProtocol );
+ context.init( null, TrustManagerFactoryImpl.X509_MANAGERS, null );
+ return context;
+ }
+
+}
Added:
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClientCallback.java
URL:
http://svn.apache.org/viewvc/mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClientCallback.java?rev=570274&view=auto
==============================================================================
---
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClientCallback.java
(added)
+++
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/AsyncHttpClientCallback.java
Mon Aug 27 17:02:26 2007
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.http.client;
+
+
+import org.apache.mina.http.codec.HttpResponseMessage;
+
+
+public interface AsyncHttpClientCallback
+{
+ public void onResponse( HttpResponseMessage message );
+
+
+ public void onException( Throwable cause );
+
+
+ public void onClosed();
+}
Added:
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/HttpIoHandler.java
URL:
http://svn.apache.org/viewvc/mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/HttpIoHandler.java?rev=570274&view=auto
==============================================================================
---
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/HttpIoHandler.java
(added)
+++
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/HttpIoHandler.java
Mon Aug 27 17:02:26 2007
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.http.client;
+
+
+import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.http.codec.HttpResponseDecoder;
+import org.apache.mina.http.codec.HttpResponseMessage;
+
+
+public class HttpIoHandler extends IoHandlerAdapter
+{
+ private AsyncHttpClientCallback callback;
+
+
+ public HttpIoHandler( AsyncHttpClientCallback callback )
+ {
+ this.callback = callback;
+ }
+
+
+ @Override
+ public void sessionOpened( IoSession ioSession ) throws Exception
+ {
+ super.sessionOpened( ioSession );
+ }
+
+
+ @Override
+ public void messageReceived( IoSession ioSession, Object object ) throws
Exception
+ {
+ HttpResponseMessage message = ( HttpResponseMessage ) object;
+ callback.onResponse( message );
+ }
+
+
+ @Override
+ public void exceptionCaught( IoSession ioSession, Throwable throwable )
throws Exception
+ {
+ //Clean up if any in-proccess decoding was occurring
+ ioSession.removeAttribute( HttpResponseDecoder.CURRENT_RESPONSE );
+ callback.onException( throwable );
+ }
+
+
+ @Override
+ public void sessionClosed( IoSession ioSession ) throws Exception
+ {
+ //Clean up if any in-proccess decoding was occurring
+ ioSession.removeAttribute( HttpResponseDecoder.CURRENT_RESPONSE );
+ callback.onClosed();
+ }
+}
Added:
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/ssl/TrustManagerFactoryImpl.java
URL:
http://svn.apache.org/viewvc/mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/ssl/TrustManagerFactoryImpl.java?rev=570274&view=auto
==============================================================================
---
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/ssl/TrustManagerFactoryImpl.java
(added)
+++
mina/trunk/mina-protocol-client-http/src/main/java/org/apache/mina/http/client/ssl/TrustManagerFactoryImpl.java
Mon Aug 27 17:02:26 2007
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.http.client.ssl;
+
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactorySpi;
+import javax.net.ssl.X509TrustManager;
+import javax.net.ssl.ManagerFactoryParameters;
+import java.security.cert.X509Certificate;
+import java.security.cert.CertificateException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.InvalidAlgorithmParameterException;
+
+public class TrustManagerFactoryImpl extends TrustManagerFactorySpi {
+ static final X509TrustManager X509 = new X509TrustManager() {
+
+
+ public void checkClientTrusted(X509Certificate[] x509Certificates,
String s) throws CertificateException {
+
+ }
+
+
+ public void checkServerTrusted(X509Certificate[] x509Certificates,
String s) throws CertificateException {
+
+ }
+
+
+ public X509Certificate[] getAcceptedIssuers() {
+
+ return new X509Certificate[0];
+
+ }
+
+ };
+
+ public static final TrustManager[] X509_MANAGERS = new
TrustManager[]{X509};
+
+
+ protected void engineInit(KeyStore keyStore) throws KeyStoreException {
+ }
+
+ protected void engineInit(ManagerFactoryParameters
managerFactoryParameters) throws InvalidAlgorithmParameterException {
+ }
+
+ protected TrustManager[] engineGetTrustManagers() {
+ return X509_MANAGERS;
+ }
+}
Added:
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AbstractTest.java
URL:
http://svn.apache.org/viewvc/mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AbstractTest.java?rev=570274&view=auto
==============================================================================
---
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AbstractTest.java
(added)
+++
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AbstractTest.java
Mon Aug 27 17:02:26 2007
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.http.client;
+
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Host;
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.core.StandardHost;
+import org.apache.catalina.startup.Embedded;
+
+
+public class AbstractTest extends TestCase
+{
+
+ protected final File BASEDIR = getBaseDir();
+ protected final File CATALINAHOME = new File( BASEDIR, "src/test/catalina"
);
+ protected final File WORK = new File( BASEDIR, "target/work" );
+ protected final File KEYSTORE = new File( CATALINAHOME, "conf/keystore" );
+ protected final File WEBAPPS = new File( CATALINAHOME, "webapps" );
+ protected final File ROOT = new File( WEBAPPS, "ROOT" );
+ protected Embedded server;
+
+
+ protected void setUp() throws Exception
+ {
+ System.out.println( "BASEDIR = " + BASEDIR.getAbsolutePath() );
+ server = new Embedded();
+ server.setCatalinaHome( CATALINAHOME.getAbsolutePath() );
+
+ Engine engine = server.createEngine();
+ engine.setDefaultHost( "localhost" );
+
+ Host host = server.createHost( "localhost", WEBAPPS.getAbsolutePath()
);
+ ( ( StandardHost ) host ).setWorkDir( WORK.getAbsolutePath() );
+ engine.addChild( host );
+
+ Context context = server.createContext( "", ROOT.getAbsolutePath() );
+ context.setParentClassLoader(
Thread.currentThread().getContextClassLoader() );
+ host.addChild( context );
+
+ server.addEngine( engine );
+
+ //Http
+ Connector http = server.createConnector( "localhost", 8282, false );
+ server.addConnector( http );
+
+ //Https
+ Connector https = server.createConnector( "localhost", 8383, true );
+ https.setAttribute( "keystoreFile", KEYSTORE.getAbsolutePath() );
+ server.addConnector( https );
+ server.start();
+ }
+
+
+ protected void tearDown() throws Exception
+ {
+ if ( server != null )
+ server.stop();
+ }
+
+
+ protected final File getBaseDir()
+ {
+ File dir;
+
+ // If ${basedir} is set, then honor it
+ String tmp = System.getProperty( "basedir" );
+ if ( tmp != null )
+ {
+ dir = new File( tmp );
+ }
+ else
+ {
+ // Find the directory which this class (or really the sub-class of
TestSupport) is defined in.
+ String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
+
+ // We expect the file to be in target/test-classes, so go up 2 dirs
+ dir = new File( path ).getParentFile().getParentFile();
+
+ // Set ${basedir} which is needed by logging to initialize
+ System.setProperty( "basedir", dir.getPath() );
+ }
+
+ return dir;
+ }
+}
Added:
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AsyncHttpClientTest.java
URL:
http://svn.apache.org/viewvc/mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AsyncHttpClientTest.java?rev=570274&view=auto
==============================================================================
---
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AsyncHttpClientTest.java
(added)
+++
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/AsyncHttpClientTest.java
Mon Aug 27 17:02:26 2007
@@ -0,0 +1,256 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.http.client;
+
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.net.URL;
+import java.util.Arrays;
+
+import org.apache.mina.http.codec.HttpRequestMessage;
+import org.apache.mina.http.codec.HttpResponseMessage;
+
+
+public class AsyncHttpClientTest extends AbstractTest
+{
+
+ protected static final Object semaphore = new Object();
+
+
+ public void testHtmlConnection() throws Exception
+ {
+ TestCallback callback = new TestCallback();
+ doGetConnection( callback, "http://localhost:8282", "/", false );
+
+ HttpResponseMessage msg = callback.getMessage();
+ assertEquals( "\nHello World!", msg.getStringContent() );
+ }
+
+
+ public void testSSLHtmlConnection() throws Exception
+ {
+ TestCallback callback = new TestCallback();
+ doGetConnection( callback, "https://localhost:8383", "/", false );
+
+ HttpResponseMessage msg = callback.getMessage();
+ assertEquals( "\nHello World!", msg.getStringContent() );
+ }
+
+
+ public void testBinaryRequest() throws Exception
+ {
+
+ //Get the real file
+ File file = new File( ROOT, "pwrd_apache.gif" );
+ FileInputStream fis = new FileInputStream( file );
+ byte realFile[] = new byte[( int ) file.length()];
+ fis.read( realFile );
+
+ TestCallback callback = new TestCallback();
+ doGetConnection( callback, "http://localhost:8282",
"/pwrd_apache.gif", false );
+
+ HttpResponseMessage msg = callback.getMessage();
+
+ assertTrue( Arrays.equals( realFile, msg.getContent() ) );
+ }
+
+
+ public void testSSLBinaryRequest() throws Exception
+ {
+
+ //Get the real file
+ File file = new File( ROOT, "pwrd_apache.gif" );
+ FileInputStream fis = new FileInputStream( file );
+ byte realFile[] = new byte[( int ) file.length()];
+ fis.read( realFile );
+
+ TestCallback callback = new TestCallback();
+ doGetConnection( callback, "https://localhost:8383",
"/pwrd_apache.gif", false );
+
+ HttpResponseMessage msg = callback.getMessage();
+
+ assertTrue( Arrays.equals( realFile, msg.getContent() ) );
+ }
+
+
+ public void testGetParameters() throws Exception
+ {
+ TestCallback callback = new TestCallback();
+ doGetConnection( callback, "http://localhost:8282", "/params.jsp",
false );
+
+ HttpResponseMessage msg = callback.getMessage();
+ assertEquals( "Test One Test Two", msg.getStringContent() );
+ }
+
+
+ public void testPostParameters() throws Exception
+ {
+ TestCallback callback = new TestCallback();
+ doPostConnection( callback, "http://localhost:8282", "/params.jsp",
false );
+
+ HttpResponseMessage msg = callback.getMessage();
+ assertEquals( "Test One Test Two", msg.getStringContent() );
+ }
+
+
+ private void doGetConnection( TestCallback callback, String url, String
uri, boolean testForException )
+ throws Exception
+ {
+ HttpRequestMessage request = new HttpRequestMessage( uri );
+ request.setParameter( "TEST1", "Test One" );
+ request.setParameter( "TEST2", "Test Two" );
+ doConnection( callback, url, request, false );
+ }
+
+
+ private void doPostConnection( TestCallback callback, String url, String
uri, boolean testForException )
+ throws Exception
+ {
+ HttpRequestMessage request = new HttpRequestMessage( uri );
+ request.setParameter( "TEST1", "Test One" );
+ request.setParameter( "TEST2", "Test Two" );
+ request.setRequestMethod( HttpRequestMessage.REQUEST_POST );
+ doConnection( callback, url, request, false );
+ }
+
+
+ private void doConnection( TestCallback callback, String url,
HttpRequestMessage request, boolean testForException )
+ throws Exception
+ {
+ URL url_connect = new URL( url );
+
+ AsyncHttpClient ahc = new AsyncHttpClient( url_connect, callback );
+ ahc.connect();
+
+ ahc.sendRequest( request );
+
+ //We are done...Thread would normally end...
+ //So this little wait simulates the thread going back in the pool
+ synchronized ( semaphore )
+ {
+ //5 second timeout due to no response
+ semaphore.wait( 5000 );
+ }
+
+ if ( !testForException )
+ {
+ if ( callback.isException() )
+ throw new Exception( callback.getThrowable() );
+ }
+
+ }
+
+ class TestCallback implements AsyncHttpClientCallback
+ {
+
+ private boolean closed = false;
+ private boolean exception = false;
+ private Throwable throwable = null;
+ private HttpResponseMessage message = null;
+
+
+ public TestCallback()
+ {
+ clear();
+ }
+
+
+ public void onResponse( HttpResponseMessage message )
+ {
+ this.message = message;
+ synchronized ( semaphore )
+ {
+ semaphore.notify();
+ }
+ }
+
+
+ public void onException( Throwable cause )
+ {
+ throwable = cause;
+ exception = true;
+ synchronized ( semaphore )
+ {
+ semaphore.notify();
+ }
+ }
+
+
+ public void onClosed()
+ {
+ closed = true;
+ synchronized ( semaphore )
+ {
+ semaphore.notify();
+ }
+ }
+
+
+ public Throwable getThrowable()
+ {
+ return throwable;
+ }
+
+
+ public void clear()
+ {
+ closed = false;
+ exception = false;
+ message = null;
+ }
+
+
+ public boolean isClosed()
+ {
+ return closed;
+ }
+
+
+ public void setClosed( boolean closed )
+ {
+ this.closed = closed;
+ }
+
+
+ public boolean isException()
+ {
+ return exception;
+ }
+
+
+ public void setException( boolean exception )
+ {
+ this.exception = exception;
+ }
+
+
+ public HttpResponseMessage getMessage()
+ {
+ return message;
+ }
+
+
+ public void setMessage( HttpResponseMessage message )
+ {
+ this.message = message;
+ }
+ }
+}
Added:
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/ChunkedTest.java
URL:
http://svn.apache.org/viewvc/mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/ChunkedTest.java?rev=570274&view=auto
==============================================================================
---
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/ChunkedTest.java
(added)
+++
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/ChunkedTest.java
Mon Aug 27 17:02:26 2007
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.http.client;
+
+
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.http.codec.HttpResponseDecoder;
+import org.apache.mina.http.codec.HttpResponseMessage;
+
+
+public class ChunkedTest extends TestCase
+{
+
+ private final static String fakeHttp = "HTTP/1.1 200 OK\r\n" + "Date: Fri,
31 Dec 1999 23:59:59 GMT\r\n"
+ + "Content-Type: text/plain\r\n" + "Transfer-Encoding: chunked\r\n" +
"\r\n" + "1a; ignore-stuff-here\r\n"
+ + "abcdefghijklmnopqrstuvwxyz\r\n" + "10\r\n" + "1234567890abcdef\r\n"
+ "0\r\n"
+ + "some-footer: some-value\r\n" + "another-footer:
another-value\r\n\r\n";
+
+ private final static String fakeHttpContinue = "HTTP/1.1 100 Continue\r\n"
+ + "Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n" + "Content-Type:
text/plain\r\n" + "\r\n" + fakeHttp;
+
+
+ public void testChunking() throws Exception
+ {
+ ByteBuffer buffer = ByteBuffer.allocate( fakeHttp.length() );
+ buffer.put( fakeHttp.getBytes() );
+ buffer.flip();
+
+ IoSession session = new FakeIoSession();
+ HttpResponseDecoder decoder = new HttpResponseDecoder();
+ FakeProtocolDecoderOutput out = new FakeProtocolDecoderOutput();
+ decoder.decode( session, buffer, out );
+
+ HttpResponseMessage response = ( HttpResponseMessage ) out.getObject();
+ assertTrue( Arrays.equals( response.getContent(),
"abcdefghijklmnopqrstuvwxyz1234567890abcdef".getBytes() ) );
+ }
+
+
+ public void testChunkingContinue() throws Exception
+ {
+ ByteBuffer buffer = ByteBuffer.allocate( fakeHttpContinue.length() );
+ buffer.put( fakeHttp.getBytes() );
+ buffer.flip();
+
+ IoSession session = new FakeIoSession();
+ HttpResponseDecoder decoder = new HttpResponseDecoder();
+ FakeProtocolDecoderOutput out = new FakeProtocolDecoderOutput();
+ decoder.decode( session, buffer, out );
+
+ HttpResponseMessage response = ( HttpResponseMessage ) out.getObject();
+ assertTrue( Arrays.equals( response.getContent(),
"abcdefghijklmnopqrstuvwxyz1234567890abcdef".getBytes() ) );
+ }
+
+}
Added:
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeIoSession.java
URL:
http://svn.apache.org/viewvc/mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeIoSession.java?rev=570274&view=auto
==============================================================================
---
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeIoSession.java
(added)
+++
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeIoSession.java
Mon Aug 27 17:02:26 2007
@@ -0,0 +1,355 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.mina.http.client;
+
+
+import java.net.SocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.mina.common.CloseFuture;
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoFilterChain;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoService;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.IoSessionConfig;
+import org.apache.mina.common.TrafficMask;
+import org.apache.mina.common.TransportMetadata;
+import org.apache.mina.common.WriteFuture;
+
+
+public class FakeIoSession implements IoSession
+{
+
+ private Map<String, Object> attributes = new HashMap<String, Object>();
+
+
+ public IoService getService()
+ {
+ return null;
+ }
+
+ public IoHandler getHandler()
+ {
+ return null;
+ }
+
+
+ public IoSessionConfig getConfig()
+ {
+ return null;
+ }
+
+
+ public IoFilterChain getFilterChain()
+ {
+ return null;
+ }
+
+
+ public WriteFuture write( Object object )
+ {
+ return null;
+ }
+
+
+ public CloseFuture close()
+ {
+ return null;
+ }
+
+
+ public Object getAttachment()
+ {
+ return null;
+ }
+
+
+ public Object setAttachment( Object object )
+ {
+ return null;
+ }
+
+
+ public Object getAttribute( String string )
+ {
+ return attributes.get( string );
+ }
+
+
+ public Object setAttribute( String string, Object object )
+ {
+ return attributes.put( string, object );
+ }
+
+
+ public Object setAttribute( String string )
+ {
+ return attributes.put( string, null );
+ }
+
+
+ public Object removeAttribute( String string )
+ {
+ return attributes.remove( string );
+ }
+
+
+ public boolean containsAttribute( String string )
+ {
+ return attributes.containsKey( string );
+ }
+
+
+ public Set<String> getAttributeKeys()
+ {
+ return attributes.keySet();
+ }
+
+
+ public boolean isConnected()
+ {
+ return false;
+ }
+
+
+ public boolean isClosing()
+ {
+ return false;
+ }
+
+
+ public CloseFuture getCloseFuture()
+ {
+ return null;
+ }
+
+
+ public SocketAddress getRemoteAddress()
+ {
+ return null;
+ }
+
+
+ public SocketAddress getLocalAddress()
+ {
+ return null;
+ }
+
+
+ public SocketAddress getServiceAddress()
+ {
+ return null;
+ }
+
+
+ public int getIdleTime( IdleStatus idleStatus )
+ {
+ return 0;
+ }
+
+
+ public long getIdleTimeInMillis( IdleStatus idleStatus )
+ {
+ return 0;
+ }
+
+
+ public void setIdleTime( IdleStatus idleStatus, int i )
+ {
+ }
+
+
+ public int getWriteTimeout()
+ {
+ return 0;
+ }
+
+
+ public long getWriteTimeoutInMillis()
+ {
+ return 0;
+ }
+
+
+ public void setWriteTimeout( int i )
+ {
+ }
+
+
+ public TrafficMask getTrafficMask()
+ {
+ return null;
+ }
+
+
+ public void setTrafficMask( TrafficMask trafficMask )
+ {
+ }
+
+
+ public void suspendRead()
+ {
+ }
+
+
+ public void suspendWrite()
+ {
+ }
+
+
+ public void resumeRead()
+ {
+ }
+
+
+ public void resumeWrite()
+ {
+ }
+
+
+ public long getReadBytes()
+ {
+ return 0;
+ }
+
+
+ public long getWrittenBytes()
+ {
+ return 0;
+ }
+
+
+ public long getReadMessages()
+ {
+ return 0;
+ }
+
+
+ public long getWrittenMessages()
+ {
+ return 0;
+ }
+
+
+ public long getWrittenWriteRequests()
+ {
+ return 0;
+ }
+
+
+ public int getScheduledWriteRequests()
+ {
+ return 0;
+ }
+
+
+ public int getScheduledWriteBytes()
+ {
+ return 0;
+ }
+
+
+ public long getCreationTime()
+ {
+ return 0;
+ }
+
+
+ public long getLastIoTime()
+ {
+ return 0;
+ }
+
+
+ public long getLastReadTime()
+ {
+ return 0;
+ }
+
+
+ public long getLastWriteTime()
+ {
+ return 0;
+ }
+
+
+ public boolean isIdle( IdleStatus idleStatus )
+ {
+ return false;
+ }
+
+
+ public int getIdleCount( IdleStatus idleStatus )
+ {
+ return 0;
+ }
+
+
+ public long getLastIdleTime( IdleStatus idleStatus )
+ {
+ return 0;
+ }
+
+
+ public Object getAttribute( String key, Object defaultValue )
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+
+ public int getScheduledWriteMessages()
+ {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+
+ public TransportMetadata getTransportMetadata()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+
+ public boolean removeAttribute( String key, Object value )
+ {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+
+ public boolean replaceAttribute( String key, Object oldValue, Object
newValue )
+ {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+
+ public Object setAttributeIfAbsent( String key, Object value )
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
Added:
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeProtocolDecoderOutput.java
URL:
http://svn.apache.org/viewvc/mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeProtocolDecoderOutput.java?rev=570274&view=auto
==============================================================================
---
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeProtocolDecoderOutput.java
(added)
+++
mina/trunk/mina-protocol-client-http/src/test/java/org/apache/mina/http/client/FakeProtocolDecoderOutput.java
Mon Aug 27 17:02:26 2007
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.mina.http.client;
+
+
+import org.apache.mina.filter.codec.ProtocolDecoderOutput;
+
+
+public class FakeProtocolDecoderOutput implements ProtocolDecoderOutput
+{
+
+ private Object object = null;
+
+
+ public void write( Object object )
+ {
+ this.object = object;
+ }
+
+
+ public void flush()
+ {
+ }
+
+
+ public Object getObject()
+ {
+ return object;
+ }
+
+}