Initialize singleton cookie specs lazily

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1623733 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-09-09 10:06:14 +00:00
parent 4478df7a70
commit d568ebdbf3
12 changed files with 366 additions and 40 deletions

View File

@ -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() {

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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.<CookieSpecProvider>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();
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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;
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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;
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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;
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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;
}
}

View File

@ -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.<CookieSpecProvider>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();
}