Request.java
Home
/
src /
main /
java /
br /
ufrgs /
inf /
prosoft /
requestssimulator /
requests /
Request.java
/*
* 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 br.ufrgs.inf.prosoft.requestssimulator.Session;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
/**
*
* @author romulo
*/
public abstract class Request {
private final RequestPlan requestPlan;
private final String URL;
private final String headers;
private final Session session;
protected Request(RequestPlan requestPlan, Session session, String URL) {
this.requestPlan = requestPlan;
this.session = session;
this.URL = URL;
this.headers = null;
}
protected Request(RequestPlan requestPlan, Session session, String URL, String headers) {
this.requestPlan = requestPlan;
this.session = session;
this.URL = URL;
this.headers = headers;
}
public String getMethod() {
return this.requestPlan.getMethod();
}
public String getURL() {
return this.URL;
}
protected String getHeaders() {
return this.headers;
}
public Stream<Map.Entry<String, String>> headers() {
Map<String, String> headers = new HashMap<>();
if (this.headers != null && !this.headers.isEmpty()) {
String[] headerPairs = this.headers.split("; ");
for (String headerPair : headerPairs) {
String[] pair = headerPair.split(": ");
if (pair.length > 1) {
headers.put(pair[0], pair[1]);
} else {
Logger.getGlobal().log(Level.SEVERE, "invalid header pair {0}", headerPair);
}
}
}
return headers.entrySet().stream();
}
public Request pickNextRequest(Session session) {
return this.requestPlan.pickNextRequest(session);
}
protected void processResponse(String response) {
if (response == null || response.isEmpty()) {
Logger.getGlobal().log(Level.SEVERE, "empty response");
return;
}
this.requestPlan.storeFields().forEach(new Consumer<String>() {
private final JsonParser jsonParser;
private final JsonObject jsonObject;
{
this.jsonParser = new JsonParser();
JsonElement jsonElement = this.jsonParser.parse(response);
if (jsonElement.isJsonObject()) {
this.jsonObject = this.jsonParser.parse(response).getAsJsonObject();
} else {
this.jsonObject = null;
}
}
@Override
public void accept(String storeField) {
String[] storeSequence = storeField.split("#");
JsonObject jsonObject = this.jsonObject;
JsonElement jsonElement = null;
for (String storeLevel : storeSequence) {
jsonElement = jsonObject.get(storeLevel);
if (jsonElement == null) {
Logger.getGlobal().log(Level.SEVERE, "field {0} not present", storeField);
return;
}
if (jsonElement.isJsonObject()) {
jsonObject = jsonElement.getAsJsonObject();
}
}
String storedValue = jsonElement.getAsString();
storeValue(storeField, storedValue);
}
});
}
protected Request storeValue(String field, String value) {
this.session.storeValue(field, value);
return this;
}
protected String fillPropertyVariable(String property) {
String filledProperty = property;
if (filledProperty.contains("#")) {
String storedField = filledProperty.substring(filledProperty.indexOf("#") + 1);
String storedValue = this.session.getStoredValue(storedField);
if (storedValue == null) {
Logger.getGlobal().log(Level.SEVERE, "variable {0} not stored", property);
} else {
filledProperty = filledProperty.replace("#" + storedField, storedValue);
}
}
return filledProperty;
}
public void fire() {
this.requestPlan.requirements().forEach(requirement -> {
boolean unfired = requirement.storeFields()
.anyMatch(field -> this.session.getStoredValue(field) == null);
if (unfired) {
requirement.build(this.session).fire();
}
});
fireRequest();
}
protected abstract void fireRequest();
public final JsonObject toJsonObject() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("reference", this.requestPlan.getReference());
jsonObject.addProperty("URL", getURL());
jsonObject.addProperty("headers", getHeaders());
if (this instanceof PostRequest) {
PostRequest postRequest = (PostRequest) this;
jsonObject.addProperty("data", postRequest.getData());
}
if (this instanceof MultipartRequest) {
MultipartRequest multipartRequest = (MultipartRequest) this;
jsonObject.addProperty("forms", multipartRequest.getForms());
}
return jsonObject;
}
}