use of com.fasterxml.jackson.databind.ObjectMapper in project camel by apache.
the class ElasticsearchActionRequestConverter method toSearchRequest.
@Converter
public static SearchRequest toSearchRequest(Object queryObject, Exchange exchange) {
SearchRequest searchRequest = new SearchRequest(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class)).types(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
String queryText = null;
if (queryObject instanceof Map<?, ?>) {
Map<String, Object> mapQuery = (Map<String, Object>) queryObject;
// Remove 'query' prefix from the query object for backward compatibility
if (mapQuery.containsKey(ElasticsearchConstants.ES_QUERY_DSL_PREFIX)) {
mapQuery = (Map<String, Object>) mapQuery.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
}
try {
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
queryText = contentBuilder.map(mapQuery).string();
} catch (IOException e) {
LOG.error(e.getMessage());
}
} else if (queryObject instanceof String) {
queryText = (String) queryObject;
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode jsonTextObject = mapper.readValue(queryText, JsonNode.class);
JsonNode parentJsonNode = jsonTextObject.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
if (parentJsonNode != null) {
queryText = parentJsonNode.toString();
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
} else {
// Cannot convert the queryObject into SearchRequest
return null;
}
searchSourceBuilder.query(QueryBuilders.wrapperQuery(queryText));
searchRequest.source(searchSourceBuilder);
return searchRequest;
}
use of com.fasterxml.jackson.databind.ObjectMapper in project camel by apache.
the class EndpointCompletionTest method assertParameterJsonSchema.
private String assertParameterJsonSchema(MBeanServer mbeanServer, ObjectName on, String componentName) throws Exception {
Object[] params = { componentName };
String[] signature = { "java.lang.String" };
String answer = assertIsInstanceOf(String.class, mbeanServer.invoke(on, "componentParameterJsonSchema", params, signature));
LOG.info("Component " + componentName + " returned JSON: " + answer);
// now lets validate that the generated JSON parses correctly
ObjectMapper mapper = new ObjectMapper();
HashMap data = mapper.readValue(answer, HashMap.class);
LOG.info("Read JSON: " + data);
return answer;
}
use of com.fasterxml.jackson.databind.ObjectMapper in project camel by apache.
the class JacksonObjectMapperTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
ObjectMapper mapper = new ObjectMapper();
JacksonDataFormat format = new JacksonDataFormat();
format.setObjectMapper(mapper);
from("direct:in").marshal(format);
from("direct:back").unmarshal(format).to("mock:reverse");
JacksonDataFormat prettyPrintDataFormat = new JacksonDataFormat();
prettyPrintDataFormat.setPrettyPrint(true);
from("direct:inPretty").marshal(prettyPrintDataFormat);
from("direct:backPretty").unmarshal(prettyPrintDataFormat).to("mock:reverse");
JacksonDataFormat formatPojo = new JacksonDataFormat(TestPojo.class);
from("direct:inPojo").marshal(formatPojo);
from("direct:backPojo").unmarshal(formatPojo).to("mock:reversePojo");
}
};
}
use of com.fasterxml.jackson.databind.ObjectMapper in project camel by apache.
the class LinkedInOAuthRequestFilter method getAccessToken.
private OAuthToken getAccessToken(String refreshToken) throws IOException {
final String tokenUrl = String.format(ACCESS_TOKEN_URL, refreshToken, oAuthParams.getRedirectUri(), oAuthParams.getClientId(), oAuthParams.getClientSecret());
final WebRequest webRequest = new WebRequest(new URL(tokenUrl), HttpMethod.POST);
final WebResponse webResponse = webClient.loadWebResponse(webRequest);
if (webResponse.getStatusCode() != HttpStatus.SC_OK) {
throw new IOException(String.format("Error getting access token: [%s: %s]", webResponse.getStatusCode(), webResponse.getStatusMessage()));
}
final long currentTime = System.currentTimeMillis();
final ObjectMapper mapper = new ObjectMapper();
final Map map = mapper.readValue(webResponse.getContentAsStream(), Map.class);
final String accessToken = map.get("access_token").toString();
final Integer expiresIn = Integer.valueOf(map.get("expires_in").toString());
return new OAuthToken(refreshToken, accessToken, currentTime + TimeUnit.MILLISECONDS.convert(expiresIn, TimeUnit.SECONDS));
}
use of com.fasterxml.jackson.databind.ObjectMapper in project camel by apache.
the class MetricsMessageHistoryService method doStart.
@Override
protected void doStart() throws Exception {
if (metricsRegistry == null) {
Registry camelRegistry = getCamelContext().getRegistry();
metricsRegistry = camelRegistry.lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class);
// create a new metricsRegistry by default
if (metricsRegistry == null) {
metricsRegistry = new MetricRegistry();
}
}
if (useJmx) {
ManagementAgent agent = getCamelContext().getManagementStrategy().getManagementAgent();
if (agent != null) {
MBeanServer server = agent.getMBeanServer();
if (server != null) {
reporter = JmxReporter.forRegistry(metricsRegistry).registerWith(server).inDomain(jmxDomain).build();
reporter.start();
}
} else {
throw new IllegalStateException("CamelContext has not enabled JMX");
}
}
// json mapper
this.mapper = new ObjectMapper().registerModule(new MetricsModule(getRateUnit(), getDurationUnit(), false));
if (getRateUnit() == TimeUnit.SECONDS && getDurationUnit() == TimeUnit.SECONDS) {
// they both use same units so reuse
this.secondsMapper = this.mapper;
} else {
this.secondsMapper = new ObjectMapper().registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false));
}
}
Aggregations