Skip to content

Commit

Permalink
Custom Pages Initial zaproxy#8
Browse files Browse the repository at this point in the history
Cleanup so that the CustomPageType enum is actually used, and IDs aren't
dependent upon sequentiality, etc.
  • Loading branch information
kingthorin committed Jan 27, 2017
1 parent 6513c9d commit 7cbac98
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 33 deletions.
38 changes: 16 additions & 22 deletions src/org/zaproxy/zap/extension/custompages/CustomPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public class CustomPage extends Enableable {
int contextId;
private String content;
private boolean regex;
private int type;
private CustomPageType type;

private static final Logger log = Logger.getLogger(CustomPage.class);

public CustomPage() {
}

public CustomPage(int contextId, String content, boolean regex, int type, boolean enabled) {
public CustomPage(int contextId, String content, boolean regex, CustomPageType type, boolean enabled) {
super();
this.contextId = contextId;
this.content = content;
Expand All @@ -50,6 +50,15 @@ public CustomPage(int contextId, String content, boolean regex, int type, boolea
this.setEnabled(enabled);
}

public CustomPage(int contextId, String content, boolean regex, int typeID, boolean enabled) {
super();
this.contextId = contextId;
this.content = content;
this.regex = regex;
this.type = CustomPageType.getCustomPageTypeWithId(typeID);
this.setEnabled(enabled);
}

public int getContextId() {
return contextId;
}
Expand All @@ -74,27 +83,11 @@ public void setRegex(boolean regex) {
this.regex = regex;
}


/**
* Returns the numeric type identifier of the custom page.
*
* @return an integer corresponding to the type of custom page
*/
public int getTypeId() {
public CustomPageType getType() {
return type;
}

/**
* Returns the name (String) of the custom page type identifier.
*
* @param key the numeric ID corresponding to the custom page type
* @return the custom page type name
*/
public String getTypeName(int key) {
return CustomPageType.getCustomPageTypeWithId(key).getName();
}

public void setTypeId(int cpt) {
public void setType(CustomPageType cpt) {
this.type = cpt;
}

Expand All @@ -107,6 +100,7 @@ public String toString() {
cp.append(", IsEnabled: ").append(this.isEnabled());
cp.append(", Is RegEx: ").append(this.isRegex());
cp.append(", Content: ").append(this.getContent()).append(".");
cp.append(", Type: ").append(this.getType().getName());
return cp.toString();
}

Expand All @@ -125,7 +119,7 @@ public static String encode(CustomPage custompage) {
if (custompage.content != null) {
encodedCP.append(Base64.encodeBase64String(custompage.content.getBytes()));
}
encodedCP.append(custompage.getTypeId());
encodedCP.append(custompage.type.getId());
encodedCP.append(FIELD_SEPARATOR);
// log.debug("Encoded CustomPage: " + out.toString());
return encodedCP.toString();
Expand All @@ -148,7 +142,7 @@ protected static CustomPage decode(int contextId, String encodedString) {
customPage.setEnabled(Boolean.parseBoolean(pieces[0]));
customPage.setRegex(Boolean.parseBoolean(pieces[1]));
customPage.setContent(new String(Base64.decodeBase64(pieces[2])));
customPage.setTypeId(Integer.parseInt(pieces[3]));
customPage.setType(CustomPageType.getCustomPageTypeWithId(Integer.parseInt(pieces[3])));
} catch (Exception ex) {
log.error("An error occured while decoding customPage from: " + encodedString, ex);
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/org/zaproxy/zap/extension/custompages/CustomPageAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public ApiResponse handleApiAction(String name, JSONObject params) throws ApiExc
cp = new CustomPage(context.getIndex(),
ApiUtils.getNonEmptyStringParam(params, PARAM_CONTENT),
getParam(params, PARAM_CONTENT_IS_REGEX, false),
getParam(params, PARAM_TYPE, CustomPageType.getDefaultType().getId()),
ApiUtils.getIntParam(params, PARAM_TYPE),//TODO
getParam(params, PARAM_ENABLED, true));
extension.getContextCustomPageManager(context.getIndex()).addCustomPage(cp);
return ApiResponseElement.OK;
Expand All @@ -120,7 +120,7 @@ public ApiResponse handleApiAction(String name, JSONObject params) throws ApiExc
cp = new CustomPage(context.getIndex(),
ApiUtils.getOptionalStringParam(params, PARAM_CONTENT),
getParam(params, PARAM_CONTENT_IS_REGEX, false),
getParam(params, PARAM_TYPE, CustomPageType.getDefaultType().getId()),
ApiUtils.getIntParam(params, PARAM_TYPE),//TODO
getParam(params, PARAM_ENABLED, true));
if (extension.getContextCustomPageManager(
context.getIndex()).removeCustomPage(cp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Object getValueAt(int rowIndex, int columnIndex) {
case 2:
return cp.isRegex();
case 3:
return cp.getTypeName(cp.getTypeId());
return cp.getType().getName();
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Defines the types supported by the Custom Page package.
*/
public enum CustomPageType {
// Type IDs should be set sequentially, they are used within maps and arrays and IDs should align with indexes/keys
ERROR(1, "ERROR PAGE"),
DUMMY(2, "DUMMY");

Expand Down Expand Up @@ -54,6 +53,9 @@ public static CustomPageType getCustomPageTypeWithId(int id) {

if (ERROR.id == id) {
return ERROR;
}
if (DUMMY.id == id) {
return DUMMY;
}
return getDefaultType();
}
Expand All @@ -74,6 +76,9 @@ public static CustomPageType getCustomPageTypeWithName(String name) {

if (ERROR.name == name) {
return ERROR;
}
if (DUMMY.name == name) {
return DUMMY;
}
return getDefaultType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class DialogAddCustomPage extends AbstractFormDialog {

private JPanel fieldsPanel;
private JCheckBox enabledCheckBox;
private JComboBox<String> newCustomPageTypesCombo;
private JComboBox<String> customPageTypesCombo;
private JCheckBox regexCheckBox;
private ZapTextField contentTextField;
protected Context workingContext;
Expand Down Expand Up @@ -134,7 +134,7 @@ protected void performAction() {
workingContext.getIndex(),
getContentTextField().getText(),
getRegexCheckBox().isSelected(),
getCustomPageTypesCombo().getSelectedIndex(),
CustomPageType.getCustomPageTypeWithName(getCustomPageTypesCombo().getSelectedItem().toString()),
this.getEnabledCheckBox().isSelected());
}

Expand All @@ -144,7 +144,7 @@ protected void clearFields() {
this.contentTextField.discardAllEdits();
this.enabledCheckBox.setSelected(true);
this.regexCheckBox.setSelected(false);
this.newCustomPageTypesCombo.setSelectedIndex(0);
this.customPageTypesCombo.setSelectedIndex(0);
this.setConfirmButtonEnabled(true);
}

Expand Down Expand Up @@ -221,10 +221,10 @@ protected ZapTextField getContentTextField() {
}

protected JComboBox<String> getCustomPageTypesCombo() {
if (newCustomPageTypesCombo == null) {
newCustomPageTypesCombo = new JComboBox<String>(CustomPageType.getAllCustomPageTypeNames());
if (customPageTypesCombo == null) {
customPageTypesCombo = new JComboBox<String>(CustomPageType.getAllCustomPageTypeNames());
}
return newCustomPageTypesCombo;
return customPageTypesCombo;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void actionPerformed(ActionEvent e) {
getEnabledCheckBox().setSelected(customPage.isEnabled());
getContentTextField().setText(customPage.getContent());
getRegexCheckBox().setSelected(customPage.isRegex());
getCustomPageTypesCombo().setSelectedIndex(customPage.getTypeId());
getCustomPageTypesCombo().setSelectedItem(customPage.getType().getName());

this.setConfirmButtonEnabled(true);

Expand Down

0 comments on commit 7cbac98

Please sign in to comment.