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);
}
};
}
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);
}
}
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);
}
}
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);
}
}
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()));
}
};
}
Aggregations