From 254de778a53e7afb6d661f943712d591162c87f8 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 13 Apr 2007 10:36:05 +0000 Subject: [PATCH] Added HttpGet, HttpHead, HttpPost, HttpPut, HttpTrace, and HttpOptions classes git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@528433 13f79535-47bb-0310-9956-ffa450edef68 --- .../HttpEntityEnclosingRequestBase.java | 70 +++++++++++++++ .../apache/http/client/methods/HttpGet.java | 80 +++++++++++++++++ .../apache/http/client/methods/HttpHead.java | 80 +++++++++++++++++ .../http/client/methods/HttpOptions.java | 78 +++++++++++++++++ .../apache/http/client/methods/HttpPost.java | 84 ++++++++++++++++++ .../apache/http/client/methods/HttpPut.java | 76 ++++++++++++++++ .../http/client/methods/HttpRequestBase.java | 87 +++++++++++++++++++ .../apache/http/client/methods/HttpTrace.java | 79 +++++++++++++++++ .../http/impl/conn/TestLocalServer.java | 4 +- 9 files changed, 636 insertions(+), 2 deletions(-) create mode 100644 src/java/org/apache/http/client/methods/HttpEntityEnclosingRequestBase.java create mode 100644 src/java/org/apache/http/client/methods/HttpGet.java create mode 100644 src/java/org/apache/http/client/methods/HttpHead.java create mode 100644 src/java/org/apache/http/client/methods/HttpOptions.java create mode 100644 src/java/org/apache/http/client/methods/HttpPost.java create mode 100644 src/java/org/apache/http/client/methods/HttpPut.java create mode 100644 src/java/org/apache/http/client/methods/HttpRequestBase.java create mode 100644 src/java/org/apache/http/client/methods/HttpTrace.java diff --git a/src/java/org/apache/http/client/methods/HttpEntityEnclosingRequestBase.java b/src/java/org/apache/http/client/methods/HttpEntityEnclosingRequestBase.java new file mode 100644 index 000000000..33343c0dd --- /dev/null +++ b/src/java/org/apache/http/client/methods/HttpEntityEnclosingRequestBase.java @@ -0,0 +1,70 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * 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.client.methods; + +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.HttpEntityEnclosingRequest; +import org.apache.http.protocol.HTTP; + +/** + * Basic implementation of an HTTP request that can be modified. + * + * @author Oleg Kalnichevski + * + * @version $Revision$ + * + * @since 4.0 + */ +abstract class HttpEntityEnclosingRequestBase + extends HttpRequestBase implements HttpEntityEnclosingRequest { + + private HttpEntity entity = null; + + public HttpEntityEnclosingRequestBase() { + super(); + } + + public HttpEntity getEntity() { + return this.entity; + } + + public void setEntity(final HttpEntity entity) { + this.entity = entity; + } + + public boolean expectContinue() { + Header expect = getFirstHeader(HTTP.EXPECT_DIRECTIVE); + return expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue()); + } + +} diff --git a/src/java/org/apache/http/client/methods/HttpGet.java b/src/java/org/apache/http/client/methods/HttpGet.java new file mode 100644 index 000000000..dd45a88bd --- /dev/null +++ b/src/java/org/apache/http/client/methods/HttpGet.java @@ -0,0 +1,80 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * 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.client.methods; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * HTTP GET method. + *

+ * The HTTP GET method is defined in section 9.3 of + * RFC2616: + *

+ * The GET method means retrieve whatever information (in the form of an + * entity) is identified by the Request-URI. If the Request-URI refers + * to a data-producing process, it is the produced data which shall be + * returned as the entity in the response and not the source text of the + * process, unless that text happens to be the output of the process. + *
+ *

+ *

+ * GetMethods will follow redirect requests from the http server by default. + * This behavour can be disabled by calling setFollowRedirects(false).

+ * + * @version $Revision$ + * + * @since 4.0 + */ +public class HttpGet extends HttpRequestBase { + + public final static String METHOD_NAME = "GET"; + + public HttpGet() { + super(); + } + + public HttpGet(final URI uri) { + super(); + setURI(uri); + } + + public HttpGet(final String uri) throws URISyntaxException { + super(); + setURI(new URI(uri)); + } + + public String getMethod() { + return METHOD_NAME; + } + +} diff --git a/src/java/org/apache/http/client/methods/HttpHead.java b/src/java/org/apache/http/client/methods/HttpHead.java new file mode 100644 index 000000000..20d813519 --- /dev/null +++ b/src/java/org/apache/http/client/methods/HttpHead.java @@ -0,0 +1,80 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * 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.client.methods; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * HTTP HEAD method. + *

+ * The HTTP HEAD method is defined in section 9.4 of + * RFC2616: + *

+ * The HEAD method is identical to GET except that the server MUST NOT + * return a message-body in the response. The metainformation contained + * in the HTTP headers in response to a HEAD request SHOULD be identical + * to the information sent in response to a GET request. This method can + * be used for obtaining metainformation about the entity implied by the + * request without transferring the entity-body itself. This method is + * often used for testing hypertext links for validity, accessibility, + * and recent modification. + *
+ *

+ * + * @version $Revision$ + * + * @since 4.0 + */ +public class HttpHead extends HttpRequestBase { + + public final static String METHOD_NAME = "HEAD"; + + public HttpHead() { + super(); + } + + public HttpHead(final URI uri) { + super(); + setURI(uri); + } + + public HttpHead(final String uri) throws URISyntaxException { + super(); + setURI(new URI(uri)); + } + + public String getMethod() { + return METHOD_NAME; + } + +} diff --git a/src/java/org/apache/http/client/methods/HttpOptions.java b/src/java/org/apache/http/client/methods/HttpOptions.java new file mode 100644 index 000000000..46cc3e1ea --- /dev/null +++ b/src/java/org/apache/http/client/methods/HttpOptions.java @@ -0,0 +1,78 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * 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.client.methods; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * HTTP OPTIONS method. + *

+ * The HTTP OPTIONS method is defined in section 9.2 of + * RFC2616: + *

+ * The OPTIONS method represents a request for information about the + * communication options available on the request/response chain + * identified by the Request-URI. This method allows the client to + * determine the options and/or requirements associated with a resource, + * or the capabilities of a server, without implying a resource action + * or initiating a resource retrieval. + *
+ *

+ * + * @version $Revision$ + * + * @since 4.0 + */ +public class HttpOptions extends HttpRequestBase { + + public final static String METHOD_NAME = "OPTIONS"; + + public HttpOptions() { + super(); + } + + public HttpOptions(final URI uri) { + super(); + setURI(uri); + } + + public HttpOptions(final String uri) throws URISyntaxException { + super(); + setURI(new URI(uri)); + } + + public String getMethod() { + return METHOD_NAME; + } + +} diff --git a/src/java/org/apache/http/client/methods/HttpPost.java b/src/java/org/apache/http/client/methods/HttpPost.java new file mode 100644 index 000000000..a4e64bdaf --- /dev/null +++ b/src/java/org/apache/http/client/methods/HttpPost.java @@ -0,0 +1,84 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * 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.client.methods; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * HTTP POST method. + *

+ * The HTTP POST method is defined in section 9.5 of + * RFC2616: + *

+ * The POST method is used to request that the origin server accept the entity + * enclosed in the request as a new subordinate of the resource identified by + * the Request-URI in the Request-Line. POST is designed to allow a uniform + * method to cover the following functions: + *
    + *
  • Annotation of existing resources
  • + *
  • Posting a message to a bulletin board, newsgroup, mailing list, or + * similar group of articles
  • + *
  • Providing a block of data, such as the result of submitting a form, + * to a data-handling process
  • + *
  • Extending a database through an append operation
  • + *
+ *
+ *

+ * + * @version $Revision$ + * + * @since 4.0 + */ +public class HttpPost extends HttpEntityEnclosingRequestBase { + + public final static String METHOD_NAME = "POST"; + + public HttpPost() { + super(); + } + + public HttpPost(final URI uri) { + super(); + setURI(uri); + } + + public HttpPost(final String uri) throws URISyntaxException { + super(); + setURI(new URI(uri)); + } + + public String getMethod() { + return METHOD_NAME; + } + +} diff --git a/src/java/org/apache/http/client/methods/HttpPut.java b/src/java/org/apache/http/client/methods/HttpPut.java new file mode 100644 index 000000000..d69874147 --- /dev/null +++ b/src/java/org/apache/http/client/methods/HttpPut.java @@ -0,0 +1,76 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * 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.client.methods; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * HTTP PUT method. + *

+ * The HTTP PUT method is defined in section 9.6 of + * RFC2616: + *

+ * The PUT method requests that the enclosed entity be stored under the + * supplied Request-URI. If the Request-URI refers to an already + * existing resource, the enclosed entity SHOULD be considered as a + * modified version of the one residing on the origin server. + *
+ *

+ * + * @version $Revision$ + * + * @since 4.0 + */ +public class HttpPut extends HttpEntityEnclosingRequestBase { + + public final static String METHOD_NAME = "PUT"; + + public HttpPut() { + super(); + } + + public HttpPut(final URI uri) { + super(); + setURI(uri); + } + + public HttpPut(final String uri) throws URISyntaxException { + super(); + setURI(new URI(uri)); + } + + public String getMethod() { + return METHOD_NAME; + } + +} diff --git a/src/java/org/apache/http/client/methods/HttpRequestBase.java b/src/java/org/apache/http/client/methods/HttpRequestBase.java new file mode 100644 index 000000000..0adba88fc --- /dev/null +++ b/src/java/org/apache/http/client/methods/HttpRequestBase.java @@ -0,0 +1,87 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * 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.client.methods; + +import java.net.URI; + +import org.apache.http.HttpRequest; +import org.apache.http.HttpVersion; +import org.apache.http.RequestLine; +import org.apache.http.message.AbstractHttpMessage; +import org.apache.http.message.BasicRequestLine; +import org.apache.http.params.HttpProtocolParams; + +/** + * Basic implementation of an HTTP request that can be modified. + * + * @author Oleg Kalnichevski + * + * @version $Revision$ + * + * @since 4.0 + */ +abstract class HttpRequestBase extends AbstractHttpMessage implements HttpRequest { + + private URI uri; + + public HttpRequestBase() { + super(); + } + + public abstract String getMethod(); + + public HttpVersion getHttpVersion() { + return HttpProtocolParams.getVersion(getParams()); + } + + public URI getURI() { + return this.uri; + } + + public RequestLine getRequestLine() { + String method = getMethod(); + HttpVersion ver = getHttpVersion(); + URI uri = getURI(); + String uritext; + if (uri != null) { + uritext = uri.toASCIIString(); + } else { + uritext = "/"; + } + return new BasicRequestLine(method, uritext, ver); + } + + public void setURI(final URI uri) { + this.uri = uri; + } + +} diff --git a/src/java/org/apache/http/client/methods/HttpTrace.java b/src/java/org/apache/http/client/methods/HttpTrace.java new file mode 100644 index 000000000..52198c5a7 --- /dev/null +++ b/src/java/org/apache/http/client/methods/HttpTrace.java @@ -0,0 +1,79 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * 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.client.methods; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * HTTP TRACE method. + *

+ * The HTTP TRACE method is defined in section 9.6 of + * RFC2616: + *

+ * The TRACE method is used to invoke a remote, application-layer loop- + * back of the request message. The final recipient of the request + * SHOULD reflect the message received back to the client as the + * entity-body of a 200 (OK) response. The final recipient is either the + * origin server or the first proxy or gateway to receive a Max-Forwards + * value of zero (0) in the request (see section 14.31). A TRACE request + * MUST NOT include an entity. + *
+ *

+ * + * @version $Revision$ + * + * @since 4.0 + */ +public class HttpTrace extends HttpRequestBase { + + public final static String METHOD_NAME = "TRACE"; + + public HttpTrace() { + super(); + } + + public HttpTrace(final URI uri) { + super(); + setURI(uri); + } + + public HttpTrace(final String uri) throws URISyntaxException { + super(); + setURI(new URI(uri)); + } + + public String getMethod() { + return METHOD_NAME; + } + +} diff --git a/src/test/org/apache/http/impl/conn/TestLocalServer.java b/src/test/org/apache/http/impl/conn/TestLocalServer.java index e1407081a..1167961d7 100644 --- a/src/test/org/apache/http/impl/conn/TestLocalServer.java +++ b/src/test/org/apache/http/impl/conn/TestLocalServer.java @@ -37,11 +37,11 @@ import org.apache.http.HttpClientConnection; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.localserver.LocalTestServer; import org.apache.http.localserver.ServerTestBase; -import org.apache.http.message.HttpGet; -import org.apache.http.message.HttpPost; import org.apache.http.util.EntityUtils;