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

no.sb1.troxy.util.RecordingFileHandler Maven / Gradle / Ivy

Go to download

Troxy is a proxy intended for simulating a remote service without actually connecting to the server. Incoming requests are matched using regular expressions and responses are created from a recorded response from the remote service and optionally modified by inserting values caught from the regular expression used to match the request.

There is a newer version: 3.2.49
Show newest version
package no.sb1.troxy.util;

import no.sb1.troxy.record.v2.Recording;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.XMLDecoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Utility class for handling reading, parsing and writing recording files.

 * @deprecated Kept for supporting old recording file format, will be removed in the future
 */
@Deprecated
public class RecordingFileHandler {
    /**
     * Logger for this class.
     */
    private static final Logger log = LoggerFactory.getLogger(RecordingFileHandler.class);

    /**
     * Load recording.
     * @param filePath File path to be loaded.
     * @return Loaded recording.
     * @throws FileNotFoundException If the file can't be found.
     */
    public static Recording loadRecording(String filePath) throws FileNotFoundException {
        Recording recording;
        final Path path = Paths.get(filePath);
        Object deserializedFile = deserializeFile(path.toFile());
        try {
            recording = (Recording) deserializedFile;
        } catch (ClassCastException e) {
            log.info("Unable to load file as a Troxy recording (format 2)");
            return null;
        }
        recording.setFilename(path.getFileName().toString());
        if (recording.getDelayStrategy() == null)
            recording.setDelayStrategy(Recording.DelayStrategy.NONE);
        recording.getDelayValues().putIfAbsent(Recording.DelayValueKeys.MIN, 0L);
        recording.getDelayValues().putIfAbsent(Recording.DelayValueKeys.MEAN, 0L);
        recording.getDelayValues().putIfAbsent(Recording.DelayValueKeys.MAX, 0L);
        return recording;
    }

    /**
     * Deserialize file to an Object.
     * @param file File to deserialize.
     * @return File deserialized to an Object.
     * @throws FileNotFoundException If the given file can't be found.
     */
    private static Object deserializeFile(File file) throws FileNotFoundException {
        try (XMLDecoder decoder = new XMLDecoder(new FileInputStream(file))) {
            return decoder.readObject();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy