mirror of https://github.com/grpc/grpc-java.git
core: hide access to Status code and message keys
This commit is contained in:
parent
1da1dba9fb
commit
6ef77e0609
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright 2017, gRPC Authors All rights reserved.
|
||||
*
|
||||
* 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 io.grpc;
|
||||
|
||||
/**
|
||||
* Accesses internal data. Do not use this.
|
||||
*/
|
||||
@Internal
|
||||
public final class InternalStatus {
|
||||
private InternalStatus() {}
|
||||
|
||||
/**
|
||||
* Key to bind status message to trailing metadata.
|
||||
*/
|
||||
@Internal
|
||||
public static final Metadata.Key<String> MESSAGE_KEY = Status.MESSAGE_KEY;
|
||||
|
||||
/**
|
||||
* Key to bind status code to trailing metadata.
|
||||
*/
|
||||
@Internal
|
||||
public static final Metadata.Key<Status> CODE_KEY = Status.CODE_KEY;
|
||||
}
|
|
@ -345,8 +345,7 @@ public final class Status {
|
|||
/**
|
||||
* Key to bind status code to trailing metadata.
|
||||
*/
|
||||
@Internal
|
||||
public static final Metadata.Key<Status> CODE_KEY
|
||||
static final Metadata.Key<Status> CODE_KEY
|
||||
= Metadata.Key.of("grpc-status", new StatusCodeMarshaller());
|
||||
|
||||
/**
|
||||
|
@ -377,8 +376,7 @@ public final class Status {
|
|||
/**
|
||||
* Key to bind status message to trailing metadata.
|
||||
*/
|
||||
@Internal
|
||||
public static final Metadata.Key<String> MESSAGE_KEY =
|
||||
static final Metadata.Key<String> MESSAGE_KEY =
|
||||
Metadata.Key.of("grpc-message", STATUS_MESSAGE_MARSHALLER);
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,6 +18,7 @@ package io.grpc.internal;
|
|||
|
||||
import com.google.common.base.Preconditions;
|
||||
import io.grpc.Attributes;
|
||||
import io.grpc.InternalStatus;
|
||||
import io.grpc.Metadata;
|
||||
import io.grpc.Status;
|
||||
import javax.annotation.Nullable;
|
||||
|
@ -131,11 +132,11 @@ public abstract class AbstractServerStream extends AbstractStream
|
|||
}
|
||||
|
||||
private void addStatusToTrailers(Metadata trailers, Status status) {
|
||||
trailers.discardAll(Status.CODE_KEY);
|
||||
trailers.discardAll(Status.MESSAGE_KEY);
|
||||
trailers.put(Status.CODE_KEY, status);
|
||||
trailers.discardAll(InternalStatus.CODE_KEY);
|
||||
trailers.discardAll(InternalStatus.MESSAGE_KEY);
|
||||
trailers.put(InternalStatus.CODE_KEY, status);
|
||||
if (status.getDescription() != null) {
|
||||
trailers.put(Status.MESSAGE_KEY, status.getDescription());
|
||||
trailers.put(InternalStatus.MESSAGE_KEY, status.getDescription());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ package io.grpc.internal;
|
|||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Preconditions;
|
||||
import io.grpc.InternalMetadata;
|
||||
import io.grpc.InternalStatus;
|
||||
import io.grpc.Metadata;
|
||||
import io.grpc.Status;
|
||||
import java.nio.charset.Charset;
|
||||
|
@ -176,9 +177,9 @@ public abstract class Http2ClientStreamTransportState extends AbstractClientStre
|
|||
* Extract the response status from trailers.
|
||||
*/
|
||||
private Status statusFromTrailers(Metadata trailers) {
|
||||
Status status = trailers.get(Status.CODE_KEY);
|
||||
Status status = trailers.get(InternalStatus.CODE_KEY);
|
||||
if (status != null) {
|
||||
return status.withDescription(trailers.get(Status.MESSAGE_KEY));
|
||||
return status.withDescription(trailers.get(InternalStatus.MESSAGE_KEY));
|
||||
}
|
||||
// No status; something is broken. Try to provide a resonanable error.
|
||||
if (headersReceived) {
|
||||
|
@ -236,7 +237,7 @@ public abstract class Http2ClientStreamTransportState extends AbstractClientStre
|
|||
*/
|
||||
private static void stripTransportDetails(Metadata metadata) {
|
||||
metadata.discardAll(HTTP2_STATUS);
|
||||
metadata.discardAll(Status.CODE_KEY);
|
||||
metadata.discardAll(Status.MESSAGE_KEY);
|
||||
metadata.discardAll(InternalStatus.CODE_KEY);
|
||||
metadata.discardAll(InternalStatus.MESSAGE_KEY);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import static org.mockito.Mockito.reset;
|
|||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import io.grpc.InternalStatus;
|
||||
import io.grpc.Metadata;
|
||||
import io.grpc.Status;
|
||||
import io.grpc.internal.AbstractServerStream.TransportState;
|
||||
|
@ -208,14 +209,15 @@ public class AbstractServerStreamTest {
|
|||
// stream actually mutates trailers, so we can't check that the fields here are the same as
|
||||
// the captured ones.
|
||||
Metadata trailers = new Metadata();
|
||||
trailers.put(Status.CODE_KEY, Status.OK);
|
||||
trailers.put(Status.MESSAGE_KEY, "Everything's super.");
|
||||
trailers.put(InternalStatus.CODE_KEY, Status.OK);
|
||||
trailers.put(InternalStatus.MESSAGE_KEY, "Everything's super.");
|
||||
|
||||
stream.close(Status.INTERNAL.withDescription("bad"), trailers);
|
||||
|
||||
verify(sink).writeTrailers(metadataCaptor.capture(), eq(false));
|
||||
assertEquals(Status.Code.INTERNAL, metadataCaptor.getValue().get(Status.CODE_KEY).getCode());
|
||||
assertEquals("bad", metadataCaptor.getValue().get(Status.MESSAGE_KEY));
|
||||
assertEquals(
|
||||
Status.Code.INTERNAL, metadataCaptor.getValue().get(InternalStatus.CODE_KEY).getCode());
|
||||
assertEquals("bad", metadataCaptor.getValue().get(InternalStatus.MESSAGE_KEY));
|
||||
}
|
||||
|
||||
private static class ServerStreamListenerBase implements ServerStreamListener {
|
||||
|
|
|
@ -41,6 +41,7 @@ import static org.mockito.Mockito.when;
|
|||
|
||||
import com.google.common.collect.ImmutableListMultimap;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import io.grpc.InternalStatus;
|
||||
import io.grpc.Metadata;
|
||||
import io.grpc.MethodDescriptor;
|
||||
import io.grpc.Status;
|
||||
|
@ -458,7 +459,7 @@ public class NettyClientStreamTest extends NettyStreamTestBase<NettyClientStream
|
|||
|
||||
private Http2Headers grpcResponseTrailers(Status status) {
|
||||
Metadata trailers = new Metadata();
|
||||
trailers.put(Status.CODE_KEY, status);
|
||||
trailers.put(InternalStatus.CODE_KEY, status);
|
||||
return Utils.convertTrailers(trailers, true);
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,7 @@ import com.google.common.collect.ImmutableList;
|
|||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
import com.google.common.util.concurrent.SettableFuture;
|
||||
import io.grpc.InternalStatus;
|
||||
import io.grpc.Metadata;
|
||||
import io.grpc.MethodDescriptor;
|
||||
import io.grpc.MethodDescriptor.MethodType;
|
||||
|
@ -1608,7 +1609,7 @@ public class OkHttpClientTransportTest {
|
|||
|
||||
private List<Header> grpcResponseTrailers() {
|
||||
return ImmutableList.of(
|
||||
new Header(Status.CODE_KEY.name(), "0"),
|
||||
new Header(InternalStatus.CODE_KEY.name(), "0"),
|
||||
// Adding Content-Type and :status for testing responses with only a single HEADERS frame.
|
||||
new Header(":status", "200"),
|
||||
CONTENT_TYPE_HEADER);
|
||||
|
|
Loading…
Reference in New Issue