Author: olamy
Date: Mon Feb 20 16:55:16 2012
New Revision: 1291370

URL: http://svn.apache.org/viewvc?rev=1291370&view=rev
Log:
[DIRECTMEMORY-61] add client side api and implementation.

Added:
    incubator/directmemory/trunk/server/directmemory-server-client/src/
    incubator/directmemory/trunk/server/directmemory-server-client/src/main/
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/AbstractDirectMemoryHttpClient.java
   (with props)
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DefaultDirectMemoryServerClient.java
   (with props)
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryHttpClient.java
   (with props)
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClient.java
   (with props)
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClientConfiguration.java
   (with props)
    
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/HttpClientDirectMemoryHttpClient.java
   (with props)
Modified:
    incubator/directmemory/trunk/server/directmemory-server-client/pom.xml
    
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheException.java
    
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheRequest.java
    
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheResponse.java

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=1291370&r1=1291369&r2=1291370&view=diff
==============================================================================
--- incubator/directmemory/trunk/server/directmemory-server-client/pom.xml 
(original)
+++ incubator/directmemory/trunk/server/directmemory-server-client/pom.xml Mon 
Feb 20 16:55:16 2012
@@ -32,5 +32,40 @@
   <name>Apache DirectMemory :: Server :: Client</name>
   <description>${project.name}</description>
 
+  <properties>
+    <httpclient.version>4.1.3</httpclient.version>
+    <httpclient.core.version>4.1.4</httpclient.core.version>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.directmemory.server</groupId>
+      <artifactId>directmemory-server-commons</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpclient</artifactId>
+      <version>${httpclient.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>commons-logging</groupId>
+          <artifactId>commons-logging</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpcore</artifactId>
+      <version>${httpclient.core.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>commons-logging</groupId>
+          <artifactId>commons-logging</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+
+  </dependencies>
 
 </project>
\ No newline at end of file

Added: 
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=1291370&view=auto
==============================================================================
--- 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/AbstractDirectMemoryHttpClient.java
 (added)
+++ 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/AbstractDirectMemoryHttpClient.java
 Mon Feb 20 16:55:16 2012
@@ -0,0 +1,70 @@
+package org.apache.directmemory.server.client;
+/*
+ * 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 org.apache.directmemory.server.commons.DirectMemoryCacheException;
+import org.apache.directmemory.server.commons.DirectMemoryCacheParser;
+import org.apache.directmemory.server.commons.DirectMemoryCacheRequest;
+import org.apache.directmemory.server.commons.DirectMemoryCacheResponse;
+import org.apache.directmemory.server.commons.DirectMemoryCacheWriter;
+
+import java.io.InputStream;
+
+/**
+ * @author Olivier Lamy
+ */
+public abstract class AbstractDirectMemoryHttpClient
+    implements DirectMemoryHttpClient
+{
+
+    private DirectMemoryCacheWriter writer = 
DirectMemoryCacheWriter.instance();
+
+    private DirectMemoryCacheParser parser = 
DirectMemoryCacheParser.instance();
+
+    protected DirectMemoryServerClientConfiguration configuration;
+
+    protected AbstractDirectMemoryHttpClient( 
DirectMemoryServerClientConfiguration configuration )
+    {
+        this.configuration = configuration;
+    }
+
+    protected byte[] getPutContent( DirectMemoryCacheRequest request )
+        throws DirectMemoryCacheException
+    {
+        // TODO handle various exchange model json raw etc..
+
+        return writer.generateJsonRequest( request ).getBytes();
+    }
+
+    protected DirectMemoryCacheResponse buildResponse( InputStream inputStream 
)
+        throws DirectMemoryCacheException
+    {
+        return parser.buildResponse( inputStream );
+    }
+
+    protected String buildRequestWithKey( DirectMemoryCacheRequest request )
+    {
+        StringBuilder uri = new StringBuilder( 
this.configuration.getProtocol() );
+        uri.append( "://" ).append( this.configuration.getHost() );
+        uri.append( ':' ).append( this.configuration.getPort() );
+        uri.append( this.configuration.getHttpPath() );
+        uri.append( '/' ).append( request.getKey() );
+        return uri.toString();
+    }
+}

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

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

Added: 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DefaultDirectMemoryServerClient.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DefaultDirectMemoryServerClient.java?rev=1291370&view=auto
==============================================================================
--- 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DefaultDirectMemoryServerClient.java
 (added)
+++ 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DefaultDirectMemoryServerClient.java
 Mon Feb 20 16:55:16 2012
@@ -0,0 +1,93 @@
+package org.apache.directmemory.server.client;
+/*
+ * 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 org.apache.directmemory.server.commons.DirectMemoryCacheException;
+import org.apache.directmemory.server.commons.DirectMemoryCacheRequest;
+import org.apache.directmemory.server.commons.DirectMemoryCacheResponse;
+
+import java.util.concurrent.Future;
+
+/**
+ * @author Olivier Lamy
+ */
+public class DefaultDirectMemoryServerClient
+    implements DirectMemoryServerClient
+{
+
+    public static DirectMemoryServerClient instance( 
DirectMemoryServerClientConfiguration configuration )
+        throws DirectMemoryCacheException
+    {
+        return new DefaultDirectMemoryServerClient( configuration );
+    }
+
+    private DirectMemoryServerClientConfiguration clientConfiguration;
+
+    private DirectMemoryHttpClient directMemoryHttpClient;
+
+    private DefaultDirectMemoryServerClient( 
DirectMemoryServerClientConfiguration configuration )
+        throws DirectMemoryCacheException
+    {
+        this.directMemoryHttpClient = 
configuration.getDirectMemoryHttpClient();
+        this.clientConfiguration = configuration;
+    }
+
+
+    @Override
+    public DirectMemoryCacheResponse retrieve( DirectMemoryCacheRequest 
directMemoryCacheRequest )
+        throws DirectMemoryCacheException
+    {
+        return this.directMemoryHttpClient.get( directMemoryCacheRequest );
+    }
+
+    @Override
+    public Future<DirectMemoryCacheResponse> asyncRetrieve( 
DirectMemoryCacheRequest directMemoryCacheRequest )
+        throws DirectMemoryCacheException
+    {
+        return this.directMemoryHttpClient.asyncGet( directMemoryCacheRequest 
);
+    }
+
+    @Override
+    public void put( DirectMemoryCacheRequest directMemoryCacheRequest )
+        throws DirectMemoryCacheException
+    {
+        this.directMemoryHttpClient.put( directMemoryCacheRequest );
+    }
+
+    @Override
+    public Future<Void> asyncPut( DirectMemoryCacheRequest 
directMemoryCacheRequest )
+        throws DirectMemoryCacheException
+    {
+        return this.directMemoryHttpClient.asyncPut( directMemoryCacheRequest 
);
+    }
+
+    @Override
+    public DirectMemoryCacheResponse delete( DirectMemoryCacheRequest 
directMemoryCacheRequest )
+        throws DirectMemoryCacheException
+    {
+        return this.directMemoryHttpClient.get( 
directMemoryCacheRequest.setDeleteRequest( true ) );
+    }
+
+    @Override
+    public Future<DirectMemoryCacheResponse> asyncDelete( 
DirectMemoryCacheRequest directMemoryCacheRequest )
+        throws DirectMemoryCacheException
+    {
+        return this.directMemoryHttpClient.asyncGet( 
directMemoryCacheRequest.setDeleteRequest( true ) );
+    }
+}

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

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

Added: 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryHttpClient.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryHttpClient.java?rev=1291370&view=auto
==============================================================================
--- 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryHttpClient.java
 (added)
+++ 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryHttpClient.java
 Mon Feb 20 16:55:16 2012
@@ -0,0 +1,47 @@
+package org.apache.directmemory.server.client;
+/*
+ * 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 org.apache.directmemory.server.commons.DirectMemoryCacheException;
+import org.apache.directmemory.server.commons.DirectMemoryCacheRequest;
+import org.apache.directmemory.server.commons.DirectMemoryCacheResponse;
+
+import java.util.concurrent.Future;
+
+/**
+ * @author Olivier Lamy
+ */
+public interface DirectMemoryHttpClient
+{
+    void configure( DirectMemoryServerClientConfiguration configuration )
+        throws DirectMemoryCacheException;
+
+    void put( DirectMemoryCacheRequest request )
+        throws DirectMemoryCacheException;
+
+    Future<Void> asyncPut( DirectMemoryCacheRequest request )
+        throws DirectMemoryCacheException;
+
+    DirectMemoryCacheResponse get( DirectMemoryCacheRequest request )
+        throws DirectMemoryCacheException;
+
+    Future<DirectMemoryCacheResponse> asyncGet( DirectMemoryCacheRequest 
request )
+        throws DirectMemoryCacheException;
+
+}

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

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

Added: 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClient.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClient.java?rev=1291370&view=auto
==============================================================================
--- 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClient.java
 (added)
+++ 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClient.java
 Mon Feb 20 16:55:16 2012
@@ -0,0 +1,50 @@
+package org.apache.directmemory.server.client;
+/*
+ * 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 org.apache.directmemory.server.commons.DirectMemoryCacheException;
+import org.apache.directmemory.server.commons.DirectMemoryCacheRequest;
+import org.apache.directmemory.server.commons.DirectMemoryCacheResponse;
+
+import java.util.concurrent.Future;
+
+/**
+ * @author Olivier Lamy
+ */
+public interface DirectMemoryServerClient
+{
+
+    DirectMemoryCacheResponse retrieve( DirectMemoryCacheRequest 
directMemoryCacheRequest )
+        throws DirectMemoryCacheException;
+
+    Future<DirectMemoryCacheResponse> asyncRetrieve( DirectMemoryCacheRequest 
directMemoryCacheRequest )
+        throws DirectMemoryCacheException;
+
+    void put( DirectMemoryCacheRequest directMemoryCacheRequest )
+        throws DirectMemoryCacheException;
+
+    Future<Void> asyncPut( DirectMemoryCacheRequest directMemoryCacheRequest )
+        throws DirectMemoryCacheException;
+
+    DirectMemoryCacheResponse delete( DirectMemoryCacheRequest 
directMemoryCacheRequest )
+        throws DirectMemoryCacheException;
+
+    Future<DirectMemoryCacheResponse> asyncDelete( DirectMemoryCacheRequest 
directMemoryCacheRequest )
+        throws DirectMemoryCacheException;
+}

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

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

Added: 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClientConfiguration.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClientConfiguration.java?rev=1291370&view=auto
==============================================================================
--- 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClientConfiguration.java
 (added)
+++ 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/DirectMemoryServerClientConfiguration.java
 Mon Feb 20 16:55:16 2012
@@ -0,0 +1,143 @@
+package org.apache.directmemory.server.client;
+/*
+ * 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.
+ */
+
+/**
+ * @author Olivier Lamy
+ */
+public class DirectMemoryServerClientConfiguration
+{
+
+
+    public static final int DEFAULT_MAX_CONCURRENT_CONNECTION = 20;
+
+    public static final long DEFAULT_CONNECTION_TIME_OUT = 5000;
+
+    public static final long DEFAULT_READ_TIME_OUT = 10000;
+
+    private String protocol = "http";
+
+    private String host;
+
+    private int port = 80;
+
+    private String httpPath;
+
+    private int maxConcurentConnection = DEFAULT_MAX_CONCURRENT_CONNECTION;
+
+    private long connectionTimeOut;
+
+    private long readTimeOut;
+
+    private DirectMemoryHttpClient directMemoryHttpClient;
+
+    public DirectMemoryServerClientConfiguration()
+    {
+        // no op
+    }
+
+    public String getHost()
+    {
+        return host;
+    }
+
+    public DirectMemoryServerClientConfiguration setHost( String host )
+    {
+        this.host = host;
+        return this;
+    }
+
+    public int getPort()
+    {
+        return port;
+    }
+
+    public DirectMemoryServerClientConfiguration setPort( int port )
+    {
+        this.port = port;
+        return this;
+    }
+
+    public String getHttpPath()
+    {
+        return httpPath;
+    }
+
+    public DirectMemoryServerClientConfiguration setHttpPath( String httpPath )
+    {
+        this.httpPath = httpPath;
+        return this;
+    }
+
+    public DirectMemoryHttpClient getDirectMemoryHttpClient()
+    {
+        return directMemoryHttpClient;
+    }
+
+    public DirectMemoryServerClientConfiguration setDirectMemoryHttpClient(
+        DirectMemoryHttpClient directMemoryHttpClient )
+    {
+        this.directMemoryHttpClient = directMemoryHttpClient;
+        return this;
+    }
+
+    public int getMaxConcurentConnection()
+    {
+        return maxConcurentConnection;
+    }
+
+    public DirectMemoryServerClientConfiguration setMaxConcurentConnection( 
int maxConcurentConnection )
+    {
+        this.maxConcurentConnection = maxConcurentConnection;
+        return this;
+    }
+
+    public long getConnectionTimeOut()
+    {
+        return connectionTimeOut;
+    }
+
+    public DirectMemoryServerClientConfiguration setConnectionTimeOut( long 
connectionTimeOut )
+    {
+        this.connectionTimeOut = connectionTimeOut;
+        return this;
+    }
+
+    public long getReadTimeOut()
+    {
+        return readTimeOut;
+    }
+
+    public DirectMemoryServerClientConfiguration setReadTimeOut( long 
readTimeOut )
+    {
+        this.readTimeOut = readTimeOut;
+        return this;
+    }
+
+    public String getProtocol()
+    {
+        return protocol;
+    }
+
+    public DirectMemoryServerClientConfiguration setProtocol( String protocol )
+    {
+        this.protocol = protocol;
+        return this;
+    }
+}

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

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

Added: 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/HttpClientDirectMemoryHttpClient.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/HttpClientDirectMemoryHttpClient.java?rev=1291370&view=auto
==============================================================================
--- 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/HttpClientDirectMemoryHttpClient.java
 (added)
+++ 
incubator/directmemory/trunk/server/directmemory-server-client/src/main/java/org/apache/directmemory/server/client/HttpClientDirectMemoryHttpClient.java
 Mon Feb 20 16:55:16 2012
@@ -0,0 +1,166 @@
+package org.apache.directmemory.server.client;
+/*
+ * 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 org.apache.directmemory.server.commons.DirectMemoryCacheException;
+import org.apache.directmemory.server.commons.DirectMemoryCacheRequest;
+import org.apache.directmemory.server.commons.DirectMemoryCacheResponse;
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+/**
+ * @author Olivier Lamy
+ */
+public class HttpClientDirectMemoryHttpClient
+    extends AbstractDirectMemoryHttpClient
+    implements DirectMemoryHttpClient
+{
+    private Logger log = LoggerFactory.getLogger( getClass() );
+
+    public static HttpClientDirectMemoryHttpClient instance( 
DirectMemoryServerClientConfiguration configuration )
+    {
+        return new HttpClientDirectMemoryHttpClient( configuration );
+    }
+
+    private HttpClient httpClient;
+
+
+    private HttpClientDirectMemoryHttpClient( 
DirectMemoryServerClientConfiguration configuration )
+    {
+        super( configuration );
+    }
+
+    @Override
+    public void configure( DirectMemoryServerClientConfiguration configuration 
)
+        throws DirectMemoryCacheException
+    {
+        this.configuration = configuration;
+        ThreadSafeClientConnManager threadSafeClientConnManager = new 
ThreadSafeClientConnManager();
+        threadSafeClientConnManager.setDefaultMaxPerRoute( 
configuration.getMaxConcurentConnection() );
+        this.httpClient = new DefaultHttpClient( threadSafeClientConnManager );
+    }
+
+    @Override
+    public void put( DirectMemoryCacheRequest request )
+        throws DirectMemoryCacheException
+    {
+        String uri = buildRequestWithKey( request );
+        if ( log.isDebugEnabled() )
+        {
+            log.debug( "put request to: {}", uri.toString() );
+        }
+        HttpPut httpPut = new HttpPut( uri.toString() );
+        httpPut.setEntity( new ByteArrayEntity( getPutContent( request ) ) );
+
+        try
+        {
+            HttpResponse response = httpClient.execute( httpPut );
+            StatusLine statusLine = response.getStatusLine();
+            if ( statusLine.getStatusCode() != 200 )
+            {
+                throw new DirectMemoryCacheException(
+                    "put cache content return http code:'" + 
statusLine.getStatusCode() + "', reasonPhrase:"
+                        + statusLine.getReasonPhrase() );
+            }
+        }
+        catch ( IOException e )
+        {
+            throw new DirectMemoryCacheException( e.getMessage(), e );
+        }
+    }
+
+    @Override
+    public Future<Void> asyncPut( final DirectMemoryCacheRequest request )
+        throws DirectMemoryCacheException
+    {
+        return Executors.newSingleThreadExecutor().submit( new Callable<Void>()
+        {
+            @Override
+            public Void call()
+                throws Exception
+            {
+                put( request );
+                return null;
+            }
+        } );
+    }
+
+    @Override
+    public DirectMemoryCacheResponse get( DirectMemoryCacheRequest request )
+        throws DirectMemoryCacheException
+    {
+        String uri = buildRequestWithKey( request );
+        if ( log.isDebugEnabled() )
+        {
+            log.debug( "get request to: {}", uri.toString() );
+        }
+
+        HttpGet httpGet = new HttpGet( uri );
+        try
+        {
+            HttpResponse httpResponse = this.httpClient.execute( httpGet );
+
+            // handle no content response
+            StatusLine statusLine = httpResponse.getStatusLine();
+            if ( statusLine.getStatusCode() == 204 )
+            {
+                return new DirectMemoryCacheResponse().setFound( false );
+            }
+
+            if ( request.isDeleteRequest() )
+            {
+                return new DirectMemoryCacheResponse().setFound( true 
).setDeleted( true );
+            }
+
+            return buildResponse( httpResponse.getEntity().getContent() );
+        }
+        catch ( IOException e )
+        {
+            throw new DirectMemoryCacheException( e.getMessage(), e );
+        }
+    }
+
+    @Override
+    public Future<DirectMemoryCacheResponse> asyncGet( final 
DirectMemoryCacheRequest request )
+        throws DirectMemoryCacheException
+    {
+        return Executors.newSingleThreadExecutor().submit( new 
Callable<DirectMemoryCacheResponse>()
+        {
+            @Override
+            public DirectMemoryCacheResponse call()
+                throws Exception
+            {
+                return get( request );
+            }
+        } );
+    }
+}

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

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

Modified: 
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheException.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheException.java?rev=1291370&r1=1291369&r2=1291370&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheException.java
 (original)
+++ 
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheException.java
 Mon Feb 20 16:55:16 2012
@@ -24,6 +24,11 @@ package org.apache.directmemory.server.c
 public class DirectMemoryCacheException
     extends Exception
 {
+    public DirectMemoryCacheException( String message )
+    {
+        super( message );
+    }
+
     public DirectMemoryCacheException( String message, Throwable throwable )
     {
         super( message, throwable );

Modified: 
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheRequest.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheRequest.java?rev=1291370&r1=1291369&r2=1291370&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheRequest.java
 (original)
+++ 
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheRequest.java
 Mon Feb 20 16:55:16 2012
@@ -23,8 +23,8 @@ import org.apache.directmemory.serializa
 /**
  * json format request:
  * {"DirectMemoryRQ":{"key":"101","put":true,"expiresIn":123,
- *    "cacheContent":""}}
- *
+ * "cacheContent":""}}
+ * <p/>
  * cache content is byte[] ie object serialisation
  *
  * @author Olivier Lamy
@@ -39,6 +39,11 @@ public class DirectMemoryCacheRequest
 
     private int expiresIn;
 
+    /**
+     * to generate a delete request <b>key mandatory</b>
+     */
+    private boolean deleteRequest = false;
+
     public boolean isUpdate()
     {
         return update;
@@ -87,4 +92,15 @@ public class DirectMemoryCacheRequest
         super.setCacheContent( cacheContent );
         return this;
     }
+
+    public boolean isDeleteRequest()
+    {
+        return deleteRequest;
+    }
+
+    public DirectMemoryCacheRequest setDeleteRequest( boolean deleteRequest )
+    {
+        this.deleteRequest = deleteRequest;
+        return this;
+    }
 }

Modified: 
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheResponse.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheResponse.java?rev=1291370&r1=1291369&r2=1291370&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheResponse.java
 (original)
+++ 
incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheResponse.java
 Mon Feb 20 16:55:16 2012
@@ -21,7 +21,7 @@ package org.apache.directmemory.server.c
 /**
  * json format response:
  * 
{"DirectMemoryRS":{"found":true,"updated":false,"key":"foo","cacheContent":""}}
- *
+ * <p/>
  * cache content is byte[] ie object serialisation
  *
  * @author Olivier Lamy
@@ -35,6 +35,8 @@ public class DirectMemoryCacheResponse
 
     private Class objectClass;
 
+    private boolean deleted = false;
+
     public boolean isFound()
     {
         return found;
@@ -79,4 +81,15 @@ public class DirectMemoryCacheResponse
         super.setKey( key );
         return this;
     }
+
+    public boolean isDeleted()
+    {
+        return deleted;
+    }
+
+    public DirectMemoryCacheResponse setDeleted( boolean deleted )
+    {
+        this.deleted = deleted;
+        return this;
+    }
 }


Reply via email to