From 7e69e4e4ba026b387e4464464b529ae19d2d1b17 Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 29 Apr 2010 05:46:13 +0000 Subject: Refactoring HTTPCacheContext to common-http module to avoid binding-atom to depend on binding-http git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@939202 13f79535-47bb-0310-9956-ffa450edef68 --- .../trunk/modules/binding-atom-runtime/pom.xml | 6 - .../atom/provider/AtomBindingListenerServlet.java | 2 +- .../http/provider/HTTPBindingListenerServlet.java | 2 +- .../sca/binding/http/TestBindingCacheImpl.java | 2 + .../tuscany/sca/binding/http/HTTPCacheContext.java | 257 --------------------- .../tuscany/sca/common/http/HTTPCacheContext.java | 257 +++++++++++++++++++++ 6 files changed, 261 insertions(+), 265 deletions(-) delete mode 100644 sca-java-2.x/trunk/modules/binding-http/src/main/java/org/apache/tuscany/sca/binding/http/HTTPCacheContext.java create mode 100644 sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPCacheContext.java diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/pom.xml b/sca-java-2.x/trunk/modules/binding-atom-runtime/pom.xml index b3ef011edd..adf37cd84a 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/pom.xml +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/pom.xml @@ -84,12 +84,6 @@ 2.0-SNAPSHOT - - org.apache.tuscany.sca - tuscany-binding-http - 2.0-SNAPSHOT - - javax.servlet servlet-api diff --git a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java index eead0b9183..371645c2d4 100644 --- a/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java +++ b/sca-java-2.x/trunk/modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java @@ -51,7 +51,7 @@ import org.apache.abdera.parser.ParseException; import org.apache.abdera.parser.Parser; import org.apache.abdera.writer.WriterFactory; import org.apache.commons.codec.binary.Base64; -import org.apache.tuscany.sca.binding.http.HTTPCacheContext; +import org.apache.tuscany.sca.common.http.HTTPCacheContext; import org.apache.tuscany.sca.common.http.HTTPUtil; import org.apache.tuscany.sca.data.collection.Entry; import org.apache.tuscany.sca.databinding.Mediator; diff --git a/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java b/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java index 3e8e396927..22e1cb7349 100644 --- a/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java +++ b/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java @@ -31,7 +31,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.tuscany.sca.assembly.Binding; -import org.apache.tuscany.sca.binding.http.HTTPCacheContext; +import org.apache.tuscany.sca.common.http.HTTPCacheContext; import org.apache.tuscany.sca.common.http.HTTPContentTypeMapper; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; diff --git a/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java b/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java index f2456f3175..959de1474c 100644 --- a/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java +++ b/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java @@ -23,6 +23,8 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Date; +import org.apache.tuscany.sca.common.http.HTTPCacheContext; + /** * Test service implementation that implements a various conditional HTTP * methods. For testing, the id==0 items are very old (Date(0)), not modified, diff --git a/sca-java-2.x/trunk/modules/binding-http/src/main/java/org/apache/tuscany/sca/binding/http/HTTPCacheContext.java b/sca-java-2.x/trunk/modules/binding-http/src/main/java/org/apache/tuscany/sca/binding/http/HTTPCacheContext.java deleted file mode 100644 index 372118fbf2..0000000000 --- a/sca-java-2.x/trunk/modules/binding-http/src/main/java/org/apache/tuscany/sca/binding/http/HTTPCacheContext.java +++ /dev/null @@ -1,257 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.binding.http; - -import java.text.SimpleDateFormat; -import java.util.Date; - -import javax.servlet.http.HttpServletRequest; - -/** - * A class to store cache settings for Atom and HTTP requests and responses. - * - * Predicates are statements that work in conjunction with - * ETags and LastModified dates to determine if a precondition - * or postcondition is satisfied. - * See HTTP specification for how predicates wrk: - * http://tools.ietf.org/html/rfc2616 - * Example predicates in HTTP include If-Match, If-None-Match, - * If-Modified-Since, If-Unmodified-Since, If-Range. - - */ -public class HTTPCacheContext { - public static final SimpleDateFormat RFC822DateFormat = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss Z" ); // RFC 822 date time - - public boolean enabled; - public String eTag; - public String lastModified; - public Date lastModifiedDate; - public boolean ifModifiedSince; - public boolean ifUnmodifiedSince; - public boolean ifMatch; - public boolean ifNoneMatch; - public boolean ifRange; - - /** - * An ETag is a unique ID for an item. It changes when - * a field in the item or the update date changes. - * See HTTP specification for how ETags work: - * http://tools.ietf.org/html/rfc2616 - * @return the eTag - */ - public String getETag() { - return eTag; - } - /** - * @param tag the eTag to set - */ - public void setETag(String tag) { - eTag = tag; - enabled = true; - } - /** - * The LastModified date is the time the item was last - * changed. See HTTP specification for how ETags work: - * http://tools.ietf.org/html/rfc2616 - * @return the lastModified - */ - public String getLastModified() { - return lastModified; - } - /** - * The LastModified date is the time the item was last - * changed. See HTTP specification for how ETags work: - * http://tools.ietf.org/html/rfc2616 - * @return the lastModified - */ - public Date getLastModifiedAsDate() { - return lastModifiedDate; - } - /** - * @param lastModified the lastModified to set - */ - public void setLastModified(String lastModified) throws java.text.ParseException { - this.lastModified = lastModified; - // Catch date formatting on input to help debugging. - lastModifiedDate = RFC822DateFormat.parse( lastModified ); - enabled = true; - } - - /** - * @param lastModified the lastModified to set - */ - public void setLastModified(Date updated) { - this.lastModified = RFC822DateFormat.format( updated ); - lastModifiedDate = updated; - enabled = true; - } - - /** - * @return the ifModifedSince - */ - public boolean isIfModifiedSince() { - return ifModifiedSince; - } - /** - * @param ifModifedSince the ifModifedSince to set - */ - public void setIfModifiedSince(boolean ifModifiedSince) { - this.ifModifiedSince = ifModifiedSince; - if ( ifModifiedSince ) - enabled = true; - } - /** - * @return the ifUnModifiedSince - */ - public boolean isIfUnmodifiedSince() { - return ifUnmodifiedSince; - } - /** - * @param ifUnModifiedSince the ifUnModifiedSince to set - */ - public void setIfUnmodifiedSince(boolean ifUnmodifiedSince) { - this.ifUnmodifiedSince = ifUnmodifiedSince; - if ( ifUnmodifiedSince ) - enabled = true; - } - /** - * @return the ifMatch - */ - public boolean isIfMatch() { - return ifMatch; - } - /** - * @param ifMatch the ifMatch to set - */ - public void setIfMatch(boolean ifMatch) { - this.ifMatch = ifMatch; - if ( ifMatch ) - enabled = true; - } - /** - * @return the ifNoneMatch - */ - public boolean isIfNoneMatch() { - return ifNoneMatch; - } - /** - * @param ifNoneMatch the ifNoneMatch to set - */ - public void setIfNoneMatch(boolean ifNoneMatch) { - this.ifNoneMatch = ifNoneMatch; - if ( ifNoneMatch ) - enabled = true; - } - /** - * @return the ifRange - */ - public boolean isIfRange() { - return ifRange; - } - /** - * @param ifRange the ifRange to set - */ - public void setIfRange(boolean ifRange) { - this.ifRange = ifRange; - if ( ifRange ) - enabled = true; - } - - public String toString() { - final String PREDPREFIX = ", predicates="; - StringBuffer sb = new StringBuffer(PREDPREFIX); - if ( ifMatch || ifNoneMatch || ifModifiedSince || ifUnmodifiedSince || ifRange ) { - if ( ifMatch ) { - if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); - sb.append("If-Match"); - } - if ( ifNoneMatch ) { - if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); - sb.append("If-None-Match"); - } - if ( ifModifiedSince ) { - if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); - sb.append("If-Modified-Since"); - } - if ( ifUnmodifiedSince ) { - if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); - sb.append("If-UnModified-Since"); - } - if ( ifRange ) { - if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); - sb.append("If-Range"); - } - } else { - sb.append("null"); - } - - return "eTag=" + eTag + ", lastModified=" + lastModified - + sb.toString(); - } - - /** - * Gets the cache context information (ETag, LastModified, predicates) from the Http request. - * @param request - * @return - */ - public static HTTPCacheContext getCacheContextFromRequest( HttpServletRequest request ) throws java.text.ParseException { - HTTPCacheContext context = new HTTPCacheContext(); - - String eTag = request.getHeader( "If-Match" ); - if ( eTag != null ) { - context.setETag( eTag ); - context.setIfMatch( true ); - } - eTag = request.getHeader( "If-None-Match" ); - if ( eTag != null ) { - context.setETag( eTag ); - context.setIfNoneMatch( true ); - } - String lastModifiedString = request.getHeader( "If-Modified-Since" ); - if ( lastModifiedString != null ) { - context.setLastModified( lastModifiedString ); - context.setIfModifiedSince( true ); - } - lastModifiedString = request.getHeader( "If-Unmodified-Since" ); - if ( lastModifiedString != null ) { - context.setLastModified( lastModifiedString ); - context.setIfUnmodifiedSince( true ); - } - lastModifiedString = request.getHeader( "If-Range" ); - if ( lastModifiedString != null ) { - context.setLastModified( lastModifiedString ); - context.setIfRange( true ); - } - return context; - } - /** - * Enabled is true whenever ETag, LastModified, or predicate is set. - * @return the enabled - */ - public boolean isEnabled() { - return enabled; - } - /** - * @param enabled the enabled to set - */ - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } -} diff --git a/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPCacheContext.java b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPCacheContext.java new file mode 100644 index 0000000000..6688fb8a94 --- /dev/null +++ b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPCacheContext.java @@ -0,0 +1,257 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.common.http; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import javax.servlet.http.HttpServletRequest; + +/** + * A class to store cache settings for Atom and HTTP requests and responses. + * + * Predicates are statements that work in conjunction with + * ETags and LastModified dates to determine if a precondition + * or postcondition is satisfied. + * See HTTP specification for how predicates wrk: + * http://tools.ietf.org/html/rfc2616 + * Example predicates in HTTP include If-Match, If-None-Match, + * If-Modified-Since, If-Unmodified-Since, If-Range. + + */ +public class HTTPCacheContext { + public static final SimpleDateFormat RFC822DateFormat = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss Z" ); // RFC 822 date time + + public boolean enabled; + public String eTag; + public String lastModified; + public Date lastModifiedDate; + public boolean ifModifiedSince; + public boolean ifUnmodifiedSince; + public boolean ifMatch; + public boolean ifNoneMatch; + public boolean ifRange; + + /** + * An ETag is a unique ID for an item. It changes when + * a field in the item or the update date changes. + * See HTTP specification for how ETags work: + * http://tools.ietf.org/html/rfc2616 + * @return the eTag + */ + public String getETag() { + return eTag; + } + /** + * @param tag the eTag to set + */ + public void setETag(String tag) { + eTag = tag; + enabled = true; + } + /** + * The LastModified date is the time the item was last + * changed. See HTTP specification for how ETags work: + * http://tools.ietf.org/html/rfc2616 + * @return the lastModified + */ + public String getLastModified() { + return lastModified; + } + /** + * The LastModified date is the time the item was last + * changed. See HTTP specification for how ETags work: + * http://tools.ietf.org/html/rfc2616 + * @return the lastModified + */ + public Date getLastModifiedAsDate() { + return lastModifiedDate; + } + /** + * @param lastModified the lastModified to set + */ + public void setLastModified(String lastModified) throws java.text.ParseException { + this.lastModified = lastModified; + // Catch date formatting on input to help debugging. + lastModifiedDate = RFC822DateFormat.parse( lastModified ); + enabled = true; + } + + /** + * @param lastModified the lastModified to set + */ + public void setLastModified(Date updated) { + this.lastModified = RFC822DateFormat.format( updated ); + lastModifiedDate = updated; + enabled = true; + } + + /** + * @return the ifModifedSince + */ + public boolean isIfModifiedSince() { + return ifModifiedSince; + } + /** + * @param ifModifedSince the ifModifedSince to set + */ + public void setIfModifiedSince(boolean ifModifiedSince) { + this.ifModifiedSince = ifModifiedSince; + if ( ifModifiedSince ) + enabled = true; + } + /** + * @return the ifUnModifiedSince + */ + public boolean isIfUnmodifiedSince() { + return ifUnmodifiedSince; + } + /** + * @param ifUnModifiedSince the ifUnModifiedSince to set + */ + public void setIfUnmodifiedSince(boolean ifUnmodifiedSince) { + this.ifUnmodifiedSince = ifUnmodifiedSince; + if ( ifUnmodifiedSince ) + enabled = true; + } + /** + * @return the ifMatch + */ + public boolean isIfMatch() { + return ifMatch; + } + /** + * @param ifMatch the ifMatch to set + */ + public void setIfMatch(boolean ifMatch) { + this.ifMatch = ifMatch; + if ( ifMatch ) + enabled = true; + } + /** + * @return the ifNoneMatch + */ + public boolean isIfNoneMatch() { + return ifNoneMatch; + } + /** + * @param ifNoneMatch the ifNoneMatch to set + */ + public void setIfNoneMatch(boolean ifNoneMatch) { + this.ifNoneMatch = ifNoneMatch; + if ( ifNoneMatch ) + enabled = true; + } + /** + * @return the ifRange + */ + public boolean isIfRange() { + return ifRange; + } + /** + * @param ifRange the ifRange to set + */ + public void setIfRange(boolean ifRange) { + this.ifRange = ifRange; + if ( ifRange ) + enabled = true; + } + + public String toString() { + final String PREDPREFIX = ", predicates="; + StringBuffer sb = new StringBuffer(PREDPREFIX); + if ( ifMatch || ifNoneMatch || ifModifiedSince || ifUnmodifiedSince || ifRange ) { + if ( ifMatch ) { + if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); + sb.append("If-Match"); + } + if ( ifNoneMatch ) { + if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); + sb.append("If-None-Match"); + } + if ( ifModifiedSince ) { + if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); + sb.append("If-Modified-Since"); + } + if ( ifUnmodifiedSince ) { + if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); + sb.append("If-UnModified-Since"); + } + if ( ifRange ) { + if ( sb.length() > PREDPREFIX.length() ) sb.append( ", "); + sb.append("If-Range"); + } + } else { + sb.append("null"); + } + + return "eTag=" + eTag + ", lastModified=" + lastModified + + sb.toString(); + } + + /** + * Gets the cache context information (ETag, LastModified, predicates) from the Http request. + * @param request + * @return + */ + public static HTTPCacheContext getCacheContextFromRequest( HttpServletRequest request ) throws java.text.ParseException { + HTTPCacheContext context = new HTTPCacheContext(); + + String eTag = request.getHeader( "If-Match" ); + if ( eTag != null ) { + context.setETag( eTag ); + context.setIfMatch( true ); + } + eTag = request.getHeader( "If-None-Match" ); + if ( eTag != null ) { + context.setETag( eTag ); + context.setIfNoneMatch( true ); + } + String lastModifiedString = request.getHeader( "If-Modified-Since" ); + if ( lastModifiedString != null ) { + context.setLastModified( lastModifiedString ); + context.setIfModifiedSince( true ); + } + lastModifiedString = request.getHeader( "If-Unmodified-Since" ); + if ( lastModifiedString != null ) { + context.setLastModified( lastModifiedString ); + context.setIfUnmodifiedSince( true ); + } + lastModifiedString = request.getHeader( "If-Range" ); + if ( lastModifiedString != null ) { + context.setLastModified( lastModifiedString ); + context.setIfRange( true ); + } + return context; + } + /** + * Enabled is true whenever ETag, LastModified, or predicate is set. + * @return the enabled + */ + public boolean isEnabled() { + return enabled; + } + /** + * @param enabled the enabled to set + */ + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } +} -- cgit v1.2.3