home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (RequestWrapper.java)

This example source code file (RequestWrapper.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java tags/keywords

abstracthttpmessage, basicrequestline, httprequest, httpurirequest, illegalargumentexception, method, net, network, notthreadsafe, protocolexception, protocolversion, requestline, requestwrapper, string, unsupportedoperationexception, uri

The RequestWrapper.java example source code

 * ====================================================================
 * 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
 * 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.client;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.annotation.NotThreadSafe;

import org.apache.http.HttpRequest;
import org.apache.http.ProtocolException;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicRequestLine;
import org.apache.http.params.HttpProtocolParams;

 * A wrapper class for {@link HttpRequest}s that can be used to change
 * properties of the current request without modifying the original
 * object.
 * </p>
 * This class is also capable of resetting the request headers to
 * the state of the original request.
 * @since 4.0
public class RequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
    private final HttpRequest original;

    private URI uri;
    private String method;
    private ProtocolVersion version;
    private int execCount;
    public RequestWrapper(final HttpRequest request) throws ProtocolException {
        if (request == null) {
            throw new IllegalArgumentException("HTTP request may not be null");
        this.original = request;
        // Make a copy of the original URI 
        if (request instanceof HttpUriRequest) {
            this.uri = ((HttpUriRequest) request).getURI();
            this.method = ((HttpUriRequest) request).getMethod();
            this.version = null;
        } else {
            RequestLine requestLine = request.getRequestLine();
            try {
                this.uri = new URI(requestLine.getUri());
            } catch (URISyntaxException ex) {
                throw new ProtocolException("Invalid request URI: " 
                        + requestLine.getUri(), ex);
            this.method = requestLine.getMethod();
            this.version = request.getProtocolVersion();
        this.execCount = 0;

    public void resetHeaders() {
        // Make a copy of original headers
    public String getMethod() {
        return this.method;

    public void setMethod(final String method) {
        if (method == null) {
            throw new IllegalArgumentException("Method name may not be null");
        this.method = method;

    public ProtocolVersion getProtocolVersion() {
        if (this.version == null) {
            this.version = HttpProtocolParams.getVersion(getParams());
        return this.version;

    public void setProtocolVersion(final ProtocolVersion version) {
        this.version = version;

    public URI getURI() {
        return this.uri;
    public void setURI(final URI uri) {
        this.uri = uri;

    public RequestLine getRequestLine() {
        String method = getMethod();
        ProtocolVersion ver = getProtocolVersion();
        String uritext = null;
        if (uri != null) {
            uritext = uri.toASCIIString();
        if (uritext == null || uritext.length() == 0) {
            uritext = "/";
        return new BasicRequestLine(method, uritext, ver);

    public void abort() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();

    public boolean isAborted() {
        return false;

    public HttpRequest getOriginal() {
        return this.original;
    public boolean isRepeatable() {
        return true;

    public int getExecCount() {
        return this.execCount;
    public void incrementExecCount() {

Other Java examples (source code examples)

Here is a short list of links related to this Java RequestWrapper.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.