This project has retired. For details please refer to its Attic page.
SaslTokenMessageRequest xref
View Javadoc

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  
19  package org.apache.giraph.comm.requests;
20  
21  import org.apache.giraph.comm.netty.SaslNettyServer;
22  import org.apache.log4j.Logger;
23  import java.io.DataInput;
24  import java.io.DataOutput;
25  import java.io.IOException;
26  
27  /**
28   * Send and receive SASL tokens.
29   */
30  public class SaslTokenMessageRequest extends WritableRequest {
31    /** Class logger */
32    private static final Logger LOG =
33        Logger.getLogger(SaslTokenMessageRequest.class);
34  
35    /** Used for client or server's token to send or receive from each other. */
36    private byte[] token;
37  
38    /**
39     * Constructor used for reflection only.
40     */
41    public SaslTokenMessageRequest() { }
42  
43   /**
44     * Constructor used to send request.
45     *
46     * @param token the SASL token, generated by a SaslClient or SaslServer.
47     */
48    public SaslTokenMessageRequest(byte[] token) {
49      this.token = token;
50    }
51  
52    /**
53     * Read accessor for SASL token
54     *
55     * @return saslToken SASL token
56     */
57    public byte[] getSaslToken() {
58      return token;
59    }
60  
61    /**
62     * Write accessor for SASL token
63     *
64     * @param token SASL token
65     */
66    public void setSaslToken(byte[] token) {
67      this.token = token;
68    }
69  
70    @Override
71    public RequestType getType() {
72      return RequestType.SASL_TOKEN_MESSAGE_REQUEST;
73    }
74  
75    @Override
76    public void readFieldsRequest(DataInput input) throws IOException {
77      int tokenSize = input.readInt();
78      token = new byte[tokenSize];
79      input.readFully(token);
80    }
81  
82    /**
83     * Update server's token in response to the SASL token received from
84     * client. Updated token is sent to client by
85     * SaslServerHandler.messageReceived().
86     *
87     * @param saslNettyServer used to create response.
88     */
89  
90    public void processToken(SaslNettyServer saslNettyServer) {
91      if (LOG.isDebugEnabled()) {
92        LOG.debug("processToken:  With nettyServer: " + saslNettyServer +
93            " and token length: " + token.length);
94      }
95      token = saslNettyServer.response(token);
96      if (LOG.isDebugEnabled()) {
97        LOG.debug("processToken: Response token's length is:" + token.length);
98      }
99    }
100 
101   @Override
102   public void writeRequest(DataOutput output) throws IOException {
103     output.writeInt(token.length);
104     output.write(token);
105   }
106 
107   @Override
108   public int getSerializedSize() {
109     return super.getSerializedSize() + 4 + token.length;
110   }
111 }