1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.hdfs.net;
19 
20 import java.io.IOException;
21 import org.apache.hadoop.classification.InterfaceAudience;
22 import org.apache.hadoop.hdfs.protocol.datatransfer.IOStreamPair;
23 import org.apache.hadoop.net.unix.DomainSocket;
24 
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.nio.channels.ReadableByteChannel;
28 
29 /**
30  * Represents a peer that we communicate with by using an encrypted
31  * communications medium.
32  */
33 @InterfaceAudience.Private
34 public class EncryptedPeer implements Peer {
35   private final Peer enclosedPeer;
36 
37   /**
38    * An encrypted InputStream.
39    */
40   private final InputStream in;
41 
42   /**
43    * An encrypted OutputStream.
44    */
45   private final OutputStream out;
46 
47   /**
48    * An encrypted ReadableByteChannel.
49    */
50   private final ReadableByteChannel channel;
51 
EncryptedPeer(Peer enclosedPeer, IOStreamPair ios)52   public EncryptedPeer(Peer enclosedPeer, IOStreamPair ios) {
53     this.enclosedPeer = enclosedPeer;
54     this.in = ios.in;
55     this.out = ios.out;
56     this.channel = ios.in instanceof ReadableByteChannel ?
57         (ReadableByteChannel)ios.in : null;
58   }
59 
60   @Override
getInputStreamChannel()61   public ReadableByteChannel getInputStreamChannel() {
62     return channel;
63   }
64 
65   @Override
setReadTimeout(int timeoutMs)66   public void setReadTimeout(int timeoutMs) throws IOException {
67     enclosedPeer.setReadTimeout(timeoutMs);
68   }
69 
70   @Override
getReceiveBufferSize()71   public int getReceiveBufferSize() throws IOException {
72     return enclosedPeer.getReceiveBufferSize();
73   }
74 
75   @Override
getTcpNoDelay()76   public boolean getTcpNoDelay() throws IOException {
77     return enclosedPeer.getTcpNoDelay();
78   }
79 
80   @Override
setWriteTimeout(int timeoutMs)81   public void setWriteTimeout(int timeoutMs) throws IOException {
82     enclosedPeer.setWriteTimeout(timeoutMs);
83   }
84 
85   @Override
isClosed()86   public boolean isClosed() {
87     return enclosedPeer.isClosed();
88   }
89 
90   @Override
close()91   public void close() throws IOException {
92     try {
93       in.close();
94     } finally {
95       try {
96         out.close();
97       } finally {
98         enclosedPeer.close();
99       }
100     }
101   }
102 
103   @Override
getRemoteAddressString()104   public String getRemoteAddressString() {
105     return enclosedPeer.getRemoteAddressString();
106   }
107 
108   @Override
getLocalAddressString()109   public String getLocalAddressString() {
110     return enclosedPeer.getLocalAddressString();
111   }
112 
113   @Override
getInputStream()114   public InputStream getInputStream() throws IOException {
115     return in;
116   }
117 
118   @Override
getOutputStream()119   public OutputStream getOutputStream() throws IOException {
120     return out;
121   }
122 
123   @Override
isLocal()124   public boolean isLocal() {
125     return enclosedPeer.isLocal();
126   }
127 
128   @Override
toString()129   public String toString() {
130     return "EncryptedPeer(" + enclosedPeer + ")";
131   }
132 
133   @Override
getDomainSocket()134   public DomainSocket getDomainSocket() {
135     return enclosedPeer.getDomainSocket();
136   }
137 
138   @Override
hasSecureChannel()139   public boolean hasSecureChannel() {
140     return true;
141   }
142 }
143