Polishing

This commit is contained in:
Juergen Hoeller
2014-09-17 19:10:33 +02:00
parent 3a1f7b6d14
commit e819999c08
15 changed files with 172 additions and 147 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,7 +23,8 @@ import org.springframework.http.HttpHeaders;
import org.springframework.util.Assert;
/**
* Abstract base for {@link ClientHttpRequest} that makes sure that headers and body are not written multiple times.
* Abstract base for {@link ClientHttpRequest} that makes sure that headers
* and body are not written multiple times.
*
* @author Arjen Poutsma
* @since 3.0

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,13 +27,13 @@ import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
/**
* {@link ClientHttpRequestFactory} implementation that uses standard J2SE facilities.
* {@link ClientHttpRequestFactory} implementation that uses standard JDK facilities.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0
* @see java.net.HttpURLConnection
* @see CommonsClientHttpRequestFactory
* @see HttpComponentsClientHttpRequestFactory
*/
public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory {
@@ -107,14 +107,12 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory
}
/**
* Set if the underlying URLConnection can be set to 'output streaming' mode. When
* output streaming is enabled, authentication and redirection cannot be handled
* automatically. If output streaming is disabled the
* {@link HttpURLConnection#setFixedLengthStreamingMode(int)
* setFixedLengthStreamingMode} and
* {@link HttpURLConnection#setChunkedStreamingMode(int) setChunkedStreamingMode}
* methods of the underlying connection will never be called.
* <p>Default is {@code true}.
* Set if the underlying URLConnection can be set to 'output streaming' mode.
* Default is {@code true}.
* <p>When output streaming is enabled, authentication and redirection cannot be handled automatically.
* If output streaming is disabled, the {@link HttpURLConnection#setFixedLengthStreamingMode} and
* {@link HttpURLConnection#setChunkedStreamingMode} methods of the underlying connection will never
* be called.
* @param outputStreaming if output streaming is enabled
*/
public void setOutputStreaming(boolean outputStreaming) {
@@ -129,8 +127,7 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory
return new SimpleBufferingClientHttpRequest(connection, this.outputStreaming);
}
else {
return new SimpleStreamingClientHttpRequest(connection, this.chunkSize,
this.outputStreaming);
return new SimpleStreamingClientHttpRequest(connection, this.chunkSize, this.outputStreaming);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.util.StringUtils;
/**
* {@link ClientHttpResponse} implementation that uses standard J2SE facilities.
* {@link ClientHttpResponse} implementation that uses standard JDK facilities.
* Obtained via {@link SimpleBufferingClientHttpRequest#execute()} and
* {@link SimpleStreamingClientHttpRequest#execute()}.
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -95,10 +95,10 @@ final class OpaqueUriComponents extends UriComponents {
@Override
protected UriComponents expandInternal(UriTemplateVariables uriVariables) {
String expandedScheme = expandUriComponent(this.getScheme(), uriVariables);
String expandedSSp = expandUriComponent(this.ssp, uriVariables);
String expandedFragment = expandUriComponent(this.getFragment(), uriVariables);
return new OpaqueUriComponents(expandedScheme, expandedSSp, expandedFragment);
String expandedScheme = expandUriComponent(getScheme(), uriVariables);
String expandedSsp = expandUriComponent(getSchemeSpecificPart(), uriVariables);
String expandedFragment = expandUriComponent(getFragment(), uriVariables);
return new OpaqueUriComponents(expandedScheme, expandedSsp, expandedFragment);
}
@Override

View File

@@ -269,8 +269,7 @@ public abstract class UriComponents implements Serializable {
public Object getValue(String name) {
if (!this.valueIterator.hasNext()) {
throw new IllegalArgumentException(
"Not enough variable values available to expand '" + name + "'");
throw new IllegalArgumentException("Not enough variable values available to expand '" + name + "'");
}
return this.valueIterator.next();
}

View File

@@ -159,20 +159,20 @@ public class UriComponentsBuilder {
*/
public static UriComponentsBuilder fromUriString(String uri) {
Assert.hasLength(uri, "'uri' must not be empty");
Matcher m = URI_PATTERN.matcher(uri);
if (m.matches()) {
Matcher matcher = URI_PATTERN.matcher(uri);
if (matcher.matches()) {
UriComponentsBuilder builder = new UriComponentsBuilder();
String scheme = m.group(2);
String userInfo = m.group(5);
String host = m.group(6);
String port = m.group(8);
String path = m.group(9);
String query = m.group(11);
String fragment = m.group(13);
String scheme = matcher.group(2);
String userInfo = matcher.group(5);
String host = matcher.group(6);
String port = matcher.group(8);
String path = matcher.group(9);
String query = matcher.group(11);
String fragment = matcher.group(13);
boolean opaque = false;
if (StringUtils.hasLength(scheme)) {
String s = uri.substring(scheme.length());
if (!s.startsWith(":/")) {
String rest = uri.substring(scheme.length());
if (!rest.startsWith(":/")) {
opaque = true;
}
}
@@ -219,21 +219,19 @@ public class UriComponentsBuilder {
*/
public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
Assert.notNull(httpUrl, "'httpUrl' must not be null");
Matcher m = HTTP_URL_PATTERN.matcher(httpUrl);
if (m.matches()) {
Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
if (matcher.matches()) {
UriComponentsBuilder builder = new UriComponentsBuilder();
String scheme = m.group(1);
builder.scheme((scheme != null) ? scheme.toLowerCase() : scheme);
builder.userInfo(m.group(4));
builder.host(m.group(5));
String port = m.group(7);
String scheme = matcher.group(1);
builder.scheme(scheme != null ? scheme.toLowerCase() : null);
builder.userInfo(matcher.group(4));
builder.host(matcher.group(5));
String port = matcher.group(7);
if (StringUtils.hasLength(port)) {
builder.port(Integer.parseInt(port));
}
builder.path(m.group(8));
builder.query(m.group(10));
builder.path(matcher.group(8));
builder.query(matcher.group(10));
return builder;
}
else {
@@ -256,7 +254,7 @@ public class UriComponentsBuilder {
* Build a {@code UriComponents} instance from the various components
* contained in this builder.
* @param encoded whether all the components set in this builder are
* encoded ({@code true}) or not ({@code false}).
* encoded ({@code true}) or not ({@code false})
* @return the URI components
*/
public UriComponents build(boolean encoded) {
@@ -457,13 +455,12 @@ public class UriComponentsBuilder {
*/
public UriComponentsBuilder query(String query) {
if (query != null) {
Matcher m = QUERY_PARAM_PATTERN.matcher(query);
while (m.find()) {
String name = m.group(1);
String eq = m.group(2);
String value = m.group(3);
queryParam(name, (value != null ? value :
(StringUtils.hasLength(eq) ? "" : null)));
Matcher matcher = QUERY_PARAM_PATTERN.matcher(query);
while (matcher.find()) {
String name = matcher.group(1);
String eq = matcher.group(2);
String value = matcher.group(3);
queryParam(name, (value != null ? value : (StringUtils.hasLength(eq) ? "" : null)));
}
}
else {
@@ -498,7 +495,7 @@ public class UriComponentsBuilder {
Assert.notNull(name, "'name' must not be null");
if (!ObjectUtils.isEmpty(values)) {
for (Object value : values) {
String valueAsString = value != null ? value.toString() : null;
String valueAsString = (value != null ? value.toString() : null);
this.queryParams.add(name, valueAsString);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -88,17 +88,17 @@ public class UriTemplate implements Serializable {
* UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
* Map&lt;String, String&gt; uriVariables = new HashMap&lt;String, String&gt;();
* uriVariables.put("booking", "42");
* uriVariables.put("hotel", "1");
* uriVariables.put("hotel", "Rest & Relax");
* System.out.println(template.expand(uriVariables));
* </pre>
* will print: <blockquote>{@code http://example.com/hotels/1/bookings/42}</blockquote>
* will print: <blockquote>{@code http://example.com/hotels/Rest%20%26%20Relax/bookings/42}</blockquote>
* @param uriVariables the map of URI variables
* @return the expanded URI
* @throws IllegalArgumentException if {@code uriVariables} is {@code null};
* or if it does not contain values for all the variable names
*/
public URI expand(Map<String, ?> uriVariables) {
UriComponents expandedComponents = uriComponents.expand(uriVariables);
UriComponents expandedComponents = this.uriComponents.expand(uriVariables);
UriComponents encodedComponents = expandedComponents.encode();
return encodedComponents.toUri();
}
@@ -109,16 +109,16 @@ public class UriTemplate implements Serializable {
* <p>Example:
* <pre class="code">
* UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
* System.out.println(template.expand("1", "42));
* System.out.println(template.expand("Rest & Relax", "42));
* </pre>
* will print: <blockquote>{@code http://example.com/hotels/1/bookings/42}</blockquote>
* will print: <blockquote>{@code http://example.com/hotels/Rest%20%26%20Relax/bookings/42}</blockquote>
* @param uriVariableValues the array of URI variables
* @return the expanded URI
* @throws IllegalArgumentException if {@code uriVariables} is {@code null}
* or if it does not contain sufficient variables
*/
public URI expand(Object... uriVariableValues) {
UriComponents expandedComponents = uriComponents.expand(uriVariableValues);
UriComponents expandedComponents = this.uriComponents.expand(uriVariableValues);
UriComponents encodedComponents = expandedComponents.encode();
return encodedComponents.toUri();
}
@@ -201,11 +201,11 @@ public class UriTemplate implements Serializable {
private Parser(String uriTemplate) {
Assert.hasText(uriTemplate, "'uriTemplate' must not be null");
Matcher m = NAMES_PATTERN.matcher(uriTemplate);
Matcher matcher = NAMES_PATTERN.matcher(uriTemplate);
int end = 0;
while (m.find()) {
this.patternBuilder.append(quote(uriTemplate, end, m.start()));
String match = m.group(1);
while (matcher.find()) {
this.patternBuilder.append(quote(uriTemplate, end, matcher.start()));
String match = matcher.group(1);
int colonIdx = match.indexOf(':');
if (colonIdx == -1) {
this.patternBuilder.append(DEFAULT_VARIABLE_PATTERN);
@@ -223,7 +223,7 @@ public class UriTemplate implements Serializable {
String variableName = match.substring(0, colonIdx);
this.variableNames.add(variableName);
}
end = m.end();
end = matcher.end();
}
this.patternBuilder.append(quote(uriTemplate, end, uriTemplate.length()));
int lastIdx = this.patternBuilder.length() - 1;