class SynchronousTransformer extends BatchRefineTransformer implements
        SyncTransformer {

    @Override
    public Entity transform(HttpRequestEntity entity) throws IOException {
        logMessage(entity.getRequest());

        final HttpRequestEntity request = cast(entity);

        final ImmutablePair<MimeType, Properties> options = exporterOptions(request);
        options.right.putAll(transformerConfig);
        final File input = downloadInput(entity);
        final File output = File.createTempFile("reply", "tmp");

        final ITransformEngine engine = getEngine();

        return new WritingEntity() {
            @Override
            public void writeData(OutputStream out) throws IOException {
                try {
                    // Can't allow more than one transform at a time as OpenRefine is not
                    // designed for that.
                    synchronized (SynchronousTransformer.this) {
                        engine.transform(input.toURI(), fetchTransform(request), output.toURI(),
                                options.right);
                    }

                    try (FileInputStream stream = new FileInputStream(output)) {
                        IOUtils.copy(stream, out);
                    }
                } finally {
                    input.delete();
                    output.delete();
                }
            }

            @Override
            public MimeType getType() {
                return options.left;
            }
        };
    }

}

class SynchronousTransformerTest extends BatchRefineTransformer {

    private static final int REFINE_PORT;

    @Test
    public void testTransform() throws Exception {

        File reference = findAndCopy("outputs/" + fInput + "_" + fTransform
                + "." + fFormat);
        Response response = doRequest(fInput, fTransform, fFormat,
                mapContentType(fFormat));
        File output = EngineTestUtils.outputFile();
        try (FileOutputStream oStream = new FileOutputStream(output)) {
            IOUtils.copy(response.asInputStream(), oStream);
        }
        assertEquals(reference, output, mapContentType(fFormat));
    }

}