use of org.zaproxy.zap.extension.api.ApiException in project zaproxy by zaproxy.
the class AutoUpdateAPI method handleApiView.
@Override
public ApiResponse handleApiView(String name, JSONObject params) throws ApiException {
ApiResponse result;
if (VIEW_LATEST_VERSION_NUMBER.equals(name)) {
result = new ApiResponseElement(name, this.getLatestVersionNumber());
} else if (VIEW_IS_LATEST_VERSION.equals(name)) {
result = new ApiResponseElement(name, Boolean.toString(this.isLatestVersion()));
} else if (VIEW_INSTALLED_ADDONS.equals(name)) {
final ApiResponseList resultList = new ApiResponseList(name);
for (AddOn ao : extension.getInstalledAddOns()) {
resultList.addItem(addonToSet(ao));
}
result = resultList;
} else if (VIEW_NEW_ADDONS.equals(name)) {
final ApiResponseList resultList = new ApiResponseList(name);
for (AddOn ao : extension.getNewAddOns()) {
resultList.addItem(addonToSet(ao));
}
result = resultList;
} else if (VIEW_UPDATED_ADDONS.equals(name)) {
final ApiResponseList resultList = new ApiResponseList(name);
for (AddOn ao : extension.getUpdatedAddOns()) {
resultList.addItem(addonToSet(ao));
}
result = resultList;
} else if (VIEW_MARKETPLACE_ADDONS.equals(name)) {
final ApiResponseList resultList = new ApiResponseList(name);
for (AddOn ao : extension.getMarketplaceAddOns()) {
resultList.addItem(addonToSet(ao));
}
result = resultList;
} else {
throw new ApiException(ApiException.Type.BAD_VIEW);
}
return result;
}
use of org.zaproxy.zap.extension.api.ApiException in project zaproxy by zaproxy.
the class AutoUpdateAPI method handleApiAction.
@Override
public ApiResponse handleApiAction(String name, JSONObject params) throws ApiException {
log.debug("handleApiAction " + name + " " + params.toString());
if (ACTION_DOWNLOAD_LATEST_RELEASE.equals(name)) {
if (this.downloadLatestRelease()) {
return ApiResponseElement.OK;
} else {
return ApiResponseElement.FAIL;
}
} else if (ACTION_INSTALL_ADDON.equals(name)) {
String id = params.getString(PARAM_ID);
AddOn ao = extension.getAddOn(id);
if (ao == null) {
throw new ApiException(Type.DOES_NOT_EXIST);
} else {
List<String> l = new ArrayList<String>();
l.add(id);
String errorMessages = extension.installAddOns(l);
if (errorMessages.length() == 0) {
return ApiResponseElement.OK;
} else {
throw new ApiException(ApiException.Type.INTERNAL_ERROR, errorMessages);
}
}
} else if (ACTION_UNINSTALL_ADDON.equals(name)) {
String id = params.getString(PARAM_ID);
AddOn ao = extension.getLocalVersionInfo().getAddOn(id);
if (ao == null) {
throw new ApiException(Type.DOES_NOT_EXIST);
} else {
List<String> l = new ArrayList<String>();
l.add(id);
String errorMessages = extension.uninstallAddOns(l);
if (errorMessages.length() == 0) {
return ApiResponseElement.OK;
} else {
throw new ApiException(ApiException.Type.INTERNAL_ERROR, errorMessages);
}
}
} else {
throw new ApiException(ApiException.Type.BAD_ACTION);
}
}
use of org.zaproxy.zap.extension.api.ApiException in project zaproxy by zaproxy.
the class FormBasedAuthenticationMethodType method getSetMethodForContextApiAction.
@Override
public ApiDynamicActionImplementor getSetMethodForContextApiAction() {
return new ApiDynamicActionImplementor(API_METHOD_NAME, new String[] { PARAM_LOGIN_URL }, new String[] { PARAM_LOGIN_REQUEST_DATA }) {
@Override
public void handleAction(JSONObject params) throws ApiException {
Context context = ApiUtils.getContextByParamId(params, AuthenticationAPI.PARAM_CONTEXT_ID);
String loginUrl = ApiUtils.getNonEmptyStringParam(params, PARAM_LOGIN_URL);
try {
new URL(loginUrl);
} catch (Exception ex) {
throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_LOGIN_URL);
}
String postData = "";
if (params.containsKey(PARAM_LOGIN_REQUEST_DATA)) {
postData = params.getString(PARAM_LOGIN_REQUEST_DATA);
}
// Set the method
FormBasedAuthenticationMethod method = createAuthenticationMethod(context.getIndex());
try {
method.setLoginRequest(loginUrl, postData);
} catch (Exception e) {
throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
}
if (!context.getAuthenticationMethod().isSameType(method))
apiChangedAuthenticationMethodForContext(context.getIndex());
context.setAuthenticationMethod(method);
}
};
}
use of org.zaproxy.zap.extension.api.ApiException in project zaproxy by zaproxy.
the class SpiderAPI method getSpiderScan.
/**
* Returns the specified GenericScanner2 or the last scan available.
*
* @param params the parameters of the API call
* @return the GenericScanner2 with the given scan ID or, if not present, the last scan available
* @throws ApiException if there's no scan with the given scan ID
* @see #PARAM_SCAN_ID
*/
private GenericScanner2 getSpiderScan(JSONObject params) throws ApiException {
GenericScanner2 spiderScan;
int id = getParam(params, PARAM_SCAN_ID, -1);
if (id == -1) {
spiderScan = extension.getLastScan();
} else {
spiderScan = extension.getScan(id);
}
if (spiderScan == null) {
throw new ApiException(ApiException.Type.DOES_NOT_EXIST, PARAM_SCAN_ID);
}
return spiderScan;
}
use of org.zaproxy.zap.extension.api.ApiException in project zaproxy by zaproxy.
the class ActiveScanAPI method getActiveScan.
/**
* Returns a {@link ActiveScan} from the available active scans or the last active scan. If a scan ID (
* {@link #PARAM_SCAN_ID}) is present in the given {@code params} it will be used to the get the {@code ActiveScan} from the
* available active scans, otherwise it's returned the last active scan.
*
* @param params the parameters of the API call
* @return the {@code ActiveScan} with the given scan ID or, if not present, the last active scan
* @throws ApiException if there's no scan with the given scan ID
*/
private ActiveScan getActiveScan(JSONObject params) throws ApiException {
int id = getParam(params, PARAM_SCAN_ID, -1);
GenericScanner2 activeScan = null;
if (id == -1) {
activeScan = controller.getLastScan();
} else {
activeScan = controller.getScan(Integer.valueOf(id));
}
if (activeScan == null) {
throw new ApiException(ApiException.Type.DOES_NOT_EXIST, PARAM_SCAN_ID);
}
return (ActiveScan) activeScan;
}
Aggregations