Search in sources :

Example 76 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project buck by facebook.

the class DistBuildTargetGraphCodec method dump.

public BuildJobStateTargetGraph dump(Collection<TargetNode<?, ?>> targetNodes, Function<Path, Integer> cellIndexer) {
    BuildJobStateTargetGraph result = new BuildJobStateTargetGraph();
    for (TargetNode<?, ?> targetNode : targetNodes) {
        Map<String, Object> rawTargetNode = nodeToRawNode.apply(targetNode);
        ProjectFilesystem projectFilesystem = targetNode.getFilesystem();
        BuildJobStateTargetNode remoteNode = new BuildJobStateTargetNode();
        remoteNode.setCellIndex(cellIndexer.apply(projectFilesystem.getRootPath()));
        remoteNode.setBuildTarget(encodeBuildTarget(targetNode.getBuildTarget()));
        try {
            remoteNode.setRawNode(objectMapper.writeValueAsString(rawTargetNode));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        result.addToNodes(remoteNode);
    }
    return result;
}
Also used : BuildJobStateTargetNode(com.facebook.buck.distributed.thrift.BuildJobStateTargetNode) BuildJobStateTargetGraph(com.facebook.buck.distributed.thrift.BuildJobStateTargetGraph) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 77 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project buck by facebook.

the class DoctorReportHelper method uploadRequest.

public DoctorEndpointResponse uploadRequest(DoctorEndpointRequest request) {
    if (!doctorConfig.getEndpointUrl().isPresent()) {
        String errorMsg = String.format("Doctor endpoint URL is not set. Please set [%s] %s on your .buckconfig", DoctorConfig.DOCTOR_SECTION, DoctorConfig.URL_FIELD);
        return createErrorDoctorEndpointResponse(errorMsg);
    }
    byte[] requestJson;
    try {
        requestJson = objectMapper.writeValueAsBytes(request);
    } catch (JsonProcessingException e) {
        return createErrorDoctorEndpointResponse("Failed to encode request to JSON. " + "Reason: " + e.getMessage());
    }
    OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).readTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).writeTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).build();
    Response httpResponse;
    try {
        RequestBody requestBody;
        ImmutableMap<String, String> extraArgs = doctorConfig.getExtraRequestArgs();
        if (extraArgs.isEmpty()) {
            requestBody = RequestBody.create(JSON, requestJson);
        } else {
            FormBody.Builder formBody = new FormBody.Builder();
            formBody.add("data", new String(requestJson));
            for (Map.Entry<String, String> entry : extraArgs.entrySet()) {
                formBody.add(entry.getKey(), entry.getValue());
            }
            requestBody = formBody.build();
        }
        Request httpRequest = new Request.Builder().url(doctorConfig.getEndpointUrl().get()).post(requestBody).build();
        httpResponse = httpClient.newCall(httpRequest).execute();
    } catch (IOException e) {
        return createErrorDoctorEndpointResponse("Failed to perform the request to " + doctorConfig.getEndpointUrl().get() + ". Reason: " + e.getMessage());
    }
    try {
        if (httpResponse.isSuccessful()) {
            String body = new String(httpResponse.body().bytes(), Charsets.UTF_8);
            return objectMapper.readValue(body, DoctorEndpointResponse.class);
        }
        return createErrorDoctorEndpointResponse("Request was not successful.");
    } catch (JsonProcessingException e) {
        return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
    } catch (IOException e) {
        return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) FormBody(okhttp3.FormBody) DoctorEndpointRequest(com.facebook.buck.doctor.config.DoctorEndpointRequest) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) DoctorEndpointResponse(com.facebook.buck.doctor.config.DoctorEndpointResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) RequestBody(okhttp3.RequestBody)

Example 78 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project moco by dreamhead.

the class JsonRequestMatcher method doMatch.

private boolean doMatch(final Request request, final byte[] content) {
    try {
        JsonNode requestNode = mapper.readTree(content);
        JsonNode resourceNode = mapper.readTree(expected.readFor(of(request)).getContent());
        return requestNode.equals(resourceNode);
    } catch (JsonProcessingException jpe) {
        return false;
    } catch (IOException e) {
        throw new MocoException(e);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) MocoException(com.github.dreamhead.moco.MocoException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 79 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project dropwizard by dropwizard.

the class DefaultLoggingFactory method configureLoggers.

private Logger configureLoggers(String name) {
    final Logger root = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    loggerContext.reset();
    final LevelChangePropagator propagator = new LevelChangePropagator();
    propagator.setContext(loggerContext);
    propagator.setResetJUL(true);
    loggerContext.addListener(propagator);
    root.setLevel(level);
    final LevelFilterFactory<ILoggingEvent> levelFilterFactory = new ThresholdLevelFilterFactory();
    final AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory = new AsyncLoggingEventAppenderFactory();
    final LayoutFactory<ILoggingEvent> layoutFactory = new DropwizardLayoutFactory();
    for (Map.Entry<String, JsonNode> entry : loggers.entrySet()) {
        final Logger logger = loggerContext.getLogger(entry.getKey());
        final JsonNode jsonNode = entry.getValue();
        if (jsonNode.isTextual()) {
            // Just a level as a string
            logger.setLevel(Level.valueOf(jsonNode.asText()));
        } else if (jsonNode.isObject()) {
            // A level and an appender
            final LoggerConfiguration configuration;
            try {
                configuration = Jackson.newObjectMapper().treeToValue(jsonNode, LoggerConfiguration.class);
            } catch (JsonProcessingException e) {
                throw new IllegalArgumentException("Wrong format of logger '" + entry.getKey() + "'", e);
            }
            logger.setLevel(configuration.getLevel());
            logger.setAdditive(configuration.isAdditive());
            for (AppenderFactory<ILoggingEvent> appender : configuration.getAppenders()) {
                logger.addAppender(appender.build(loggerContext, name, layoutFactory, levelFilterFactory, asyncAppenderFactory));
            }
        } else {
            throw new IllegalArgumentException("Unsupported format of logger '" + entry.getKey() + "'");
        }
    }
    return root;
}
Also used : AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) AsyncAppenderFactory(io.dropwizard.logging.async.AsyncAppenderFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) ThresholdLevelFilterFactory(io.dropwizard.logging.filter.ThresholdLevelFilterFactory) AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) LevelChangePropagator(ch.qos.logback.classic.jul.LevelChangePropagator) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 80 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project kafka by apache.

the class VerifiableSourceTask method poll.

@Override
public List<SourceRecord> poll() throws InterruptedException {
    long sendStartMs = System.currentTimeMillis();
    if (throttler.shouldThrottle(seqno - startingSeqno, sendStartMs))
        throttler.throttle();
    long nowMs = System.currentTimeMillis();
    Map<String, Object> data = new HashMap<>();
    data.put("name", name);
    data.put("task", id);
    data.put("topic", this.topic);
    data.put("time_ms", nowMs);
    data.put("seqno", seqno);
    String dataJson;
    try {
        dataJson = JSON_SERDE.writeValueAsString(data);
    } catch (JsonProcessingException e) {
        dataJson = "Bad data can't be written as json: " + e.getMessage();
    }
    System.out.println(dataJson);
    Map<String, Long> ccOffset = Collections.singletonMap(SEQNO_FIELD, seqno);
    SourceRecord srcRecord = new SourceRecord(partition, ccOffset, topic, Schema.INT32_SCHEMA, id, Schema.INT64_SCHEMA, seqno);
    List<SourceRecord> result = Arrays.asList(srcRecord);
    seqno++;
    return result;
}
Also used : HashMap(java.util.HashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SourceRecord(org.apache.kafka.connect.source.SourceRecord)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)153 IOException (java.io.IOException)43 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)42 HashMap (java.util.HashMap)18 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)14 InputStream (java.io.InputStream)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 List (java.util.List)8 Map (java.util.Map)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 ErrorResponseBody (com.nike.riposte.server.error.handler.ErrorResponseBody)6 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)6 OutputStreamWriter (java.io.OutputStreamWriter)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 TaskDTO (com.linkedin.thirdeye.datalayer.dto.TaskDTO)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 SimpleDateFormat (java.text.SimpleDateFormat)4 Test (org.testng.annotations.Test)4