bit.changepurse.wdk.http.MockedApacheResponse.java Source code

Java tutorial

Introduction

Here is the source code for bit.changepurse.wdk.http.MockedApacheResponse.java

Source

/*
 * This file is part of ChangePurse.
 *
 * Copyright (c) 2014 Germn Fuentes Capella
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package bit.changepurse.wdk.http;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpEntity;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicStatusLine;
import org.apache.http.params.HttpParams;

@SuppressWarnings("deprecation")
public class MockedApacheResponse implements CloseableHttpResponse {

    public static final String HTTP = "HTTP";

    private static final ProtocolVersion DEFAULT_PROTOCOL_VERSION = new ProtocolVersion(HTTP, 1, 1);

    private StatusLine statusLine;
    private HttpEntity httpEntity;
    private final Map<String, List<Header>> headers = new HashMap<String, List<Header>>();

    @Override
    public StatusLine getStatusLine() {
        return statusLine;
    }

    @Override
    public void setStatusLine(StatusLine aStatusline) {
        statusLine = aStatusline;
    }

    @Override
    public void setStatusLine(ProtocolVersion version, int statusCode) {
        statusLine = new BasicStatusLine(version, statusCode, "");
    }

    @Override
    public void setStatusLine(ProtocolVersion version, int statusCode, String reason) {
        statusLine = new BasicStatusLine(version, statusCode, reason);
    }

    @Override
    public void setStatusCode(int code) throws IllegalStateException {
        if (statusLine == null) {
            statusLine = new BasicStatusLine(DEFAULT_PROTOCOL_VERSION, code, "");
        } else {
            statusLine = new BasicStatusLine(statusLine.getProtocolVersion(), code, statusLine.getReasonPhrase());
        }
    }

    @Override
    public void setReasonPhrase(String reason) throws IllegalStateException {
        if (statusLine == null) {
            statusLine = new BasicStatusLine(DEFAULT_PROTOCOL_VERSION, 200, reason);
        } else {
            statusLine = new BasicStatusLine(statusLine.getProtocolVersion(), statusLine.getStatusCode(), reason);
        }
    }

    @Override
    public HttpEntity getEntity() {
        return httpEntity;
    }

    @Override
    public void setEntity(HttpEntity entity) {
        httpEntity = entity;
    }

    @Override
    public Locale getLocale() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setLocale(Locale loc) {
        // TODO Auto-generated method stub

    }

    @Override
    public ProtocolVersion getProtocolVersion() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean containsHeader(String name) {
        return headers.containsKey(name);
    }

    private List<Header> getHeaderWithPredefinedList(String name) {
        List<Header> header = headers.get(name);
        if (header == null) {
            header = new ArrayList<Header>();
            headers.put(name, header);
        }
        return header;
    }

    @Override
    public Header[] getHeaders(String name) {
        return getHeaderWithPredefinedList(name).toArray(new Header[0]);
    }

    @Override
    public Header getFirstHeader(String name) {
        List<Header> header = getHeaderWithPredefinedList(name);
        if (header.size() > 0) {
            return header.get(0);
        }
        return null;
    }

    @Override
    public Header getLastHeader(String name) {
        List<Header> header = getHeaderWithPredefinedList(name);
        if (header.size() > 0) {
            return header.get(header.size() - 1);
        }
        return null;
    }

    @Override
    public Header[] getAllHeaders() {
        List<Header> allHeaders = new ArrayList<Header>();
        for (List<Header> groupedHeaders : headers.values()) {
            allHeaders.addAll(groupedHeaders);
        }
        return allHeaders.toArray(new Header[0]);
    }

    @Override
    public void addHeader(Header header) {
        List<Header> groupedHeaders = getHeaderWithPredefinedList(header.getName());
        groupedHeaders.add(header);
    }

    @Override
    public void addHeader(String name, String value) {
        List<Header> groupedHeaders = getHeaderWithPredefinedList(name);
        groupedHeaders.add(new BasicHeader(name, value));
    }

    @Override
    public void setHeader(Header header) {
        addHeader(header);
    }

    @Override
    public void setHeader(String name, String value) {
        addHeader(name, value);
    }

    @Override
    public void setHeaders(Header[] headers) {
        if (headers != null) {
            for (Header h : headers) {
                addHeader(h);
            }
        }
    }

    @Override
    public void removeHeader(Header header) {
        List<Header> groupedHeaders = headers.get(header.getName());
        groupedHeaders.remove(header);
    }

    @Override
    public void removeHeaders(String name) {
        headers.remove(name);
    }

    @Override
    public HeaderIterator headerIterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public HeaderIterator headerIterator(String name) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public HttpParams getParams() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setParams(HttpParams params) {
        // TODO Auto-generated method stub

    }

    @Override
    public void close() throws IOException {
        // TODO Auto-generated method stub

    }

}