Request.java

61 lines | 1.563 kB Blame History Raw Download
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrgs.inf.prosoft.requestssimulator.requests;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 *
 * @author romulo
 */
public abstract class Request {

    private final RequestPlan requestPlan;
    private final String URL;
    private final String headers;
    private Map<String, String> storedValues;

    protected Request(RequestPlan requestPlan, String URL) {
        this.requestPlan = requestPlan;
        this.URL = URL;
        this.headers = null;
    }

    protected Request(RequestPlan requestPlan, String URL, String headers) {
        this.requestPlan = requestPlan;
        this.URL = URL;
        this.headers = headers;
    }

    public String getMethod() {
        return this.requestPlan.getMethod();
    }

    public String getURL() {
        return this.URL;
    }

    public Stream<Map.Entry<String, String>> getHeaders() {
        Map<String, String> headers = new HashMap<>();
        if (this.headers != null) {
            String[] headerPairs = this.headers.split("; ");
            for (String headerPair : headerPairs) {
                String[] pair = headerPair.split(": ");
                headers.put(pair[0], pair[1]);
            }
        }
        return headers.entrySet().stream();
    }

    public Request pickNextRequest() {
        return this.requestPlan.pickNextRequest();
    }

    public abstract void fire();
}