1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.comm.requests;
2021import org.apache.giraph.comm.netty.SaslNettyServer;
22import org.apache.log4j.Logger;
23import java.io.DataInput;
24import java.io.DataOutput;
25import java.io.IOException;
2627/**28 * Send and receive SASL tokens.29 */30publicclassSaslTokenMessageRequestextendsWritableRequest {
31/** Class logger */32privatestaticfinal Logger LOG =
33 Logger.getLogger(SaslTokenMessageRequest.class);
3435/** Used for client or server's token to send or receive from each other. */36private byte[] token;
3738/**39 * Constructor used for reflection only.40 */41publicSaslTokenMessageRequest() { }
4243/**44 * Constructor used to send request.45 *46 * @param token the SASL token, generated by a SaslClient or SaslServer.47 */48publicSaslTokenMessageRequest(byte[] token) {
49this.token = token;
50 }
5152/**53 * Read accessor for SASL token54 *55 * @return saslToken SASL token56 */57public byte[] getSaslToken() {
58return token;
59 }
6061/**62 * Write accessor for SASL token63 *64 * @param token SASL token65 */66publicvoid setSaslToken(byte[] token) {
67this.token = token;
68 }
6970 @Override
71publicRequestType getType() {
72return RequestType.SASL_TOKEN_MESSAGE_REQUEST;
73 }
7475 @Override
76publicvoid readFieldsRequest(DataInput input) throws IOException {
77int tokenSize = input.readInt();
78 token = new byte[tokenSize];
79 input.readFully(token);
80 }
8182/**83 * Update server's token in response to the SASL token received from84 * client. Updated token is sent to client by85 * SaslServerHandler.messageReceived().86 *87 * @param saslNettyServer used to create response.88 */8990publicvoid processToken(SaslNettyServer saslNettyServer) {
91if (LOG.isDebugEnabled()) {
92 LOG.debug("processToken: With nettyServer: " + saslNettyServer +
93" and token length: " + token.length);
94 }
95 token = saslNettyServer.response(token);
96if (LOG.isDebugEnabled()) {
97 LOG.debug("processToken: Response token's length is:" + token.length);
98 }
99 }
100101 @Override
102publicvoid writeRequest(DataOutput output) throws IOException {
103 output.writeInt(token.length);
104 output.write(token);
105 }
106107 @Override
108publicint getSerializedSize() {
109returnsuper.getSerializedSize() + 4 + token.length;
110 }
111 }