diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/IgnoreSpecFactory.java b/httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/IgnoreSpecFactory.java similarity index 95% rename from httpclient/src/main/java/org/apache/http/impl/cookie/IgnoreSpecFactory.java rename to httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/IgnoreSpecFactory.java index ba7397e3d..91052c1ff 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/IgnoreSpecFactory.java +++ b/httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/IgnoreSpecFactory.java @@ -37,10 +37,12 @@ import org.apache.http.protocol.HttpContext; /** * {@link CookieSpecProvider} implementation that ignores all cookies. * + * @deprecated (4.4) Use {@link org.apache.http.impl.cookie.IgnoreSpecProvider}. + * * @since 4.1 */ @Immutable -@SuppressWarnings("deprecation") +@Deprecated public class IgnoreSpecFactory implements CookieSpecFactory, CookieSpecProvider { public IgnoreSpecFactory() { diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpecFactory.java b/httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/NetscapeDraftSpecFactory.java similarity index 96% rename from httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpecFactory.java rename to httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/NetscapeDraftSpecFactory.java index a5f4a0c50..e5e72d82d 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpecFactory.java +++ b/httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/NetscapeDraftSpecFactory.java @@ -42,10 +42,12 @@ import org.apache.http.protocol.HttpContext; * {@link org.apache.http.impl.cookie.NetscapeDraftSpec}. The instance returned by this factory * can be shared by multiple threads. * + * @deprecated (4.4) Use {@link org.apache.http.impl.cookie.NetscapeDraftSpecProvider}. + * * @since 4.0 */ @Immutable -@SuppressWarnings("deprecation") +@Deprecated public class NetscapeDraftSpecFactory implements CookieSpecFactory, CookieSpecProvider { private final CookieSpec cookieSpec; diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecFactory.java b/httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/RFC2109SpecFactory.java similarity index 96% rename from httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecFactory.java rename to httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/RFC2109SpecFactory.java index 511911cf6..278e24000 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecFactory.java +++ b/httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/RFC2109SpecFactory.java @@ -42,10 +42,12 @@ import org.apache.http.protocol.HttpContext; * {@link org.apache.http.impl.cookie.RFC2109Spec}. The instance returned by this factory * can be shared by multiple threads. * + * @deprecated (4.4) Use {@link org.apache.http.impl.cookie.RFC2109SpecProvider}. + * * @since 4.0 */ @Immutable -@SuppressWarnings("deprecation") +@Deprecated public class RFC2109SpecFactory implements CookieSpecFactory, CookieSpecProvider { private final CookieSpec cookieSpec; diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecFactory.java b/httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/RFC2965SpecFactory.java similarity index 96% rename from httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecFactory.java rename to httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/RFC2965SpecFactory.java index 80f85d121..95ba80142 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecFactory.java +++ b/httpclient/src/main/java-deprecated/org/apache/http/impl/cookie/RFC2965SpecFactory.java @@ -42,10 +42,12 @@ import org.apache.http.protocol.HttpContext; * {@link org.apache.http.impl.cookie.RFC2965Spec}. The instance returned by this factory can * be shared by multiple threads. * + * @deprecated (4.4) Use {@link org.apache.http.impl.cookie.RFC2965SpecProvider}. + * * @since 4.0 */ @Immutable -@SuppressWarnings("deprecation") +@Deprecated public class RFC2965SpecFactory implements CookieSpecFactory, CookieSpecProvider { private final CookieSpec cookieSpec; diff --git a/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java b/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java index 182be034e..33b05b95e 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java @@ -98,9 +98,9 @@ import org.apache.http.impl.conn.DefaultSchemePortResolver; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.impl.conn.SystemDefaultRoutePlanner; import org.apache.http.impl.cookie.DefaultCookieSpecProvider; -import org.apache.http.impl.cookie.IgnoreSpecFactory; -import org.apache.http.impl.cookie.NetscapeDraftSpecFactory; -import org.apache.http.impl.cookie.RFC2965SpecFactory; +import org.apache.http.impl.cookie.IgnoreSpecProvider; +import org.apache.http.impl.cookie.NetscapeDraftSpecProvider; +import org.apache.http.impl.cookie.RFC2965SpecProvider; import org.apache.http.impl.execchain.BackoffStrategyExec; import org.apache.http.impl.execchain.ClientExecChain; import org.apache.http.impl.execchain.MainClientExec; @@ -1048,9 +1048,9 @@ public class HttpClientBuilder { if (cookieSpecRegistryCopy == null) { cookieSpecRegistryCopy = RegistryBuilder.create() .register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider()) - .register(CookieSpecs.STANDARD, new RFC2965SpecFactory()) - .register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecFactory()) - .register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecFactory()) + .register(CookieSpecs.STANDARD, new RFC2965SpecProvider()) + .register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider()) + .register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider()) .build(); } diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpec.java b/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpec.java index a11d2f62f..2dea1df6e 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpec.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpec.java @@ -52,19 +52,22 @@ import org.apache.http.util.CharArrayBuffer; @ThreadSafe public class DefaultCookieSpec implements CookieSpec { - public enum CompatibilityLevel { - DEFAULT, - IE_MEDIUM_SECURITY - } - private final RFC2965Spec strict; private final RFC2109Spec obsoleteStrict; private final NetscapeDraftSpec netscapeDraft; + DefaultCookieSpec( + final RFC2965Spec strict, + final RFC2109Spec obsoleteStrict, + final NetscapeDraftSpec netscapeDraft) { + this.strict = strict; + this.obsoleteStrict = obsoleteStrict; + this.netscapeDraft = netscapeDraft; + } + public DefaultCookieSpec( final String[] datepatterns, - final boolean oneHeader, - final CompatibilityLevel compatibilityLevel) { + final boolean oneHeader) { super(); this.strict = new RFC2965Spec(oneHeader, new RFC2965VersionAttributeHandler(), @@ -85,25 +88,13 @@ public class DefaultCookieSpec implements CookieSpec { new BasicCommentHandler()); this.netscapeDraft = new NetscapeDraftSpec( new BasicDomainHandler(), - compatibilityLevel == CompatibilityLevel.IE_MEDIUM_SECURITY ? - new BasicPathHandler() { - @Override - public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException { - // No validation - } - } : new BasicPathHandler(), + new BasicPathHandler(), new BasicSecureHandler(), new BasicCommentHandler(), new BasicExpiresHandler( datepatterns != null ? datepatterns.clone() : new String[]{NetscapeDraftSpec.EXPIRES_PATTERN})); } - public DefaultCookieSpec( - final String[] datepatterns, - final boolean oneHeader) { - this(datepatterns, oneHeader, null); - } - public DefaultCookieSpec() { this(null, false); } diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpecProvider.java b/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpecProvider.java index d3bff3434..a56fb58e5 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpecProvider.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpecProvider.java @@ -28,8 +28,11 @@ package org.apache.http.impl.cookie; import org.apache.http.annotation.Immutable; +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.CookieOrigin; import org.apache.http.cookie.CookieSpec; import org.apache.http.cookie.CookieSpecProvider; +import org.apache.http.cookie.MalformedCookieException; import org.apache.http.protocol.HttpContext; /** @@ -42,19 +45,72 @@ import org.apache.http.protocol.HttpContext; @Immutable public class DefaultCookieSpecProvider implements CookieSpecProvider { - private final CookieSpec cookieSpec; + public enum CompatibilityLevel { + DEFAULT, + IE_MEDIUM_SECURITY + } - public DefaultCookieSpecProvider(final String[] datepatterns, final boolean oneHeader) { + private final CompatibilityLevel compatibilityLevel; + private final String[] datepatterns; + private final boolean oneHeader; + + private volatile CookieSpec cookieSpec; + + public DefaultCookieSpecProvider( + final CompatibilityLevel compatibilityLevel, + final String[] datepatterns, + final boolean oneHeader) { super(); - this.cookieSpec = new DefaultCookieSpec(datepatterns, oneHeader);; + this.compatibilityLevel = compatibilityLevel != null ? compatibilityLevel : CompatibilityLevel.DEFAULT; + this.datepatterns = datepatterns; + this.oneHeader = oneHeader; } public DefaultCookieSpecProvider() { - this(null, false); + this(CompatibilityLevel.DEFAULT, null, false); } @Override public CookieSpec create(final HttpContext context) { + if (cookieSpec == null) { + synchronized (this) { + if (cookieSpec == null) { + final RFC2965Spec strict = new RFC2965Spec(this.oneHeader, + new RFC2965VersionAttributeHandler(), + new BasicPathHandler(), + new RFC2965DomainAttributeHandler(), + new RFC2965PortAttributeHandler(), + new BasicMaxAgeHandler(), + new BasicSecureHandler(), + new BasicCommentHandler(), + new RFC2965CommentUrlAttributeHandler(), + new RFC2965DiscardAttributeHandler()); + final RFC2109Spec obsoleteStrict = new RFC2109Spec(this.oneHeader, + new RFC2109VersionHandler(), + new BasicPathHandler(), + new RFC2109DomainHandler(), + new BasicMaxAgeHandler(), + new BasicSecureHandler(), + new BasicCommentHandler()); + final NetscapeDraftSpec netscapeDraft = new NetscapeDraftSpec( + new BasicDomainHandler(), + this.compatibilityLevel == CompatibilityLevel.IE_MEDIUM_SECURITY ? + new BasicPathHandler() { + @Override + public void validate( + final Cookie cookie, + final CookieOrigin origin) throws MalformedCookieException { + // No validation + } + } : new BasicPathHandler(), + new BasicSecureHandler(), + new BasicCommentHandler(), + new BasicExpiresHandler(this.datepatterns != null ? this.datepatterns.clone() : + new String[]{NetscapeDraftSpec.EXPIRES_PATTERN})); + this.cookieSpec = new DefaultCookieSpec(strict, obsoleteStrict, netscapeDraft); + } + } + } return this.cookieSpec; } diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/IgnoreSpecProvider.java b/httpclient/src/main/java/org/apache/http/impl/cookie/IgnoreSpecProvider.java new file mode 100644 index 000000000..97bedf6d1 --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/IgnoreSpecProvider.java @@ -0,0 +1,61 @@ +/* + * ==================================================================== + * 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.impl.cookie; + +import org.apache.http.annotation.Immutable; +import org.apache.http.cookie.CookieSpec; +import org.apache.http.cookie.CookieSpecProvider; +import org.apache.http.protocol.HttpContext; + +/** + * {@link org.apache.http.cookie.CookieSpecProvider} implementation that ignores all cookies. + * + * @since 4.4 + */ +@Immutable +public class IgnoreSpecProvider implements CookieSpecProvider { + + private volatile CookieSpec cookieSpec; + + public IgnoreSpecProvider() { + super(); + } + + @Override + public CookieSpec create(final HttpContext context) { + if (cookieSpec == null) { + synchronized (this) { + if (cookieSpec == null) { + this.cookieSpec = new IgnoreSpec(); + } + } + } + return this.cookieSpec; + } + +} diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpecProvider.java b/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpecProvider.java new file mode 100644 index 000000000..5dcd34e8f --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpecProvider.java @@ -0,0 +1,70 @@ +/* + * ==================================================================== + * 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.impl.cookie; + +import org.apache.http.annotation.Immutable; +import org.apache.http.cookie.CookieSpec; +import org.apache.http.cookie.CookieSpecProvider; +import org.apache.http.protocol.HttpContext; + +/** + * {@link org.apache.http.cookie.CookieSpecProvider} implementation that provides an instance of + * {@link NetscapeDraftSpec}. The instance returned by this factory + * can be shared by multiple threads. + * + * @since 4.4 + */ +@Immutable +public class NetscapeDraftSpecProvider implements CookieSpecProvider { + + private final String[] datepatterns; + + private volatile CookieSpec cookieSpec; + + public NetscapeDraftSpecProvider(final String[] datepatterns) { + super(); + this.datepatterns = datepatterns; + } + + public NetscapeDraftSpecProvider() { + this(null); + } + + @Override + public CookieSpec create(final HttpContext context) { + if (cookieSpec == null) { + synchronized (this) { + if (cookieSpec == null) { + this.cookieSpec = new NetscapeDraftSpec(this.datepatterns); + } + } + } + return this.cookieSpec; + } + +} diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecProvider.java b/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecProvider.java new file mode 100644 index 000000000..7429c430a --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecProvider.java @@ -0,0 +1,70 @@ +/* + * ==================================================================== + * 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.impl.cookie; + +import org.apache.http.annotation.Immutable; +import org.apache.http.cookie.CookieSpec; +import org.apache.http.cookie.CookieSpecProvider; +import org.apache.http.protocol.HttpContext; + +/** + * {@link org.apache.http.cookie.CookieSpecProvider} implementation that provides an instance of + * {@link RFC2109Spec}. The instance returned by this factory + * can be shared by multiple threads. + * + * @since 4.4 + */ +@Immutable +public class RFC2109SpecProvider implements CookieSpecProvider { + + private final boolean oneHeader; + + private volatile CookieSpec cookieSpec; + + public RFC2109SpecProvider(final boolean oneHeader) { + super(); + this.oneHeader = oneHeader; + } + + public RFC2109SpecProvider() { + this(false); + } + + @Override + public CookieSpec create(final HttpContext context) { + if (cookieSpec == null) { + synchronized (this) { + if (cookieSpec == null) { + this.cookieSpec = new RFC2109Spec(null, this.oneHeader); + } + } + } + return this.cookieSpec; + } + +} diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecProvider.java b/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecProvider.java new file mode 100644 index 000000000..62f595593 --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecProvider.java @@ -0,0 +1,70 @@ +/* + * ==================================================================== + * 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.impl.cookie; + +import org.apache.http.annotation.Immutable; +import org.apache.http.cookie.CookieSpec; +import org.apache.http.cookie.CookieSpecProvider; +import org.apache.http.protocol.HttpContext; + +/** + * {@link org.apache.http.cookie.CookieSpecProvider} implementation that provides an instance of + * {@link RFC2965Spec}. The instance returned by this factory can + * be shared by multiple threads. + * + * @since 4.4 + */ +@Immutable +public class RFC2965SpecProvider implements CookieSpecProvider { + + private final boolean oneHeader; + + private volatile CookieSpec cookieSpec; + + public RFC2965SpecProvider(final boolean oneHeader) { + super(); + this.oneHeader = oneHeader; + } + + public RFC2965SpecProvider() { + this(false); + } + + @Override + public CookieSpec create(final HttpContext context) { + if (cookieSpec == null) { + synchronized (this) { + if (cookieSpec == null) { + this.cookieSpec = new RFC2965Spec(null, this.oneHeader); + } + } + } + return this.cookieSpec; + } + +} diff --git a/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAddCookies.java b/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAddCookies.java index 24d4fe4c7..9bd8ece44 100644 --- a/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAddCookies.java +++ b/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAddCookies.java @@ -48,10 +48,10 @@ import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.cookie.BasicClientCookie; import org.apache.http.impl.cookie.BasicClientCookie2; import org.apache.http.impl.cookie.DefaultCookieSpecProvider; -import org.apache.http.impl.cookie.IgnoreSpecFactory; +import org.apache.http.impl.cookie.IgnoreSpecProvider; import org.apache.http.impl.cookie.NetscapeDraftSpec; -import org.apache.http.impl.cookie.NetscapeDraftSpecFactory; -import org.apache.http.impl.cookie.RFC2965SpecFactory; +import org.apache.http.impl.cookie.NetscapeDraftSpecProvider; +import org.apache.http.impl.cookie.RFC2965SpecProvider; import org.apache.http.message.BasicHttpRequest; import org.apache.http.protocol.HttpCoreContext; import org.junit.Assert; @@ -81,9 +81,9 @@ public class TestRequestAddCookies { this.cookieSpecRegistry = RegistryBuilder.create() .register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider()) - .register(CookieSpecs.STANDARD, new RFC2965SpecFactory()) - .register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecFactory()) - .register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecFactory()) + .register(CookieSpecs.STANDARD, new RFC2965SpecProvider()) + .register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider()) + .register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider()) .build(); }