You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/08/22 21:29:34 UTC
svn commit: r1160386 - in /httpcomponents/httpclient/trunk: ./ fluent-hc/
fluent-hc/src/main/java/org/apache/http/client/fluent/
fluent-hc/src/main/java/org/apache/http/client/fluent/header/
fluent-hc/src/test/ fluent-hc/src/test/java/ fluent-hc/src/te...
Author: olegk
Date: Mon Aug 22 19:29:33 2011
New Revision: 1160386
URL: http://svn.apache.org/viewvc?rev=1160386&view=rev
Log:
HTTPCLIENT-1076: fluent facade API for HttpClient developed by Xu Lilu for the Google summer of code 2011: exception handling improvements; some test coverage
Contributed by Xu Lilu <cookieme at gmail.com>
Added:
httpcomponents/httpclient/trunk/fluent-hc/src/test/
httpcomponents/httpclient/trunk/fluent-hc/src/test/java/
httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/
httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/
httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/
httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/
httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/
httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluentRequest.java (with props)
Removed:
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentHttp.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/RequestBuilder.java
Modified:
httpcomponents/httpclient/trunk/fluent-hc/pom.xml
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentExecutor.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentHttpMethod.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentRequest.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentResponse.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/CacheControl.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/ContentType.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/DateUtils.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/HttpHeader.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/UriBuilder.java
httpcomponents/httpclient/trunk/pom.xml
Modified: httpcomponents/httpclient/trunk/fluent-hc/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/pom.xml?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/pom.xml (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/pom.xml Mon Aug 22 19:29:33 2011
@@ -33,7 +33,7 @@
<version>4.2-alpha1-SNAPSHOT</version>
</parent>
<artifactId>fluent-hc</artifactId>
- <name>HttpClient fluent</name>
+ <name>Fluent HttpClient</name>
<description>
HttpComponents Client fluent API
</description>
@@ -48,6 +48,13 @@
<scope>compile</scope>
</dependency>
<dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ <classifier>tests</classifier>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentExecutor.java?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentExecutor.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentExecutor.java Mon Aug 22 19:29:33 2011
@@ -15,8 +15,13 @@
* 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.client.fluent;
@@ -55,9 +60,9 @@ public class FluentExecutor {
public FluentResponse exec(FluentRequest req)
throws ClientProtocolException, IOException {
DefaultHttpClient client = getClient();
- client.setCredentialsProvider(req.credentialsProvider);
- client.setParams(req.localParams);
- HttpResponse resp = client.execute(req, req.localContext);
+ client.setCredentialsProvider(req.getCredentialsProvider());
+ client.setParams(req.getLocalParams());
+ HttpResponse resp = client.execute(req, req.getLocalContext());
FluentResponse fresp = new FluentResponse(resp);
return fresp;
}
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentHttpMethod.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentHttpMethod.java?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentHttpMethod.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentHttpMethod.java Mon Aug 22 19:29:33 2011
@@ -15,8 +15,13 @@
* 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.client.fluent;
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentRequest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentRequest.java?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentRequest.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentRequest.java Mon Aug 22 19:29:33 2011
@@ -15,8 +15,13 @@
* 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.client.fluent;
@@ -24,6 +29,7 @@ package org.apache.http.client.fluent;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -71,126 +77,68 @@ import org.apache.http.protocol.BasicHtt
import org.apache.http.protocol.HttpContext;
public class FluentRequest implements HttpUriRequest {
- static FluentRequest build(final URI uri, final FluentHttpMethod method) {
- FluentRequest req = new FluentRequest();
- req.by(method, uri);
- req.init();
- return req;
- }
-
- HttpParams localParams;
- HttpContext localContext;
- CredentialsProvider credentialsProvider;
+ private HttpParams localParams;
+ private HttpContext localContext;
+ private CredentialsProvider credentialsProvider;
private HttpUriRequest request;
private FluentHttpMethod method;
private HttpHost localProxy;
- protected static final Log log = LogFactory.getLog(FluentRequest.class);
- private FluentRequest() {
- // DO NOTHING
- }
+ protected static final Log log = LogFactory.getLog(FluentRequest.class);
public FluentRequest(final HttpUriRequest req) {
- this.request = req;
- String methodName = request.getMethod().toUpperCase();
+ URI uri = req.getURI();
+ String methodName = req.getMethod().toUpperCase();
+ FluentHttpMethod method = FluentHttpMethod.GET_METHOD;
if (methodName.equals("GET"))
- this.method = FluentHttpMethod.GET_METHOD;
+ method = FluentHttpMethod.GET_METHOD;
else if (methodName.equals("POST"))
- this.method = FluentHttpMethod.POST_METHOD;
+ method = FluentHttpMethod.POST_METHOD;
else if (methodName.equals("OPTIONS"))
- this.method = FluentHttpMethod.OPTIONS_METHOD;
+ method = FluentHttpMethod.OPTIONS_METHOD;
else if (methodName.equals("DELETE"))
- this.method = FluentHttpMethod.DELETE_METHOD;
+ method = FluentHttpMethod.DELETE_METHOD;
else if (methodName.equals("HEAD"))
- this.method = FluentHttpMethod.HEAD_METHOD;
+ method = FluentHttpMethod.HEAD_METHOD;
else if (methodName.equals("PUT"))
- this.method = FluentHttpMethod.PUT_METHOD;
+ method = FluentHttpMethod.PUT_METHOD;
else if (methodName.equals("TRACE"))
- this.method = FluentHttpMethod.TRACE_METHOD;
- else
- this.method = FluentHttpMethod.GET_METHOD;
- init();
+ method = FluentHttpMethod.TRACE_METHOD;
+ init(uri, method);
}
public FluentRequest(final String uri) {
- copyFrom(RequestBuilder.build(uri));
+ init(uri, FluentHttpMethod.GET_METHOD);
}
public FluentRequest(final String uri, final FluentHttpMethod method) {
- copyFrom(RequestBuilder.build(uri, method));
+ init(uri, method);
}
public FluentRequest(final URI uri) {
- copyFrom(RequestBuilder.build(uri));
+ init(uri, FluentHttpMethod.GET_METHOD);
}
public FluentRequest(final URI uri, final FluentHttpMethod method) {
- copyFrom(RequestBuilder.build(uri, method));
+ init(uri, method);
}
public void abort() throws UnsupportedOperationException {
this.request.abort();
}
-
public void addHeader(final Header header) {
this.request.addHeader(header);
}
-
public void addHeader(final String name, final String value) {
this.request.addHeader(name, value);
}
- /**
- * Change the HTTP method used within this request.
- *
- * @param method
- * which indicates the HTTP method need to use
- * @return modified request
- */
- private FluentRequest by(final FluentHttpMethod method, final URI uri) {
- switch (method) {
- case GET_METHOD:
- this.request = new HttpGet(uri);
- break;
- case POST_METHOD:
- this.request = new HttpPost(uri);
- break;
- case OPTIONS_METHOD:
- this.request = new HttpOptions(uri);
- break;
- case DELETE_METHOD:
- this.request = new HttpDelete(uri);
- break;
- case HEAD_METHOD:
- this.request = new HttpHead(uri);
- break;
- case PUT_METHOD:
- this.request = new HttpPut(uri);
- break;
- case TRACE_METHOD:
- this.request = new HttpTrace(uri);
- break;
- }
- this.method = method;
- return this;
- }
-
-
public boolean containsHeader(final String name) {
return this.request.containsHeader(name);
}
- private void copyFrom(FluentRequest other) {
- this.request = other.request;
- this.method = other.method;
- this.localContext = other.localContext;
- this.localParams = other.localParams;
- this.localProxy = other.localProxy;
- this.credentialsProvider = other.credentialsProvider;
- }
-
/**
*
* @return a <code>FluentResponse</code> instance referring to the response
@@ -203,7 +151,6 @@ public class FluentRequest implements Ht
return new FluentResponse(client.execute(request));
}
-
public Header[] getAllHeaders() {
return this.request.getAllHeaders();
}
@@ -235,7 +182,7 @@ public class FluentRequest implements Ht
return getValueOfHeader(HttpHeader.CONTENT_TYPE);
}
- public CredentialsProvider getCredentialProvider() {
+ public CredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}
@@ -248,12 +195,10 @@ public class FluentRequest implements Ht
.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET);
}
-
public Header getFirstHeader(final String name) {
return this.request.getFirstHeader(name);
}
-
public Header[] getHeaders(final String name) {
return this.request.getHeaders(name);
}
@@ -270,6 +215,11 @@ public class FluentRequest implements Ht
return method;
}
+ public HttpVersion getHttpVersion() {
+ return (HttpVersion) this.localParams
+ .getParameter(CoreProtocolPNames.PROTOCOL_VERSION);
+ }
+
public String getIfModifiedSince() {
return getValueOfHeader(HttpHeader.IF_MODIFIED_SINCE);
}
@@ -278,27 +228,30 @@ public class FluentRequest implements Ht
return getValueOfHeader(HttpHeader.IF_UNMODIFIED_SINCE);
}
-
public Header getLastHeader(final String name) {
return this.request.getLastHeader(name);
}
+ public HttpContext getLocalContext() {
+ return localContext;
+ }
+
+ public HttpParams getLocalParams() {
+ return localParams;
+ }
public String getMethod() {
return this.request.getMethod();
}
-
public HttpParams getParams() {
return this.request.getParams();
}
-
public ProtocolVersion getProtocolVersion() {
return this.request.getProtocolVersion();
}
-
public RequestLine getRequestLine() {
return this.request.getRequestLine();
}
@@ -307,21 +260,10 @@ public class FluentRequest implements Ht
return HttpConnectionParams.getSoTimeout(localParams);
}
- public boolean isStrictTransferEncoding() {
- return (Boolean) localParams
- .getParameter(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
- }
-
-
public URI getURI() {
return this.request.getURI();
}
- public boolean isUseExpectContinue() {
- return (Boolean) localParams
- .getParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE);
- }
-
public String getUserAgent() {
return (String) localParams.getParameter(CoreProtocolPNames.USER_AGENT);
}
@@ -339,38 +281,76 @@ public class FluentRequest implements Ht
.getParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE);
}
-
public HeaderIterator headerIterator() {
return this.request.headerIterator();
}
-
public HeaderIterator headerIterator(final String name) {
return this.request.headerIterator(name);
}
- private void init() {
+ private void init(final String uriString, final FluentHttpMethod method) {
+ try {
+ URI uri = new URI(uriString);
+ init(uri, method);
+ } catch (URISyntaxException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ private void init(final URI uri, final FluentHttpMethod method) {
+ switch (method) {
+ case GET_METHOD:
+ this.request = new HttpGet(uri);
+ break;
+ case POST_METHOD:
+ this.request = new HttpPost(uri);
+ break;
+ case OPTIONS_METHOD:
+ this.request = new HttpOptions(uri);
+ break;
+ case DELETE_METHOD:
+ this.request = new HttpDelete(uri);
+ break;
+ case HEAD_METHOD:
+ this.request = new HttpHead(uri);
+ break;
+ case PUT_METHOD:
+ this.request = new HttpPut(uri);
+ break;
+ case TRACE_METHOD:
+ this.request = new HttpTrace(uri);
+ break;
+ }
+ this.method = method;
localParams = request.getParams();
localContext = new BasicHttpContext();
credentialsProvider = new BasicCredentialsProvider();
localProxy = null;
}
-
public boolean isAborted() {
return this.request.isAborted();
}
+ public boolean isStrictTransferEncoding() {
+ return (Boolean) localParams
+ .getParameter(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
+ }
+
+ public boolean isUseExpectContinue() {
+ return (Boolean) localParams
+ .getParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE);
+ }
+
public FluentRequest removeAuth() {
return setAuth(null);
}
-
public void removeHeader(final Header header) {
this.request.removeHeader(header);
}
-
public void removeHeaders(final String name) {
this.request.removeHeaders(name);
}
@@ -450,13 +430,28 @@ public class FluentRequest implements Ht
}
public FluentRequest setEntity(final HttpEntity entity) {
- log.warn("");
- this.by(FluentHttpMethod.POST_METHOD, this.request.getURI());
- HttpPost post = (HttpPost) this.request;
- post.setEntity(entity);
+ if (method == FluentHttpMethod.POST_METHOD) {
+ HttpPost post = (HttpPost) this.request;
+ post.setEntity(entity);
+ } else {
+ throw new IllegalStateException(
+ "Only POST method can have an entity.");
+ }
return this;
}
+ public void setHeader(final Header header) {
+ this.request.setHeader(header);
+ }
+
+ public void setHeader(final String name, final String value) {
+ this.request.setHeader(name, value);
+ }
+
+ public void setHeaders(final Header[] headers) {
+ this.request.setHeaders(headers);
+ }
+
public FluentRequest setHTMLFormEntity(final Map<String, String> form,
final String encoding) throws UnsupportedEncodingException {
List<NameValuePair> formparams = new ArrayList<NameValuePair>(
@@ -469,19 +464,9 @@ public class FluentRequest implements Ht
return setEntity(entity);
}
-
- public void setHeader(final Header header) {
- this.request.setHeader(header);
- }
-
-
- public void setHeader(final String name, final String value) {
- this.request.setHeader(name, value);
- }
-
-
- public void setHeaders(final Header[] headers) {
- this.request.setHeaders(headers);
+ public FluentRequest setHttpVersion(HttpVersion version) {
+ localParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, version);
+ return this;
}
public FluentRequest setIfModifiedSince(final Date date) {
@@ -496,16 +481,10 @@ public class FluentRequest implements Ht
return this;
}
-
public void setParams(final HttpParams params) {
this.request.setParams(params);
}
- public FluentRequest setProtocolVersion(HttpVersion version) {
- localParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, version);
- return this;
- }
-
public FluentRequest setProxy(final String proxyAddr, final int proxyPort) {
return setProxy(proxyAddr, proxyPort, null, null);
}
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentResponse.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentResponse.java?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentResponse.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/FluentResponse.java Mon Aug 22 19:29:33 2011
@@ -15,8 +15,13 @@
* 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.client.fluent;
@@ -24,8 +29,6 @@ package org.apache.http.client.fluent;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
-import java.util.Scanner;
-import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -39,10 +42,6 @@ import org.apache.http.client.fluent.hea
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
-
public class FluentResponse implements HttpResponse {
protected static final Log log = LogFactory.getLog(FluentResponse.class);
private HttpResponse response;
@@ -55,21 +54,6 @@ public class FluentResponse implements H
consumed = false;
}
- public int getStatusCode() {
- return this.getStatusLine().getStatusCode();
- }
-
- public FluentResponse loadContent() throws IOException {
- if (getEntity() == null)
- content = null;
- else {
- content = EntityUtils.toByteArray(getEntity());
- EntityUtils.consume(getEntity());
- }
- consumed = true;
- return this;
- }
-
public void addHeader(Header header) {
this.response.addHeader(header);
}
@@ -107,19 +91,6 @@ public class FluentResponse implements H
}
public String getContentCharset() {
- // if (this.getEntity() == null)
- // throw new IllegalStateException("Response does not contain data");
- // Header contentType = this.getEntity().getContentType();
- // if (contentType == null)
- // throw new IllegalStateException(
- // "Reponse does not contain Content-Type header");
- // NameValuePair charset = contentType.getElements()[0]
- // .getParameterByName("charset");
- // if (charset == null || charset.getValue().trim().equals("")) {
- // log.warn("Charset could not be found in response");
- // return Charset.defaultCharset().name();
- // } else
- // return charset.getValue();
return EntityUtils.getContentCharSet(getEntity());
}
@@ -203,6 +174,10 @@ public class FluentResponse implements H
return this.response.getProtocolVersion();
}
+ public int getStatusCode() {
+ return this.getStatusLine().getStatusCode();
+ }
+
public StatusLine getStatusLine() {
return this.response.getStatusLine();
}
@@ -223,6 +198,17 @@ public class FluentResponse implements H
return this.response.headerIterator(name);
}
+ public FluentResponse loadContent() throws IOException {
+ if (getEntity() == null)
+ content = null;
+ else {
+ content = EntityUtils.toByteArray(getEntity());
+ EntityUtils.consume(getEntity());
+ }
+ consumed = true;
+ return this;
+ }
+
public void removeHeader(Header header) {
this.response.removeHeader(header);
}
@@ -274,49 +260,4 @@ public class FluentResponse implements H
public void setStatusLine(StatusLine statusline) {
this.response.setStatusLine(statusline);
}
-
- public FluentResponse assertStatus(int expected) {
- assertNotNull(this.getStatusLine().toString(), this.getStatusLine());
- int actual = this.getStatusCode();
- assertEquals(this + ": expecting status " + expected, expected, actual);
- return this;
- }
-
- public FluentResponse assertContentType(String expected) {
- try {
- String actual = this.getContentType();
- assertEquals(this + ": expecting content type " + expected,
- expected, actual);
- } catch (Exception e) {
- fail(this + ": " + e.getMessage());
- }
- return this;
- }
-
- public FluentResponse assertContentRegexp(String encoding, String... regexp) {
- try {
- String content = encoding == null ? getContentString()
- : getContentString(encoding);
- assertNotNull(this.toString(), content);
- nextPattern: for (String expr : regexp) {
- final Pattern p = Pattern.compile(".*" + expr + ".*");
- final Scanner scan = new Scanner(content);
- while (scan.hasNext()) {
- final String line = scan.nextLine();
- if (p.matcher(line).matches()) {
- continue nextPattern;
- }
- }
- fail(this + ": no match for regexp '" + expr + "', content=\n"
- + content);
- }
- } catch (IOException e) {
- fail(this + ":ã" + e.getMessage());
- }
- return this;
- }
-
- public FluentResponse assertContentRegexp(String... regexp) {
- return assertContentRegexp(null, regexp);
- }
}
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/CacheControl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/CacheControl.java?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/CacheControl.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/CacheControl.java Mon Aug 22 19:29:33 2011
@@ -15,8 +15,13 @@
* 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.client.fluent.header;
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/ContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/ContentType.java?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/ContentType.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/ContentType.java Mon Aug 22 19:29:33 2011
@@ -15,8 +15,13 @@
* 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.client.fluent.header;
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/DateUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/DateUtils.java?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/DateUtils.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/DateUtils.java Mon Aug 22 19:29:33 2011
@@ -15,8 +15,13 @@
* 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.client.fluent.header;
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/HttpHeader.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/HttpHeader.java?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/HttpHeader.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/header/HttpHeader.java Mon Aug 22 19:29:33 2011
@@ -15,8 +15,13 @@
* 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.client.fluent.header;
Added: httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluentRequest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluentRequest.java?rev=1160386&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluentRequest.java (added)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluentRequest.java Mon Aug 22 19:29:33 2011
@@ -0,0 +1,392 @@
+/*
+ * ====================================================================
+ *
+ * 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.client.fluent;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.net.SocketTimeoutException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Date;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpVersion;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.fluent.header.ContentType;
+import org.apache.http.client.fluent.header.DateUtils;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.localserver.LocalTestServer;
+import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestFluentRequest {
+ private static class SimpleService implements HttpRequestHandler {
+
+ public SimpleService() {
+ super();
+ }
+
+ public void handle(final HttpRequest request,
+ final HttpResponse response, final HttpContext context)
+ throws HttpException, IOException {
+ response.setStatusCode(HttpStatus.SC_OK);
+ StringEntity entity = new StringEntity("Whatever");
+ response.setEntity(entity);
+ }
+ }
+
+ private LocalTestServer localServer;
+
+ private URI getLocalServerURI() {
+ int hostPort = localServer.getServiceAddress().getPort();
+ String hostAddr = localServer.getServiceAddress().getAddress()
+ .getHostAddress();
+ URI uri;
+ try {
+ uri = new URI("http", null, hostAddr, hostPort, null, null, null);
+ return uri;
+ } catch (URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ localServer = new LocalTestServer(null, null);
+ localServer.registerDefaultHandlers();
+ localServer.start();
+ localServer.register("*", new SimpleService());
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ if (localServer != null)
+ localServer.stop();
+ }
+
+ @Test
+ public void testCacheControl() {
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ String cacheControl = "no-cache";
+ req.setCacheControl(cacheControl);
+ assertEquals(cacheControl, req.getCacheControl());
+ assertEquals(req.getFirstHeader("Cache-Control").getValue(),
+ req.getCacheControl());
+ }
+
+ @Test
+ public void testConnectionTimeout() throws Exception {
+ // TODO how to delay the response from the localServer?
+ int timeout = 1;
+ URI uri = getLocalServerURI();
+ FluentRequest req = new FluentRequest(uri);
+ req.setConnectionTimeout(timeout);
+ assertEquals(timeout, req.getConnectionTimeout());
+ try {
+ req.exec();
+ // TODO: Delay local server's response
+ // fail("ConnectTimeoutException exception is expected.");
+ } catch (Exception e) {
+ if (!(e instanceof ConnectTimeoutException)) {
+ throw e;
+ }
+ }
+ }
+
+ @Test
+ public void testContentCharset() {
+ URI uri = getLocalServerURI();
+ FluentRequest req = new FluentRequest(uri);
+ String charset = "UTF-8";
+ req.setContentCharset(charset);
+ assertEquals(charset, req.getContentCharset());
+ assertEquals(
+ req.getLocalParams().getParameter(
+ CoreProtocolPNames.HTTP_CONTENT_CHARSET),
+ req.getContentCharset());
+ }
+
+ @Test
+ public void testContentLength() {
+ int contentLength = 1000;
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setContentLength(contentLength);
+ assertEquals(contentLength, req.getContentLength());
+ }
+
+ @Test
+ public void testContentType() {
+ String contentType = ContentType.HTML;
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setContentType(contentType);
+ assertEquals(contentType, req.getContentType());
+ }
+
+ @Test
+ public void testDate() {
+ Date date = new Date();
+ String dateValue = DateUtils.format(date);
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setDate(date);
+ assertEquals(dateValue, req.getDate());
+ }
+
+ @Test
+ public void testElementCharset() {
+ String charset = "UTF-8";
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setElementCharset(charset);
+ assertEquals(charset, req.getElementCharset());
+ }
+
+ @Test
+ public void testExec() throws ClientProtocolException, IOException,
+ URISyntaxException {
+ URI uri = getLocalServerURI();
+ FluentRequest req = new FluentRequest(uri);
+ FluentResponse resp = req.exec();
+ assertEquals(HttpStatus.SC_OK, resp.getStatusCode());
+ }
+
+ @Test
+ public void testFluentRequestHttpUriRequest() {
+ String uriString = "http://www.apache.org/";
+ HttpUriRequest httpRequest = new HttpGet(uriString);
+ FluentRequest req = new FluentRequest(httpRequest);
+ assertEquals(uriString, req.getURI().toASCIIString());
+ assertEquals("GET", req.getMethod().toUpperCase());
+ }
+
+ @Test
+ public void testFluentRequestString() {
+ String uriString = "http://www.apache.org/";
+ FluentRequest req = new FluentRequest(uriString);
+ assertEquals(uriString, req.getURI().toASCIIString());
+ assertEquals("GET", req.getMethod().toUpperCase());
+ }
+
+ @Test
+ public void testFluentRequestStringFluentHttpMethod() {
+ String uriString = "http://www.apache.org/";
+ FluentRequest req = new FluentRequest(uriString,
+ FluentHttpMethod.POST_METHOD);
+ assertEquals(uriString, req.getURI().toASCIIString());
+ assertEquals("POST", req.getMethod().toUpperCase());
+ }
+
+ @Test
+ public void testFluentRequestURI() throws URISyntaxException {
+ String uriString = "http://www.apache.org/";
+ URI uri = new URI(uriString);
+ FluentRequest req = new FluentRequest(uri);
+ assertEquals(req.getURI(), uri);
+ assertEquals("GET", req.getMethod().toUpperCase());
+ }
+
+ @Test
+ public void testFluentRequestURIFluentHttpMethod()
+ throws URISyntaxException {
+ String uriString = "http://www.apache.org/";
+ URI uri = new URI(uriString);
+ FluentRequest req = new FluentRequest(uri, FluentHttpMethod.HEAD_METHOD);
+ assertEquals(req.getURI(), uri);
+ assertEquals("HEAD", req.getMethod().toUpperCase());
+ }
+
+ @Test
+ public void testGetHttpMethod() {
+ FluentHttpMethod method = FluentHttpMethod.POST_METHOD;
+ FluentRequest req = new FluentRequest("http://www.apache.org/", method);
+ assertEquals(method, req.getHttpMethod());
+ }
+
+ @Test
+ public void testGetURI() {
+ URI uri = getLocalServerURI();
+ FluentRequest req = new FluentRequest(uri);
+ assertEquals(uri, req.getURI());
+ }
+
+ @Test
+ public void testHttpVersion() {
+ HttpVersion procVersion = HttpVersion.HTTP_1_1;
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setHttpVersion(procVersion);
+ assertEquals(procVersion, req.getHttpVersion());
+ }
+
+ @Test
+ public void testIfModifiedSince() {
+ Date date = new Date();
+ String dateValue = DateUtils.format(date);
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setIfModifiedSince(date);
+ assertEquals(dateValue, req.getIfModifiedSince());
+ }
+
+ @Test
+ public void testIfUnmodifiedSince() {
+ Date date = new Date();
+ String dateValue = DateUtils.format(date);
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setIfUnmodifiedSince(date);
+ assertEquals(dateValue, req.getIfUnmodifiedSince());
+ }
+
+ @Test
+ public void testIsUseExpectContinue() {
+ boolean ueCont = true;
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setUseExpectContinue(ueCont);
+ assertEquals(ueCont, req.isUseExpectContinue());
+ }
+
+ @Test
+ public void testRemoveAuth() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testRemoveProxy() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetAuthCredentials() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetAuthStringString() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetAuthStringStringStringString() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetCredentialProvider() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetEntity() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetHTMLFormEntity() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetParams() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetProxyAuthCredentials() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetProxyAuthStringString() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetProxyAuthStringStringStringString() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetProxyStringInt() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSetProxyStringIntStringString() {
+ // fail("Not yet implemented");
+ }
+
+ @Test
+ public void testSocketTimeout() throws Exception {
+ // TODO how to delay the response from the localServer?
+ int timeout = 1;
+ URI uri = getLocalServerURI();
+ FluentRequest req = new FluentRequest(uri);
+ req.setSocketTimeout(timeout);
+ assertEquals(timeout, req.getSocketTimeout());
+ try {
+ req.exec();
+ // TODO: Delay local server's response
+ // fail("SocketTimeoutException exception is expected.");
+ } catch (Exception e) {
+ if (!(e instanceof SocketTimeoutException)) {
+ throw e;
+ }
+ }
+ }
+
+ @Test
+ public void testStrictTransferEncoding() {
+ boolean stEnc = true;
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setStrictTransferEncoding(stEnc);
+ assertEquals(stEnc, req.isStrictTransferEncoding());
+ }
+
+ @Test
+ public void testUserAgent() {
+ String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setUserAgent(userAgent);
+ assertEquals(userAgent, req.getUserAgent());
+ }
+
+ @Test
+ public void testWaitForContinue() {
+ int wait = 1000;
+ FluentRequest req = new FluentRequest("http://www.apache.org/");
+ req.setWaitForContinue(wait);
+ assertEquals(wait, req.getWaitForContinue());
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluentRequest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluentRequest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluentRequest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/UriBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/UriBuilder.java?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/UriBuilder.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/UriBuilder.java Mon Aug 22 19:29:33 2011
@@ -15,8 +15,13 @@
* 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.client.utils;
@@ -91,7 +96,8 @@ public class UriBuilder {
if (uri != null)
return uri;
else
- throw new IllegalStateException("Not enough information to build URI");
+ throw new IllegalStateException(
+ "Not enough information to build URI");
}
private void digestURI(URI uri, boolean raw) {
@@ -199,9 +205,8 @@ public class UriBuilder {
return URLDecoder.decode(string, enc);
}
} catch (UnsupportedEncodingException e) {
- e.printStackTrace();
+ throw new IllegalStateException(e);
}
- return string;
}
/**
@@ -337,19 +342,6 @@ public class UriBuilder {
try {
uri = new URI(scheme, userInfo, host, port, path, query,
fragment);
-
- // StringBuffer sb = new StringBuffer();
- // sb.append(scheme).append("://");
- // if(userInfo != null)
- // sb.append(userInfo).append("@");
- // sb.append(host);
- // if(path != null)
- // sb.append(path);
- // if(query != null)
- // sb.append('?').append(query);
- // if(fragment != null)
- // sb.append('#').append(fragment);
- // uri = new URI(sb.toString());
digestURI(uri, false);
} catch (URISyntaxException e) {
// roll back
Modified: httpcomponents/httpclient/trunk/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/pom.xml?rev=1160386&r1=1160385&r2=1160386&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/pom.xml (original)
+++ httpcomponents/httpclient/trunk/pom.xml Mon Aug 22 19:29:33 2011
@@ -91,7 +91,7 @@
</snapshots>
</repository>
</repositories>
-
+
<dependencyManagement>
<dependencies>
<dependency>
@@ -155,6 +155,7 @@
<module>httpclient</module>
<module>httpmime</module>
<module>httpclient-cache</module>
+ <module>fluent-hc</module>
<module>httpclient-osgi</module>
</modules>