incubator-directmemory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simone Tripodi <simonetrip...@apache.org>
Subject Re: svn commit: r1291364 - in /incubator/directmemory/trunk/server: ./ directmemory-server-client/ directmemory-server-commons/ directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/ directmemory-server/ directmemory-server
Date Mon, 20 Feb 2012 17:47:00 GMT
yeah, a servlet is more than enough to achieve our goal! :)

http://people.apache.org/~simonetripodi/
http://simonetripodi.livejournal.com/
http://twitter.com/simonetripodi
http://www.99soft.org/



On Mon, Feb 20, 2012 at 5:52 PM,  <olamy@apache.org> wrote:
> Author: olamy
> Date: Mon Feb 20 16:52:25 2012
> New Revision: 1291364
>
> URL: http://svn.apache.org/viewvc?rev=1291364&view=rev
> Log:
> [DIRECTMEMORY-61] remove REST interface not needed
> add first implementation based on json exchagnge.
>
> Added:
>    incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/CacheServlet.java
  (with props)
>    incubator/directmemory/trunk/server/directmemory-server/src/main/webapp/
>    incubator/directmemory/trunk/server/directmemory-server/src/main/webapp/WEB-INF/
>    incubator/directmemory/trunk/server/directmemory-server/src/main/webapp/WEB-INF/web.xml
  (with props)
>    incubator/directmemory/trunk/server/directmemory-server/src/test/
>    incubator/directmemory/trunk/server/directmemory-server/src/test/java/
>    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/
>    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/
>    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/
>    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/
>    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/
>    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/CacheServletTest.java
  (with props)
>    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ExtractKeyFromPathTest.java
>      - copied, changed from r1291362, incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/DirectMemoryCacheService.java
>    incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/Wine.java
  (contents, props changed)
>      - copied, changed from r1291362, incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/DirectMemoryCacheService.java
>    incubator/directmemory/trunk/server/directmemory-server/src/test/resources/
>    incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml
>      - copied, changed from r1291362, incubator/directmemory/trunk/server/directmemory-server-client/pom.xml
> Removed:
>    incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/DirectMemoryCacheService.java
> Modified:
>    incubator/directmemory/trunk/server/directmemory-server-client/pom.xml
>    incubator/directmemory/trunk/server/directmemory-server-commons/pom.xml
>    incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheWriter.java
>    incubator/directmemory/trunk/server/directmemory-server/pom.xml
>    incubator/directmemory/trunk/server/pom.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=1291364&r1=1291363&r2=1291364&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:52:25 2012
> @@ -29,7 +29,6 @@
>   <modelVersion>4.0.0</modelVersion>
>
>   <artifactId>directmemory-server-client</artifactId>
> -  <packaging>war</packaging>
>   <name>Apache DirectMemory :: Server :: Client</name>
>   <description>${project.name}</description>
>
>
> Modified: incubator/directmemory/trunk/server/directmemory-server-commons/pom.xml
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-commons/pom.xml?rev=1291364&r1=1291363&r2=1291364&view=diff
> ==============================================================================
> --- incubator/directmemory/trunk/server/directmemory-server-commons/pom.xml (original)
> +++ incubator/directmemory/trunk/server/directmemory-server-commons/pom.xml Mon Feb 20
16:52:25 2012
> @@ -29,7 +29,6 @@
>   <modelVersion>4.0.0</modelVersion>
>
>   <artifactId>directmemory-server-commons</artifactId>
> -  <packaging>war</packaging>
>   <name>Apache DirectMemory :: Server :: Commons</name>
>   <description>${project.name}</description>
>
>
> Modified: incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheWriter.java
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheWriter.java?rev=1291364&r1=1291363&r2=1291364&view=diff
> ==============================================================================
> --- incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheWriter.java
(original)
> +++ incubator/directmemory/trunk/server/directmemory-server-commons/src/main/java/org/apache/directmemory/server/commons/DirectMemoryCacheWriter.java
Mon Feb 20 16:52:25 2012
> @@ -56,54 +56,6 @@ public class DirectMemoryCacheWriter
>         this.jsonFactory = new JsonFactory();
>     }
>
> -    public String generateXmlRequest( DirectMemoryCacheRequest request )
> -        throws DirectMemoryCacheException
> -    {
> -        try
> -        {
> -            StringWriter stringWriter = new StringWriter();
> -            XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(
stringWriter );
> -            xmlStreamWriter.writeStartDocument( "1.0" );
> -
> -            xmlStreamWriter.writeStartElement( DirectMemoryCacheConstants.ROOT_RQ_NAME
);
> -
> -            xmlStreamWriter.writeAttribute( DirectMemoryCacheConstants.KEY_FIELD_NAME,
request.getKey() );
> -            xmlStreamWriter.writeAttribute( DirectMemoryCacheConstants.PUT_FIELD_NAME,
> -                                            Boolean.toString(
request.isUpdate() ) );
> -            xmlStreamWriter.writeAttribute( DirectMemoryCacheConstants.EXPIRES_IN_FIELD_NAME,
> -                                            Integer.toString(
request.getExpiresIn() ) );
> -
> -            if ( request.isUpdate() )
> -            {
> -                // FIXME take care of NPE
> -                // cache content generation
> -                Serializer serializer = request.getSerializer();
> -                // if no Object users are able to pass a string content
> -                byte[] bytes = request.getObject() != null
> -                    ? request.getSerializer().serialize( request.getObject()
)
> -                    : request.getCacheContent();
> -                xmlStreamWriter.writeStartElement( DirectMemoryCacheConstants.CACHE_CONTENT_FIELD_NAME
);
> -                xmlStreamWriter.writeCData( new String( bytes ) );// charset
?
> -                xmlStreamWriter.writeEndElement();
> -            }
> -
> -            xmlStreamWriter.writeEndElement();
> -
> -            xmlStreamWriter.writeEndDocument();
> -
> -            return stringWriter.toString();
> -        }
> -        catch ( IOException e )
> -        {
> -            throw new DirectMemoryCacheException( e.getMessage(), e );
> -        }
> -        catch ( XMLStreamException e )
> -        {
> -            throw new DirectMemoryCacheException( e.getMessage(), e );
> -        }
> -
> -    }
> -
>     public String generateJsonRequest( DirectMemoryCacheRequest request )
>         throws DirectMemoryCacheException
>     {
>
> Modified: incubator/directmemory/trunk/server/directmemory-server/pom.xml
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/pom.xml?rev=1291364&r1=1291363&r2=1291364&view=diff
> ==============================================================================
> --- incubator/directmemory/trunk/server/directmemory-server/pom.xml (original)
> +++ incubator/directmemory/trunk/server/directmemory-server/pom.xml Mon Feb 20 16:52:25
2012
> @@ -41,6 +41,15 @@
>       <version>1.1.1</version>
>     </dependency>
>     <dependency>
> +      <groupId>commons-lang</groupId>
> +      <artifactId>commons-lang</artifactId>
> +    </dependency>
> +    <dependency>
> +      <groupId>javax.servlet</groupId>
> +      <artifactId>servlet-api</artifactId>
> +      <version>2.5</version>
> +    </dependency>
> +    <dependency>
>       <groupId>org.apache.directmemory.server</groupId>
>       <artifactId>directmemory-server-commons</artifactId>
>     </dependency>
> @@ -48,6 +57,49 @@
>       <groupId>org.slf4j</groupId>
>       <artifactId>slf4j-api</artifactId>
>     </dependency>
> +    <dependency>
> +      <groupId>junit</groupId>
> +      <artifactId>junit</artifactId>
> +      <scope>test</scope>
> +    </dependency>
> +    <dependency>
> +      <groupId>ch.qos.logback</groupId>
> +      <artifactId>logback-core</artifactId>
> +      <scope>test</scope>
> +    </dependency>
> +    <dependency>
> +      <groupId>ch.qos.logback</groupId>
> +      <artifactId>logback-classic</artifactId>
> +      <scope>test</scope>
> +    </dependency>
> +    <dependency>
> +      <groupId>org.springframework</groupId>
> +      <artifactId>spring-test</artifactId>
> +      <version>3.1.1.RELEASE</version>
> +      <scope>test</scope>
> +    </dependency>
> +    <dependency>
> +      <groupId>org.springframework</groupId>
> +      <artifactId>spring-core</artifactId>
> +      <version>3.1.1.RELEASE</version>
> +      <scope>test</scope>
> +    </dependency>
>   </dependencies>
>
> +  <build>
> +    <plugins>
> +      <plugin>
> +        <groupId>org.apache.maven.plugins</groupId>
> +        <artifactId>maven-surefire-plugin</artifactId>
> +        <configuration>
> +          <systemPropertyVariables>
> +            <directMemory.numberOfBuffers>10</directMemory.numberOfBuffers>
> +            <!-- must be enough confortable for test beans -->
> +            <directMemory.size>10000</directMemory.size>
> +          </systemPropertyVariables>
> +        </configuration>
> +      </plugin>
> +    </plugins>
> +  </build>
> +
>  </project>
> \ No newline at end of file
>
> Added: incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/CacheServlet.java
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/CacheServlet.java?rev=1291364&view=auto
> ==============================================================================
> --- incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/CacheServlet.java
(added)
> +++ incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/CacheServlet.java
Mon Feb 20 16:52:25 2012
> @@ -0,0 +1,179 @@
> +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 org.apache.commons.lang.StringUtils;
> +import org.apache.directmemory.cache.CacheService;
> +import org.apache.directmemory.cache.CacheServiceImpl;
> +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 org.slf4j.Logger;
> +import org.slf4j.LoggerFactory;
> +
> +import javax.servlet.ServletConfig;
> +import javax.servlet.ServletException;
> +import javax.servlet.http.HttpServlet;
> +import javax.servlet.http.HttpServletRequest;
> +import javax.servlet.http.HttpServletResponse;
> +import java.io.IOException;
> +
> +/**
> + * TODO add some listener plugin mechanism to store figures/statistics on cache access
> + *
> + * @author Olivier Lamy
> + */
> +public class CacheServlet
> +    extends HttpServlet
> +{
> +
> +    private Logger log = LoggerFactory.getLogger( getClass() );
> +
> +    private CacheService cacheService = new CacheServiceImpl();
> +
> +    private DirectMemoryCacheParser parser = DirectMemoryCacheParser.instance();
> +
> +    private DirectMemoryCacheWriter writer = DirectMemoryCacheWriter.instance();
> +
> +    @Override
> +    public void init( ServletConfig config )
> +        throws ServletException
> +    {
> +        super.init( config );
> +        // TODO some configuration for cacheService.init( .... ); different from
sysproperties
> +        //int numberOfBuffers, int size, int initialCapacity, int concurrencyLevel
> +        int numberOfBuffers = Integer.getInteger( "directMemory.numberOfBuffers",
1000 );
> +        int size = Integer.getInteger( "directMemory.size", 10 );
> +        int initialCapacity =
> +            Integer.getInteger( "directMemory.initialCapacity", CacheService.DEFAULT_INITIAL_CAPACITY
);
> +        int concurrencyLevel =
> +            Integer.getInteger( "directMemory.concurrencyLevel", CacheService.DEFAULT_CONCURRENCY_LEVEL
);
> +        cacheService.init( numberOfBuffers, size, initialCapacity, concurrencyLevel
);
> +    }
> +
> +    @Override
> +    public void destroy()
> +    {
> +        super.destroy();
> +    }
> +
> +    @Override
> +    protected void doPost( HttpServletRequest req, HttpServletResponse resp )
> +        throws ServletException, IOException
> +    {
> +        this.doPut( req, resp );
> +    }
> +
> +    @Override
> +    protected void doPut( HttpServletRequest req, HttpServletResponse resp )
> +        throws ServletException, IOException
> +    {
> +        //TODO check request content to send HttpServletResponse.SC_BAD_REQUEST
> +        // if missing parameter in json request
> +
> +        String path = req.getPathInfo();
> +        String servletPath = req.getServletPath();
> +        String key = retrieveKeyFromPath( path );
> +
> +        try
> +        {
> +            DirectMemoryCacheRequest cacheRequest = parser.buildRequest( req.getInputStream()
);
> +
> +            //exists ?
> +            if ( cacheService.retrieveByteArray( key ) == null )
> +            {
> +                cacheService.putByteArray( key, cacheRequest.getCacheContent(),
cacheRequest.getExpiresIn() );
> +            }
> +            else
> +            {
> +                cacheService.updateByteArray( key, cacheRequest.getCacheContent()
);
> +            }
> +        }
> +        catch ( DirectMemoryCacheException e )
> +        {
> +            resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()
);
> +        }
> +    }
> +
> +    @Override
> +    protected void doDelete( HttpServletRequest req, HttpServletResponse resp )
> +        throws ServletException, IOException
> +    {
> +        String path = req.getPathInfo();
> +        String servletPath = req.getServletPath();
> +        String key = retrieveKeyFromPath( path );
> +
> +        // TODO if key == null -> BAD_REQUEST http response
> +
> +        cacheService.free( key );
> +    }
> +
> +    @Override
> +    protected void doGet( HttpServletRequest req, HttpServletResponse resp )
> +        throws ServletException, IOException
> +    {
> +        // url format = /cache/key so get the key from path
> +        String path = req.getPathInfo();
> +        String servletPath = req.getServletPath();
> +        String key = retrieveKeyFromPath( path );
> +
> +        if ( StringUtils.isEmpty( key ) )
> +        {
> +            resp.sendError( HttpServletResponse.SC_BAD_REQUEST, "key missing in
path" );
> +            return;
> +        }
> +
> +        byte[] bytes = cacheService.retrieveByteArray( key );
> +
> +        if ( bytes == null )
> +        {
> +            resp.sendError( HttpServletResponse.SC_NO_CONTENT, "No content for
key: " + key );
> +            return;
> +        }
> +        DirectMemoryCacheResponse response = new DirectMemoryCacheResponse().setKey(
key ).setCacheContent( bytes );
> +
> +        try
> +        {
> +            // TODO directly write in output stream
> +            String json = writer.generateJsonResponse( response );
> +            resp.getWriter().write( json );
> +        }
> +        catch ( DirectMemoryCacheException e )
> +        {
> +            resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()
);
> +        }
> +    }
> +
> +    /**
> +     * protected for unit test reason
> +     *
> +     * @param path
> +     * @return
> +     */
> +    protected String retrieveKeyFromPath( String path )
> +    {
> +        if ( StringUtils.endsWith( path, "/" ) )
> +        {
> +            return StringUtils.substringAfterLast( StringUtils.substringBeforeLast(
path, "/" ), "/" );
> +        }
> +        return StringUtils.substringAfterLast( path, "/" );
> +    }
> +}
>
> Propchange: incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/CacheServlet.java
> ------------------------------------------------------------------------------
>    svn:eol-style = native
>
> Propchange: incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/CacheServlet.java
> ------------------------------------------------------------------------------
>    svn:keywords = Author Date Id Revision
>
> Added: incubator/directmemory/trunk/server/directmemory-server/src/main/webapp/WEB-INF/web.xml
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/main/webapp/WEB-INF/web.xml?rev=1291364&view=auto
> ==============================================================================
> --- incubator/directmemory/trunk/server/directmemory-server/src/main/webapp/WEB-INF/web.xml
(added)
> +++ incubator/directmemory/trunk/server/directmemory-server/src/main/webapp/WEB-INF/web.xml
Mon Feb 20 16:52:25 2012
> @@ -0,0 +1,36 @@
> +<?xml version="1.0" encoding="UTF-8"?>
> +
> +<!--
> +  ~ Copyright 2005-2006 The Apache Software Foundation.
> +  ~
> +  ~ Licensed 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.
> +  -->
> +
> +<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
> +         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> +         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
> +
> +  <display-name>Apache DirectMemory</display-name>
> +
> +  <servlet>
> +    <servlet-name>CacheServlet</servlet-name>
> +    <servlet-class>org.apache.directmemory.server.services.CacheServlet</servlet-class>
> +  </servlet>
> +
> +  <servlet-mapping>
> +    <servlet-name>CacheServlet</servlet-name>
> +    <url-pattern>/cache/*</url-pattern>
> +  </servlet-mapping>
> +
> +
> +</web-app>
> \ No newline at end of file
>
> Propchange: incubator/directmemory/trunk/server/directmemory-server/src/main/webapp/WEB-INF/web.xml
> ------------------------------------------------------------------------------
>    svn:eol-style = native
>
> Propchange: incubator/directmemory/trunk/server/directmemory-server/src/main/webapp/WEB-INF/web.xml
> ------------------------------------------------------------------------------
>    svn:keywords = Author Date Id Revision
>
> Added: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/CacheServletTest.java
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/CacheServletTest.java?rev=1291364&view=auto
> ==============================================================================
> --- incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/CacheServletTest.java
(added)
> +++ incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/CacheServletTest.java
Mon Feb 20 16:52:25 2012
> @@ -0,0 +1,273 @@
> +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 org.apache.directmemory.serialization.Serializer;
> +import org.apache.directmemory.serialization.SerializerFactory;
> +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 org.junit.Before;
> +import org.junit.Test;
> +import org.slf4j.Logger;
> +import org.slf4j.LoggerFactory;
> +import org.springframework.mock.web.MockHttpServletRequest;
> +import org.springframework.mock.web.MockHttpServletResponse;
> +import org.springframework.mock.web.MockServletConfig;
> +import org.springframework.mock.web.MockServletContext;
> +
> +import javax.servlet.http.HttpServletResponse;
> +import java.io.ByteArrayInputStream;
> +
> +import static org.junit.Assert.assertEquals;
> +
> +/**
> + * @author Olivier Lamy
> + */
> +public class CacheServletTest
> +{
> +    private Logger log = LoggerFactory.getLogger( getClass() );
> +
> +    CacheServlet cacheServlet = new CacheServlet();
> +
> +    DirectMemoryCacheWriter writer = DirectMemoryCacheWriter.instance();
> +
> +    DirectMemoryCacheParser parser = DirectMemoryCacheParser.instance();
> +
> +    @Before
> +    public void init()
> +        throws Exception
> +    {
> +
> +        MockServletContext mockServletContext = new MockServletContext();
> +        mockServletContext.setContextPath( "direct-memory" );
> +
> +        MockServletConfig mockServletConfig = new MockServletConfig( mockServletContext
);
> +
> +        cacheServlet.init( mockServletConfig );
> +    }
> +
> +    @Test
> +    public void badRequest()
> +        throws Exception
> +    {
> +
> +        MockHttpServletRequest request = new MockHttpServletRequest();
> +
> +        MockHttpServletResponse response = new MockHttpServletResponse();
> +
> +        cacheServlet.doGet( request, response );
> +
> +        assertEquals( HttpServletResponse.SC_BAD_REQUEST, response.getStatus() );
> +
> +
> +    }
> +
> +    @Test
> +    public void keyNotFound()
> +        throws Exception
> +    {
> +
> +        MockHttpServletRequest request = new MockHttpServletRequest();
> +
> +        request.setServletPath( "cache" );
> +
> +        request.setPathInfo( "/foo" );
> +
> +        MockHttpServletResponse response = new MockHttpServletResponse();
> +
> +        cacheServlet.doGet( request, response );
> +
> +        assertEquals( HttpServletResponse.SC_NO_CONTENT, response.getStatus() );
> +
> +    }
> +
> +    @Test
> +    public void storeObject()
> +        throws Exception
> +    {
> +
> +        Serializer serializer = SerializerFactory.createNewSerializer();
> +
> +        Wine bordeaux = new Wine( "Bordeaux", "very great wine" );
> +
> +        DirectMemoryCacheRequest directMemoryCacheRequest =
> +            new DirectMemoryCacheRequest().setKey( "bordeaux" ).setCacheContent(
serializer.serialize( bordeaux ) );
> +
> +        String rq = writer.generateJsonRequest( directMemoryCacheRequest );
> +
> +        MockHttpServletRequest putRequest = new MockHttpServletRequest();
> +
> +        putRequest.setServletPath( "cache" );
> +
> +        putRequest.setPathInfo( "/bordeaux" );
> +
> +        putRequest.setContent( rq.getBytes() );
> +
> +        MockHttpServletResponse putResponse = new MockHttpServletResponse();
> +
> +        cacheServlet.doPut( putRequest, putResponse );
> +
> +        assertEquals( HttpServletResponse.SC_OK, putResponse.getStatus() );
> +
> +        // now retrieve the content
> +
> +        MockHttpServletRequest getRequest = new MockHttpServletRequest();
> +
> +        getRequest.setPathInfo( "/bordeaux" );
> +
> +        MockHttpServletResponse getResponse = new MockHttpServletResponse();
> +
> +        cacheServlet.doGet( getRequest, getResponse );
> +
> +        assertEquals( HttpServletResponse.SC_OK, getResponse.getStatus() );
> +
> +        DirectMemoryCacheResponse response =
> +            parser.buildResponse( new ByteArrayInputStream( getResponse.getContentAsByteArray()
) );
> +
> +        Wine wineFromCache = serializer.deserialize( response.getCacheContent(),
Wine.class );
> +
> +        assertEquals( bordeaux.getName(), wineFromCache.getName() );
> +        assertEquals( bordeaux.getDescription(), wineFromCache.getDescription()
);
> +
> +    }
> +
> +    @Test
> +    public void storeExpiredObject()
> +        throws Exception
> +    {
> +
> +        Serializer serializer = SerializerFactory.createNewSerializer();
> +
> +        Wine bordeaux = new Wine( "Bordeaux", "very great wine" );
> +
> +        DirectMemoryCacheRequest directMemoryCacheRequest =
> +            new DirectMemoryCacheRequest().setKey( "bordeaux" ).setCacheContent(
> +                serializer.serialize( bordeaux ) ).setExpiresIn( 3 );
> +
> +        String rq = writer.generateJsonRequest( directMemoryCacheRequest );
> +
> +        MockHttpServletRequest putRequest = new MockHttpServletRequest();
> +
> +        putRequest.setServletPath( "cache" );
> +
> +        putRequest.setPathInfo( "/bordeaux" );
> +
> +        putRequest.setContent( rq.getBytes() );
> +
> +        MockHttpServletResponse putResponse = new MockHttpServletResponse();
> +
> +        cacheServlet.doPut( putRequest, putResponse );
> +
> +        assertEquals( HttpServletResponse.SC_OK, putResponse.getStatus() );
> +
> +        Thread.sleep( 10 );
> +
> +        // now retrieve the content
> +
> +        MockHttpServletRequest getRequest = new MockHttpServletRequest();
> +
> +        getRequest.setPathInfo( "/bordeaux" );
> +
> +        MockHttpServletResponse getResponse = new MockHttpServletResponse();
> +
> +        cacheServlet.doGet( getRequest, getResponse );
> +
> +        assertEquals( HttpServletResponse.SC_NO_CONTENT, getResponse.getStatus()
);
> +
> +
> +    }
> +
> +
> +    @Test
> +    public void storeObjectThenRemove()
> +        throws Exception
> +    {
> +
> +        Serializer serializer = SerializerFactory.createNewSerializer();
> +
> +        Wine bordeaux = new Wine( "Bordeaux", "very great wine" );
> +
> +        DirectMemoryCacheRequest directMemoryCacheRequest =
> +            new DirectMemoryCacheRequest().setKey( "bordeaux" ).setCacheContent(
serializer.serialize( bordeaux ) );
> +
> +        String rq = writer.generateJsonRequest( directMemoryCacheRequest );
> +
> +        MockHttpServletRequest putRequest = new MockHttpServletRequest();
> +
> +        putRequest.setServletPath( "cache" );
> +
> +        putRequest.setPathInfo( "/bordeaux" );
> +
> +        putRequest.setContent( rq.getBytes() );
> +
> +        MockHttpServletResponse putResponse = new MockHttpServletResponse();
> +
> +        cacheServlet.doPut( putRequest, putResponse );
> +
> +        assertEquals( HttpServletResponse.SC_OK, putResponse.getStatus() );
> +
> +        // now retrieve the content
> +
> +        MockHttpServletRequest getRequest = new MockHttpServletRequest();
> +
> +        getRequest.setPathInfo( "/bordeaux" );
> +
> +        MockHttpServletResponse getResponse = new MockHttpServletResponse();
> +
> +        cacheServlet.doGet( getRequest, getResponse );
> +
> +        assertEquals( HttpServletResponse.SC_OK, getResponse.getStatus() );
> +
> +        DirectMemoryCacheResponse response =
> +            parser.buildResponse( new ByteArrayInputStream( getResponse.getContentAsByteArray()
) );
> +
> +        Wine wineFromCache = serializer.deserialize( response.getCacheContent(),
Wine.class );
> +
> +        assertEquals( bordeaux.getName(), wineFromCache.getName() );
> +        assertEquals( bordeaux.getDescription(), wineFromCache.getDescription()
);
> +
> +        // now delete the content
> +
> +        MockHttpServletRequest deleteRq = new MockHttpServletRequest();
> +
> +        deleteRq.setPathInfo( "/bordeaux" );
> +
> +        MockHttpServletResponse deleteResponse = new MockHttpServletResponse();
> +
> +        cacheServlet.doDelete( deleteRq, deleteResponse );
> +
> +        assertEquals( HttpServletResponse.SC_OK, deleteResponse.getStatus() );
> +
> +        // now try again a read MUST be not content
> +
> +        getRequest = new MockHttpServletRequest();
> +
> +        getRequest.setPathInfo( "/bordeaux" );
> +
> +        getResponse = new MockHttpServletResponse();
> +
> +        cacheServlet.doGet( getRequest, getResponse );
> +
> +        assertEquals( HttpServletResponse.SC_NO_CONTENT, getResponse.getStatus()
);
> +
> +    }
> +
> +}
>
> Propchange: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/CacheServletTest.java
> ------------------------------------------------------------------------------
>    svn:eol-style = native
>
> Propchange: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/CacheServletTest.java
> ------------------------------------------------------------------------------
>    svn:keywords = Author Date Id Revision
>
> Copied: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ExtractKeyFromPathTest.java
(from r1291362, incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/DirectMemoryCacheService.java)
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ExtractKeyFromPathTest.java?p2=incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ExtractKeyFromPathTest.java&p1=incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/DirectMemoryCacheService.java&r1=1291362&r2=1291364&rev=1291364&view=diff
> ==============================================================================
> --- incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/DirectMemoryCacheService.java
(original)
> +++ incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/ExtractKeyFromPathTest.java
Mon Feb 20 16:52:25 2012
> @@ -1,5 +1,4 @@
>  package org.apache.directmemory.server.services;
> -
>  /*
>  * Licensed to the Apache Software Foundation (ASF) under one
>  * or more contributor license agreements.  See the NOTICE file
> @@ -19,37 +18,32 @@ package org.apache.directmemory.server.s
>  * under the License.
>  */
>
> -import org.apache.directmemory.server.commons.DirectMemoryCacheRequest;
> -import org.apache.directmemory.server.commons.DirectMemoryCacheResponse;
> +import org.junit.Test;
>
> -import javax.ws.rs.Consumes;
> -import javax.ws.rs.DELETE;
> -import javax.ws.rs.GET;
> -import javax.ws.rs.PUT;
> -import javax.ws.rs.Path;
> -import javax.ws.rs.PathParam;
> -import javax.ws.rs.Produces;
> -import javax.ws.rs.core.MediaType;
> +import static junit.framework.Assert.assertEquals;
>
>  /**
>  * @author Olivier Lamy
>  */
> -@Path( "directMemoryService" )
> -public interface DirectMemoryCacheService
> +public class ExtractKeyFromPathTest
>  {
> -    @GET
> -    @Path( "retrieve/{key}" )
> -    @Produces( MediaType.APPLICATION_JSON )
> -    DirectMemoryCacheResponse retrieve( @PathParam( "key" ) String key );
> -
> -    @PUT
> -    @Path( "store" )
> -    @Produces( MediaType.APPLICATION_JSON )
> -    @Consumes( MediaType.APPLICATION_JSON )
> -    Boolean store( DirectMemoryCacheRequest request );
> -
> -    @DELETE
> -    @Path( "delete/{key}" )
> -    Boolean delete( @PathParam( "key" ) String key );
>
> +    private CacheServlet cacheServlet = new CacheServlet();
> +
> +    @Test
> +    public void pathEndedWithSlash()
> +        throws Exception
> +    {
> +        String key = cacheServlet.retrieveKeyFromPath( "cache/foo/" );
> +        assertEquals( "foo", key );
> +    }
> +
> +    @Test
> +    public void pathNotEndedWithSlash()
> +        throws Exception
> +    {
> +        String key = cacheServlet.retrieveKeyFromPath( "/cache/foo" );
> +        assertEquals( "foo", key );
> +
> +    }
>  }
>
> Copied: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/Wine.java
(from r1291362, incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/DirectMemoryCacheService.java)
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/Wine.java?p2=incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/Wine.java&p1=incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/DirectMemoryCacheService.java&r1=1291362&r2=1291364&rev=1291364&view=diff
> ==============================================================================
> --- incubator/directmemory/trunk/server/directmemory-server/src/main/java/org/apache/directmemory/server/services/DirectMemoryCacheService.java
(original)
> +++ incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/Wine.java
Mon Feb 20 16:52:25 2012
> @@ -1,5 +1,4 @@
>  package org.apache.directmemory.server.services;
> -
>  /*
>  * Licensed to the Apache Software Foundation (ASF) under one
>  * or more contributor license agreements.  See the NOTICE file
> @@ -19,37 +18,41 @@ package org.apache.directmemory.server.s
>  * under the License.
>  */
>
> -import org.apache.directmemory.server.commons.DirectMemoryCacheRequest;
> -import org.apache.directmemory.server.commons.DirectMemoryCacheResponse;
> -
> -import javax.ws.rs.Consumes;
> -import javax.ws.rs.DELETE;
> -import javax.ws.rs.GET;
> -import javax.ws.rs.PUT;
> -import javax.ws.rs.Path;
> -import javax.ws.rs.PathParam;
> -import javax.ws.rs.Produces;
> -import javax.ws.rs.core.MediaType;
> +import java.io.Serializable;
>
>  /**
>  * @author Olivier Lamy
>  */
> -@Path( "directMemoryService" )
> -public interface DirectMemoryCacheService
> +public class Wine
> +    implements Serializable
>  {
> -    @GET
> -    @Path( "retrieve/{key}" )
> -    @Produces( MediaType.APPLICATION_JSON )
> -    DirectMemoryCacheResponse retrieve( @PathParam( "key" ) String key );
> -
> -    @PUT
> -    @Path( "store" )
> -    @Produces( MediaType.APPLICATION_JSON )
> -    @Consumes( MediaType.APPLICATION_JSON )
> -    Boolean store( DirectMemoryCacheRequest request );
> -
> -    @DELETE
> -    @Path( "delete/{key}" )
> -    Boolean delete( @PathParam( "key" ) String key );
> +    private String name;
> +
> +    private String description;
>
> +    public Wine( String name, String description )
> +    {
> +        this.name = name;
> +        this.description = description;
> +    }
> +
> +    public String getName()
> +    {
> +        return name;
> +    }
> +
> +    public void setName( String name )
> +    {
> +        this.name = name;
> +    }
> +
> +    public String getDescription()
> +    {
> +        return description;
> +    }
> +
> +    public void setDescription( String description )
> +    {
> +        this.description = description;
> +    }
>  }
>
> Propchange: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/Wine.java
> ------------------------------------------------------------------------------
>    svn:eol-style = native
>
> Propchange: incubator/directmemory/trunk/server/directmemory-server/src/test/java/org/apache/directmemory/server/services/Wine.java
> ------------------------------------------------------------------------------
>    svn:keywords = Author Date Id Revision
>
> Copied: incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml
(from r1291362, incubator/directmemory/trunk/server/directmemory-server-client/pom.xml)
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml?p2=incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml&p1=incubator/directmemory/trunk/server/directmemory-server-client/pom.xml&r1=1291362&r2=1291364&rev=1291364&view=diff
> ==============================================================================
> --- incubator/directmemory/trunk/server/directmemory-server-client/pom.xml (original)
> +++ incubator/directmemory/trunk/server/directmemory-server/src/test/resources/logback-test.xml
Mon Feb 20 16:52:25 2012
> @@ -1,37 +1,33 @@
> -<?xml version="1.0" encoding="UTF-8"?>
> -<!--
> -  ~ 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.
> -  -->
> -
> -<project xmlns="http://maven.apache.org/POM/4.0.0"
> -         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> -         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
> -  <parent>
> -    <groupId>org.apache.directmemory.server</groupId>
> -    <artifactId>server</artifactId>
> -    <version>0.5.5-SNAPSHOT</version>
> -  </parent>
> -  <modelVersion>4.0.0</modelVersion>
> -
> -  <artifactId>directmemory-server-client</artifactId>
> -  <packaging>war</packaging>
> -  <name>Apache DirectMemory :: Server :: Client</name>
> -  <description>${project.name}</description>
> -
> -
> -</project>
> \ No newline at end of file
> +<?xml version="1.0" encoding="UTF-8"?>
> +<!--
> +  ~ 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.
> +  -->
> +<configuration>
> +
> +  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
> +    <encoder>
> +      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %method - %msg%n</pattern>
> +    </encoder>
> +  </appender>
> +
> +  <logger name="org.apache.directmemory.server" level="INFO"/>
> +  <root level="INFO">
> +    <appender-ref ref="CONSOLE"/>
> +  </root>
> +
> +</configuration>
> \ No newline at end of file
>
> Modified: incubator/directmemory/trunk/server/pom.xml
> URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/server/pom.xml?rev=1291364&r1=1291363&r2=1291364&view=diff
> ==============================================================================
> --- incubator/directmemory/trunk/server/pom.xml (original)
> +++ incubator/directmemory/trunk/server/pom.xml Mon Feb 20 16:52:25 2012
> @@ -43,6 +43,11 @@
>   <dependencyManagement>
>     <dependencies>
>       <dependency>
> +        <groupId>commons-lang</groupId>
> +        <artifactId>commons-lang</artifactId>
> +        <version>2.6</version>
> +      </dependency>
> +      <dependency>
>         <groupId>org.apache.directmemory.server</groupId>
>         <artifactId>directmemory-server-commons</artifactId>
>         <version>${project.version}</version>
>
>

Mime
View raw message