| 1 | package de.ugoe.cs.eventbench.web.data;
|
|---|
| 2 |
|
|---|
| 3 | import java.util.ArrayList;
|
|---|
| 4 | import java.util.List;
|
|---|
| 5 |
|
|---|
| 6 | import de.ugoe.cs.eventbench.data.IReplayable;
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * <p>
|
|---|
| 10 | * Contains all information related to a web request, i.e., the path, the POST
|
|---|
| 11 | * variables and the GET variables. The generated replay are for the command
|
|---|
| 12 | * line tool {@code curl}. The requests do not contain correct values for the
|
|---|
| 13 | * POST and GET request. Instead, only the parameters that are part of the
|
|---|
| 14 | * requests are added and the values of the parameters are
|
|---|
| 15 | * DATA_$PARAMNAME$_DATA, where $PARAMNAME$ is the upper case string of the
|
|---|
| 16 | * parameter name. This allows test data generators to insert concrete values,
|
|---|
| 17 | * as EventBench does not include a test data generator for web software.
|
|---|
| 18 | * </p>
|
|---|
| 19 | *
|
|---|
| 20 | * @author Steffen Herbold
|
|---|
| 21 | * @version 1.0
|
|---|
| 22 | */
|
|---|
| 23 | public class WebRequest implements IReplayable {
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * <p>
|
|---|
| 27 | * Id for object serialization.
|
|---|
| 28 | * </p>
|
|---|
| 29 | */
|
|---|
| 30 | private static final long serialVersionUID = 1L;
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * <p>
|
|---|
| 34 | * POST variables of the web request.
|
|---|
| 35 | * </p>
|
|---|
| 36 | */
|
|---|
| 37 | List<String> postVars;
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * <p>
|
|---|
| 41 | * GET variables of the web request.
|
|---|
| 42 | * </p>
|
|---|
| 43 | */
|
|---|
| 44 | List<String> getVars;
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * <p>
|
|---|
| 48 | * URI of the web request.
|
|---|
| 49 | * </p>
|
|---|
| 50 | */
|
|---|
| 51 | String targetUri;
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * <p>
|
|---|
| 55 | * URL of the server.
|
|---|
| 56 | * </p>
|
|---|
| 57 | */
|
|---|
| 58 | String serverUrl;
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * <p>
|
|---|
| 62 | * Constructor. Creates a new WebRequest.
|
|---|
| 63 | * </p>
|
|---|
| 64 | *
|
|---|
| 65 | * @param uri
|
|---|
| 66 | * URI of the request
|
|---|
| 67 | * @param postVars
|
|---|
| 68 | * POST variables of the request
|
|---|
| 69 | * @param getVars
|
|---|
| 70 | * GET variables of the request
|
|---|
| 71 | */
|
|---|
| 72 | public WebRequest(String url, String uri, List<String> postVars,
|
|---|
| 73 | List<String> getVars) {
|
|---|
| 74 | serverUrl = url;
|
|---|
| 75 | targetUri = uri;
|
|---|
| 76 | this.postVars = new ArrayList<String>(postVars); // defensive copy
|
|---|
| 77 | this.getVars = new ArrayList<String>(getVars);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /*
|
|---|
| 81 | * (non-Javadoc)
|
|---|
| 82 | *
|
|---|
| 83 | * @see de.ugoe.cs.eventbench.data.IReplayable#getReplay()
|
|---|
| 84 | */
|
|---|
| 85 | @Override
|
|---|
| 86 | public String getReplay() {
|
|---|
| 87 | StringBuilder builder = new StringBuilder();
|
|---|
| 88 | builder.append("curl");
|
|---|
| 89 | if (!postVars.isEmpty()) {
|
|---|
| 90 | boolean isFirstPost = true;
|
|---|
| 91 | for (String postVar : postVars) {
|
|---|
| 92 | if (isFirstPost) {
|
|---|
| 93 | builder.append(" --data \"");
|
|---|
| 94 | isFirstPost = false;
|
|---|
| 95 | } else {
|
|---|
| 96 | builder.append('&');
|
|---|
| 97 | }
|
|---|
| 98 | builder.append(postVar + "=DATA_" + postVar.toUpperCase()
|
|---|
| 99 | + "_DATA");
|
|---|
| 100 | }
|
|---|
| 101 | builder.append('\"');
|
|---|
| 102 | }
|
|---|
| 103 | builder.append(' ');
|
|---|
| 104 | if (serverUrl != null) {
|
|---|
| 105 | builder.append(serverUrl);
|
|---|
| 106 | }
|
|---|
| 107 | builder.append(targetUri);
|
|---|
| 108 | if (!getVars.isEmpty()) {
|
|---|
| 109 | boolean isFirstGet = true;
|
|---|
| 110 | for (String getVar : getVars) {
|
|---|
| 111 | if (isFirstGet) {
|
|---|
| 112 | builder.append('?');
|
|---|
| 113 | isFirstGet = false;
|
|---|
| 114 | } else {
|
|---|
| 115 | builder.append('&');
|
|---|
| 116 | }
|
|---|
| 117 | builder.append(getVar + "=DATA_" + getVar.toUpperCase()
|
|---|
| 118 | + "_DATA");
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | return builder.toString();
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /*
|
|---|
| 125 | * (non-Javadoc)
|
|---|
| 126 | *
|
|---|
| 127 | * @see de.ugoe.cs.eventbench.data.IReplayable#getTarget()
|
|---|
| 128 | */
|
|---|
| 129 | @Override
|
|---|
| 130 | public String getTarget() {
|
|---|
| 131 | return targetUri;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * <p>
|
|---|
| 136 | * Two {@link WebRequest}s are equal, if their {@link #targetUri},
|
|---|
| 137 | * {@link #postVars}, and {@link #getVars} are equal.
|
|---|
| 138 | * </p>
|
|---|
| 139 | *
|
|---|
| 140 | * @see java.lang.Object#equals(java.lang.Object)
|
|---|
| 141 | */
|
|---|
| 142 | @Override
|
|---|
| 143 | public boolean equals(Object other) {
|
|---|
| 144 | if (this == other) {
|
|---|
| 145 | return true;
|
|---|
| 146 | }
|
|---|
| 147 | if (other instanceof WebRequest) {
|
|---|
| 148 | return targetUri.equals(((WebRequest) other).targetUri)
|
|---|
| 149 | && postVars.equals(((WebRequest) other).postVars)
|
|---|
| 150 | && getVars.equals(((WebRequest) other).getVars);
|
|---|
| 151 | }
|
|---|
| 152 | return false;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /*
|
|---|
| 156 | * (non-Javadoc)
|
|---|
| 157 | *
|
|---|
| 158 | * @see java.lang.Object#hashCode()
|
|---|
| 159 | */
|
|---|
| 160 | @Override
|
|---|
| 161 | public int hashCode() {
|
|---|
| 162 | int multiplier = 17;
|
|---|
| 163 | int hash = 42;
|
|---|
| 164 |
|
|---|
| 165 | hash = multiplier * hash + targetUri.hashCode();
|
|---|
| 166 | hash = multiplier * hash + postVars.hashCode();
|
|---|
| 167 | hash = multiplier * hash + getVars.hashCode();
|
|---|
| 168 |
|
|---|
| 169 | return hash;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | }
|
|---|