com.tinspx.util.net.ConfigHttpURLConnection.java Source code

Java tutorial

Introduction

Here is the source code for com.tinspx.util.net.ConfigHttpURLConnection.java

Source

/* Copyright (C) 2013-2014 Ian Teune <ian.teune@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tinspx.util.net;

import com.google.common.base.Strings;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownServiceException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import static org.junit.Assert.*;

/**
 *
 * @author Ian
 */
@Accessors(fluent = true)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class ConfigHttpURLConnection extends HttpURLConnection {

    @Getter
    volatile boolean disconnected;
    @Setter
    IOException connectThrow;
    @Setter
    IOException outThrow;
    @Setter
    OutputStream out;
    @Setter
    IOException inThrow;
    @Setter
    InputStream in;
    @Setter
    InputStream error;
    @Setter
    boolean fixedLongThrow;
    @Setter
    Headers headers;
    int code = -1;
    String message;
    boolean thrown;

    public ConfigHttpURLConnection(URL url) {
        super(url);
    }

    public ConfigHttpURLConnection with(int code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    @Override
    public boolean usingProxy() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void disconnect() {
        disconnected = true;
    }

    public void assertFixedInt(int len) {
        assertEquals(len, fixedContentLength);
    }

    public void assertFixedLong(long len) {
        assertEquals(len, fixedContentLengthLong);
    }

    public void assertChunked(int len) {
        assertEquals(len, chunkLength);
    }

    public boolean connected() {
        return connected;
    }

    @Override
    public void setFixedLengthStreamingMode(long contentLength) {
        if (fixedLongThrow) {
            throw new UnsupportedOperationException();
        }
        super.setFixedLengthStreamingMode(contentLength);
    }

    @Override
    public void connect() throws IOException {
        if (connectThrow != null) {
            throw connectThrow;
        }
        connected = true;
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        if (!getDoOutput()) {
            throw new IOException("doOutput is false");
        }
        connect();
        if (outThrow != null) {
            throw outThrow;
        }
        if (out == null) {
            throw new UnknownServiceException();
        }
        return out;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        if (!getDoInput()) {
            throw new IOException("doInput is false");
        }
        connect();
        if (inThrow != null) {
            throw inThrow;
        }
        if (in == null) {
            throw new UnknownServiceException();
        }
        return in;
    }

    @Override
    public InputStream getErrorStream() {
        return error;
    }

    @Override
    public String getHeaderField(int n) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String getHeaderFieldKey(int n) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Map<String, List<String>> getHeaderFields() {
        if (headers != null) {
            return Collections.unmodifiableMap(headers.asMap());
        } else {
            return Collections.emptyMap();
        }
    }

    @Override
    public String getHeaderField(String name) {
        return headers != null ? Strings.emptyToNull(headers.first(name)) : null;
    }

    @Override
    public String getResponseMessage() throws IOException {
        getInputStream();
        if (!thrown && code >= 400 && code < 600) {
            thrown = true;
            throw new IOException("http error " + code);
        }
        return message;
    }

    @Override
    public int getResponseCode() throws IOException {
        getInputStream();
        if (!thrown && code >= 400 && code < 600) {
            thrown = true;
            throw new IOException("http error " + code);
        }
        return code;
    }
}