Search in sources :

Example 36 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project databus by linkedin.

the class InteractiveSchemaGenerator method filterUserFields.

private String filterUserFields(String schema) throws IOException, DatabusException {
    //No filtering is necessary, just return the same schema
    if (!_areFieldsFiltered) {
        return schema;
    }
    Schema avroSchema = Schema.parse(schema);
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = new JsonFactory();
    StringWriter writer = new StringWriter();
    JsonGenerator jgen = factory.createJsonGenerator(writer);
    jgen.useDefaultPrettyPrinter();
    @SuppressWarnings("checked") HashMap<String, Object> schemaMap = new ObjectMapper().readValue(schema, HashMap.class);
    @SuppressWarnings("checked") ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) schemaMap.get("fields");
    int i = 0;
    while (i < list.size()) {
        Schema.Field field = avroSchema.getField(list.get(i).get("name"));
        String dbFieldName;
        if (field.schema().getType() == Schema.Type.ARRAY) {
            throw new DatabusException("Field not supported for filtering");
        //TODO fix this
        /*
        String innerSchema = field.getProp("items");
        if(innerSchema == null)
          throw new DatabusException("Unable to the inner schema type of the array");
        Schema innerSchemaParsed = Schema.parse(innerSchema);
        dbFieldName = SchemaHelper.getMetaField(innerSchemaParsed, "dbFieldName");
        */
        } else
            dbFieldName = SchemaHelper.getMetaField(field, "dbFieldName");
        if (dbFieldName == null)
            throw new DatabusException("Unable to determine the dbFieldName from the meta information");
        if (!_userFields.contains(dbFieldName.toUpperCase(Locale.ENGLISH)))
            list.remove(i);
        else
            i++;
    }
    mapper.writeValue(jgen, schemaMap);
    return writer.getBuffer().toString();
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) JsonFactory(org.codehaus.jackson.JsonFactory) ArrayList(java.util.ArrayList) StringWriter(java.io.StringWriter) DatabusException(com.linkedin.databus2.core.DatabusException) JsonGenerator(org.codehaus.jackson.JsonGenerator) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 37 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project databus by linkedin.

the class SchemaMetaDataManager method persistPhysicalToLogicalSrcMap.

private void persistPhysicalToLogicalSrcMap() throws IOException {
    ObjectMapper m = new ObjectMapper();
    String str = m.defaultPrettyPrintingWriter().writeValueAsString(_physicalToLogicalSrcMap);
    File tmpFile = File.createTempFile("phyToLogicalSrc", ".json.tmp");
    FileWriter oStream = null;
    try {
        oStream = new FileWriter(tmpFile);
        oStream.append(str);
    } finally {
        if (null != oStream)
            oStream.close();
    }
    File destFile = new File(_physicalToLogicalSrcMapFile);
    boolean success = tmpFile.renameTo(destFile);
    if (!success)
        throw new RuntimeException("Unable to persist the mapping json file !!");
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 38 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project databus by linkedin.

the class TestPhysicalSourceConfig method mapToJsonStr.

private String mapToJsonStr(Map<String, Object> map) throws JsonGenerationException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    StringWriter writer = new StringWriter();
    mapper.writeValue(writer, map);
    String str = writer.toString();
    return str;
}
Also used : StringWriter(java.io.StringWriter) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 39 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project databus by linkedin.

the class RegisterRequestProcessor method process.

@Override
public DatabusRequest process(DatabusRequest request) throws IOException, RequestProcessingException {
    try {
        // fail early if optional version param is included but isn't valid
        // 2 and 3 are same for us; 4 is a superset only newer clients understand
        int registerRequestProtocolVersion = 3;
        String registerRequestProtocolVersionStr = request.getParams().getProperty(DatabusHttpHeaders.PROTOCOL_VERSION_PARAM);
        if (registerRequestProtocolVersionStr != null) {
            try {
                registerRequestProtocolVersion = Integer.parseInt(registerRequestProtocolVersionStr);
            } catch (NumberFormatException e) {
                LOG.error("Could not parse /register request protocol version: " + registerRequestProtocolVersionStr);
                throw new InvalidRequestParamValueException(COMMAND_NAME, DatabusHttpHeaders.PROTOCOL_VERSION_PARAM, registerRequestProtocolVersionStr);
            }
            if (registerRequestProtocolVersion < 2 || registerRequestProtocolVersion > 4) {
                LOG.error("Out-of-range /register request protocol version: " + registerRequestProtocolVersionStr);
                throw new InvalidRequestParamValueException(COMMAND_NAME, DatabusHttpHeaders.PROTOCOL_VERSION_PARAM, registerRequestProtocolVersionStr);
            }
        }
        Collection<LogicalSource> logicalSources = null;
        HttpStatisticsCollector relayStatsCollector = _relay.getHttpStatisticsCollector();
        String sources = request.getParams().getProperty(SOURCES_PARAM);
        if (null == sources) {
            // need to return all schemas, so first get all sources
            logicalSources = _relay.getSourcesIdNameRegistry().getAllSources();
        } else {
            String[] sourceIds = sources.split(",");
            logicalSources = new ArrayList<LogicalSource>(sourceIds.length);
            for (String sourceId : sourceIds) {
                int srcId;
                String trimmedSourceId = sourceId.trim();
                try {
                    srcId = Integer.valueOf(trimmedSourceId);
                    LogicalSource lsource = _relay.getSourcesIdNameRegistry().getSource(srcId);
                    if (null != lsource)
                        logicalSources.add(lsource);
                    else {
                        LOG.error("No source name for source id: " + srcId);
                        throw new InvalidRequestParamValueException(COMMAND_NAME, SOURCES_PARAM, sourceId);
                    }
                } catch (NumberFormatException nfe) {
                    if (relayStatsCollector != null) {
                        relayStatsCollector.registerInvalidRegisterCall();
                    }
                    throw new InvalidRequestParamValueException(COMMAND_NAME, SOURCES_PARAM, sourceId);
                }
            }
        }
        SchemaRegistryService schemaRegistry = _relay.getSchemaRegistryService();
        ArrayList<RegisterResponseEntry> registeredSources = new ArrayList<RegisterResponseEntry>(20);
        for (LogicalSource lsource : logicalSources) {
            getSchemas(schemaRegistry, lsource.getName(), lsource.getId(), sources, registeredSources);
        }
        // Note that, as of April 2013, the Espresso sandbox's schema registry
        // (in JSON format) is 4.5 MB and growing.  But 100 KB is probably OK
        // for regular production cases.
        StringWriter out = new StringWriter(102400);
        ObjectMapper mapper = new ObjectMapper();
        // any circumstances under which we might want to override this?
        int registerResponseProtocolVersion = registerRequestProtocolVersion;
        if (// DDSDBUS-2009
        registerRequestProtocolVersion == 4) {
            LOG.debug("Got version 4 /register request; fetching metadata schema.");
            // Get (replication) metadata schema from registry; format it as list
            // of schemas (multiple only if more than one version exists).  Per
            // https://iwww.corp.linkedin.com/wiki/cf/display/ENGS/Espresso+Metadata+Schema,
            // name of replication metadata is simply "metadata".
            ArrayList<RegisterResponseMetadataEntry> registeredMetadata = new ArrayList<RegisterResponseMetadataEntry>(2);
            getMetadataSchemas(schemaRegistry, registeredMetadata);
            // Set up the v4 response as a map:  one entry is the existing list of source
            // schemas, and the others (if present) are the new lists of metadata schema(s)
            // and (TODO) key schemas.
            HashMap<String, List<Object>> responseMap = new HashMap<String, List<Object>>(4);
            responseMap.put(RegisterResponseEntry.SOURCE_SCHEMAS_KEY, (List<Object>) (List<?>) registeredSources);
            if (registeredMetadata.size() > 0) {
                LOG.debug("Sending v4 /register response with metadata schema.");
                responseMap.put(RegisterResponseMetadataEntry.METADATA_SCHEMAS_KEY, (List<Object>) (List<?>) registeredMetadata);
            } else {
                LOG.debug("No metadata schema available; sending v4 /register response without.");
            }
            // TODO:  figure out how to retrieve key schemas and include via RegisterResponseEntry.KEY_SCHEMAS_KEY
            mapper.writeValue(out, responseMap);
        } else // fall back to old style (v2/v3 response)
        {
            mapper.writeValue(out, registeredSources);
        }
        ChunkedWritableByteChannel responseContent = request.getResponseContent();
        byte[] resultBytes = out.toString().getBytes(Charset.defaultCharset());
        responseContent.addMetadata(DatabusHttpHeaders.DBUS_CLIENT_RELAY_PROTOCOL_VERSION_HDR, registerResponseProtocolVersion);
        responseContent.write(ByteBuffer.wrap(resultBytes));
        if (null != relayStatsCollector) {
            HttpStatisticsCollector connStatsCollector = (HttpStatisticsCollector) request.getParams().get(relayStatsCollector.getName());
            if (null != connStatsCollector) {
                connStatsCollector.registerRegisterCall(registeredSources);
            } else {
                relayStatsCollector.registerRegisterCall(registeredSources);
            }
        }
        return request;
    } catch (InvalidRequestParamValueException e) {
        HttpStatisticsCollector relayStatsCollector = _relay.getHttpStatisticsCollector();
        if (null != relayStatsCollector)
            relayStatsCollector.registerInvalidRegisterCall();
        throw e;
    }
}
Also used : ChunkedWritableByteChannel(com.linkedin.databus2.core.container.ChunkedWritableByteChannel) HashMap(java.util.HashMap) SchemaRegistryService(com.linkedin.databus2.schemas.SchemaRegistryService) ArrayList(java.util.ArrayList) LogicalSource(com.linkedin.databus.core.data_model.LogicalSource) InvalidRequestParamValueException(com.linkedin.databus2.core.container.request.InvalidRequestParamValueException) StringWriter(java.io.StringWriter) RegisterResponseMetadataEntry(com.linkedin.databus2.core.container.request.RegisterResponseMetadataEntry) HttpStatisticsCollector(com.linkedin.databus2.core.container.monitoring.mbean.HttpStatisticsCollector) RegisterResponseEntry(com.linkedin.databus2.core.container.request.RegisterResponseEntry) ArrayList(java.util.ArrayList) List(java.util.List) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 40 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project databus by linkedin.

the class SourcesRequestProcessor method process.

@Override
public DatabusRequest process(DatabusRequest request) throws IOException, RequestProcessingException {
    int protoVersion = request.getOptionalIntParam(VERSION_PARAM_NAME, 1);
    ObjectMapper mapper = new ObjectMapper();
    StringWriter out = new StringWriter(10240);
    Collection<LogicalSource> sources = _relay.getSourcesIdNameRegistry().getAllSources();
    if (1 == protoVersion) {
        ArrayList<IdNamePair> sourcePairs = new ArrayList<IdNamePair>(sources.size());
        for (LogicalSource source : sources) sourcePairs.add(new IdNamePair(source.getId().longValue(), source.getName()));
        mapper.writeValue(out, sourcePairs);
    } else if (2 == protoVersion)
        mapper.writeValue(out, sources);
    else
        throw new InvalidRequestParamValueException(COMMAND_NAME, VERSION_PARAM_NAME, Integer.toString(protoVersion));
    byte[] resultBytes = out.toString().getBytes(Charset.defaultCharset());
    request.getResponseContent().write(ByteBuffer.wrap(resultBytes));
    HttpStatisticsCollector relayStatsCollector = _relay.getHttpStatisticsCollector();
    if (null != relayStatsCollector) {
        HttpStatisticsCollector connStatsCollector = (HttpStatisticsCollector) request.getParams().get(relayStatsCollector.getName());
        if (null != connStatsCollector) {
            connStatsCollector.registerSourcesCall();
        } else {
            relayStatsCollector.registerSourcesCall();
        }
    }
    return request;
}
Also used : StringWriter(java.io.StringWriter) HttpStatisticsCollector(com.linkedin.databus2.core.container.monitoring.mbean.HttpStatisticsCollector) ArrayList(java.util.ArrayList) IdNamePair(com.linkedin.databus.core.util.IdNamePair) LogicalSource(com.linkedin.databus.core.data_model.LogicalSource) InvalidRequestParamValueException(com.linkedin.databus2.core.container.request.InvalidRequestParamValueException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

ObjectMapper (org.codehaus.jackson.map.ObjectMapper)386 IOException (java.io.IOException)85 Test (org.junit.Test)61 JsonNode (org.codehaus.jackson.JsonNode)50 HashMap (java.util.HashMap)46 ArrayList (java.util.ArrayList)45 Test (org.testng.annotations.Test)37 Map (java.util.Map)34 List (java.util.List)26 File (java.io.File)22 StringWriter (java.io.StringWriter)22 JSONObject (org.json.JSONObject)18 SimpleModule (org.codehaus.jackson.map.module.SimpleModule)17 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 Version (org.codehaus.jackson.Version)14 JsonFactory (org.codehaus.jackson.JsonFactory)13 JSONObject (org.codehaus.jettison.json.JSONObject)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 JsonGenerator (org.codehaus.jackson.JsonGenerator)11