Search in sources :

Example 1 with ApiException

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;
}
Also used : AddOn(org.zaproxy.zap.control.AddOn) ApiResponseElement(org.zaproxy.zap.extension.api.ApiResponseElement) ApiResponseList(org.zaproxy.zap.extension.api.ApiResponseList) ApiResponse(org.zaproxy.zap.extension.api.ApiResponse) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 2 with ApiException

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);
    }
}
Also used : AddOn(org.zaproxy.zap.control.AddOn) ArrayList(java.util.ArrayList) ApiResponseList(org.zaproxy.zap.extension.api.ApiResponseList) ArrayList(java.util.ArrayList) List(java.util.List) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 3 with ApiException

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);
        }
    };
}
Also used : ApiDynamicActionImplementor(org.zaproxy.zap.extension.api.ApiDynamicActionImplementor) Context(org.zaproxy.zap.model.Context) PopupMenuItemContext(org.zaproxy.zap.view.popup.PopupMenuItemContext) RecordContext(org.parosproxy.paros.db.RecordContext) JSONObject(net.sf.json.JSONObject) URL(java.net.URL) URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) ApiException(org.zaproxy.zap.extension.api.ApiException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 4 with ApiException

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;
}
Also used : GenericScanner2(org.zaproxy.zap.model.GenericScanner2) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 5 with ApiException

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;
}
Also used : GenericScanner2(org.zaproxy.zap.model.GenericScanner2) ApiException(org.zaproxy.zap.extension.api.ApiException)

Aggregations

ApiException (org.zaproxy.zap.extension.api.ApiException)57 JSONObject (net.sf.json.JSONObject)22 Context (org.zaproxy.zap.model.Context)20 ApiResponseList (org.zaproxy.zap.extension.api.ApiResponseList)16 ApiResponseElement (org.zaproxy.zap.extension.api.ApiResponseElement)15 DatabaseException (org.parosproxy.paros.db.DatabaseException)13 HashMap (java.util.HashMap)12 ApiDynamicActionImplementor (org.zaproxy.zap.extension.api.ApiDynamicActionImplementor)10 ApiResponse (org.zaproxy.zap.extension.api.ApiResponse)9 User (org.zaproxy.zap.users.User)9 ArrayList (java.util.ArrayList)8 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)8 HttpMessage (org.parosproxy.paros.network.HttpMessage)7 JSONException (net.sf.json.JSONException)6 ConfigurationException (org.apache.commons.configuration.ConfigurationException)6 IOException (java.io.IOException)5 PatternSyntaxException (java.util.regex.PatternSyntaxException)5 URIException (org.apache.commons.httpclient.URIException)5 RecordContext (org.parosproxy.paros.db.RecordContext)5 ExtensionUserManagement (org.zaproxy.zap.extension.users.ExtensionUserManagement)5