diff --git a/spring-cloud-openfeign-core/pom.xml b/spring-cloud-openfeign-core/pom.xml index 9a910f62..d191c0da 100644 --- a/spring-cloud-openfeign-core/pom.xml +++ b/spring-cloud-openfeign-core/pom.xml @@ -188,6 +188,18 @@ spring-cloud-starter-netflix-hystrix test + + com.googlecode.protobuf-java-format + protobuf-java-format + 1.4 + test + + + com.google.protobuf + protobuf-java + 3.4.0 + test + diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java index 7525dd0c..0ee52016 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Type; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collection; import org.apache.commons.logging.Log; @@ -33,6 +34,7 @@ import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter; import feign.RequestTemplate; import feign.codec.EncodeException; @@ -43,6 +45,7 @@ import static org.springframework.cloud.openfeign.support.FeignUtils.getHttpHead /** * @author Spencer Gibb + * @author ScienJus */ public class SpringEncoder implements Encoder { @@ -99,12 +102,17 @@ public class SpringEncoder implements Encoder { // with the modified headers request.headers(getHeaders(outputMessage.getHeaders())); - // do not use charset for binary data + // do not use charset for binary data and protobuf + Charset charset; if (messageConverter instanceof ByteArrayHttpMessageConverter) { - request.body(outputMessage.getOutputStream().toByteArray(), null); + charset = null; + } else if (messageConverter instanceof ProtobufHttpMessageConverter && + ProtobufHttpMessageConverter.PROTOBUF.isCompatibleWith(outputMessage.getHeaders().getContentType())) { + charset = null; } else { - request.body(outputMessage.getOutputStream().toByteArray(), Charset.forName("UTF-8")); + charset = StandardCharsets.UTF_8; } + request.body(outputMessage.getOutputStream().toByteArray(), charset); return; } } diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/ProtobufNotInClasspathTest.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/ProtobufNotInClasspathTest.java new file mode 100644 index 00000000..8a0dad3a --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/ProtobufNotInClasspathTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed 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 + * + * http://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 org.springframework.cloud.netflix.feign.encoding.proto; + +import feign.RequestTemplate; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.ObjectFactory; +import org.springframework.boot.autoconfigure.web.HttpMessageConverters; +import org.springframework.cloud.ClassPathExclusions; +import org.springframework.cloud.FilteredClassPathRunner; +import org.springframework.cloud.netflix.feign.support.SpringEncoder; +import org.springframework.http.converter.StringHttpMessageConverter; + +/** + * Test {@link SpringEncoder} when protobuf is not in classpath + * + * @author ScienJus + */ +@RunWith(FilteredClassPathRunner.class) +@ClassPathExclusions("protobuf-*.jar") +public class ProtobufNotInClasspathTest { + + @Test + public void testEncodeWhenProtobufNotInClasspath() { + ObjectFactory converters = new ObjectFactory() { + @Override + public HttpMessageConverters getObject() throws BeansException { + return new HttpMessageConverters(new StringHttpMessageConverter()); + } + }; + RequestTemplate requestTemplate = new RequestTemplate(); + requestTemplate.method("POST"); + new SpringEncoder(converters).encode("a=b", String.class, requestTemplate); + } + +} diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/ProtobufSpringEncoderTest.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/ProtobufSpringEncoderTest.java new file mode 100644 index 00000000..a712d69f --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/ProtobufSpringEncoderTest.java @@ -0,0 +1,139 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed 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 + * + * http://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 org.springframework.cloud.netflix.feign.encoding.proto; + +import com.google.protobuf.InvalidProtocolBufferException; +import feign.RequestTemplate; +import feign.httpclient.ApacheHttpClient; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.ProtocolVersion; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.message.BasicStatusLine; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.BDDMockito; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.ObjectFactory; +import org.springframework.boot.autoconfigure.web.HttpMessageConverters; +import org.springframework.cloud.netflix.feign.support.SpringEncoder; +import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +/** + * Test {@link SpringEncoder} with {@link ProtobufHttpMessageConverter} + * + * @author ScienJus + */ +@RunWith(MockitoJUnitRunner.class) +public class ProtobufSpringEncoderTest { + + @Mock + private HttpClient httpClient; + + // a protobuf object with some content + private Request request = Request.newBuilder() + .setId(1000000) + .setMsg("Erlang/OTP 最初是爱立信为开发电信设备系统设计的编程语言平台," + + "电信设备(路由器、接入网关、…)典型设计是通过背板连接主控板卡与多块业务板卡的分布式系统。") + .build(); + + @Test + public void testProtobuf() throws IOException, URISyntaxException { + // protobuf convert to request by feign and ProtobufHttpMessageConverter + RequestTemplate requestTemplate = newRequestTemplate(); + newEncoder().encode(request, Request.class, requestTemplate); + HttpEntity entity = toApacheHttpEntity(requestTemplate); + byte[] bytes = read(entity.getContent(), (int) entity.getContentLength()); + + Assert.assertArrayEquals(bytes, request.toByteArray()); + Request copy = Request.parseFrom(bytes); + Assert.assertEquals(request, copy); + } + + @Test + public void testProtobufWithCharsetWillFail() throws IOException, URISyntaxException { + // protobuf convert to request by feign and ProtobufHttpMessageConverter + RequestTemplate requestTemplate = newRequestTemplate(); + newEncoder().encode(request, Request.class, requestTemplate); + // set a charset + requestTemplate.body(requestTemplate.body(), StandardCharsets.UTF_8); + HttpEntity entity = toApacheHttpEntity(requestTemplate); + byte[] bytes = read(entity.getContent(), (int) entity.getContentLength()); + + // http request-body is different with original protobuf body + Assert.assertNotEquals(bytes.length, request.toByteArray().length); + try { + Request copy = Request.parseFrom(bytes); + Assert.fail("Expected an InvalidProtocolBufferException to be thrown"); + } catch (InvalidProtocolBufferException e) { + // success + } + } + + private SpringEncoder newEncoder() { + ObjectFactory converters = new ObjectFactory() { + @Override + public HttpMessageConverters getObject() throws BeansException { + return new HttpMessageConverters(new ProtobufHttpMessageConverter()); + } + }; + return new SpringEncoder(converters); + } + + private RequestTemplate newRequestTemplate() { + RequestTemplate requestTemplate = new RequestTemplate(); + requestTemplate.method("POST"); + return requestTemplate; + } + + private HttpEntity toApacheHttpEntity(RequestTemplate requestTemplate) throws IOException, URISyntaxException { + final List request = new ArrayList<>(1); + BDDMockito.given(httpClient.execute(Matchers.any())).will(new Answer() { + @Override + public HttpResponse answer(InvocationOnMock invocationOnMock) throws Throwable { + request.add((HttpUriRequest) invocationOnMock.getArguments()[0]); + return new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, null)); + } + }); + new ApacheHttpClient(httpClient).execute(requestTemplate.request(), new feign.Request.Options()); + HttpUriRequest httpUriRequest = request.get(0); + return ((HttpEntityEnclosingRequestBase)httpUriRequest).getEntity(); + } + + private byte[] read(InputStream in, int length) throws IOException { + byte[] bytes = new byte[length]; + in.read(bytes); + return bytes; + } + +} diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/ProtobufTest.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/ProtobufTest.java new file mode 100644 index 00000000..b71c8337 --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/ProtobufTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed 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 + * + * http://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. + */ + +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: protobuf_test.proto + +package org.springframework.cloud.netflix.feign.encoding.proto; + +public final class ProtobufTest { + private ProtobufTest() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistryLite registry) { + } + + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + registerAllExtensions( + (com.google.protobuf.ExtensionRegistryLite) registry); + } + static final com.google.protobuf.Descriptors.Descriptor + internal_static_Request_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_Request_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + String[] descriptorData = { + "\n\023protobuf_test.proto\"\"\n\007Request\022\n\n\002id\030\001" + + " \001(\005\022\013\n\003msg\030\002 \001(\tB\024\n\020feign.httpclientP\001b" + + "\006proto3" + }; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = + new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors( + com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }, assigner); + internal_static_Request_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_Request_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_Request_descriptor, + new String[] { "Id", "Msg", }); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/Request.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/Request.java new file mode 100644 index 00000000..0d29f78b --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/Request.java @@ -0,0 +1,587 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed 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 + * + * http://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. + */ + +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: protobuf_test.proto + +package org.springframework.cloud.netflix.feign.encoding.proto; + +/** + * Protobuf type {@code Request} + */ +public final class Request extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:Request) + RequestOrBuilder { +private static final long serialVersionUID = 0L; + // Use Request.newBuilder() to construct. + private Request(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private Request() { + id_ = 0; + msg_ = ""; + } + + @Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private Request( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + int mutable_bitField0_ = 0; + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + default: { + if (!parseUnknownFieldProto3( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + case 8: { + + id_ = input.readInt32(); + break; + } + case 18: { + String s = input.readStringRequireUtf8(); + + msg_ = s; + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return ProtobufTest.internal_static_Request_descriptor; + } + + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return ProtobufTest.internal_static_Request_fieldAccessorTable + .ensureFieldAccessorsInitialized( + Request.class, Request.Builder.class); + } + + public static final int ID_FIELD_NUMBER = 1; + private int id_; + /** + * int32 id = 1; + */ + public int getId() { + return id_; + } + + public static final int MSG_FIELD_NUMBER = 2; + private volatile Object msg_; + /** + * string msg = 2; + */ + public String getMsg() { + Object ref = msg_; + if (ref instanceof String) { + return (String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + msg_ = s; + return s; + } + } + /** + * string msg = 2; + */ + public com.google.protobuf.ByteString + getMsgBytes() { + Object ref = msg_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (String) ref); + msg_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (id_ != 0) { + output.writeInt32(1, id_); + } + if (!getMsgBytes().isEmpty()) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 2, msg_); + } + unknownFields.writeTo(output); + } + + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (id_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(1, id_); + } + if (!getMsgBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, msg_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @Override + public boolean equals(final Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof Request)) { + return super.equals(obj); + } + Request other = (Request) obj; + + boolean result = true; + result = result && (getId() + == other.getId()); + result = result && getMsg() + .equals(other.getMsg()); + result = result && unknownFields.equals(other.unknownFields); + return result; + } + + @Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + ID_FIELD_NUMBER; + hash = (53 * hash) + getId(); + hash = (37 * hash) + MSG_FIELD_NUMBER; + hash = (53 * hash) + getMsg().hashCode(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static Request parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static Request parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static Request parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static Request parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static Request parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static Request parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static Request parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static Request parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static Request parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static Request parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static Request parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static Request parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(Request prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @Override + protected Builder newBuilderForType( + BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code Request} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:Request) + RequestOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return ProtobufTest.internal_static_Request_descriptor; + } + + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return ProtobufTest.internal_static_Request_fieldAccessorTable + .ensureFieldAccessorsInitialized( + Request.class, Request.Builder.class); + } + + // Construct using org.springframework.cloud.netflix.feign.encoding.proto.Request.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + public Builder clear() { + super.clear(); + id_ = 0; + + msg_ = ""; + + return this; + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return ProtobufTest.internal_static_Request_descriptor; + } + + public Request getDefaultInstanceForType() { + return Request.getDefaultInstance(); + } + + public Request build() { + Request result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + public Request buildPartial() { + Request result = new Request(this); + result.id_ = id_; + result.msg_ = msg_; + onBuilt(); + return result; + } + + public Builder clone() { + return (Builder) super.clone(); + } + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return (Builder) super.setField(field, value); + } + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return (Builder) super.clearField(field); + } + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return (Builder) super.clearOneof(oneof); + } + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, Object value) { + return (Builder) super.setRepeatedField(field, index, value); + } + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return (Builder) super.addRepeatedField(field, value); + } + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof Request) { + return mergeFrom((Request)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(Request other) { + if (other == Request.getDefaultInstance()) return this; + if (other.getId() != 0) { + setId(other.getId()); + } + if (!other.getMsg().isEmpty()) { + msg_ = other.msg_; + onChanged(); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + Request parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (Request) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private int id_ ; + /** + * int32 id = 1; + */ + public int getId() { + return id_; + } + /** + * int32 id = 1; + */ + public Builder setId(int value) { + + id_ = value; + onChanged(); + return this; + } + /** + * int32 id = 1; + */ + public Builder clearId() { + + id_ = 0; + onChanged(); + return this; + } + + private Object msg_ = ""; + /** + * string msg = 2; + */ + public String getMsg() { + Object ref = msg_; + if (!(ref instanceof String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + msg_ = s; + return s; + } else { + return (String) ref; + } + } + /** + * string msg = 2; + */ + public com.google.protobuf.ByteString + getMsgBytes() { + Object ref = msg_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (String) ref); + msg_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string msg = 2; + */ + public Builder setMsg( + String value) { + if (value == null) { + throw new NullPointerException(); + } + + msg_ = value; + onChanged(); + return this; + } + /** + * string msg = 2; + */ + public Builder clearMsg() { + + msg_ = getDefaultInstance().getMsg(); + onChanged(); + return this; + } + /** + * string msg = 2; + */ + public Builder setMsgBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + msg_ = value; + onChanged(); + return this; + } + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFieldsProto3(unknownFields); + } + + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:Request) + } + + // @@protoc_insertion_point(class_scope:Request) + private static final Request DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new Request(); + } + + public static Request getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + public Request parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new Request(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + public Request getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/RequestOrBuilder.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/RequestOrBuilder.java new file mode 100644 index 00000000..252ed319 --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/proto/RequestOrBuilder.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed 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 + * + * http://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. + */ + +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: protobuf_test.proto + +package org.springframework.cloud.netflix.feign.encoding.proto; + +public interface RequestOrBuilder extends + // @@protoc_insertion_point(interface_extends:Request) + com.google.protobuf.MessageOrBuilder { + + /** + * int32 id = 1; + */ + int getId(); + + /** + * string msg = 2; + */ + String getMsg(); + /** + * string msg = 2; + */ + com.google.protobuf.ByteString + getMsgBytes(); +}