Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public void customize(List<BeanFieldMaskerSelector> selectors, ObjectMapper obje
module.setDeserializerModifier(new MaskingBeanDeserializerModifier(maskerCache));
module.addSerializer(new AnnotatedRequestJsonSerializer(maskerCache));
module.addSerializer(new AnnotatedResponseJsonSerializer(maskerCache));
module.addSerializer(RpcRequestSerializer.INSTANCE);
module.addSerializer(RpcResponseSerializer.INSTANCE);
customizeWellKnownSerializers(module);
objectMapper.registerModule(module);
// default to logging "{}" instead of throwing an exception for beans not intended for jackson
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2025 LY Corporation
*
* LY Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.internal.server.annotation;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import com.linecorp.armeria.common.RpcRequest;

final class RpcRequestSerializer extends JsonSerializer<RpcRequest> {

static final RpcRequestSerializer INSTANCE = new RpcRequestSerializer();

@Override
public Class<RpcRequest> handledType() {
return RpcRequest.class;
}

@Override
public void serialize(RpcRequest value, JsonGenerator jsonGenerator, SerializerProvider serializers)
throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("service", value.serviceName());
jsonGenerator.writeStringField("method", value.method());
jsonGenerator.writeArrayFieldStart("params");
for (Object param : value.params()) {
jsonGenerator.writeObject(param);
}
jsonGenerator.writeEndArray();
jsonGenerator.writeEndObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2025 LY Corporation
*
* LY Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.internal.server.annotation;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import com.linecorp.armeria.common.RpcResponse;

final class RpcResponseSerializer extends JsonSerializer<RpcResponse> {

static final RpcResponseSerializer INSTANCE = new RpcResponseSerializer();
private static final Object DEFAULT = new Object();

@Override
public Class<RpcResponse> handledType() {
return RpcResponse.class;
}

@Override
public void serialize(RpcResponse value, JsonGenerator jsonGenerator, SerializerProvider serializers)
throws IOException {
jsonGenerator.writeStartObject();
final Object res = value.getNow(DEFAULT);
jsonGenerator.writeObjectField("res", res);
jsonGenerator.writeEndObject();
}
}
11 changes: 10 additions & 1 deletion gradle/scripts/lib/java-rpc-thrift.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ def libDir = buildscript.sourceFile.parentFile
configure(projectsWithFlags('java')) {
def thriftJsonEnabled = true
def fullCamel = false
def annotationsAsMetadata = false

ext {
thriftVersion = null
thriftPath = null
enableThriftFullCamel = { fullCamel = true }
disableThriftJson = { thriftJsonEnabled = false }
enableAnnotationsAsMetadata = {annotationsAsMetadata = true}
}

// TODO(anuraaga): Consider replacing with https://github.com/yodle/griddle
Expand Down Expand Up @@ -57,6 +59,7 @@ configure(projectsWithFlags('java')) {
it.includeDirs.addAll(includeDirs)
it.thriftJsonEnabled = thriftJsonEnabled
it.fullCamel = fullCamel
it.annotationsAsMetadata = annotationsAsMetadata
it.thriftBinary = project.ext.thriftPath != null ? project.file(project.ext.thriftPath) : null
it.thriftVersion = project.ext.thriftVersion

Expand Down Expand Up @@ -118,6 +121,9 @@ class CompileThriftTask extends DefaultTask {
@Input
Boolean fullCamel

@Input
Boolean annotationsAsMetadata

@OutputDirectory
File javaOutputDir

Expand All @@ -144,7 +150,10 @@ class CompileThriftTask extends DefaultTask {
project.mkdir(javaOutputDir)
def javaGenerator = 'java'
if (fullCamel) {
javaGenerator = 'java:fullcamel'
javaGenerator += ':fullcamel'
}
if (annotationsAsMetadata) {
javaGenerator += ':annotations_as_metadata'
}
project.exec {
commandLine actualThriftPath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2025 LY Corporation
*
* LY Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.common.thrift.logging;

import org.apache.thrift.meta_data.FieldMetaData;

import com.linecorp.armeria.common.annotation.UnstableApi;
import com.linecorp.armeria.common.logging.FieldMasker;

/**
* Holds information about a thrift struct field.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Holds information about a thrift struct field.
* Holds information about a Thrift struct field.

* Users may use the information conveyed in this object to decide whether to mask a field
* via {@link FieldMasker}.
*/
@UnstableApi
@FunctionalInterface
public interface ThriftFieldInfo {
Comment thread
jrhee17 marked this conversation as resolved.

/**
* The {@link FieldMetaData} for a field that can be obtained via
* {@link FieldMetaData#getStructMetaDataMap(Class)}.
*/
FieldMetaData fieldMetaData();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2025 LY Corporation
*
* LY Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.common.thrift.logging;

import static java.util.Objects.requireNonNull;

import java.util.function.Function;

import com.linecorp.armeria.common.annotation.UnstableApi;
import com.linecorp.armeria.common.logging.FieldMasker;
import com.linecorp.armeria.common.logging.FieldMaskerSelector;

/**
* A {@link FieldMaskerSelector} implementation for masking thrift struct types.
* A simple use-case may look like the following:
* <pre>{@code
*
* ThriftFieldMaskSelector selector =
* ThriftFieldMaskSelector.of(fieldInfo -> {
* Map<String, String> annotations = fieldInfo.fieldMetaData().getFieldAnnotations()
* if (!annotations.containsKey("sensitive")) {
* return FieldMasker.fallthrough();
* }
* return FieldMasker.nullify();
* });
* }</pre>
* Note that the {@code annotations_as_metadata} thrift compiler option must be used to use field annotations.
*/
@UnstableApi
@FunctionalInterface
public interface ThriftFieldMaskerSelector extends FieldMaskerSelector<ThriftFieldInfo> {

/**
* A helper method to create a {@link ThriftFieldMaskerSelector}.
*/
static ThriftFieldMaskerSelector of(Function<ThriftFieldInfo, FieldMasker> fieldMaskerFunction) {
requireNonNull(fieldMaskerFunction, "fieldMaskerFunction");
return fieldMaskerFunction::apply;
}

/**
* Delegates {@link FieldMasker} selection to a different {@link ThriftFieldMaskerSelector}
* if the current {@link ThriftFieldMaskerSelector} returns {@link FieldMasker#fallthrough()}.
*/
default ThriftFieldMaskerSelector orElse(ThriftFieldMaskerSelector other) {
requireNonNull(other, "other");
return fieldInfo -> {
final FieldMasker fieldMasker = fieldMasker(fieldInfo);
if (fieldMasker != FieldMasker.fallthrough()) {
return fieldMasker;
}
return other.fieldMasker(fieldInfo);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2025 LY Corporation
*
* LY Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

/**
* Thrift-related classes for logging.
*/
@NonNullByDefault
package com.linecorp.armeria.common.thrift.logging;

import com.linecorp.armeria.common.annotation.NonNullByDefault;
Loading
Loading