Skip to content

Commit 383fc99

Browse files
committed
added better filters for resource viewer
1 parent abbbce4 commit 383fc99

File tree

6 files changed

+123
-97
lines changed

6 files changed

+123
-97
lines changed

sample_config.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
{
22
"merge":[],
33
"options":{
4+
"cacheTime":0,
45
"forcedLatency":0,
56
"maxBitrate":0
67
},
78
"port":9247,
89
"proxy":[
910
{
1011
"cache":true,
12+
"cacheDuration":0,
1113
"host":"jsonplaceholder.typicode.com",
12-
"path":"/.*",
14+
"path":".*",
1315
"port":80
1416
}
1517
],
16-
"resource":{
17-
"track":true
18-
},
18+
"resource":{},
1919
"resourceBase":"",
2020
"showIndex":false,
21+
"tamper":{
22+
"items":[]
23+
},
2124
"tracker":{
2225
"track":false
2326
},

src/main/java/com/thedeanda/ajaxproxy/ui/model/ResourceListModel.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@
1717
import org.apache.http.Header;
1818

1919
import com.thedeanda.ajaxproxy.service.ResourceService;
20+
import com.thedeanda.ajaxproxy.ui.resourceviewer.filter.RequestFilter;
21+
import com.thedeanda.ajaxproxy.ui.resourceviewer.filter.RequestType;
2022

2123
public class ResourceListModel extends AbstractListModel<Resource> {
2224
private static final long serialVersionUID = -8347782415802894185L;
2325
private Set<Resource> unfilteredItems = new TreeSet<>();
2426
private List<Resource> items = new ArrayList<>();
2527
private Map<String, Resource> resourceMap = new HashMap<>();
2628

27-
private Pattern filterRegEx;
2829
private ResourceService resourceService;
29-
30+
private RequestFilter requestFilter;
31+
3032
public ResourceListModel(ResourceService resourceService) {
3133
this.resourceService = resourceService;
34+
requestFilter = new RequestFilter();
3235
}
3336

3437
public void add(Resource item) {
@@ -38,8 +41,7 @@ public void add(Resource item) {
3841
}
3942
unfilteredItems.add(item);
4043
}
41-
if (filterRegEx == null
42-
|| filterRegEx.matcher(item.getPath()).matches()) {
44+
if (requestFilter.accept(item)) {
4345
addSorted(item, items);
4446
}
4547
}
@@ -74,14 +76,9 @@ private void resetFilter() {
7476
copyOfAllItems = new TreeSet<>(unfilteredItems);
7577
}
7678

77-
if (filterRegEx == null) {
78-
// just add all
79-
items.addAll(copyOfAllItems);
80-
} else {
81-
for (Resource item : copyOfAllItems) {
82-
if (filterRegEx.matcher(item.getPath()).matches()) {
83-
items.add(item);
84-
}
79+
for (Resource item : copyOfAllItems) {
80+
if (requestFilter.accept(item)) {
81+
items.add(item);
8582
}
8683
}
8784

@@ -167,8 +164,7 @@ public void remove(int index) {
167164
fireIntervalRemoved(this, index, index);
168165
}
169166

170-
public void startRequest(UUID id, URL url, Header[] requestHeaders,
171-
byte[] data) {
167+
public void startRequest(UUID id, URL url, Header[] requestHeaders, byte[] data) {
172168

173169
Resource resource = get(id);
174170
if (resource != null) {
@@ -179,8 +175,8 @@ public void startRequest(UUID id, URL url, Header[] requestHeaders,
179175
}
180176
}
181177

182-
public void requestComplete(UUID id, int status, String reason,
183-
long duration, Header[] responseHeaders, byte[] data) {
178+
public void requestComplete(UUID id, int status, String reason, long duration, Header[] responseHeaders,
179+
byte[] data) {
184180
Resource resource = get(id);
185181
if (resource != null) {
186182
resource.setStatus(status);
@@ -202,8 +198,10 @@ public void error(UUID id, String message, Exception e) {
202198
}
203199
}
204200

205-
public void setFilter(Pattern filterRegEx) {
206-
this.filterRegEx = filterRegEx;
201+
public void setFilter(Pattern filterRegEx, List<RequestType> checkedItems) {
202+
requestFilter.setRegEx(filterRegEx);
203+
requestFilter.setRequestTypes(checkedItems);
207204
resetFilter();
208205
}
206+
209207
}

src/main/java/com/thedeanda/ajaxproxy/ui/resourceviewer/FilterPanel.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.awt.Color;
44
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
6+
import java.util.ArrayList;
7+
import java.util.List;
68
import java.util.regex.Pattern;
79
import java.util.regex.PatternSyntaxException;
810

@@ -17,6 +19,8 @@
1719

1820
import org.apache.commons.lang3.StringUtils;
1921
import org.japura.gui.CheckComboBox;
22+
import org.japura.gui.event.ListCheckListener;
23+
import org.japura.gui.event.ListEvent;
2024
import org.japura.gui.model.ListCheckModel;
2125
import org.slf4j.Logger;
2226
import org.slf4j.LoggerFactory;
@@ -25,7 +29,6 @@
2529
import com.thedeanda.ajaxproxy.ui.border.BottomBorder;
2630
import com.thedeanda.ajaxproxy.ui.model.ResourceListModel;
2731
import com.thedeanda.ajaxproxy.ui.resourceviewer.filter.RequestType;
28-
import com.thedeanda.ajaxproxy.ui.resourceviewer.filter.RequestTypeFilter;
2932

3033
public class FilterPanel extends JPanel {
3134
private static final Logger log = LoggerFactory.getLogger(FilterPanel.class);
@@ -41,6 +44,7 @@ public class FilterPanel extends JPanel {
4144
private Color filterBadColor;
4245

4346
private ResourceListModel model;
47+
private ListCheckModel requestTypeFilterModel;
4448

4549
public FilterPanel() {
4650
log.warn("new filter panel");
@@ -106,23 +110,34 @@ public void insertUpdate(DocumentEvent e) {
106110
rtFilter.setTextFor(CheckComboBox.MULTIPLE, "...");
107111
rtFilter.setTextFor(CheckComboBox.ALL, "-- ALL --");
108112

109-
ListCheckModel checkModel = rtFilter.getModel();
113+
requestTypeFilterModel = rtFilter.getModel();
110114
for (RequestType color : RequestType.values()) {
111-
checkModel.addElement(color);
115+
requestTypeFilterModel.addElement(color);
112116
}
117+
requestTypeFilterModel.addListCheckListener(new ListCheckListener() {
118+
@Override
119+
public void removeCheck(ListEvent event) {
120+
resetFilter();
121+
}
122+
123+
@Override
124+
public void addCheck(ListEvent event) {
125+
resetFilter();
126+
}
127+
});
113128

114129
// rtFilter = new RequestTypeFilter();
115130
add(rtFilter);
116-
rtFilter.setEnabled(false);
131+
// rtFilter.setEnabled(false);
117132

118133
clearBtn = new JButton("Clear");
119-
134+
120135
exportBtn = new JButton("Export");
121136
exportBtn.setEnabled(false);
122-
137+
123138
toggleBtn = new JCheckBox("Monitor Resources");
124139
toggleBtn.setEnabled(false);
125-
140+
126141
clearBtn.addActionListener(new ActionListener() {
127142
@Override
128143
public void actionPerformed(ActionEvent e) {
@@ -150,6 +165,8 @@ public void actionPerformed(ActionEvent e) {
150165
}
151166

152167
private void resetFilter() {
168+
// TODO: throttle processing here since it works as you type
169+
153170
Pattern filterRegEx = null;
154171
if (!StringUtils.isBlank(filter.getText())) {
155172
try {
@@ -160,12 +177,21 @@ private void resetFilter() {
160177
filter.setBackground(filterBadColor);
161178
}
162179
}
180+
181+
List<Object> checked = requestTypeFilterModel.getCheckeds();
182+
final List<RequestType> checkedItems = new ArrayList<>();
183+
if (checked != null && !checked.isEmpty()) {
184+
for (Object o : checked) {
185+
checkedItems.add((RequestType) o);
186+
}
187+
}
188+
163189
final Pattern filter = filterRegEx;
164190
SwingUtils.executNonUi(new Runnable() {
165191
@Override
166192
public void run() {
167193
if (model != null) {
168-
model.setFilter(filter);
194+
model.setFilter(filter, checkedItems);
169195
}
170196
}
171197
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.thedeanda.ajaxproxy.ui.resourceviewer.filter;
2+
3+
import java.util.List;
4+
import java.util.regex.Pattern;
5+
6+
import com.thedeanda.ajaxproxy.ui.model.Resource;
7+
8+
public class RequestFilter {
9+
private Pattern filterRegEx;
10+
private List<RequestType> items;
11+
12+
public void setRequestTypes(List<RequestType> checkedItems) {
13+
this.items = checkedItems;
14+
}
15+
16+
public boolean accept(Resource item) {
17+
if (filterRegEx != null && !filterRegEx.matcher(item.getPath()).matches()) {
18+
return false;
19+
}
20+
21+
boolean accept = true;
22+
if (items != null && !items.isEmpty()) {
23+
accept = false;
24+
for (RequestType rt : items) {
25+
if (rt.isMethod()) {
26+
if (rt.name().equalsIgnoreCase(item.getMethod())) {
27+
accept = true;
28+
}
29+
} else if (rt.isStatusCode()) {
30+
if (rt.isStatusInRange(item.getStatus())) {
31+
accept = true;
32+
}
33+
}
34+
}
35+
}
36+
return accept;
37+
}
38+
39+
public void setRegEx(Pattern filterRegEx) {
40+
this.filterRegEx = filterRegEx;
41+
}
42+
}
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
package com.thedeanda.ajaxproxy.ui.resourceviewer.filter;
22

33
public enum RequestType {
4-
GET(true, false), POST(true, false), PUT(true, false), DELETE(true, false), HEAD(true, false), PATCH(true,
5-
false), OPTIONS(true, false), TRACE(true,
6-
false), STATUS_200S(false, true), STATUS_400S(false, true), STATUS_600S(false, true);
4+
GET(true, false), POST(true, false), PUT(true, false), DELETE(true, false),
5+
HEAD(true, false), PATCH(true, false), OPTIONS(true, false), TRACE(true, false),
6+
STATUS_1xx(false, true, 1), STATUS_2xx(false, true, 2), STATUS_3xx(false, true, 3),
7+
STATUS_4xx(false, true, 4), STATUS_5xx(false, true, 5);
78

89
private boolean method;
910
private boolean response;
11+
private int status = 0;
1012

1113
private RequestType(boolean method, boolean response) {
14+
this(method, response, 0);
15+
}
16+
17+
private RequestType(boolean method, boolean response, int status) {
1218
this.method = method;
1319
this.response = response;
20+
this.status = status;
1421
}
1522

1623
public boolean isMethod() {
1724
return method;
1825
}
1926

20-
public boolean isResponse() {
27+
public boolean isStatusCode() {
2128
return response;
2229
}
30+
31+
public boolean isStatusInRange(int status) {
32+
boolean ret = false;
33+
if (status > 0) {
34+
ret = (status / 100) == this.status;
35+
}
36+
return ret;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return name().replace("_", ": ");
42+
}
2343
}

src/main/java/com/thedeanda/ajaxproxy/ui/resourceviewer/filter/RequestTypeFilter.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)