Search in sources :

Example 16 with JSONException

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();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) JSONException(net.sf.json.JSONException) CamelExecutionException(org.apache.camel.CamelExecutionException) JSONException(net.sf.json.JSONException) Test(org.junit.Test)

Example 17 with JSONException

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();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) JSONException(net.sf.json.JSONException) CamelExecutionException(org.apache.camel.CamelExecutionException) JSONException(net.sf.json.JSONException) Test(org.junit.Test)

Example 18 with JSONException

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);
    }
}
Also used : StringWriter(java.io.StringWriter) JSONException(net.sf.json.JSONException) PrintWriter(java.io.PrintWriter)

Example 19 with JSONException

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;
}
Also used : JSONException(net.sf.json.JSONException) JSONObject(net.sf.json.JSONObject) Method(java.lang.reflect.Method) JSONException(net.sf.json.JSONException)

Example 20 with JSONException

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;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(net.sf.json.JSONArray) JSONException(net.sf.json.JSONException)

Aggregations

JSONException (net.sf.json.JSONException)24 JSONObject (net.sf.json.JSONObject)13 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 DatabaseException (org.parosproxy.paros.db.DatabaseException)4 ApiException (org.zaproxy.zap.extension.api.ApiException)4 Context (org.zaproxy.zap.model.Context)4 JSONArray (net.sf.json.JSONArray)3 CamelExecutionException (org.apache.camel.CamelExecutionException)3 Exchange (org.apache.camel.Exchange)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 URIException (org.apache.commons.httpclient.URIException)3 Test (org.junit.Test)3 Session (org.parosproxy.paros.model.Session)3 ApiResponseElement (org.zaproxy.zap.extension.api.ApiResponseElement)3 User (org.zaproxy.zap.users.User)3 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2