incubator-directmemory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1331405 - in /incubator/directmemory/trunk/server: directmemory-server-client/ directmemory-server-client/src/main/java/org/apache/directmemory/server/client/ directmemory-server-client/src/main/java/org/apache/directmemory/server/client/p...
Date Fri, 27 Apr 2012 12:57:09 GMT
Author: olamy
Date: Fri Apr 27 12:57:08 2012
New Revision: 1331405

URL: http://svn.apache.org/viewvc?rev=1331405&view=rev
Log:
add a http client implementation based on asynchttpclient

Added:
    incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/
    incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/AsyncHttpClientDirectMemoryHttpClient.java
  (with props)
    incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryDeleteHandler.java
  (with props)
    incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryGetHandler.java
  (with props)
    incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryPutHandler.java
  (with props)
    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AsyncHttpClientTest.java
  (with props)
Modified:
    incubator/directmemory/trunk/server/directmemory-server-client/pom.xml
    incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/AbstractDirectMemoryHttpClient.java
    incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryClientConfiguration.java
    incubator/directmemory/trunk/server/directmemory-server/pom.xml
    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AbstractServletWithClientTest.java
    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ServletWithClientBinaryTypeTest.java
    incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml

Modified: incubator/directmemory/trunk/server/directmemory-server-client/pom.xml
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/pom.xml?rev=1331405&r1=1331404&r2=1331405&view=diff
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server-client/pom.xml (original)
+++ incubator/directmemory/trunk/server/directmemory-server-client/pom.xml Fri Apr 27 12:57:08
2012
@@ -69,6 +69,13 @@
       </exclusions>
     </dependency>
 
+    <dependency>
+      <groupId>com.ning</groupId>
+      <artifactId>async-http-client</artifactId>
+      <version>1.7.4</version>
+      <optional>true</optional>
+    </dependency>
+
 
   </dependencies>
 

Modified: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/AbstractDirectMemoryHttpClient.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/AbstractDirectMemoryHttpClient.java?rev=1331405&r1=1331404&r2=1331405&view=diff
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/AbstractDirectMemoryHttpClient.java
(original)
+++ incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/AbstractDirectMemoryHttpClient.java
Fri Apr 27 12:57:08 2012
@@ -41,7 +41,6 @@ public abstract class AbstractDirectMemo
 
     private DirectMemoryWriter writer = DirectMemoryWriter.instance();
 
-    private DirectMemoryParser parser = DirectMemoryParser.instance();
 
     protected DirectMemoryClientConfiguration configuration;
 
@@ -75,14 +74,14 @@ public abstract class AbstractDirectMemo
         }
     }
 
-    protected DirectMemoryResponse buildResponse( InputStream inputStream, DirectMemoryRequest
request )
+    public static DirectMemoryResponse buildResponse( InputStream inputStream, DirectMemoryRequest
request )
         throws DirectMemoryException
     {
 
         switch ( request.getExchangeType() )
         {
             case JSON:
-                return parser.buildResponse( inputStream );
+                return DirectMemoryParser.instance().buildResponse( inputStream );
             case JAVA_SERIALIZED_OBJECT:
                 try
                 {
@@ -120,7 +119,8 @@ public abstract class AbstractDirectMemo
                     throw new DirectMemoryException( e.getMessage(), e );
                 }
             default:
-                log.error( "exchange type unknown {}", request.getExchangeType() );
+                LoggerFactory.getLogger( AbstractDirectMemoryHttpClient.class ).error( "exchange
type unknown {}",
+                                                                                       request.getExchangeType()
);
                 throw new DirectMemoryException( "exchange type unknown " + request.getExchangeType()
);
         }
 

Modified: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryClientConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryClientConfiguration.java?rev=1331405&r1=1331404&r2=1331405&view=diff
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryClientConfiguration.java
(original)
+++ incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryClientConfiguration.java
Fri Apr 27 12:57:08 2012
@@ -164,9 +164,10 @@ public class DirectMemoryClientConfigura
         return httpClientClassName;
     }
 
-    public void setHttpClientClassName( String httpClientClassName )
+    public DirectMemoryClientConfiguration setHttpClientClassName( String httpClientClassName
)
     {
         this.httpClientClassName = httpClientClassName;
+        return this;
     }
 
     @Override

Added: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/AsyncHttpClientDirectMemoryHttpClient.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/AsyncHttpClientDirectMemoryHttpClient.java?rev=1331405&view=auto
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/AsyncHttpClientDirectMemoryHttpClient.java
(added)
+++ incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/AsyncHttpClientDirectMemoryHttpClient.java
Fri Apr 27 12:57:08 2012
@@ -0,0 +1,236 @@
+package org.apache.directmemory.server.client.providers.asynchttpclient;
+/*
+ * 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.
+ */
+
+import com.ning.http.client.AsyncHttpClient;
+import com.ning.http.client.AsyncHttpClientConfig;
+import com.ning.http.client.ListenableFuture;
+import com.ning.http.client.providers.netty.NettyAsyncHttpProvider;
+import org.apache.directmemory.server.client.AbstractDirectMemoryHttpClient;
+import org.apache.directmemory.server.client.DirectMemoryClientConfiguration;
+import org.apache.directmemory.server.client.DirectMemoryHttpClient;
+import org.apache.directmemory.server.commons.DirectMemoryException;
+import org.apache.directmemory.server.commons.DirectMemoryHttpConstants;
+import org.apache.directmemory.server.commons.DirectMemoryRequest;
+import org.apache.directmemory.server.commons.DirectMemoryResponse;
+import org.apache.directmemory.server.commons.ExchangeType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * @author Olivier Lamy
+ */
+public class AsyncHttpClientDirectMemoryHttpClient
+    extends AbstractDirectMemoryHttpClient
+    implements DirectMemoryHttpClient
+{
+    private Logger log = LoggerFactory.getLogger( getClass() );
+
+    private AsyncHttpClient asyncHttpClient;
+
+    public AsyncHttpClientDirectMemoryHttpClient( DirectMemoryClientConfiguration configuration
)
+    {
+        super( configuration );
+        // String providerClass, AsyncHttpClientConfig config
+        AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
+        builder.setConnectionTimeoutInMs( (int) configuration.getConnectionTimeOut() );
+        builder.setMaximumConnectionsTotal( configuration.getMaxConcurentConnections() );
+
+        asyncHttpClient = new AsyncHttpClient( NettyAsyncHttpProvider.class.getName(), builder.build()
);
+
+    }
+
+    @Override
+    public DirectMemoryResponse put( DirectMemoryRequest request )
+        throws DirectMemoryException
+    {
+
+        try
+        {
+            return internalPut( request ).get( configuration.getReadTimeOut(), TimeUnit.MILLISECONDS
);
+        }
+        catch ( InterruptedException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+        catch ( TimeoutException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+        catch ( ExecutionException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+
+    }
+
+    @Override
+    public Future<DirectMemoryResponse> asyncPut( DirectMemoryRequest request )
+        throws DirectMemoryException
+    {
+        return internalPut( request );
+
+    }
+
+    public ListenableFuture<DirectMemoryResponse> internalPut( DirectMemoryRequest
request )
+        throws DirectMemoryException
+    {
+        String uri = buildRequestWithKey( request );
+        log.debug( "put request to: {}", uri );
+        AsyncHttpClient.BoundRequestBuilder requestBuilder = this.asyncHttpClient.preparePut(
uri );
+        requestBuilder.addHeader( "Content-Type", getRequestContentType( request ) );
+
+        if ( request.getExpiresIn() > 0 )
+        {
+            requestBuilder.addHeader( DirectMemoryHttpConstants.EXPIRES_IN_HTTP_HEADER,
+                                      Integer.toString( request.getExpiresIn() ) );
+        }
+
+        if ( request.getExchangeType() == ExchangeType.TEXT_PLAIN )
+        {
+            requestBuilder.addHeader( DirectMemoryHttpConstants.SERIALIZER_HTTP_HEADER,
+                                      request.getSerializer().getClass().getName() );
+        }
+
+        requestBuilder.setBody( getPutContent( request ) );
+        try
+        {
+            return asyncHttpClient.executeRequest( requestBuilder.build(), new DirectMemoryPutHandler(
request ) );
+        }
+        catch ( IOException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+
+    }
+
+    @Override
+    public DirectMemoryResponse get( DirectMemoryRequest request )
+        throws DirectMemoryException
+    {
+        try
+        {
+            return internalGet( request ).get( configuration.getReadTimeOut(), TimeUnit.MILLISECONDS
);
+        }
+        catch ( InterruptedException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+        catch ( TimeoutException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+        catch ( ExecutionException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+    }
+
+    @Override
+    public Future<DirectMemoryResponse> asyncGet( DirectMemoryRequest request )
+        throws DirectMemoryException
+    {
+        return internalGet( request );
+    }
+
+    public ListenableFuture<DirectMemoryResponse> internalGet( DirectMemoryRequest
request )
+        throws DirectMemoryException
+    {
+        String uri = buildRequestWithKey( request );
+        log.debug( "get request to: {}", uri );
+
+        AsyncHttpClient.BoundRequestBuilder requestBuilder = this.asyncHttpClient.prepareGet(
uri );
+        requestBuilder.addHeader( "Accept", getAcceptContentType( request ) );
+
+        if ( request.getExchangeType() == ExchangeType.TEXT_PLAIN )
+        {
+            requestBuilder.addHeader( DirectMemoryHttpConstants.SERIALIZER_HTTP_HEADER,
+                                      request.getSerializer().getClass().getName() );
+        }
+        try
+        {
+            return asyncHttpClient.executeRequest( requestBuilder.build(), new DirectMemoryGetHandler(
request ) );
+        }
+        catch ( IOException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+    }
+
+    @Override
+    public DirectMemoryResponse delete( DirectMemoryRequest request )
+        throws DirectMemoryException
+    {
+        try
+        {
+            return internalDelete( request ).get( this.configuration.getReadTimeOut(), TimeUnit.MILLISECONDS
);
+        }
+        catch ( InterruptedException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+        catch ( TimeoutException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+        catch ( ExecutionException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+    }
+
+    @Override
+    public Future<DirectMemoryResponse> asyncDelete( DirectMemoryRequest request )
+        throws DirectMemoryException
+    {
+        return internalDelete( request );
+    }
+
+    public ListenableFuture<DirectMemoryResponse> internalDelete( DirectMemoryRequest
request )
+        throws DirectMemoryException
+    {
+        String uri = buildRequestWithKey( request );
+        log.debug( "get request to: {}", uri );
+
+        AsyncHttpClient.BoundRequestBuilder requestBuilder = this.asyncHttpClient.prepareGet(
uri );
+        requestBuilder.addHeader( "Accept", getAcceptContentType( request ) );
+
+        if ( request.getExchangeType() == ExchangeType.TEXT_PLAIN )
+        {
+            requestBuilder.addHeader( DirectMemoryHttpConstants.SERIALIZER_HTTP_HEADER,
+                                      request.getSerializer().getClass().getName() );
+        }
+        try
+        {
+            return asyncHttpClient.executeRequest( requestBuilder.build(), new DirectMemoryGetHandler(
request ) );
+        }
+        catch ( IOException e )
+        {
+            throw new DirectMemoryException( e.getMessage(), e );
+        }
+    }
+
+
+}

Propchange: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/AsyncHttpClientDirectMemoryHttpClient.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/AsyncHttpClientDirectMemoryHttpClient.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryDeleteHandler.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryDeleteHandler.java?rev=1331405&view=auto
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryDeleteHandler.java
(added)
+++ incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryDeleteHandler.java
Fri Apr 27 12:57:08 2012
@@ -0,0 +1,52 @@
+package org.apache.directmemory.server.client.providers.asynchttpclient;
+/*
+ * 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.
+ */
+
+import com.ning.http.client.AsyncCompletionHandler;
+import com.ning.http.client.Response;
+import org.apache.directmemory.server.commons.DirectMemoryRequest;
+import org.apache.directmemory.server.commons.DirectMemoryResponse;
+
+/**
+ * @author Olivier Lamy
+ */
+public class DirectMemoryDeleteHandler
+    extends AsyncCompletionHandler<DirectMemoryResponse>
+{
+
+    private DirectMemoryRequest request;
+
+    public DirectMemoryDeleteHandler( DirectMemoryRequest request )
+    {
+        this.request = request;
+    }
+
+    @Override
+    public DirectMemoryResponse onCompleted( Response response )
+        throws Exception
+    {
+        int statusCode = response.getStatusCode();
+
+        // handle no content response
+
+        return statusCode == 204
+            ? new DirectMemoryResponse().setFound( false ).setDeleted( false )
+            : new DirectMemoryResponse().setFound( true ).setDeleted( true );
+    }
+}

Propchange: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryDeleteHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryDeleteHandler.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryGetHandler.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryGetHandler.java?rev=1331405&view=auto
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryGetHandler.java
(added)
+++ incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryGetHandler.java
Fri Apr 27 12:57:08 2012
@@ -0,0 +1,63 @@
+package org.apache.directmemory.server.client.providers.asynchttpclient;
+/*
+ * 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.
+ */
+
+import com.ning.http.client.AsyncCompletionHandler;
+import com.ning.http.client.Response;
+import org.apache.directmemory.server.client.AbstractDirectMemoryHttpClient;
+import org.apache.directmemory.server.commons.DirectMemoryRequest;
+import org.apache.directmemory.server.commons.DirectMemoryResponse;
+
+/**
+ * @author Olivier Lamy
+ */
+public class DirectMemoryGetHandler
+    extends AsyncCompletionHandler<DirectMemoryResponse>
+{
+
+    private DirectMemoryRequest request;
+
+    public DirectMemoryGetHandler( DirectMemoryRequest request )
+    {
+        this.request = request;
+    }
+
+    @Override
+    public DirectMemoryResponse onCompleted( Response response )
+        throws Exception
+    {
+        int statusCode = response.getStatusCode();
+
+        // handle no content response
+
+        if ( statusCode == 204 )
+        {
+            return new DirectMemoryResponse().setFound( false );
+        }
+
+        if ( request.isDeleteRequest() )
+        {
+            return new DirectMemoryResponse().setFound( true ).setDeleted( true );
+        }
+
+        return AbstractDirectMemoryHttpClient.
+            buildResponse( response.getResponseBodyAsStream(), request ).setFound( true );
+
+    }
+}

Propchange: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryGetHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryGetHandler.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryPutHandler.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryPutHandler.java?rev=1331405&view=auto
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryPutHandler.java
(added)
+++ incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryPutHandler.java
Fri Apr 27 12:57:08 2012
@@ -0,0 +1,60 @@
+package org.apache.directmemory.server.client.providers.asynchttpclient;
+/*
+ * 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.
+ */
+
+import com.ning.http.client.AsyncCompletionHandler;
+import com.ning.http.client.Response;
+import org.apache.directmemory.server.commons.DirectMemoryException;
+import org.apache.directmemory.server.commons.DirectMemoryHttpConstants;
+import org.apache.directmemory.server.commons.DirectMemoryRequest;
+import org.apache.directmemory.server.commons.DirectMemoryResponse;
+
+/**
+ * @author Olivier Lamy
+ */
+public class DirectMemoryPutHandler
+    extends AsyncCompletionHandler<DirectMemoryResponse>
+{
+    private DirectMemoryRequest request;
+
+    public DirectMemoryPutHandler( DirectMemoryRequest request )
+    {
+        this.request = request;
+    }
+
+    @Override
+    public DirectMemoryResponse onCompleted( Response response )
+        throws Exception
+    {
+        int statusCode = response.getStatusCode();
+        switch ( statusCode )
+        {
+            case 200:
+                String headerValue = response.getHeader( DirectMemoryHttpConstants.EXPIRES_SERIALIZE_SIZE
);
+                int storedSize = headerValue == null ? -1 : Integer.valueOf( headerValue
);
+                return new DirectMemoryResponse().setStored( Boolean.TRUE ).setStoredSize(
storedSize );
+            case 204:
+                return new DirectMemoryResponse().setStored( Boolean.FALSE );
+            default:
+                throw new DirectMemoryException(
+                    "put cache content return http code:'" + statusCode + "', reasonPhrase:"
+                        + response.getStatusText() );
+        }
+    }
+}

Propchange: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryPutHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/providers/asynchttpclient/DirectMemoryPutHandler.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: incubator/directmemory/trunk/server/directmemory-server/pom.xml
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/pom.xml?rev=1331405&r1=1331404&r2=1331405&view=diff
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server/pom.xml (original)
+++ incubator/directmemory/trunk/server/directmemory-server/pom.xml Fri Apr 27 12:57:08 2012
@@ -132,6 +132,13 @@
       <version>${tomcat7Version}</version>
     </dependency>
 
+    <dependency>
+      <groupId>com.ning</groupId>
+      <artifactId>async-http-client</artifactId>
+      <version>1.7.4</version>
+      <scope>test</scope>
+    </dependency>
+
   </dependencies>
 
   <build>

Modified: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AbstractServletWithClientTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AbstractServletWithClientTest.java?rev=1331405&r1=1331404&r2=1331405&view=diff
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AbstractServletWithClientTest.java
(original)
+++ incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AbstractServletWithClientTest.java
Fri Apr 27 12:57:08 2012
@@ -24,6 +24,7 @@ import org.apache.directmemory.serializa
 import org.apache.directmemory.server.client.DirectMemoryClient;
 import org.apache.directmemory.server.client.DirectMemoryClientBuilder;
 import org.apache.directmemory.server.client.DirectMemoryClientConfiguration;
+import org.apache.directmemory.server.client.providers.httpclient.HttpClientDirectMemoryHttpClient;
 import org.apache.directmemory.server.commons.DirectMemoryRequest;
 import org.apache.directmemory.server.commons.DirectMemoryResponse;
 import org.apache.directmemory.server.commons.ExchangeType;
@@ -50,6 +51,13 @@ public abstract class AbstractServletWit
 
     StringBuilder hugeStr = new StringBuilder( "" );
 
+    public static String httpClientClassName = HttpClientDirectMemoryHttpClient.class.getName();
+
+    public AbstractServletWithClientTest()
+    {
+        // no op
+    }
+
 
     protected abstract ExchangeType getExchangeType();
 
@@ -79,18 +87,21 @@ public abstract class AbstractServletWit
                 .setPort( port )
                 .setHttpPath( "/direct-memory/DirectMemoryServlet" )
                 .setSerializer( SerializerFactory.createNewSerializer() )
+                .setHttpClientClassName( httpClientClassName )
                 .setExchangeType( getExchangeType() );
 
         client = DirectMemoryClientBuilder.newBuilder( configuration ).buildClient();
 
         // or
 
-        client = DirectMemoryClientBuilder.newBuilder()
+        client = DirectMemoryClientBuilder
+            .newBuilder()
             .toHost( "localhost" )
             .onPort( port )
             .toHttpPath( "/direct-memory/DirectMemoryServlet" )
             .withSerializer( SerializerFactory.createNewSerializer() )
             .forExchangeType( getExchangeType() )
+            .withHttpClientClassName( httpClientClassName )
             .buildClient();
 
         // END SNIPPET: client-configuration
@@ -101,6 +112,7 @@ public abstract class AbstractServletWit
         }
     }
 
+
     public void shutdown()
         throws Exception
     {

Added: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AsyncHttpClientTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AsyncHttpClientTest.java?rev=1331405&view=auto
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AsyncHttpClientTest.java
(added)
+++ incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AsyncHttpClientTest.java
Fri Apr 27 12:57:08 2012
@@ -0,0 +1,62 @@
+package org.apache.directmemory.server.services;
+/*
+ * 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.
+ */
+
+import junit.framework.TestCase;
+import org.apache.directmemory.server.client.providers.asynchttpclient.AsyncHttpClientDirectMemoryHttpClient;
+import org.apache.directmemory.server.client.providers.httpclient.HttpClientDirectMemoryHttpClient;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.JUnitCore;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * @author Olivier Lamy
+ */
+@RunWith( JUnit4.class )
+public class AsyncHttpClientTest
+    extends TestCase
+{
+    @BeforeClass
+    public static void setupHttpClientClassName()
+    {
+        ServletWithClientBinaryTypeTest.httpClientClassName = AsyncHttpClientDirectMemoryHttpClient.class.getName();
+        ServletWithClientTextPlainTypeTest.httpClientClassName = AsyncHttpClientDirectMemoryHttpClient.class.getName();
+        ServletWithClientJsonTypeTest.httpClientClassName = AsyncHttpClientDirectMemoryHttpClient.class.getName();
+    }
+
+    @AfterClass
+    public static void restoreHttpClientClassName()
+    {
+        ServletWithClientBinaryTypeTest.httpClientClassName = HttpClientDirectMemoryHttpClient.class.getName();
+        ServletWithClientTextPlainTypeTest.httpClientClassName = HttpClientDirectMemoryHttpClient.class.getName();
+        ServletWithClientJsonTypeTest.httpClientClassName = HttpClientDirectMemoryHttpClient.class.getName();
+    }
+
+    @Test
+    public void testRunAll()
+    {
+
+        JUnitCore core = new JUnitCore();
+        core.run( ServletWithClientBinaryTypeTest.class );
+
+    }
+}

Propchange: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AsyncHttpClientTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/AsyncHttpClientTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ServletWithClientBinaryTypeTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ServletWithClientBinaryTypeTest.java?rev=1331405&r1=1331404&r2=1331405&view=diff
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ServletWithClientBinaryTypeTest.java
(original)
+++ incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ServletWithClientBinaryTypeTest.java
Fri Apr 27 12:57:08 2012
@@ -26,6 +26,13 @@ import org.apache.directmemory.server.co
 public class ServletWithClientBinaryTypeTest
     extends AbstractServletWithClientTest
 {
+    public ServletWithClientBinaryTypeTest()
+    {
+        super();
+    }
+
+
+
     @Override
     protected ExchangeType getExchangeType()
     {

Modified: incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml?rev=1331405&r1=1331404&r2=1331405&view=diff
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml
(original)
+++ incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml
Fri Apr 27 12:57:08 2012
@@ -26,6 +26,7 @@
   </appender>
 
   <logger name="org.apache.directmemory.server" level="INFO"/>
+  <logger name="com.ning.http" level="DEBUG"/>
   <root level="INFO">
     <appender-ref ref="CONSOLE"/>
   </root>



Mime
View raw message