|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.logstash.log; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 23 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 24 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 25 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 26 | +import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
| 27 | +import com.fasterxml.jackson.databind.util.ClassUtil; |
| 28 | +import org.apache.logging.log4j.LogManager; |
| 29 | +import org.apache.logging.log4j.Logger; |
| 30 | +import org.jruby.RubyBasicObject; |
| 31 | +import org.jruby.RubyMethod; |
| 32 | +import org.jruby.RubyString; |
| 33 | +import org.jruby.exceptions.NameError; |
| 34 | +import org.logstash.RubyUtil; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.Optional; |
| 38 | + |
| 39 | +/** |
| 40 | + * Default serializer for {@link org.jruby.RubyBasicObject} since Jackson can't handle that type natively. |
| 41 | + * Arrays, Collections and Maps are delegated to the default Jackson's serializer - which might end-up invoking this |
| 42 | + * serializer for its elements. |
| 43 | + * Values which inspect method is implemented and owned by the LogStash module will be serialized using this method return. |
| 44 | + * If none of the above conditions match, it gets the serialized value by invoking the Ruby's {@code to_s} method, falling back |
| 45 | + * to {@link RubyBasicObject#to_s()} and {@link RubyBasicObject#anyToString()} in case of errors. |
| 46 | + */ |
| 47 | +public final class RubyBasicObjectSerializer extends StdSerializer<RubyBasicObject> { |
| 48 | + |
| 49 | + private static final long serialVersionUID = -5557562960691452054L; |
| 50 | + private static final Logger LOGGER = LogManager.getLogger(RubyBasicObjectSerializer.class); |
| 51 | + private static final String METHOD_INSPECT = "inspect"; |
| 52 | + private static final String METHOD_TO_STRING = "to_s"; |
| 53 | + |
| 54 | + public RubyBasicObjectSerializer() { |
| 55 | + super(RubyBasicObject.class); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void serialize(final RubyBasicObject value, final JsonGenerator gen, final SerializerProvider provider) throws IOException { |
| 60 | + final Optional<JsonSerializer<Object>> serializer = findTypeSerializer(value, provider); |
| 61 | + if (serializer.isPresent()) { |
| 62 | + try { |
| 63 | + serializer.get().serialize(value, gen, provider); |
| 64 | + return; |
| 65 | + } catch (IOException e) { |
| 66 | + LOGGER.debug("Failed to serialize value type {} using default serializer {}", value.getClass(), serializer.get().getClass(), e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (isCustomInspectMethodDefined(value)) { |
| 71 | + try { |
| 72 | + gen.writeString(value.callMethod(METHOD_INSPECT).asJavaString()); |
| 73 | + return; |
| 74 | + } catch (Exception e) { |
| 75 | + LOGGER.debug("Failed to serialize value type {} using the custom `inspect` method", value.getMetaClass(), e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + gen.writeString(value.callMethod(METHOD_TO_STRING).asJavaString()); |
| 81 | + return; |
| 82 | + } catch (Exception e) { |
| 83 | + LOGGER.debug("Failed to serialize value type {} using `to_s` method", value.getMetaClass(), e); |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + gen.writeString(value.to_s().asJavaString()); |
| 88 | + } catch (Exception e) { |
| 89 | + LOGGER.debug("Failed to serialize value type {} using `RubyBasicObject#to_s()` method", value.getMetaClass(), e); |
| 90 | + gen.writeString(value.anyToString().asJavaString()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private Optional<JsonSerializer<Object>> findTypeSerializer(final RubyBasicObject value, final SerializerProvider provider) { |
| 95 | + if (ClassUtil.isCollectionMapOrArray(value.getClass())) { |
| 96 | + try { |
| 97 | + // Delegates the serialization to the Jackson's default serializers, which might |
| 98 | + // end up using this serializer for its elements. |
| 99 | + return Optional.ofNullable(provider.findTypedValueSerializer(value.getJavaClass(), false, null)); |
| 100 | + } catch (JsonMappingException e) { |
| 101 | + // Ignored |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return Optional.empty(); |
| 106 | + } |
| 107 | + |
| 108 | + private boolean isCustomInspectMethodDefined(final RubyBasicObject value) { |
| 109 | + try { |
| 110 | + final Object candidate = value.method(RubyString.newString(RubyUtil.RUBY, METHOD_INSPECT)); |
| 111 | + return candidate instanceof RubyMethod && ((RubyMethod) candidate).owner(RubyUtil.RUBY.getCurrentContext()).toString().toLowerCase().startsWith("logstash"); |
| 112 | + } catch (NameError e) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments