Search in sources :

Example 21 with JSONObject

use of net.sf.json.JSONObject in project zaproxy by zaproxy.

the class UsernamePasswordAuthenticationCredentials method getSetCredentialsForUserApiAction.

/**
	 * Gets the api action for setting a {@link UsernamePasswordAuthenticationCredentials} for an
	 * User.
	 * 
	 * @param methodType the method type for which this is called
	 * @return the sets the credentials for user api action
	 */
public static ApiDynamicActionImplementor getSetCredentialsForUserApiAction(final AuthenticationMethodType methodType) {
    return new ApiDynamicActionImplementor(ACTION_SET_CREDENTIALS, new String[] { PARAM_USERNAME, PARAM_PASSWORD }, null) {

        @Override
        public void handleAction(JSONObject params) throws ApiException {
            Context context = ApiUtils.getContextByParamId(params, UsersAPI.PARAM_CONTEXT_ID);
            int userId = ApiUtils.getIntParam(params, UsersAPI.PARAM_USER_ID);
            // Make sure the type of authentication method is compatible
            if (!methodType.isTypeForMethod(context.getAuthenticationMethod()))
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, "User's credentials should match authentication method type of the context: " + context.getAuthenticationMethod().getType().getName());
            // NOTE: no need to check if extension is loaded as this method is called only if
            // the Users
            // extension is loaded
            ExtensionUserManagement extensionUserManagement = (ExtensionUserManagement) Control.getSingleton().getExtensionLoader().getExtension(ExtensionUserManagement.NAME);
            User user = extensionUserManagement.getContextUserAuthManager(context.getIndex()).getUserById(userId);
            if (user == null)
                throw new ApiException(ApiException.Type.USER_NOT_FOUND, UsersAPI.PARAM_USER_ID);
            // Build and set the credentials
            UsernamePasswordAuthenticationCredentials credentials = new UsernamePasswordAuthenticationCredentials();
            credentials.username = ApiUtils.getNonEmptyStringParam(params, PARAM_USERNAME);
            credentials.password = ApiUtils.getNonEmptyStringParam(params, PARAM_PASSWORD);
            user.setAuthenticationCredentials(credentials);
        }
    };
}
Also used : ApiDynamicActionImplementor(org.zaproxy.zap.extension.api.ApiDynamicActionImplementor) Context(org.zaproxy.zap.model.Context) ExtensionUserManagement(org.zaproxy.zap.extension.users.ExtensionUserManagement) User(org.zaproxy.zap.users.User) JSONObject(net.sf.json.JSONObject) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 22 with JSONObject

use of net.sf.json.JSONObject in project Xponents by OpenSextant.

the class TweetGeocoder method readTweet.

/**
     * Need references to current methodologies for what data is available,
     * reliable, etc and where/when to use it.
     */
private void readTweet(String json, Tweet tw) throws ParseException {
    try {
        JSONObject twj = JSONObject.fromObject(json.trim());
        tw.fromJSON(twj);
        // RESET using a cleaned up status text
        tw.setText(scrubText(separateHashMark(tw.getText())));
    } catch (Exception twerr) {
        throw new ParseException("Failed to parse Tweet " + twerr.getMessage(), 0);
    }
}
Also used : JSONObject(net.sf.json.JSONObject) ParseException(java.text.ParseException) ProcessingException(org.opensextant.processing.ProcessingException) ParseException(java.text.ParseException) ConfigException(org.opensextant.ConfigException) IOException(java.io.IOException)

Example 23 with JSONObject

use of net.sf.json.JSONObject in project tdi-studio-se by Talend.

the class HttpUtil method handHttpResponse.

public static void handHttpResponse(org.apache.http.HttpResponse response) throws java.io.IOException {
    if (response == null) {
        return;
    }
    if (response.getStatusLine().getStatusCode() != 200) {
        String message = "";
        String returnedValue = EntityUtils.toString(response.getEntity());
        ContentType contentType = ContentType.get(response.getEntity());
        if (contentType != null && ContentType.APPLICATION_JSON.getMimeType().equals(contentType.getMimeType())) {
            JSONObject object = JSONObject.fromObject(returnedValue);
            message = getValueFromJson(object, "message");
            String cause = getValueFromJson(object, "cause");
            if (!cause.isEmpty()) {
                if (!message.isEmpty()) {
                    message = message + "\n" + cause;
                } else {
                    message = cause;
                }
            }
            if (message.isEmpty()) {
                message = response.getStatusLine().getReasonPhrase();
            }
        } else {
            if (returnedValue.isEmpty()) {
                message = response.getStatusLine().getReasonPhrase();
            }
        }
        throw new RuntimeException(message);
    }
}
Also used : ContentType(org.apache.http.entity.ContentType) JSONObject(net.sf.json.JSONObject)

Example 24 with JSONObject

use of net.sf.json.JSONObject in project zaproxy by zaproxy.

the class UsersAPI method handleApiAction.

@Override
public ApiResponse handleApiAction(String name, JSONObject params) throws ApiException {
    log.debug("handleApiAction " + name + " " + params.toString());
    User user;
    Context context;
    switch(name) {
        case ACTION_NEW_USER:
            context = ApiUtils.getContextByParamId(params, PARAM_CONTEXT_ID);
            String userName = ApiUtils.getNonEmptyStringParam(params, PARAM_USER_NAME);
            user = new User(context.getIndex(), userName);
            user.setAuthenticationCredentials(context.getAuthenticationMethod().createAuthenticationCredentials());
            extension.getContextUserAuthManager(context.getIndex()).addUser(user);
            context.save();
            return new ApiResponseElement(PARAM_USER_ID, String.valueOf(user.getId()));
        case ACTION_REMOVE_USER:
            context = ApiUtils.getContextByParamId(params, PARAM_CONTEXT_ID);
            int userId = ApiUtils.getIntParam(params, PARAM_USER_ID);
            boolean deleted = extension.getContextUserAuthManager(context.getIndex()).removeUserById(userId);
            if (deleted) {
                context.save();
                return ApiResponseElement.OK;
            } else
                return ApiResponseElement.FAIL;
        case ACTION_SET_ENABLED:
            boolean enabled = false;
            try {
                enabled = params.getBoolean(PARAM_ENABLED);
            } catch (JSONException e) {
                throw new ApiException(Type.ILLEGAL_PARAMETER, PARAM_ENABLED + " - should be boolean");
            }
            user = getUser(params);
            user.setEnabled(enabled);
            user.getContext().save();
            return ApiResponseElement.OK;
        case ACTION_SET_NAME:
            String nameSN = params.getString(PARAM_USER_NAME);
            if (nameSN == null || nameSN.isEmpty())
                throw new ApiException(Type.MISSING_PARAMETER, PARAM_USER_NAME);
            user = getUser(params);
            user.setName(nameSN);
            user.getContext().save();
            return ApiResponseElement.OK;
        case ACTION_SET_AUTH_CREDENTIALS:
            // Prepare the params
            JSONObject actionParams;
            if (params.has(PARAM_CREDENTIALS_CONFIG_PARAMS))
                actionParams = API.getParams(params.getString(PARAM_CREDENTIALS_CONFIG_PARAMS));
            else
                actionParams = new JSONObject();
            context = ApiUtils.getContextByParamId(params, PARAM_CONTEXT_ID);
            actionParams.put(PARAM_CONTEXT_ID, context.getIndex());
            actionParams.put(PARAM_USER_ID, getUserId(params));
            // Run the method
            ApiDynamicActionImplementor a = loadedAuthenticationMethodActions.get(context.getAuthenticationMethod().getType().getUniqueIdentifier());
            a.handleAction(actionParams);
            context.save();
            return ApiResponseElement.OK;
        default:
            throw new ApiException(Type.BAD_ACTION);
    }
}
Also used : Context(org.zaproxy.zap.model.Context) ApiDynamicActionImplementor(org.zaproxy.zap.extension.api.ApiDynamicActionImplementor) User(org.zaproxy.zap.users.User) JSONObject(net.sf.json.JSONObject) ApiResponseElement(org.zaproxy.zap.extension.api.ApiResponseElement) JSONException(net.sf.json.JSONException) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 25 with JSONObject

use of net.sf.json.JSONObject in project zaproxy by zaproxy.

the class CookieBasedSessionManagementMethodType method getSetMethodForContextApiAction.

@Override
public ApiDynamicActionImplementor getSetMethodForContextApiAction() {
    return new ApiDynamicActionImplementor(API_METHOD_NAME, null, null) {

        @Override
        public void handleAction(JSONObject params) throws ApiException {
            Context context = ApiUtils.getContextByParamId(params, SessionManagementAPI.PARAM_CONTEXT_ID);
            context.setSessionManagementMethod(createSessionManagementMethod(context.getIndex()));
        }
    };
}
Also used : ApiDynamicActionImplementor(org.zaproxy.zap.extension.api.ApiDynamicActionImplementor) Context(org.zaproxy.zap.model.Context) JSONObject(net.sf.json.JSONObject)

Aggregations

JSONObject (net.sf.json.JSONObject)493 Test (org.junit.Test)99 JSONArray (net.sf.json.JSONArray)94 IOException (java.io.IOException)49 HashMap (java.util.HashMap)48 ArrayList (java.util.ArrayList)36 JSON (net.sf.json.JSON)26 PrintWriter (java.io.PrintWriter)25 Map (java.util.Map)23 File (java.io.File)21 InputStream (java.io.InputStream)18 URISyntaxException (java.net.URISyntaxException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 JsonConfig (net.sf.json.JsonConfig)14 FreeStyleBuild (hudson.model.FreeStyleBuild)13 URI (java.net.URI)13 URL (java.net.URL)13 JSONException (net.sf.json.JSONException)13 Context (org.zaproxy.zap.model.Context)12 Transactional (org.springframework.transaction.annotation.Transactional)11