Search in sources :

Example 1 with IllegalContextNameException

use of org.zaproxy.zap.model.IllegalContextNameException in project zaproxy by zaproxy.

the class ContextAPI method handleApiAction.

@Override
public ApiResponse handleApiAction(String name, JSONObject params) throws ApiException {
    log.debug("handleApiAction " + name + " " + params.toString());
    Context context;
    TechSet techSet;
    String[] techNames;
    String filename;
    File f;
    switch(name) {
        case EXCLUDE_FROM_CONTEXT_REGEX:
            try {
                addExcludeToContext(getContext(params), params.getString(REGEX_PARAM));
            } catch (IllegalArgumentException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, REGEX_PARAM, e);
            }
            break;
        case INCLUDE_IN_CONTEXT_REGEX:
            try {
                addIncludeToContext(getContext(params), params.getString(REGEX_PARAM));
            } catch (IllegalArgumentException e) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, REGEX_PARAM, e);
            }
            break;
        case ACTION_NEW_CONTEXT:
            String contextName = params.getString(CONTEXT_NAME);
            try {
                context = Model.getSingleton().getSession().getNewContext(contextName);
            } catch (IllegalContextNameException e) {
                throw new ApiException(ApiException.Type.ALREADY_EXISTS, contextName, e);
            }
            Model.getSingleton().getSession().saveContext(context);
            return new ApiResponseElement(CONTEXT_ID, String.valueOf(context.getIndex()));
        case ACTION_REMOVE_CONTEXT:
            context = getContext(params);
            Model.getSingleton().getSession().deleteContext(context);
            break;
        case ACTION_SET_CONTEXT_IN_SCOPE:
            context = getContext(params);
            context.setInScope(params.getBoolean(IN_SCOPE));
            Model.getSingleton().getSession().saveContext(context);
            break;
        case ACTION_IMPORT_CONTEXT:
            filename = params.getString(CONTEXT_FILE_PARAM);
            f = new File(filename);
            if (!f.exists()) {
                // Try relative to the contexts dir
                f = new File(Constant.getContextsDir(), filename);
            }
            if (!f.exists()) {
                throw new ApiException(ApiException.Type.DOES_NOT_EXIST, f.getAbsolutePath());
            } else {
                try {
                    context = Model.getSingleton().getSession().importContext(f);
                } catch (IllegalContextNameException e) {
                    throw new ApiException(ApiException.Type.BAD_EXTERNAL_DATA, e);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
                }
            }
            return new ApiResponseElement(CONTEXT_ID, String.valueOf(context.getIndex()));
        case ACTION_EXPORT_CONTEXT:
            filename = params.getString(CONTEXT_FILE_PARAM);
            context = getContext(params);
            f = new File(filename);
            if (!f.getAbsolutePath().equals(filename)) {
                // Not an absolute filename, use one relative to the contexts dir
                f = new File(Constant.getContextsDir(), filename);
            }
            if (!f.getParentFile().canWrite()) {
                // Cant write to the parent dir so not looking good
                throw new ApiException(ApiException.Type.NO_ACCESS, f.getAbsolutePath());
            } else {
                try {
                    Model.getSingleton().getSession().exportContext(context, f);
                } catch (Exception e) {
                    throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
                }
            }
            break;
        case ACTION_INCLUDE_TECHS:
            context = getContext(params);
            techSet = context.getTechSet();
            techNames = getParam(params, PARAM_TECH_NAMES, "").split(",");
            for (String techName : techNames) {
                techSet.include(getTech(techName));
            }
            context.save();
            break;
        case ACTION_INCLUDE_ALL_TECHS:
            context = getContext(params);
            techSet = new TechSet(Tech.builtInTech);
            context.setTechSet(techSet);
            context.save();
            break;
        case ACTION_EXCLUDE_TECHS:
            context = getContext(params);
            techSet = context.getTechSet();
            techNames = getParam(params, PARAM_TECH_NAMES, "").split(",");
            for (String techName : techNames) {
                techSet.exclude(getTech(techName));
            }
            context.save();
            break;
        case ACTION_EXCLUDE_ALL_TECHS:
            context = getContext(params);
            techSet = context.getTechSet();
            for (Tech tech : Tech.builtInTech) {
                techSet.exclude(tech);
            }
            context.save();
            break;
        default:
            throw new ApiException(Type.BAD_ACTION);
    }
    return ApiResponseElement.OK;
}
Also used : Context(org.zaproxy.zap.model.Context) Tech(org.zaproxy.zap.model.Tech) TechSet(org.zaproxy.zap.model.TechSet) ApiResponseElement(org.zaproxy.zap.extension.api.ApiResponseElement) IllegalContextNameException(org.zaproxy.zap.model.IllegalContextNameException) File(java.io.File) IllegalContextNameException(org.zaproxy.zap.model.IllegalContextNameException)

Example 2 with IllegalContextNameException

use of org.zaproxy.zap.model.IllegalContextNameException in project zaproxy by zaproxy.

the class MenuFileControl method importContext.

/**
	 * Prompt the user to export a context
	 */
public void importContext() {
    JFileChooser chooser = new JFileChooser(Constant.getContextsDir());
    File file = null;
    chooser.setFileFilter(new FileFilter() {

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".context")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("file.format.zap.context");
        }
    });
    int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        try {
            file = chooser.getSelectedFile();
            if (file == null || !file.exists()) {
                return;
            }
            // Import the context
            Model.getSingleton().getSession().importContext(file);
            // Show the dialog
            View.getSingleton().showSessionDialog(Model.getSingleton().getSession(), Constant.messages.getString("context.list"), true);
        } catch (IllegalContextNameException e) {
            String detailError;
            if (e.getReason() == IllegalContextNameException.Reason.EMPTY_NAME) {
                detailError = Constant.messages.getString("context.error.name.empty");
            } else if (e.getReason() == IllegalContextNameException.Reason.DUPLICATED_NAME) {
                detailError = Constant.messages.getString("context.error.name.duplicated");
            } else {
                detailError = Constant.messages.getString("context.error.name.unknown");
            }
            View.getSingleton().showWarningDialog(MessageFormat.format(Constant.messages.getString("context.import.error"), detailError));
        } catch (Exception e1) {
            log.debug(e1.getMessage(), e1);
            View.getSingleton().showWarningDialog(MessageFormat.format(Constant.messages.getString("context.import.error"), e1.getMessage()));
        }
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) IllegalContextNameException(org.zaproxy.zap.model.IllegalContextNameException) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) IllegalContextNameException(org.zaproxy.zap.model.IllegalContextNameException) DatabaseException(org.parosproxy.paros.db.DatabaseException)

Aggregations

File (java.io.File)2 IllegalContextNameException (org.zaproxy.zap.model.IllegalContextNameException)2 JFileChooser (javax.swing.JFileChooser)1 FileFilter (javax.swing.filechooser.FileFilter)1 DatabaseException (org.parosproxy.paros.db.DatabaseException)1 ApiResponseElement (org.zaproxy.zap.extension.api.ApiResponseElement)1 Context (org.zaproxy.zap.model.Context)1 Tech (org.zaproxy.zap.model.Tech)1 TechSet (org.zaproxy.zap.model.TechSet)1