Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.github.kristofa.test.http.file;
 
 import java.io.File;
 
Is able to write a com.github.kristofa.test.http.HttpRequest to file. A written request can be read in again with HttpRequestFileReaderImpl.

Author(s):
kristof
See also:
HttpRequestFileReaderImpl
class HttpRequestFileWriterImpl implements HttpRequestFileWriter {

    
    @Override
    public void write(final HttpRequest requestfinal File httpRequestFilefinal File httpRequestEntityFile) {
        try {
            writeRequest(requesthttpRequestFile);
            writeRequestEntity(requesthttpRequestEntityFile);
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }
    private void writeRequest(final HttpRequest requestfinal File requestFilethrows IOException {
        final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(requestFile), "UTF-8"));
        try {
            writer.write("[Method]");
            writer.newLine();
            writer.write(request.getMethod().toString());
            writer.newLine();
            writer.write("[HttpMessageHeader]");
            writer.newLine();
            for (final HttpMessageHeader header : request.getHttpMessageHeaders()) {
                writer.write(header.getName() + "=" + header.getValue());
                writer.newLine();
            }
            writer.write("[Path]");
            writer.newLine();
            writer.write(request.getPath());
            writer.newLine();
            writer.write("[QueryParameters]");
            writer.newLine();
            for (final QueryParameter parameter : request.getQueryParameters()) {
                writer.write(parameter.getKey() + "=" + parameter.getValue());
                writer.newLine();
            }
        } finally {
            writer.close();
        }
    }
    private void writeRequestEntity(final HttpRequest requestfinal File requestEntityFilethrows IOException {
        if (request.getContent() != null) {
            FileUtils.writeByteArrayToFile(requestEntityFilerequest.getContent());
        }
    }
New to GrepCode? Check out our FAQ X