All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.kristofa.test.http.HttpResponseFileWriterImpl Maven / Gradle / Ivy

Go to download

Mock and Proxy HTTP Server for testing purposes. Forked from https://github.com/jharlap/mock-http-server

There is a newer version: 4.1
Show newest version
package com.github.kristofa.test.http;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.commons.io.FileUtils;

class HttpResponseFileWriterImpl implements HttpResponseFileWriter {

    /**
     * {@inheritDoc}
     */
    @Override
    public void write(final HttpResponse response, final File httpResponseFile, final File httpResponseEntityFile) {
        try {
            writeResponse(response, httpResponseFile);
            writeResponseEntity(response, httpResponseEntityFile);
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }

    private void writeResponse(final HttpResponse httpResponse, final File httpResponseFile) throws IOException {
        final BufferedWriter writer =
            new BufferedWriter(new OutputStreamWriter(new FileOutputStream(httpResponseFile), "UTF-8"));
        try {
            writer.write("[HttpCode]");
            writer.newLine();
            writer.write(String.valueOf(httpResponse.getHttpCode()));
            writer.newLine();

            writer.write("[ContentType]");
            writer.newLine();
            writer.write(httpResponse.getContentType());
            writer.newLine();
        } finally {
            writer.close();
        }
    }

    private void writeResponseEntity(final HttpResponse httpResponse, final File httpResponseEntityFile) throws IOException {
        if (httpResponse.getContent() != null) {
            FileUtils.writeByteArrayToFile(httpResponseEntityFile, httpResponse.getContent());
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy