Search in sources :

Example 11 with JSONException

use of net.sf.json.JSONException in project phabricator-jenkins-plugin by uber.

the class BuildResultProcessor method processLintResults.

/**
 * Fetch remote lint violations from the build workspace and process
 *
 * @param lintFile     the path pattern of the file
 * @param lintFileSize maximum number of bytes to read from the remote file
 */
public void processLintResults(String lintFile, String lintFileSize) {
    RemoteFileFetcher lintFetcher = new RemoteFileFetcher(workspace, logger, lintFile, lintFileSize);
    try {
        String input = lintFetcher.getRemoteFile();
        if (input != null && input.length() > 0) {
            lintResults = new LintResults();
            BufferedReader reader = new BufferedReader(new StringReader(input));
            String lint = "";
            String line;
            while ((line = reader.readLine()) != null) {
                lint += line;
                try {
                    JSONObject json = JSONObject.fromObject(lint);
                    lintResults.add(LintResult.fromJsonObject(json));
                    lint = "";
                } catch (JSONException e) {
                    e.printStackTrace(logger.getStream());
                }
            }
        }
    } catch (InterruptedException e) {
        e.printStackTrace(logger.getStream());
    } catch (IOException e) {
        e.printStackTrace(logger.getStream());
    }
}
Also used : JSONObject(net.sf.json.JSONObject) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) JSONException(net.sf.json.JSONException) LintResults(com.uber.jenkins.phabricator.lint.LintResults) IOException(java.io.IOException)

Example 12 with JSONException

use of net.sf.json.JSONException in project sonar-scanner-jenkins by SonarSource.

the class SonarQubeWebHook method doIndex.

@RequirePOST
public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException {
    String payload = IOUtils.toString(req.getReader());
    LOGGER.info("Received POST from " + req.getRemoteHost());
    try {
        JSONObject o = JSONObject.fromObject(payload);
        LOGGER.fine(() -> "Full details of the POST was " + o.toString());
        String taskId = o.getString("taskId");
        String status = o.getString("status");
        String qgStatus = null;
        if (CETask.STATUS_SUCCESS.equals(status)) {
            qgStatus = o.has("qualityGate") ? o.getJSONObject("qualityGate").getString("status") : "NONE";
        }
        for (Listener listener : listeners) {
            listener.onTaskCompleted(taskId, status, qgStatus);
        }
    } catch (JSONException e) {
        LOGGER.log(Level.WARNING, e, () -> "Invalid payload " + payload);
        rsp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid JSON Payload");
    }
    rsp.setStatus(HttpServletResponse.SC_OK);
}
Also used : JSONObject(net.sf.json.JSONObject) JSONException(net.sf.json.JSONException) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 13 with JSONException

use of net.sf.json.JSONException in project sonar-scanner-jenkins by SonarSource.

the class WsClient method getCETask.

public CETask getCETask(String taskId) {
    String url = serverUrl + API_CE_TASK + taskId;
    String text = client.getHttp(url, token);
    try {
        JSONObject json = (JSONObject) JSONSerializer.toJSON(text);
        JSONObject task = json.getJSONObject("task");
        String status = task.getString(STATUS_ATTR);
        String componentName = task.getString("componentName");
        String componentKey = task.getString("componentKey");
        // No analysisId if task is pending
        String analysisId = task.optString("analysisId", null);
        return new CETask(status, componentName, componentKey, url, analysisId);
    } catch (JSONException e) {
        throw new IllegalStateException("Unable to parse response from " + url + ":\n" + text, e);
    }
}
Also used : JSONObject(net.sf.json.JSONObject) JSONException(net.sf.json.JSONException)

Example 14 with JSONException

use of net.sf.json.JSONException in project sonar-scanner-jenkins by SonarSource.

the class WsClient method requestQualityGateStatus.

public String requestQualityGateStatus(String analysisId) {
    String url = serverUrl + API_PROJECT_STATUS_WITH_ANALYSISID + encode(analysisId);
    String text = client.getHttp(url, token);
    try {
        JSONObject json = (JSONObject) JSONSerializer.toJSON(text);
        JSONObject projectStatus = json.getJSONObject("projectStatus");
        return projectStatus.getString(STATUS_ATTR);
    } catch (JSONException e) {
        throw new IllegalStateException("Unable to parse response from " + url + ":\n" + text, e);
    }
}
Also used : JSONObject(net.sf.json.JSONObject) JSONException(net.sf.json.JSONException)

Example 15 with JSONException

use of net.sf.json.JSONException in project camel by apache.

the class XmlJsonExceptionsTest method testSendJsonToXML.

@Test
public void testSendJsonToXML() throws Exception {
    String in = "{ \"a\": 123, \"b\": true, \"c\": true2 }";
    MockEndpoint mockJSON = getMockEndpoint("mock:xml");
    mockJSON.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)

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