use of net.sf.json.JSONException in project camel by apache.
the class XmlJsonExceptionsTest method testMalformedXML.
@Test
public void testMalformedXML() throws Exception {
String in = "<noRoot>abc</noRoot><noRoot>abc</noRoot>";
MockEndpoint mockJSON = getMockEndpoint("mock:json");
mockJSON.expectedMessageCount(0);
MockEndpoint mockException = getMockEndpoint("mock:exception");
mockException.expectedMessageCount(1);
try {
template.requestBody("direct:marshal", in);
fail("Exception expected");
} catch (CamelExecutionException e) {
assertEquals("JSONException expected", JSONException.class, e.getCause().getClass());
}
List<Exchange> exchs = mockException.getExchanges();
assertEquals("Only one exchange was expected in mock:exception", 1, exchs.size());
Exception e = (Exception) exchs.get(0).getProperty(Exchange.EXCEPTION_CAUGHT);
assertNotNull("Exception expected", e);
assertEquals("JSONException expected", JSONException.class, e.getClass());
assertMockEndpointsSatisfied();
}
use of net.sf.json.JSONException in project camel by apache.
the class XmlJsonExceptionsTest method testMalformedJson.
@Test
public void testMalformedJson() throws Exception {
String in = "{ \"a\": 123, \"b\": true, \"c\": true2 }";
MockEndpoint mockXML = getMockEndpoint("mock:xml");
mockXML.expectedMessageCount(0);
MockEndpoint mockException = getMockEndpoint("mock:exception");
mockException.expectedMessageCount(1);
try {
template.requestBody("direct:unmarshal", in);
fail("Exception expected");
} catch (CamelExecutionException e) {
assertEquals("JSONException expected", JSONException.class, e.getCause().getClass());
}
List<Exchange> exchs = mockException.getExchanges();
assertEquals("Only one exchange was expected in mock:exception", 1, exchs.size());
Exception e = (Exception) exchs.get(0).getProperty(Exchange.EXCEPTION_CAUGHT);
assertNotNull("Exception expected", e);
assertEquals("JSONException expected", JSONException.class, e.getClass());
assertMockEndpointsSatisfied();
}
use of net.sf.json.JSONException in project hudson-2.x by hudson.
the class Job method doConfigSubmit.
//
//
// actions
//
//
/**
* Accepts submission from the configuration page.
*/
public synchronized void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, FormException {
checkPermission(CONFIGURE);
try {
setAllowSave(false);
submit(req, rsp);
setAllowSave(true);
save();
String newName = req.getParameter("name");
if (newName != null && !newName.equals(name)) {
// check this error early to avoid HTTP response splitting.
Hudson.checkGoodName(newName);
rsp.sendRedirect("rename?newName=" + URLEncoder.encode(newName, "UTF-8"));
} else {
rsp.sendRedirect(".");
}
} catch (JSONException e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("Failed to parse form data. Please report this problem as a bug");
pw.println("JSON=" + req.getSubmittedForm());
pw.println();
e.printStackTrace(pw);
rsp.setStatus(SC_BAD_REQUEST);
sendError(sw.toString(), req, rsp, true);
}
}
use of net.sf.json.JSONException in project zaproxy by zaproxy.
the class ApiImplementor method handleApiOptionAction.
public ApiResponse handleApiOptionAction(String name, JSONObject params) throws ApiException {
if (this.param == null) {
return null;
}
boolean isApiOption = false;
if (name.startsWith(SET_OPTION_PREFIX)) {
name = "set" + name.substring(SET_OPTION_PREFIX.length());
isApiOption = true;
} else if (name.startsWith(ADD_OPTION_PREFIX)) {
name = "add" + name.substring(ADD_OPTION_PREFIX.length());
isApiOption = true;
} else if (name.startsWith(REMOVE_OPTION_PREFIX)) {
name = "remove" + name.substring(REMOVE_OPTION_PREFIX.length());
isApiOption = true;
}
if (isApiOption) {
try {
Method[] methods = param.getClass().getDeclaredMethods();
for (Method method : methods) {
if (isIgnored(method)) {
continue;
}
if (method.getName().equals(name) && method.getParameterTypes().length == 1) {
Object val = null;
if (method.getParameterTypes()[0].equals(String.class)) {
val = params.getString("String");
} else if (method.getParameterTypes()[0].equals(Integer.class) || method.getParameterTypes()[0].equals(int.class)) {
try {
val = params.getInt("Integer");
} catch (JSONException e) {
throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, "Integer");
}
} else if (method.getParameterTypes()[0].equals(Boolean.class) || method.getParameterTypes()[0].equals(boolean.class)) {
try {
val = params.getBoolean("Boolean");
} catch (JSONException e) {
throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, "Boolean");
}
}
if (val == null) {
// Value supplied doesnt match the type - try the next one
continue;
}
method.invoke(this.param, val);
return ApiResponseElement.OK;
}
}
} catch (ApiException e) {
throw e;
} catch (Exception e) {
throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
}
}
return null;
}
use of net.sf.json.JSONException in project compiler by boalang.
the class GetGithubRepoLanguagesMulti method getLanguages.
private static ArrayList<GithubLanguage> getLanguages(String content) {
ArrayList<GithubLanguage> languages = new ArrayList<GithubLanguage>();
JSONArray repos = null;
try {
repos = (JSONArray) JSONSerializer.toJSON(content);
} catch (JSONException e) {
}
if (repos == null) {
System.err.println("Error parsing file\n" + content);
return languages;
}
int status = 0, s = 0;
String name = null, count;
for (int i = 0; i < content.length(); i++) {
if (status == 0 && content.charAt(i) == '\"') {
status = 1;
s = i + 1;
} else if (status == 1 && content.charAt(i) == '\"') {
status = 2;
name = content.substring(s, i);
} else if (status == 2 && content.charAt(i) == ':') {
status = 3;
s = i + 1;
} else if (status == 3 && !Character.isDigit(content.charAt(i))) {
status = 0;
count = content.substring(s, i);
languages.add(new GithubLanguage(name, count));
}
}
return languages;
}
Aggregations